<?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.casic.missiles.mapper.meter.MeterCertificateMapper">

    <insert id="addCertificateList" parameterType="java.util.List">
        INSERT INTO meter_certificate ( `id`, `staff_id`, `certificate_no`, `certificate_name`, `certificate_date`,
        `certificate_company`, `valid_date` )
        VALUES
        <foreach collection ="list" item="bean" separator =",">
            (#{bean.certificateId}, #{bean.id}, #{bean.certificateNo}, #{bean.certificateName},
            STR_TO_DATE(#{bean.certificateDate},'%Y-%m-%d'), #{bean.certificateCompany},
            STR_TO_DATE(#{bean.validDate},'%Y-%m-%d')
            )
        </foreach >
    </insert>
    <insert id="addCertificate" parameterType="java.util.List">
        INSERT INTO meter_certificate ( `id`, `staff_id`, `certificate_no`, `certificate_name`, `certificate_date`,
        `certificate_company`, `valid_date` )
        VALUES
        <foreach collection ="list" item="bean" separator =",">
            (#{bean.id}, #{bean.staffNo}, #{bean.certificateNo}, #{bean.certificateName},
            STR_TO_DATE(#{bean.certificateDate},'%Y-%m-%d'), #{bean.certificateCompany},
            STR_TO_DATE(#{bean.validDate},'%Y-%m-%d')
            )
        </foreach >
    </insert>
    <select id="queryCertificateList" resultType="com.casic.missiles.dto.meter.MeterCertificateDto" parameterType="com.casic.missiles.dto.meter.MeterStaffRequest">
        SELECT
        t1.id,
        t1.staff_id,
        t1.certificate_no,
        t1.certificate_name,
        t1.certificate_date,
        t1.certificate_company,
        DATE_FORMAT(t1.valid_date,'%Y-%m-%d') as valid_date,
        t1.certificate_status,
        t1.remark,
        t2.NAME,
        t2.sex,
        t2.staff_no,
        t2.education,
        t2.technology_job,
        t2.administration_job,
        t3.FULL_NAME as dept_name,
        t2.dept_id
        FROM
        meter_certificate AS t1
        LEFT JOIN meter_staff t2 ON t1.staff_id = t2.id
        LEFT JOIN sys_dept t3 on t2.dept_id = t3.id
        <where>
            and t1.is_del = 0
            and t2.is_del = 0
<!--            <if test="request.userId != null  and request.userId != ''">-->
<!--                and t2.account = #{request.userId}-->
<!--            </if>-->
            <if test="request.staffNo != null  and request.staffNo != ''">
                and t2.staff_no like CONCAT('%',#{request.staffNo},'%')
            </if>
            <if test="request.name != null  and request.name != ''">
                and t2.`NAME` like concat('%', #{request.name}, '%')
            </if>
            <if test="request.deptId != null  and  request.deptId != ''">
                and t2.dept_id = #{request.deptId}
            </if>
            <if test="request.verifierCertificateNo != null  and request.verifierCertificateNo != ''">
                and t1.certificate_no like concat('%', #{request.verifierCertificateNo}, '%')
            </if>
            <if test="request.certificateStatus != null  and request.certificateStatus != ''">
<!--                and t1.certificate_status = #{request.certificateStatus}-->
                <if test="request.certificateStatus == 0">
                    and curdate() &lt;= t1.valid_date
                </if>
                <if test="request.certificateStatus == 1">
                    and curdate() &gt;= t1.valid_date
                </if>
            </if>
            <if test="request.remindType == 'remind' ">
                and DATE_FORMAT(t1.valid_date,'%Y-%m-%d') &lt;= DATE_SUB(curdate(),INTERVAL -30 day)
            </if>
            <if test="request.ids != null and request.ids.size() > 0 ">
                and t1.id in
                <foreach collection='request.ids' item='id' open='(' separator=',' close=')'>
                    #{id}
                </foreach>
            </if>
        </where>
        ORDER BY t1.valid_date DESC
    </select>
    <select id="queryReminderList" resultType="com.casic.missiles.model.workbench.WorkbenchRemindMessage">
        SELECT
            su.id AS remind_id,
            su.dept_id AS remind_dept_id,
            mc.id AS business_id,
            mc.certificate_no AS business_code,
            mc.certificate_name AS message_content
        FROM
            meter_certificate AS mc
                LEFT JOIN meter_staff ms ON mc.staff_id = ms.id
                LEFT JOIN sys_user su ON ms.account = su.ACCOUNT
        WHERE
                DATE_FORMAT( mc.valid_date, '%Y-%m-%d' ) = DATE_SUB(
                    curdate(),
                    INTERVAL - 30 DAY)
    </select>

</mapper>
