<?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.archives.mapper.TEmployeeContractPreMapper">

  <resultMap id="tEmployeeContractPreMap" type="com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeContractPre">
    <id property="id" column="id"/>
    <result property="deptName" column="dept_name"/>
    <result property="deptNo" column="dept_no"/>
    <result property="deptId" column="dept_id"/>
    <result property="employeeName" column="employee_name"/>
    <result property="empIdcard" column="emp_idcard"/>
    <result property="empPhone" column="emp_phone"/>
    <result property="position" column="position"/>
    <result property="joinLeaveDate" column="join_leave_date"/>
    <result property="contractType" column="contract_type"/>
    <result property="processStatus" column="process_status"/>
    <result property="signatory" column="signatory"/>
    <result property="customerUsername" column="customer_username"/>
    <result property="customerUserLoginname" column="customer_user_loginname"/>
    <result property="signType" column="sign_type"/>
    <result property="signFlag" column="sign_flag"/>
    <result property="errorInfo" column="error_info"/>
    <result property="errorTime" column="error_time"/>
    <result property="fadadaTemplate" column="fadada_template"/>
    <result property="dataSource" column="data_source"/>
    <result property="expectedCollectionTime" column="expected_collection_time"/>
    <result property="contractStart" column="contract_start"/>
    <result property="contractEnd" column="contract_end"/>
    <result property="situation" column="situation"/>
    <result property="contractDurationYear" column="contract_duration_year"/>
    <result property="contractDurationMonth" column="contract_duration_month"/>
    <result property="contractTerm" column="CONTRACT_TERM"/>
    <result property="tryPeriodStart" column="try_period_start"/>
    <result property="tryPeriodEnd" column="try_period_end"/>
	<result property="task" column="TASK"/>
	<result property="taskType" column="TASK_TYPE"/>
	<result property="taskEndStandard" column="TASK_END_STANDARD"/>
    <result property="expectedConfirmTime" column="expected_confirm_time"/>
    <result property="configName" column="config_name"/>
    <result property="updateFlag" column="update_flag"/>
    <result property="tryPeriodType" column="TRY_PERIOD_TYPE"/>
    <result property="tryPeriod" column="TRY_PERIOD"/>
    <result property="salaryType" column="SALARY_TYPE"/>
    <result property="salaryStandardPerHour" column="SALARY_STANDARD_PER_HOUR"/>
    <result property="salaryStandardPerPiece" column="SALARY_STANDARD_PER_PIECE"/>
    <result property="configId" column="config_id"/>
    <result property="workingDept" column="WORKING_DEPT"/>
    <result property="workingCompany" column="WORKING_COMPANY"/>
    <result property="workingHours" column="working_hours"/>
    <result property="workingReward" column="working_reward"/>
    <result property="riskBuyDesc" column="RISK_BUY_DESC"/>
    <result property="otherWage" column="OTHER_WAGE"/>
    <result property="laborCost" column="LABOR_COST"/>
    <result property="defaultAmount" column="DEFAULT_AMOUNT"/>
    <result property="settlementCycle" column="SETTLEMENT_CYCLE"/>
    <result property="paymentTime" column="PAYMENT_TIME"/>
    <result property="paymentType" column="PAYMENT_TYPE"/>
    <result property="dispatchPeriodYear" column="DISPATCH_PERIOD_YEAR"/>
    <result property="dispatchPeriodMonth" column="DISPATCH_PERIOD_MONTH"/>
    <result property="dispatchPeriodStart" column="DISPATCH_PERIOD_START"/>
	<result property="registerId" column="register_id"/>
	<result property="contractFlag" column="contract_flag"/>
	<result property="revokeReason" column="revoke_reason"/>
    <result property="dispatchPeriodEnd" column="DISPATCH_PERIOD_END"/>
    <result property="workType" column="WORK_TYPE"/>
    <result property="workDayCount" column="WORK_DAY_COUNT"/>
    <result property="workDay" column="WORK_DAY"/>
    <result property="workHours" column="WORK_HOURS"/>
    <result property="otherWorkDay" column="OTHER_WORK_DAY"/>
    <result property="internshipPeriod" column="INTERNSHIP_PERIOD"/>
    <result property="internshipPeriodStart" column="INTERNSHIP_PERIOD_START"/>
    <result property="internshipPeriodEnd" column="INTERNSHIP_PERIOD_END"/>
	<result property="confirmEmpSelect" column="CONFIRM_EMP_SELECT"/>
	<result property="timeoutElecSign" column="TIMEOUT_ELEC_SIGN"/>
	<result property="contractEndType" column="CONTRACT_END_TYPE"/>
    <result property="postType" column="POST_TYPE"/>
    <result property="post" column="POST"/>
    <result property="contractId" column="contract_id"/>
    <result property="workAddress" column="WORK_ADDRESS"/>
    <result property="updateTime" column="update_time"/>
    <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="contractSubName" column="CONTRACT_SUB_NAME,"/>
  </resultMap>
    <sql id="Base_Column_List">
			id
			,dept_name
			,dept_no
			,dept_id
			,employee_name
			,emp_idcard
			,emp_phone
			,position
			,join_leave_date
			,contract_type
			,process_status
			,signatory
			,customer_username
			,customer_user_loginname
			,sign_type
			,fadada_template
			,data_source
			,expected_confirm_time
			,expected_collection_time
			,contract_start
			,contract_end
			,situation
			,try_period_start
			,try_period_end
			,config_name
			,update_flag
			,try_period_type
			,try_period
			,salary_type
			,salary_standard_per_hour
			,salary_standard_per_piece
			,config_id
			,working_dept
			,working_company
			,risk_buy_desc
			,other_wage
			,labor_cost
			,default_amount
			,settlement_cycle
			,payment_time
			,payment_type
			,dispatch_period_start
			,dispatch_period_end
			,work_type
			,work_day_count
			,work_day
			,work_hours
			,other_work_day
			,internship_period
			,internship_period_start
			,internship_period_end
			,post_type
			,post
			,work_address
			,contract_term
			,contract_duration_year
			,contract_duration_month
			,register_id
			,sign_flag
			,error_info
			,contract_id
			,revoke_reason
			,working_reward
			,working_hours
			,dispatch_period_year
			,dispatch_period_month
			,error_time
			,contract_end_type
			,confirm_emp_select
			,timeout_elec_sign
			,update_time
			,create_by
			,create_name
			,create_time
			,update_by
		    ,TASK
		    ,TASK_TYPE
		    ,TASK_END_STANDARD,contract_flag,a.CONTRACT_SUB_NAME
    </sql>
    <sql id="tEmployeeContractPre_where">
        <if test="tEmployeeContractPre != null">
			<if test="tEmployeeContractPre.id != null and tEmployeeContractPre.id.trim() != ''">
				AND a.id = #{tEmployeeContractPre.id}
			</if>
			<if test="tEmployeeContractPre.idList != null">
				AND a.id in
				<foreach item="idStr" index="index" collection="tEmployeeContractPre.idList" open="(" separator=","
						 close=")">
					#{idStr}
				</foreach>
			</if>
			<if test="tEmployeeContractPre.deptName != null and tEmployeeContractPre.deptName.trim() != ''">
				AND a.dept_name like CONCAT('%',#{tEmployeeContractPre.deptName},'%')
			</if>
			<if test="tEmployeeContractPre.deptNo != null and tEmployeeContractPre.deptNo.trim() != ''">
				AND a.dept_no = #{tEmployeeContractPre.deptNo}
			</if>
			<if test="tEmployeeContractPre.deptId != null and tEmployeeContractPre.deptId.trim() != ''">
				AND a.dept_id = #{tEmployeeContractPre.deptId}
			</if>
			<if test="tEmployeeContractPre.employeeName != null and tEmployeeContractPre.employeeName.trim() != ''">
				AND a.employee_name = #{tEmployeeContractPre.employeeName}
			</if>
			<if test="tEmployeeContractPre.empIdcard != null and tEmployeeContractPre.empIdcard.trim() != ''">
				AND a.emp_idcard like CONCAT('%',#{tEmployeeContractPre.empIdcard},'%')
			</if>
			<if test="tEmployeeContractPre.empPhone != null and tEmployeeContractPre.empPhone.trim() != ''">
				AND a.emp_phone = #{tEmployeeContractPre.empPhone}
			</if>
			<if test="tEmployeeContractPre.position != null and tEmployeeContractPre.position.trim() != ''">
				AND a.position like CONCAT('%',#{tEmployeeContractPre.position},'%')
			</if>
			<if test="tEmployeeContractPre.joinTimeStart != null">
				AND a.join_leave_date <![CDATA[ >= ]]> #{tEmployeeContractPre.joinTimeStart}
			</if>
			<if test="tEmployeeContractPre.joinTimeEnd != null">
				AND a.join_leave_date <![CDATA[ <= ]]> #{tEmployeeContractPre.joinTimeEnd}
			</if>
			<if test="tEmployeeContractPre.contractType != null and tEmployeeContractPre.contractType.trim() != ''">
				AND a.contract_type = #{tEmployeeContractPre.contractType}
			</if>
			<if test="tEmployeeContractPre.processStatus != null and tEmployeeContractPre.processStatus.trim() != ''">
				AND a.process_status = #{tEmployeeContractPre.processStatus}
			</if>
			<if test="tEmployeeContractPre.statusList != null and tEmployeeContractPre.statusList.size() > 0">
				AND a.process_status in
				<foreach item="item" index="index" collection="tEmployeeContractPre.statusList" open="(" separator=","
						 close=")">
					'${item}'
				</foreach>
			</if>
			<if test="tEmployeeContractPre.signatory != null and tEmployeeContractPre.signatory.trim() != ''">
				AND a.signatory like CONCAT('%',#{tEmployeeContractPre.signatory},'%')
			</if>
			<if test="tEmployeeContractPre.customerUsername != null and tEmployeeContractPre.customerUsername.trim() != ''">
				AND a.customer_username = #{tEmployeeContractPre.customerUsername}
			</if>
			<if test="tEmployeeContractPre.customerUserLoginname != null and tEmployeeContractPre.customerUserLoginname.trim() != ''">
				AND a.customer_user_loginname = #{tEmployeeContractPre.customerUserLoginname}
			</if>
			<if test="tEmployeeContractPre.signType != null and tEmployeeContractPre.signType.trim() != ''">
				AND a.sign_type = #{tEmployeeContractPre.signType}
			</if>
			<if test="tEmployeeContractPre.fadadaTemplate != null and tEmployeeContractPre.fadadaTemplate.trim() != ''">
				AND a.fadada_template like CONCAT('%',#{tEmployeeContractPre.fadadaTemplate},'%')
			</if>
			<if test="tEmployeeContractPre.expectedCollectionTime != null">
				AND a.expected_collection_time = #{tEmployeeContractPre.expectedCollectionTime}
			</if>
			<if test="tEmployeeContractPre.expectedConfirmTime != null">
				AND DATE_FORMAT(a.expected_confirm_time,"%Y-%m-%d") =
				DATE_FORMAT(#{tEmployeeContractPre.expectedConfirmTime},"%Y-%m-%d")
			</if>
			<if test="tEmployeeContractPre.authSql != null and tEmployeeContractPre.authSql.trim() != ''">
				${tEmployeeContractPre.authSql}
			</if>
			<if test="tEmployeeContractPre.deptNoList != null and tEmployeeContractPre.deptNoList.size > 0">
				AND a.dept_no in
				<foreach item="idStr" index="index" collection="tEmployeeContractPre.deptNoList" open="(" separator=","
						 close=")">
					#{idStr}
				</foreach>
			</if>
        </if>
    </sql>
    <!--tEmployeeContractPre简单分页查询-->
    <select id="getTEmployeeContractPrePage" resultMap="tEmployeeContractPreMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_employee_contract_pre a
        <where>
            1=1
            <include refid="tEmployeeContractPre_where"/>
        </where>
		order by a.create_time desc
    </select>

	<!--tEmployeeContractPre c端不分页查询-->
	<select id="getTEmployeeContractPreNoPage" resultMap="tEmployeeContractPreMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_employee_contract_pre a
		<where>
			1=1
			<include refid="tEmployeeContractPre_where"/>
		</where>
		order by a.create_time desc
		limit 0,2000
	</select>

	<!--tEmployeeContractPre导出查询-->
	<select id="getTEmployeeContractPreExportCount" resultType="java.lang.Long">
		SELECT
		 count(1)
		FROM t_employee_contract_pre a
		<where>
			1=1
			<include refid="tEmployeeContractPre_where"/>
		</where>
	</select>

	<!--tEmployeeContractPre导出查询-->
	<select id="getTEmployeeContractPreExport" resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.TEmployeeContractPreExportVo">
		SELECT
			a.dept_name,
			a.dept_no,
			a.employee_name,
			a.emp_idcard,
			a.emp_phone,
			a.position,
			DATE_FORMAT(a.join_leave_date,'%Y-%m-%d') join_leave_date,
			a.contract_type,
			a.signatory,
			a.customer_username,
			if(a.sign_type = '0','线下签','电子签') sign_type,
			a.fadada_template,
			DATE_FORMAT(a.expected_confirm_time,'%Y-%m-%d %H:%i') expected_confirm_time,
			DATE_FORMAT(a.expected_collection_time,'%Y-%m-%d %H:%i') expected_collection_time,
			CASE WHEN a.process_status=0 THEN "待确认"
			WHEN a.process_status =1 THEN "待发起"
			WHEN a.process_status =2 THEN "线下签待审核"
			WHEN a.process_status =3 THEN "线下签审核不通过"
			WHEN a.process_status =4 THEN "线下签待归档"
			WHEN a.process_status =5 THEN "发起失败"
			WHEN a.process_status =6 THEN "签署中"
			WHEN a.process_status =7 THEN "签署失败"
			WHEN a.process_status =8 THEN "电子待归档"
			ELSE a.process_status END as process_status
		FROM t_employee_contract_pre a
		<where>
			1=1
			<include refid="tEmployeeContractPre_where"/>
		</where>
		order by a.create_time desc
		<if test="tEmployeeContractPre.limitStart != null">
			limit #{tEmployeeContractPre.limitStart},#{tEmployeeContractPre.limitEnd}
		</if>
	</select>

	<!--tEmployeeContractPre合同派单查询-->
	<select id="getDisPatcherContractList" resultType="com.yifu.cloud.plus.v1.yifu.archives.vo.EmployeeContractVO">
		SELECT
		(@i :=@i+1) as rowIndex,
		a.employee_name empName,
		a.emp_idcard empIdcard,
		a.dept_no deptNo,
		a.situation,
		a.contract_type contractName,
		a.CONTRACT_SUB_NAME contractSubName,
		null reason,
		a.signatory contractParty,
		if(a.CONTRACT_TERM = '' or a.CONTRACT_TERM is null,'2',a.CONTRACT_TERM) contractType,
		a.CONTRACT_START as contractStart,
		if(a.contract_type = '实习协议',a.CONTRACT_END,if(a.CONTRACT_TERM ='1' ,a.CONTRACT_END,null)) as contractEnd,
		a.post,
		if(a.working_hours = '' or a.working_hours is null,'4',a.working_hours) as workingHours,
		null contractNo,
		null fileCabinetNo,
		null leaveDate,
		null reduceReason,
		null remark,
		null changeContractAndEmployee,
		if(a.TRY_PERIOD = '' or a.TRY_PERIOD is null,'无',a.TRY_PERIOD) as tryPeriod,
		a.id preId,
		if(#{type} = '1',concat('手动签署-',a.customer_username),concat('自动化签署-',a.customer_username)) preName
		FROM t_employee_contract_pre a,(select @i:=0) as itable
		<where>
			1=1
			AND a.id in
			<foreach item="idStr" index="index" collection="idList" open="(" separator="," close=")">
				#{idStr}
			</foreach>
			AND a.process_status in
			<foreach item="status" index="index" collection="statusList" open="(" separator="," close=")">
				#{status}
			</foreach>
		</where>
	</select>

	<select id="getAllUnconfimData" resultMap="tEmployeeContractPreMap">
		SELECT
		<include refid="Base_Column_List"/>
		FROM t_employee_contract_pre a
		where a.process_status = '0'  and a.expected_confirm_time is not null
		and DATE_FORMAT(a.expected_confirm_time,"%Y-%m-%d") = CURDATE()
		group by a.customer_user_loginname
	</select>

	<select id="getAllUnDisData" resultType="java.lang.String">
		SELECT
			id
		FROM t_employee_contract_pre a
			where
			  a.expected_collection_time is not null
		  	  and DATE_FORMAT(a.expected_collection_time,"%Y-%m-%d") <![CDATA[ <= ]]> CURDATE()
		  	  and ((a.timeout_elec_sign = '0' and a.process_status = '0' and a.sign_type = '0') or
				a.process_status in ('1','3','5','7'))
	</select>
</mapper>
