<?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.TInsuranceWarnMapper">

	<resultMap id="BaseResultMap" type="com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceAlert">
		<id property="id" column="ID" jdbcType="VARCHAR"/>
		<result property="empName" column="EMP_NAME" jdbcType="VARCHAR"/>
		<result property="empIdcardNo" column="EMP_IDCARD_NO" jdbcType="VARCHAR"/>
		<result property="deptNo" column="DEPT_NO" jdbcType="VARCHAR"/>
		<result property="post" column="POST" jdbcType="VARCHAR"/>
		<result property="insuranceCompanyName" column="INSURANCE_COMPANY_NAME" jdbcType="VARCHAR"/>
		<result property="insuranceTypeName" column="INSURANCE_TYPE_NAME" jdbcType="VARCHAR"/>
		<result property="buyStandard" column="BUY_STANDARD" jdbcType="VARCHAR"/>
		<result property="medicalQuota" column="MEDICAL_QUOTA" jdbcType="VARCHAR"/>
		<result property="dieDisableQuota" column="DIE_DISABLE_QUOTA" 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="policyEffect" column="POLICY_EFFECT" jdbcType="DATE"/>
		<result property="settleMonth" column="SETTLE_MONTH" jdbcType="VARCHAR"/>
		<result property="invoiceNo" column="INVOICE_NO" jdbcType="VARCHAR"/>
		<result property="insuranceProvinceName" column="INSURANCE_PROVINCE_NAME" jdbcType="VARCHAR"/>
		<result property="insuranceCityName" column="INSURANCE_CITY_NAME" jdbcType="VARCHAR"/>
		<result property="insuranceHandleProvinceName" column="INSURANCE_HANDLE_PROVINCE_NAME" jdbcType="VARCHAR"/>
		<result property="insuranceHandleCityName" column="INSURANCE_HANDLE_CITY_NAME" jdbcType="VARCHAR"/>
		<result property="settleType" column="SETTLE_TYPE" jdbcType="TINYINT"/>
		<result property="actualPremium" column="ACTUAL_PREMIUM" jdbcType="DECIMAL"/>
		<result property="estimatePremium" column="ESTIMATE_PREMIUM" jdbcType="DECIMAL"/>
		<result property="isEffect" column="IS_EFFECT" jdbcType="TINYINT"/>
		<result property="isOverdue" column="IS_OVERDUE" jdbcType="TINYINT"/>
		<result property="remark" column="REMARK" jdbcType="VARCHAR"/>
		<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"/>
		<result property="deleteFlag" column="DELETE_FLAG" jdbcType="TINYINT"/>
		<result property="createUserDeptName" column="CREATE_USER_DEPT_NAME" jdbcType="VARCHAR"/>
		<result property="deptName" column="DEPT_NAME" jdbcType="VARCHAR"/>
		<result property="unitName" column="UNIT_NAME" jdbcType="VARCHAR"/>
		<result property="unitNo" column="UNIT_NO" jdbcType="VARCHAR"/>
		<result property="expireIgnoreFlag" column="EXPIRE_IGNORE_FLAG" jdbcType="VARCHAR"/>
		<result property="expireRemark" column="EXPIRE_REMARK" jdbcType="VARCHAR"/>
	</resultMap>


	<select id="selectInsuranceAlert"  resultMap="BaseResultMap">
		SELECT
			a.ID,
			a.EMP_NAME,
			a.EMP_IDCARD_NO,
			a.DEPT_NO,
			a.DEPT_NAME,
			a.POST,
			a.INSURANCE_COMPANY_NAME,
			a.INSURANCE_TYPE_NAME,
			a.INSURANCE_CITY_NAME,
			a.INSURANCE_HANDLE_CITY_NAME,
			a.INSURANCE_HANDLE_PROVINCE_NAME,
			a.INSURANCE_PROVINCE_NAME,
			a.BUY_STANDARD,
			a.UNIT_NAME,
			a.UNIT_NO,
			a.SETTLE_TYPE,
			a.POLICY_EFFECT,
			a.EXPIRE_REMARK,
			a.EXPIRE_IGNORE_FLAG,
			a.SETTLE_MONTH,
			a.POLICY_START,
			a.POLICY_END,
			a.ACTUAL_PREMIUM,
			a.ESTIMATE_PREMIUM,
			a.INVOICE_NO,
			a.MEDICAL_QUOTA,
			a.DIE_DISABLE_QUOTA,
			a.DEPT_ID,
			a.POLICY_NO,
			IF( a.POLICY_END >= curdate(), 0, 1 ) IS_OVERDUE,
			a.REMARK,
			a.CREATE_BY,
			a.DELETE_FLAG,
			a.CREATE_NAME,
			a.CREATE_TIME,
			a.UPDATE_BY,
			a.UPDATE_TIME,
			a.IS_EFFECT,
			a.CREATE_USER_DEPT_NAME
		FROM
			t_insurance_detail a
				INNER JOIN (
				SELECT
					h.endTime,
					max( l.POLICY_START ) POLICY_START,
					l.EMP_IDCARD_NO
				FROM
					(
						SELECT
							b.EMP_IDCARD_NO,
							max( b.POLICY_END ) endTime
						FROM
							t_insurance_detail b
						WHERE
							b.POLICY_START BETWEEN date_sub( date_add( curdate(), INTERVAL 1 DAY ), INTERVAL 1 YEAR )
								AND curdate()
						  AND b.IS_EFFECT = 0
						  AND b.DELETE_FLAG = '0'
						  AND NOT EXISTS (
								SELECT
									1
								FROM
									t_insurance_detail ea
								WHERE
									ea.POLICY_START >= date_sub( date_add( curdate(), INTERVAL 1 DAY ), INTERVAL 1 YEAR )
								  AND ea.IS_EFFECT = 0
								  AND ea.DELETE_FLAG = '0'
								  AND ea.POLICY_END >= date_add( curdate(), INTERVAL 1 MONTH )
								  AND ea.EMP_IDCARD_NO = b.EMP_IDCARD_NO
								  AND ea.INSURANCE_COMPANY_NAME = b.INSURANCE_COMPANY_NAME
								  AND ea.INSURANCE_TYPE_NAME = b.INSURANCE_TYPE_NAME
							)
						GROUP BY
							b.EMP_IDCARD_NO,
							b.INSURANCE_COMPANY_NAME,
							b.INSURANCE_TYPE_NAME
					) h
						INNER JOIN t_insurance_detail l ON h.EMP_IDCARD_NO = l.EMP_IDCARD_NO
						AND l.DELETE_FLAG = '0'
						AND h.endTime = l.POLICY_END
						AND l.POLICY_START BETWEEN date_sub( date_add( curdate(), INTERVAL 1 DAY ), INTERVAL 1 YEAR )
															   AND curdate()
				WHERE
					l.EXPIRE_IGNORE_FLAG = '1'
				GROUP BY
					l.EMP_IDCARD_NO,
					l.INSURANCE_COMPANY_NAME,
					l.INSURANCE_TYPE_NAME,
					l.POLICY_END
			) ll ON ll.EMP_IDCARD_NO = a.EMP_IDCARD_NO
				AND ll.POLICY_START = a.POLICY_START
				AND ll.endTime = a.POLICY_END
		where a.DELETE_FLAG = '0'
	</select>
</mapper>
