<?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.business.mapper.system.TBusWarningEmployeeMapper">
    <resultMap id="tBusWarningEmployeeMap" type="com.yifu.cloud.plus.v1.business.entity.system.TBusWarningEmployee">
        <id property="id" column="id"/>
        <result property="warningId" column="warning_id"/>
        <result property="busDeptId" column="bus_dept_id"/>
        <result property="busDeptCode" column="bus_dept_code"/>
        <result property="busDeptName" column="bus_dept_name"/>
        <result property="empName" column="emp_name"/>
        <result property="empIdcard" column="emp_idcard"/>
        <result property="empPhone" column="emp_phone"/>
        <result property="sendDate" column="send_date"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="sendStatus" column="send_status"/>
        <result property="remark" column="remark"/>
        <result property="bizId" column="Biz_Id"/>
        <result property="updateStatus" column="update_status"/>
        <result property="message" column="message"/>
        <result property="businessTelecomNumber" column="BUSINESS_TELECOM_NUMBER"/>
        <result property="certType" column="cert_type"/>
        <result property="notUsedVacationDuration" column="NOT_USED_VACATION_DURATION"/>
    </resultMap>
    <sql id="Base_Column_List">
             a.id,
              a.warning_id,
              a.bus_dept_id,
              a.bus_dept_code,
              a.bus_dept_name,
              a.emp_name,
              a.emp_idcard,
              a.emp_phone,
              a.send_date,
              a.CREATE_USER,
              a.send_status,
              a.remark,
              a.Biz_Id,
              a.update_status,
              a.message,
              a.BUSINESS_TELECOM_NUMBER,
              a.cert_type,
              a.NOT_USED_VACATION_DURATION
     </sql>
    <sql id="tBusWarningEmployee_where">
        <if test="tBusWarningEmployee != null">
            <if test="tBusWarningEmployee.id != null and tBusWarningEmployee.id.trim() != ''">
                AND a.id = #{tBusWarningEmployee.id}
            </if>
            <if test="tBusWarningEmployee.warningId != null and tBusWarningEmployee.warningId.trim() != ''">
                AND a.warning_id = #{tBusWarningEmployee.warningId}
            </if>
            <if test="tBusWarningEmployee.busDeptId != null and tBusWarningEmployee.busDeptId.trim() != ''">
                AND a.bus_dept_id = #{tBusWarningEmployee.busDeptId}
            </if>
            <if test="tBusWarningEmployee.busDeptCode != null and tBusWarningEmployee.busDeptCode.trim() != ''">
                AND a.bus_dept_code = #{tBusWarningEmployee.busDeptCode}
            </if>
            <if test="tBusWarningEmployee.busDeptName != null and tBusWarningEmployee.busDeptName.trim() != ''">
                AND a.bus_dept_name = #{tBusWarningEmployee.busDeptName}
            </if>
            <if test="tBusWarningEmployee.empName != null and tBusWarningEmployee.empName.trim() != ''">
                AND a.emp_name = #{tBusWarningEmployee.empName}
            </if>
            <if test="tBusWarningEmployee.empIdcard != null and tBusWarningEmployee.empIdcard.trim() != ''">
                AND a.emp_idcard = #{tBusWarningEmployee.empIdcard}
            </if>
            <if test="tBusWarningEmployee.empPhone != null and tBusWarningEmployee.empPhone.trim() != ''">
                AND a.emp_phone = #{tBusWarningEmployee.empPhone}
            </if>
            <if test="tBusWarningEmployee.sendDate != null">
                AND a.send_date = #{tBusWarningEmployee.sendDate}
            </if>
            <if test="tBusWarningEmployee.createUser != null and tBusWarningEmployee.createUser.trim() != ''">
                AND a.CREATE_USER = #{tBusWarningEmployee.createUser}
            </if>
            <if test="tBusWarningEmployee.sendStatus != null and tBusWarningEmployee.sendStatus.trim() != ''">
                AND a.send_status = #{tBusWarningEmployee.sendStatus}
            </if>
            <if test="tBusWarningEmployee.remark != null and tBusWarningEmployee.remark.trim() != ''">
                AND a.remark = #{tBusWarningEmployee.remark}
            </if>
            <if test="tBusWarningEmployee.bizId != null and tBusWarningEmployee.bizId.trim() != ''">
                AND a.Biz_Id = #{tBusWarningEmployee.bizId}
            </if>
            <if test="tBusWarningEmployee.updateStatus != null and tBusWarningEmployee.updateStatus.trim() != ''">
                AND a.update_status = #{tBusWarningEmployee.updateStatus}
            </if>
            <if test="tBusWarningEmployee.message != null and tBusWarningEmployee.message.trim() != ''">
                AND a.message = #{tBusWarningEmployee.message}
            </if>
            <if test="null != tBusWarningEmployee.sendDateStart and tBusWarningEmployee.sendDateStart.trim() != ''">
                AND a.send_date >= concat(#{tBusWarningEmployee.sendDateStart},' 00:00:00')
            </if>
            <if test="null != tBusWarningEmployee.sendDateEnd and tBusWarningEmployee.sendDateEnd.trim() != ''">
                AND a.send_date <![CDATA[ <= ]]>  concat(#{tBusWarningEmployee.sendDateEnd},' 23:59:59')
            </if>
            <if test="null != tBusWarningEmployee.businessTelecomNumber and tBusWarningEmployee.businessTelecomNumber.trim() != ''">
                AND a.BUSINESS_TELECOM_NUMBER like concat('%', #{tBusWarningEmployee.businessTelecomNumber}, '%')
            </if>
        </if>
    </sql>
    <!--tBusWarningEmployee简单分页查询-->
    <select id="getTBusWarningEmployeePage" resultMap="tBusWarningEmployeeMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_bus_warning_employee a
        <where>
            1=1
            <include refid="tBusWarningEmployee_where"/>
        </where>
        order by a.send_date desc
    </select>

    <!--简单list-->
    <select id="getTBusWarningEmployeeListToday" resultMap="tBusWarningEmployeeMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM t_bus_warning_employee a
        <where>
            DATE_FORMAT(a.send_date,"%Y%m%d")  = #{dateStr}
        </where>
    </select>

    <!--简单list-->
    <select id="getByBizId" resultMap="tBusWarningEmployeeMap">
        SELECT
            <include refid="Base_Column_List"/>
        FROM t_bus_warning_employee a
        where a.Biz_Id = #{bizId}
    </select>

    <!--更新-->
    <update id="updateSendStatusByBizId" >
        update t_bus_warning_employee e set e.send_status = #{sendStatus},e.update_status = #{updateStatus} where e.Biz_Id = #{bizId}
    </update>

    <!-- 基础sql -->
    <insert id="saveNewEmployee">
        INSERT into
        t_bus_warning_employee
        set warning_id = #{se.warningId},bus_dept_id = #{se.busDeptId},bus_dept_code = #{se.busDeptCode},bus_dept_name = #{se.busDeptName}
        ,emp_name = #{se.empName},EMP_IDCARD = #{se.empIdcard},EMP_PHONE = #{se.empPhone},send_date = #{se.sendDate}
        ,CREATE_USER = #{se.createUser},send_status = #{se.sendStatus},Biz_Id = #{se.bizId},update_status = #{se.updateStatus}
        ,BUSINESS_TELECOM_NUMBER = #{se.businessTelecomNumber},cert_type = #{se.certType},NOT_USED_VACATION_DURATION = #{se.notUsedVacationDuration}
    </insert>

    <!--更新-->
    <update id="updateBizIdByBizId" >
        update t_bus_warning_employee e set e.Biz_Id = #{newBizId} where e.Biz_Id = #{oldBizId}
    </update>

    <!-- 复审到期日 -->
    <insert id="insertByReviewDate">
        INSERT into t_bus_warning_employee
        (warning_id,bus_dept_id,bus_dept_code,bus_dept_name,emp_name,EMP_IDCARD,EMP_PHONE,send_date,CREATE_USER,send_status,Biz_Id,update_status,business_telecom_number,cert_type)
        SELECT #{warningId} warning_id,d.id bus_dept_id,d.tree_logo bus_dept_code,d.name bus_dept_name,c.emp_name,c.EMP_IDCARD,e.phone EMP_PHONE
        ,now() send_date, #{userId} CREATE_USER,'1' send_status,#{bizId} Biz_Id,'0' update_status,c.business_telecom_number,sd.label cert_type
        FROM t_cert_info c
        left join t_bus_dept d on d.id=c.organ_id
        left join sys_bus_dict sd on sd.value=c.cert_type and sd.item_type = 'cert_type'
        join view_employee_phone e on e.dx_no = c.business_telecom_number
		where  datediff(c.review_date,now()) = #{days} and c.delete_flag ='0'
		and not exists (
            select 1 from t_bus_leave l where l.tele_no = c.business_telecom_number
        )
		GROUP BY c.id
    </insert>

    <!-- 证件到期日 -->
    <insert id="insertByTermValidityEnd">
        INSERT into t_bus_warning_employee
        (warning_id,bus_dept_id,bus_dept_code,bus_dept_name,emp_name,EMP_IDCARD,EMP_PHONE,send_date,CREATE_USER,send_status,Biz_Id,update_status,business_telecom_number,cert_type)
        SELECT #{warningId} warning_id,d.id bus_dept_id,d.tree_logo bus_dept_code,d.name bus_dept_name,c.emp_name,c.EMP_IDCARD,e.phone EMP_PHONE
        ,now() send_date, #{userId} CREATE_USER,'1' send_status,#{bizId} Biz_Id,'0' update_status,c.business_telecom_number,sd.label cert_type
        FROM t_cert_info c
        left join sys_bus_dict sd on sd.value=c.cert_type and sd.item_type = 'cert_type'
        left join t_bus_dept d on d.id=c.organ_id
        join view_employee_phone e on e.dx_no = c.business_telecom_number
		where  datediff(c.term_validity_end,now()) = #{days} and c.delete_flag ='0'
		and not exists (
            select 1 from t_bus_leave l where l.tele_no = c.business_telecom_number
        )
		GROUP BY c.id
    </insert>

    <!-- 年假未休时长 -->
    <insert id="insertByYearVacation">
        INSERT into t_bus_warning_employee
        (warning_id,bus_dept_id,bus_dept_code,bus_dept_name,emp_name,EMP_IDCARD,EMP_PHONE,send_date,CREATE_USER,send_status,Biz_Id,update_status,business_telecom_number,NOT_USED_VACATION_DURATION)
        SELECT #{warningId} warning_id,d.id bus_dept_id,d.tree_logo bus_dept_code,d.name bus_dept_name,c.emp_name,c.EMP_IDCARD,e.phone EMP_PHONE
        ,now() send_date, #{userId} CREATE_USER,'1' send_status,#{bizId} Biz_Id,'0' update_status,c.business_telecom_number,concat(c.NOT_USED_VACATION_DURATION,'小时')
        FROM t_vacation_monitor c
        left join t_bus_dept d on d.id=c.DEPART_ID
        join view_employee_phone e on e.dx_no = c.business_telecom_number
        where c.NOT_USED_VACATION_DURATION>0 and DATE_FORMAT(now(),'%Y') = c.VACATION_YEAR
        and not exists (
            select 1 from t_bus_leave l where l.tele_no = c.business_telecom_number
        )
        GROUP BY c.id
    </insert>

	<!--假期结束提醒-->
	<select id="getVacationEmpName" resultType="java.lang.String">
		SELECT c.EMP_NAME
		FROM t_vacation_monitor c
		where c.NOT_USED_VACATION_DURATION>0 and DATE_FORMAT(now(),'%Y') = c.VACATION_YEAR
		and not exists (
			select 1 from t_bus_leave l where l.tele_no = c.business_telecom_number
		)
        limit 1
	</select>

    <!--假期结束提醒-->
    <select id="getVacationInfoEmpName" resultType="java.lang.String">
        SELECT c.EMP_NAME
        FROM t_vacation_info c
        where c.VACATION_STATUS != '3' and datediff(c.VACATION_END_TIME,now()) = -1 and c.EMP_NAME is not null
        and not exists (
            select 1 from t_bus_leave l where l.tele_no = c.business_telecom_number
        )
        limit 1
    </select>


</mapper>