<?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.TInsuranceUnpurchaseApplyMapper">
	<resultMap id="tInsuranceUnpurchaseApplyMap"
			   type="com.yifu.cloud.plus.v1.yifu.salary.entity.TInsuranceUnpurchaseApply">
		<id property="id" column="ID"/>
		<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="auditFlag" column="AUDIT_FLAG"/>
		<result property="companyFlag" column="COMPANY_FLAG"/>
		<result property="createBy" column="CREATE_BY"/>
		<result property="createName" column="CREATE_NAME"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="createTimeZc" column="CREATE_TIME_ZC"/>
		<result property="updateBy" column="UPDATE_BY"/>
		<result property="updateTime" column="UPDATE_TIME"/>
		<result property="deleteFlag" column="DELETE_FLAG"/>
		<result property="status" column="STATUS"/>
		<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="auditUser" column="AUDIT_USER"/>
		<result property="auditUserId" column="AUDIT_USER_ID"/>
		<result property="unbuyNum" column="UNBUY_NUM"/>
	</resultMap>

	<resultMap id="tInsuranceUnpurchaseApplyExportMap"
			   type="com.yifu.cloud.plus.v1.yifu.salary.vo.TInsuranceUnpurchaseApplyVo">
		<result property="deptNo" column="DEPT_NO"/>
		<result property="deptName" column="DEPT_NAME"/>
		<result property="createUserDeptName" column="CREATE_USER_DEPT_NAME"/>
		<result property="reasonType" column="REASON_TYPE"/>
		<result property="applyNo" column="APPLY_NO"/>
		<result property="createName" column="CREATE_NAME"/>
		<result property="createTime" column="CREATE_TIME"/>
		<result property="status" column="STATUS"/>
		<result property="auditUser" column="AUDIT_USER"/>
	</resultMap>

	<sql id="Base_Column_List">
		a.ID, 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.AUDIT_FLAG, a.COMPANY_FLAG, a.CREATE_BY, a.CREATE_NAME, a.CREATE_TIME,
		a.UPDATE_BY, a.UPDATE_TIME, a.DELETE_FLAG, a.STATUS, a.HAS_CONTAIN_RISKS,
		a.NEW_LINE, a.DIVISION, a.INSURANCE_FLAG, a.AUDIT_USER, a.AUDIT_USER_ID,a.CREATE_TIME_ZC,UNBUY_NUM
	</sql>
	<sql id="tInsuranceUnpurchaseApply_where">
		<if test="tInsuranceUnpurchaseApply != null">
			<if test="tInsuranceUnpurchaseApply.id != null and tInsuranceUnpurchaseApply.id.trim() != ''">
				AND a.ID = #{tInsuranceUnpurchaseApply.id}
			</if>
			<if test="tInsuranceUnpurchaseApply.deptNo != null and tInsuranceUnpurchaseApply.deptNo.trim() != ''">
				AND a.DEPT_NO = #{tInsuranceUnpurchaseApply.deptNo}
			</if>
			<if test="tInsuranceUnpurchaseApply.deptName != null and tInsuranceUnpurchaseApply.deptName.trim() != ''">
				AND a.DEPT_NAME like concat('%',#{tInsuranceUnpurchaseApply.deptName},'%')
			</if>
			<if test="tInsuranceUnpurchaseApply.applyNo != null and tInsuranceUnpurchaseApply.applyNo.trim() != ''">
				AND a.APPLY_NO = #{tInsuranceUnpurchaseApply.applyNo}
			</if>
			<if test="tInsuranceUnpurchaseApply.createBy != null and tInsuranceUnpurchaseApply.createBy.trim() != ''">
				AND a.CREATE_BY = #{tInsuranceUnpurchaseApply.createBy}
			</if>
			<if test="tInsuranceUnpurchaseApply.createName != null and tInsuranceUnpurchaseApply.createName.trim() != ''">
				AND a.CREATE_NAME = #{tInsuranceUnpurchaseApply.createName}
			</if>
			<if test="tInsuranceUnpurchaseApply.createTimeStart != null">
				AND a.CREATE_TIME_ZC <![CDATA[ >= ]]> #{tInsuranceUnpurchaseApply.createTimeStart}
			</if>
			<if test="tInsuranceUnpurchaseApply.createTimeEnd != null">
				AND a.CREATE_TIME_ZC <![CDATA[ <= ]]> #{tInsuranceUnpurchaseApply.createTimeEnd}
			</if>
			<if test="tInsuranceUnpurchaseApply.status != null and tInsuranceUnpurchaseApply.status.trim() != ''">
				AND a.STATUS = #{tInsuranceUnpurchaseApply.status}
			</if>
			<if test="tInsuranceUnpurchaseApply.auditUser != null and tInsuranceUnpurchaseApply.auditUser.trim() != ''">
				AND a.AUDIT_USER = #{tInsuranceUnpurchaseApply.auditUser}
			</if>
			<if test="tInsuranceUnpurchaseApply.auditUserId != null and tInsuranceUnpurchaseApply.auditUserId.trim() != ''">
				AND a.AUDIT_USER_ID = #{tInsuranceUnpurchaseApply.auditUserId}
			</if>
			<if test="tInsuranceUnpurchaseApply.authSql != null and tInsuranceUnpurchaseApply.authSql.trim() != ''">
				${tInsuranceUnpurchaseApply.authSql}
			</if>
		</if>
	</sql>

	<!-- tInsuranceUnpurchaseApply简单分页查询 -->
	<select id="getTInsuranceUnpurchaseApplyPage" resultMap="tInsuranceUnpurchaseApplyMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_insurance_unpurchase_apply a
		<where>
			1=1 and a.DELETE_FLAG = '0'
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
		order by
		ifnull(a.CREATE_TIME_ZC,'2999-12-31 00:00:00') DESC
	</select>

	<!-- tInsuranceUnpurchaseApply简单分页查询 -->
	<select id="getTInsuranceUnpurchaseApplyExportCount" resultType="java.lang.Long">
		SELECT
		count(1)
		FROM t_insurance_unpurchase_apply a
		<where>
			1=1 and a.DELETE_FLAG = '0'
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
	</select>

	<select id="selectSocialCount" resultType="java.lang.Long">
		SELECT
		count(1)
		FROM t_insurance_unpurchase_apply a
		inner join t_insurance_unpurchase_person b
		on a.id = b.PARNET_ID
		where
			1=1 and a.DELETE_FLAG = '0' and a.reason_type = '1' and a.status in ('2','3')
			<if test="id != null ">
				AND a.id != #{id}
			</if>
			and a.DEPT_NO = #{deptNo} and b.EMP_IDCARD_NO = #{cardNo}
	</select>

	<select id="selectArchCount" resultType="java.lang.Long">
		SELECT
		count(1)
		FROM t_insurance_unpurchase_apply a
		inner join t_insurance_unpurchase_person b
		on a.id = b.PARNET_ID
		where
		1=1 and a.DELETE_FLAG = '0' and a.reason_type = '2' and a.status in ('2','3') and b.SALARY_NUM = 0
		<if test="id != null ">
			AND a.id != #{id}
		</if>
		and a.DEPT_NO = #{deptNo} and b.EMP_IDCARD_NO = #{cardNo}
	</select>

	<!-- tInsuranceUnpurchaseApply简单分页查询 -->
	<select id="getTInsuranceUnpurchaseApplyExportList" resultMap="tInsuranceUnpurchaseApplyExportMap">
		SELECT
		a.APPLY_NO,
		a.DEPT_NAME,
		a.DEPT_NO,
		a.CREATE_NAME,
		a.CREATE_USER_DEPT_NAME,
		a.CREATE_TIME_ZC as CREATE_TIME,
		a.AUDIT_USER,
		if(a.REASON_TYPE = '1','已购买社保','人员已离职') REASON_TYPE,
		CASE WHEN a.STATUS=0 THEN "草稿"
		WHEN a.STATUS =1 THEN "待提交"
		WHEN a.STATUS =2 THEN "待审核"
		WHEN a.STATUS =3 THEN "审核通过"
		WHEN a.STATUS =4 THEN "审核不通过"
		ELSE a.STATUS END as "STATUS"
		FROM t_insurance_unpurchase_apply a
		<where>
			 a.DELETE_FLAG = '0'
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
		order by ifnull(a.CREATE_TIME_ZC,'2999-12-31 00:00:00') DESC
		<if test="tInsuranceUnpurchaseApply.limitStart != null">
			limit #{tInsuranceUnpurchaseApply.limitStart},#{tInsuranceUnpurchaseApply.limitEnd}
		</if>
	</select>

	<select id="getAuditPage" resultMap="tInsuranceUnpurchaseApplyMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_insurance_unpurchase_apply a
		left join t_audit_record r on a.id=r.domain_id
		<where>
			a.DELETE_FLAG='0' and r.approval_man= #{userId} and r.audit_type='0' and r.approval_result = '2' and r.delete_flag='0'
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
		order by r.CREATE_TIME asc
	</select>

	<select id="getHistoryHandlePage" resultMap="tInsuranceUnpurchaseApplyMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_insurance_unpurchase_apply a
		left join (
		   select min(approval_time) approval_time,domain_id,approval_man from t_audit_record
		   where approval_result in('0','1') and audit_type = '0' and delete_flag='0'
		   group by domain_id,approval_man
		) r on a.id=r.domain_id
		<where>
			a.DELETE_FLAG='0' and r.approval_man= #{userId}
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
		order by r.approval_time desc
	</select>

	<select id="getAuditCount" resultType="integer">
		SELECT count(1) FROM t_insurance_unpurchase_apply a
		left join t_audit_record r on a.id=r.domain_id
		<where>
			a.DELETE_FLAG='0' and r.approval_man= #{userId} and r.audit_type='0' and r.approval_result = '2' and r.delete_flag='0'
		</where>
	</select>

	<select id="getHistoryHandleCount" resultType="integer">
		SELECT count(1) FROM t_insurance_unpurchase_apply a
		left join (
		select min(approval_time) approval_time,domain_id,approval_man from t_audit_record
		where approval_result in('0','1') and audit_type = '0' and delete_flag='0'
		group by domain_id,approval_man
		) r on a.id=r.domain_id
		<where>
			a.DELETE_FLAG='0' and r.approval_man= #{userId}
		</where>
	</select>

	<select id="getCopyPage" resultMap="tInsuranceUnpurchaseApplyMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_insurance_unpurchase_apply a
		left join (
		   select min(create_time)  create_time,domain_id,audit_user_id from t_audit_copy_info
		   where audit_type = '0' and delete_flag = '0'
		   group by domain_id,audit_user_id
		)  r on a.id=r.domain_id
		<where>
			a.DELETE_FLAG='0' and r.audit_user_id= #{userId}
			<include refid="tInsuranceUnpurchaseApply_where"/>
		</where>
		order by  r.create_time desc
	</select>

	<!-- 获取当日最大的编码 -->
	<select id="getMaxInusuranceCode" resultType="java.lang.String">
		SELECT ifnull(max(right (e.APPLY_NO, 3)), 0) EMP_CODE
		FROM t_insurance_unpurchase_apply e
		where e.DELETE_FLAG = '0'
		and e.CREATE_TIME_ZC > DATE_FORMAT(now(), '%Y-%m-%d')
	</select>

	<!-- 获取当日最大的编码 -->
	<select id="getMaxInusuranceDetailCode" resultType="java.lang.String">
		SELECT ifnull(max(right (e.APPLY_NO_DETAIL, 5)), 0) EMP_CODE
		FROM t_insurance_unpurchase_person e
		where e.CREATE_TIME > DATE_FORMAT(now(), '%Y-%m-%d')
	</select>


</mapper>
