<?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.salary.mapper.TInsuranceUnpurchasePersonMapper">

  <resultMap id="tInsuranceUnpurchasePersonMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TInsuranceUnpurchasePerson">
    <id property="id" column="ID"/>
    <result property="parnetId" column="PARNET_ID"/>
    <result property="empName" column="EMP_NAME"/>
    <result property="empIdcardNo" column="EMP_IDCARD_NO"/>
    <result property="post" column="POST"/>
    <result property="applyNoDetail" column="APPLY_NO_DETAIL"/>
    <result property="deptId" column="DEPT_ID"/>
    <result property="deptNo" column="DEPT_NO"/>
    <result property="deptName" column="DEPT_NAME"/>
    <result property="createUserDeptId" column="CREATE_USER_DEPT_ID"/>
    <result property="createUserDeptName" column="CREATE_USER_DEPT_NAME"/>
    <result property="reasonType" column="REASON_TYPE"/>
    <result property="reasonInfo" column="REASON_INFO"/>
    <result property="applyNo" column="APPLY_NO"/>
    <result property="socialStatus" column="SOCIAL_STATUS"/>
    <result property="salaryNum" column="SALARY_NUM"/>
    <result property="createBy" column="CREATE_BY"/>
    <result property="createName" column="CREATE_NAME"/>
    <result property="createTime" column="CREATE_TIME"/>
    <result property="updateBy" column="UPDATE_BY"/>
    <result property="updateTime" column="UPDATE_TIME"/>
    <result property="hasContainRisks" column="HAS_CONTAIN_RISKS"/>
    <result property="newLine" column="NEW_LINE"/>
    <result property="division" column="DIVISION"/>
    <result property="insuranceFlag" column="INSURANCE_FLAG"/>
	<result property="historicalApplicationDate" column="HISTORICAL_APPLICATION_DATE"/>
  </resultMap>

	<resultMap id="tInsuranceUnpurchasePersonExportMap" type="com.yifu.cloud.plus.v1.yifu.salary.vo.TInsuranceUnpurchasePersonVo">
		<result property="empName" column="EMP_NAME"/>
		<result property="empIdcardNo" column="EMP_IDCARD_NO"/>
		<result property="applyNoDetail" column="APPLY_NO_DETAIL"/>
		<result property="deptNo" column="DEPT_NO"/>
		<result property="deptName" column="DEPT_NAME"/>
		<result property="reasonType" column="REASON_TYPE"/>
		<result property="applyNo" column="APPLY_NO"/>
		<result property="socialStatus" column="SOCIAL_STATUS"/>
		<result property="salaryNum" column="SALARY_NUM"/>
		<result property="createName" column="CREATE_NAME"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="newLine" column="NEW_LINE"/>
		<result property="division" column="DIVISION"/>
	</resultMap>

    <sql id="Base_Column_List">
                a.ID,
                a.PARNET_ID,
                a.EMP_NAME,
                a.EMP_IDCARD_NO,
                a.POST,
                a.APPLY_NO_DETAIL,
                a.DEPT_ID,
                a.DEPT_NO,
                a.DEPT_NAME,
                a.CREATE_USER_DEPT_ID,
                a.CREATE_USER_DEPT_NAME,
                a.REASON_TYPE,
                a.REASON_INFO,
                a.APPLY_NO,
                a.SOCIAL_STATUS,
                a.SALARY_NUM,
                a.CREATE_BY,
                a.CREATE_NAME,
                a.CREATE_TIME,
                a.UPDATE_BY,
                a.UPDATE_TIME,
                a.HAS_CONTAIN_RISKS,
				a.NEW_LINE,
				a.DIVISION,
				a.INSURANCE_FLAG,
				a.HISTORICAL_APPLICATION_DATE
	</sql>
    <sql id="tInsuranceUnpurchasePerson_where">
        <if test="tInsuranceUnpurchasePerson != null">
                    <if test="tInsuranceUnpurchasePerson.id != null and tInsuranceUnpurchasePerson.id.trim() != ''">
                        AND a.ID = #{tInsuranceUnpurchasePerson.id}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.empName != null and tInsuranceUnpurchasePerson.empName.trim() != ''">
                        AND a.EMP_NAME = #{tInsuranceUnpurchasePerson.empName}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.empIdcardNo != null and tInsuranceUnpurchasePerson.empIdcardNo.trim() != ''">
                        AND a.EMP_IDCARD_NO = #{tInsuranceUnpurchasePerson.empIdcardNo}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.applyNoDetail != null and tInsuranceUnpurchasePerson.applyNoDetail.trim() != ''">
                        AND a.APPLY_NO_DETAIL = #{tInsuranceUnpurchasePerson.applyNoDetail}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.deptNo != null and tInsuranceUnpurchasePerson.deptNo.trim() != ''">
                        AND a.DEPT_NO = #{tInsuranceUnpurchasePerson.deptNo}
                    </if>
					<if test="tInsuranceUnpurchasePerson.deptName != null and tInsuranceUnpurchasePerson.deptName.trim() != ''">
						AND a.DEPT_NAME like concat('%',#{tInsuranceUnpurchasePerson.deptName},'%')
					</if>
                    <if test="tInsuranceUnpurchasePerson.reasonType != null and tInsuranceUnpurchasePerson.reasonType.trim() != ''">
                        AND a.REASON_TYPE = #{tInsuranceUnpurchasePerson.reasonType}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.applyNo != null and tInsuranceUnpurchasePerson.applyNo.trim() != ''">
                        AND a.APPLY_NO = #{tInsuranceUnpurchasePerson.applyNo}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.socialStatus != null and tInsuranceUnpurchasePerson.socialStatus.trim() != ''">
                        AND a.SOCIAL_STATUS = #{tInsuranceUnpurchasePerson.socialStatus}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.salaryNumStart != null">
                        AND a.SALARY_NUM <![CDATA[ >= ]]> #{tInsuranceUnpurchasePerson.salaryNumStart}
                    </if>
					<if test="tInsuranceUnpurchasePerson.salaryNumEnd != null">
						AND a.SALARY_NUM <![CDATA[ <= ]]> #{tInsuranceUnpurchasePerson.salaryNumEnd}
					</if>
                    <if test="tInsuranceUnpurchasePerson.createBy != null and tInsuranceUnpurchasePerson.createBy.trim() != ''">
                        AND a.CREATE_BY = #{tInsuranceUnpurchasePerson.createBy}
                    </if>
                    <if test="tInsuranceUnpurchasePerson.createName != null and tInsuranceUnpurchasePerson.createName.trim() != ''">
                        AND a.CREATE_NAME = #{tInsuranceUnpurchasePerson.createName}
                    </if>
					<if test="tInsuranceUnpurchasePerson.createTimeStart != null">
						AND a.CREATE_TIME <![CDATA[ >= ]]> #{tInsuranceUnpurchasePerson.createTimeStart}
					</if>
					<if test="tInsuranceUnpurchasePerson.createTimeEnd != null">
						AND a.CREATE_TIME <![CDATA[ <= ]]> #{tInsuranceUnpurchasePerson.createTimeEnd}
					</if>
					<if test="tInsuranceUnpurchasePerson.authSql != null and tInsuranceUnpurchasePerson.authSql.trim() != ''">
						${tInsuranceUnpurchasePerson.authSql}
					</if>
        </if>
    </sql>
    <!--tInsuranceUnpurchasePerson简单分页查询-->
    <select id="getTInsuranceUnpurchasePersonPage" resultMap="tInsuranceUnpurchasePersonMap">
        SELECT
		a.ID,
		a.PARNET_ID,
		a.EMP_NAME,
		a.EMP_IDCARD_NO,
		a.POST,
		a.APPLY_NO_DETAIL,
		a.DEPT_ID,
		a.DEPT_NO,
		a.DEPT_NAME,
		a.CREATE_USER_DEPT_ID,
		a.CREATE_USER_DEPT_NAME,
		a.REASON_TYPE,
		a.REASON_INFO,
		a.APPLY_NO,
		if(a.REASON_TYPE = '2','',a.SOCIAL_STATUS) SOCIAL_STATUS,
		a.SALARY_NUM,
		a.CREATE_BY,
		a.CREATE_NAME,
		a.CREATE_TIME,
		a.UPDATE_BY,
		a.UPDATE_TIME,
		a.HAS_CONTAIN_RISKS,
		a.NEW_LINE,
		a.DIVISION,
		a.INSURANCE_FLAG
        FROM t_insurance_unpurchase_person a
        <where>
            1=1 and STATUS = '3'
            <include refid="tInsuranceUnpurchasePerson_where"/>
        </where>
		order by a.CREATE_TIME desc
    </select>

	<!--tInsuranceUnpurchasePerson导出条数查询-->
	<select id="getTInsuranceUnpurchaseInfoExportCount" resultType="java.lang.Long">
		SELECT
		count(1)
		FROM t_insurance_unpurchase_person a
		<where>
			1=1 and STATUS = '3'
			<include refid="tInsuranceUnpurchasePerson_where"/>
		</where>
	</select>

	<!--tInsuranceUnpurchasePerson导出查询-->
	<select id="getTInsuranceUnpurchaseInfoExportList" resultMap="tInsuranceUnpurchasePersonExportMap">
		SELECT
		APPLY_NO_DETAIL,
		APPLY_NO,
		DEPT_NAME,
		DEPT_NO,
		NEW_LINE,
		DIVISION,
		EMP_NAME,
		EMP_IDCARD_NO,
		if(a.REASON_TYPE = '1','已购买社保','人员已离职') REASON_TYPE,
		if(a.REASON_TYPE = '2','',if(a.SOCIAL_STATUS='0','在保','不在保')) SOCIAL_STATUS,
		SALARY_NUM,
		CREATE_NAME,
		CREATE_TIME
		FROM t_insurance_unpurchase_person a
		<where>
			1=1 and STATUS = '3'
			<include refid="tInsuranceUnpurchasePerson_where"/>
		</where>
		order by a.CREATE_TIME desc
		<if test="tInsuranceUnpurchasePerson.limitStart != null">
			limit #{tInsuranceUnpurchasePerson.limitStart},#{tInsuranceUnpurchasePerson.limitEnd}
		</if>
	</select>

	<select id="getTInsuranceUnpurchasePersonList" resultType="com.yifu.cloud.plus.v1.yifu.salary.vo.TInsuranceUnpurchasePersonAddVo">
		SELECT
		a.ID,
		a.EMP_NAME empName,
		a.EMP_IDCARD_NO empIdcardNo,
		if(a.POST = '--','',post) post,
		a.HISTORICAL_APPLICATION_DATE
		FROM t_insurance_unpurchase_person a
		where a.PARNET_ID = #{id}
	</select>


	<!--不购买商险，薪资使用 hgw-->
	<select id="getBuyList" resultMap="tInsuranceUnpurchasePersonMap">
		SELECT
		ID,
		EMP_NAME,
		EMP_IDCARD_NO,
		REASON_TYPE,
		SOCIAL_STATUS,
		SALARY_NUM
		FROM t_insurance_unpurchase_person a
		where STATUS = '3' AND (REASON_TYPE = '1' or (REASON_TYPE = '2' AND SALARY_NUM = 0) ) AND DEPT_NO = #{deptNo} AND EMP_IDCARD_NO in
		<foreach item="idStr" index="index" collection="idCardList" open="(" separator="," close=")">
			#{idStr}
		</foreach>
	</select>

	<!--不购买商险-社保，薪资定时任务刷社保状态使用 hgw-->
	<select id="getSocialBuyList" resultType="java.lang.String">
		SELECT
		EMP_IDCARD_NO
		FROM t_insurance_unpurchase_person a
		where STATUS = '3' AND REASON_TYPE = '1'
	</select>

	<!--更新是否在保为0：在保 hgw-->
	<update id="updateSocialStatusZero">
		update t_insurance_unpurchase_person a
		set a.SOCIAL_STATUS = '0'
		where STATUS = '3' AND REASON_TYPE = '1' AND EMP_IDCARD_NO in
		<foreach item="idStr" index="index" collection="idCardList" open="(" separator="," close=")">
			#{idStr}
		</foreach>
	</update>

	<!--更新是否在保为1：不在保 hgw-->
	<update id="updateSocialStatusOne">
		update t_insurance_unpurchase_person a
		set a.SOCIAL_STATUS = '1'
		where STATUS = '3' AND REASON_TYPE = '1' AND EMP_IDCARD_NO in
		<foreach item="idStr" index="index" collection="idCardList" open="(" separator="," close=")">
			#{idStr}
		</foreach>
	</update>

</mapper>
