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

  <resultMap id="tInsuranceUnpurchasePersonSalaryMap" type="com.yifu.cloud.plus.v1.yifu.salary.entity.TInsuranceUnpurchasePersonSalary">
    <id property="id" column="ID"/>
    <result property="parentId" column="PARENT_ID"/>
    <result property="salaryId" column="SALARY_ID"/>
    <result property="applyNo" column="APPLY_NO"/>
    <result property="salaryMonth" column="SALARY_MONTH"/>
    <result property="deleteFlag" column="DELETE_FLAG"/>
    <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"/>
  </resultMap>
    <sql id="Base_Column_List">
			a.ID,
			a.PARENT_ID,
			a.SALARY_ID,
			a.APPLY_NO,
			a.SALARY_MONTH,
			a.DELETE_FLAG,
			a.CREATE_BY,
			a.CREATE_NAME,
			a.CREATE_TIME,
			a.UPDATE_BY,
			a.UPDATE_TIME
	</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.parentId != null and tInsuranceUnpurchasePerson.parentId.trim() != ''">
					AND a.PARENT_ID = #{tInsuranceUnpurchasePerson.parentId}
				</if>
				<if test="tInsuranceUnpurchasePerson.salaryId != null and tInsuranceUnpurchasePerson.salaryId.trim() != ''">
					AND a.SALARY_ID = #{tInsuranceUnpurchasePerson.salaryId}
				</if>
				<if test="tInsuranceUnpurchasePerson.applyNo != null and tInsuranceUnpurchasePerson.applyNo.trim() != ''">
					AND a.APPLY_NO = #{tInsuranceUnpurchasePerson.applyNo}
				</if>
				<if test="tInsuranceUnpurchasePerson.salaryMonth != null and tInsuranceUnpurchasePerson.salaryMonth.trim() != ''">
					AND a.SALARY_MONTH = #{tInsuranceUnpurchasePerson.salaryMonth}
				</if>
        </if>
    </sql>
    <!--tInsuranceUnpurchasePerson简单分页查询-->
    <select id="getTInsuranceUnpurchasePersonSalaryPage" resultMap="tInsuranceUnpurchasePersonSalaryMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_insurance_unpurchase_person_salary a
        where  DELETE_FLAG = 0
		<include refid="tInsuranceUnpurchasePerson_where"/>
    </select>

	<!-- 获取parentId -->
    <select id="getParentIdBySalaryId" resultType="java.lang.String">
		SELECT
			a.id
		FROM t_insurance_unpurchase_person a
		join t_insurance_unpurchase_person_salary s on s.PARENT_ID=a.ID
		where s.SALARY_ID = #{salaryId}
	</select>

    <!--删除关联项-->
    <update id="deletePersonSalaryBySalaryId">
		update t_insurance_unpurchase_person_salary a
		set a.DELETE_FLAG = 1
        where  a.SALARY_ID = #{salaryId}
    </update>

	<!-- 更新次数 -->
    <update id="updatePersonSalaryNumByPersonIdList">
		update t_insurance_unpurchase_person a
		JOIN (
			select sum(if(c.DELETE_FLAG = 0,1,0)) num,c.PARENT_ID from t_insurance_unpurchase_person_salary c where c.PARENT_ID in
			<foreach item="idStr" index="index" collection="idList" open="(" separator="," close=")">
				#{idStr}
			</foreach>
		    GROUP BY c.PARENT_ID
		) d on d.PARENT_ID=a.id
		set a.SALARY_NUM = d.num
		where a.id in
		<foreach item="idStr" index="index" collection="idList" open="(" separator="," close=")">
			#{idStr}
		</foreach>
    </update>

</mapper>
