<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yifu.cloud.plus.v1.yifu.insurances.mapper.insurances.TInsurancePolicyMapper">

    <resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsurancePolicy">
            <id property="id" column="ID" jdbcType="VARCHAR"/>
            <result property="policyNo" column="POLICY_NO" jdbcType="VARCHAR"/>
            <result property="policyStart" column="POLICY_START" jdbcType="DATE"/>
            <result property="policyEnd" column="POLICY_END" jdbcType="DATE"/>
            <result property="insuranceCompanyName" column="INSURANCE_COMPANY_NAME" jdbcType="VARCHAR"/>
            <result property="insuranceTypeName" column="INSURANCE_TYPE_NAME" jdbcType="VARCHAR"/>
            <result property="enableFlag" column="ENABLE_FLAG" jdbcType="TINYINT"/>
            <result property="deleteFlag" column="DELETE_FLAG" jdbcType="TINYINT"/>
            <result property="createBy" column="CREATE_BY" jdbcType="VARCHAR"/>
            <result property="createName" column="CREATE_NAME" jdbcType="VARCHAR"/>
            <result property="createTime" column="CREATE_TIME" jdbcType="TIMESTAMP"/>
            <result property="updateBy" column="UPDATE_BY" jdbcType="VARCHAR"/>
            <result property="updateTime" column="UPDATE_TIME" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        ID,POLICY_NO,POLICY_START,
        POLICY_END,INSURANCE_COMPANY_NAME,INSURANCE_TYPE_NAME,
        ENABLE_FLAG,DELETE_FLAG,CREATE_BY,
        CREATE_NAME,CREATE_TIME,UPDATE_BY,
        UPDATE_TIME
    </sql>
	<select id="getInsurancePolicyListPage" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List"></include>
		FROM t_insurance_policy
		where DELETE_FLAG = 0
		<if test="param.policyNo != null and param.policyNo.trim() != ''">
			and POLICY_NO like concat('%',replace(replace(#{param.policyNo},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.policyStart != null and param.policyStart.trim() != '' and param.policyEnd == null">
			AND DATE(POLICY_START) <![CDATA[ >= ]]> #{param.policyStart}
		</if>
		<if test="param.policyEnd != null and param.policyEnd.trim() != '' and param.policyStart == null">
			AND DATE(POLICY_END) <![CDATA[ <= ]]> #{param.policyEnd}
		</if>
		<if test="param.policyStart != null and param.policyStart.trim() != '' and param.policyEnd != null and param.policyEnd.trim() != ''">
			and (
			(DATE(POLICY_START) <![CDATA[ >= ]]> #{param.policyStart} and DATE(POLICY_START) <![CDATA[ <= ]]> #{param.policyEnd}) OR
			(DATE(POLICY_END) <![CDATA[ >= ]]> #{param.policyStart} and DATE(POLICY_END) <![CDATA[ <= ]]> #{param.policyEnd})
			)
		</if>
		<if test="param.insuranceCompanyName != null and param.insuranceCompanyName.trim() != ''">
			and INSURANCE_COMPANY_NAME like concat('%',replace(replace(#{param.insuranceCompanyName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.insuranceTypeName != null and param.insuranceTypeName.trim() != ''">
			and INSURANCE_TYPE_NAME like concat('%',replace(replace(#{param.insuranceTypeName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.enableFlag != null">
			and ENABLE_FLAG = #{param.enableFlag}
		</if>
		ORDER BY CREATE_TIME DESC
	</select>

	<select id="getInsurancePolicyList" resultMap="BaseResultMap">
		SELECT
		<include refid="Base_Column_List"></include>
		FROM t_insurance_policy
		where DELETE_FLAG = 0
		<if test="param.policyNo != null and param.policyNo.trim() != ''">
			and POLICY_NO like concat('%',replace(replace(#{param.policyNo},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.policyStart != null and param.policyStart.trim() != '' and param.policyEnd == null">
			AND DATE(POLICY_START) <![CDATA[ >= ]]> #{param.policyStart}
		</if>
		<if test="param.policyEnd != null and param.policyEnd.trim() != '' and param.policyStart == null">
			AND DATE(POLICY_END) <![CDATA[ <= ]]> #{param.policyEnd}
		</if>
		<if test="param.policyStart != null and param.policyStart.trim() != '' and param.policyEnd != null and param.policyEnd.trim() != ''">
			and (
			(DATE(POLICY_START) <![CDATA[ >= ]]> #{param.policyStart} and DATE(POLICY_START) <![CDATA[ <= ]]> #{param.policyEnd}) OR
			(DATE(POLICY_END) <![CDATA[ >= ]]> #{param.policyStart} and DATE(POLICY_END) <![CDATA[ <= ]]> #{param.policyEnd})
			)
		</if>
		<if test="param.insuranceCompanyName != null and param.insuranceCompanyName.trim() != ''">
			and INSURANCE_COMPANY_NAME like concat('%',replace(replace(#{param.insuranceCompanyName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.insuranceTypeName != null and param.insuranceTypeName.trim() != ''">
			and INSURANCE_TYPE_NAME like concat('%',replace(replace(#{param.insuranceTypeName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.enableFlag != null">
			and ENABLE_FLAG = #{param.enableFlag}
		</if>
		ORDER BY CREATE_TIME DESC
	</select>

	<!--商险自动化-针对批增的查询保单号的分页接口-->
	<select id="policyPageToAutoSelect" resultType="com.yifu.cloud.plus.v1.yifu.insurances.vo.PolicyListVo">
		select
		a.id as id,
		a.POLICY_NO as policyNo,
		a.POLICY_START as policyStart,
		a.POLICY_END as policyEnd,
		a.INSURANCE_COMPANY_NAME as insuranceCompanyName,
		a.INSURANCE_TYPE_NAME as insuranceTypeName,
		c.id as insuranceTypeId,
		d.BUY_STANDARD as buyStandard
		from t_insurance_policy a
		LEFT JOIN t_insurance_company b on a.INSURANCE_COMPANY_NAME=b.COMPANY_NAME
		LEFT JOIN t_insurance_type c on a.INSURANCE_TYPE_NAME = c.`NAME`
		LEFT JOIN t_insurance_type_standard d on c.ID = d.INSURANCE_TYPE_ID
		where
		a.DELETE_FLAG = 0 and a.ENABLE_FLAG = 0
		and a.POLICY_NO is not null
		and a.POLICY_NO != ""
		and d.DELETE_FLAG='0' and d.`ENABLE`='0'
		and c.DELETE_FLAG='0' and c.`ENABLE`='0'
		and b.DELETE_FLAG='0'

		<if test="param.policyNo != null and param.policyNo.trim() != ''">
			and a.POLICY_NO like concat('%',replace(replace(#{param.policyNo},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.insuranceCompanyName != null and param.insuranceCompanyName.trim() != ''">
			and a.INSURANCE_COMPANY_NAME like concat('%',replace(replace(#{param.insuranceCompanyName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.insuranceTypeName != null and param.insuranceTypeName.trim() != ''">
			and a.INSURANCE_TYPE_NAME like concat('%',replace(replace(#{param.insuranceTypeName},'_','\_'),'%','\%'),'%')
		</if>
		<if test="param.buyStandardMin != null and param.buyStandardMin.trim() != ''">
			AND d.BUY_STANDARD+0 <![CDATA[ >= ]]> #{param.buyStandardMin}+0
		</if>
		<if test="param.buyStandardMax != null and param.buyStandardMax.trim() != ''">
			AND d.BUY_STANDARD+0 <![CDATA[ <= ]]> #{param.buyStandardMax}+0
		</if>
		<if test="param.policyEnd != null and param.policyEnd.trim() != ''">
			AND DATE(a.POLICY_END) <![CDATA[ >= ]]> #{param.policyEnd}
		</if>
		group by a.POLICY_NO,d.BUY_STANDARD
		ORDER BY a.CREATE_TIME DESC ,a.ID ASC
	</select>
</mapper>
