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

    <insert id="addStaffList" parameterType="java.util.List">
        INSERT INTO meter_staff (
            `id`,
            `name`,
            `id_card`,
            `account`,
            `staff_no`,
            `sex`,
            `birthday`,
            `education`,
            `technology_job`,
            `administration_job`,
            `dept_id`,
            `major`,
            `work_date`,
            `technology_exam`,
            `main_examiner`,
            `special_operator`,
            `remark`
            )
        VALUES
        <foreach collection ="list" item="bean" separator =",">
            (#{bean.id}, #{bean.name}, #{bean.idCard}, #{bean.account}, #{bean.staffNo}
            , #{bean.sex}, STR_TO_DATE(#{bean.birthday},'%Y-%m-%d'), #{bean.education}, #{bean.technologyJob}
            , #{bean.administrationJob}, #{bean.deptId}, #{bean.major}, STR_TO_DATE(#{bean.workDate},'%Y-%m-%d'), #{bean.technologyExam}
            , #{bean.mainExaminer}, #{bean.specialOperator}, #{bean.remark}
            )
        </foreach >
    </insert>

    <select id="selectStaffList" resultType="com.casic.missiles.model.meter.MeterStaff" parameterType="com.casic.missiles.dto.meter.MeterStaffRequest">
        SELECT
            *
        FROM
        (
            SELECT
                t.id,
                t.`NAME`,
                t.staff_no,
                t.sex,
                t.education,
                t.technology_job,
                t.administration_job,
                t.dept_id,
                ( SELECT `certificate_no` FROM meter_certificate WHERE staff_id = t.id ORDER BY create_time LIMIT 1 ) AS verifier_certificate_no,
                ( SELECT `valid_date` FROM meter_certificate WHERE staff_id = t.id ORDER BY create_time LIMIT 1 ) AS certificate_date,
                t.major,
                t.technology_exam,
                su.id AS user_id
            FROM
                meter_staff t
            LEFT JOIN sys_user su ON t.account = su.ACCOUNT
            WHERE
                t.is_del = 0
            ) t2
        <where>
            1 = 1
            <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.major != null  and request.major != ''">
                and t2.major = #{request.major}
            </if>
            <if test="request.verifierCertificateNo != null  and request.verifierCertificateNo != ''">
                and t2.verifier_certificate_no like concat('%', #{request.verifierCertificateNo}, '%')
            </if>
            <if test="request.administrationJob != null  and request.administrationJob != ''">
                and t2.administration_job = #{request.administrationJob}
            </if>
            <if test="request.ids != null and request.ids.size() > 0 ">
                and t2.id in
                <foreach collection='request.ids' item='id' open='(' separator=',' close=')'>
                    #{id}
                </foreach>
            </if>
        </where>
    </select>
    <select id="selectStaffInfo" resultType="com.casic.missiles.model.meter.MeterStaff" parameterType="String">
        SELECT
            t.id,
            t.id_card,
            t.account,
            t.`NAME`,
            t.staff_no,
            t.sex,
            t.birthday,
            t.certificate_company,
            t.work_date,
            t.main_examiner,
            t.special_operator,
            t.education,
            t.technology_job,
            t.administration_job,
            t.dept_id,
            sd.FULL_NAME as dept_name,
            (select `certificate_no` from meter_certificate where staff_id = t.id ORDER BY create_time limit 1) as verifier_certificate_no,
            (select `valid_date` from meter_certificate where staff_id = t.id ORDER BY create_time limit 1) as certificate_date,
            t.major,
            t.technology_exam,
			t.remark,
            t.minio_file_name
        FROM
            meter_staff t LEFT JOIN sys_dept sd on t.dept_id = sd.ID where t.id =#{id}
    </select>

    <select id="meterStaffStatistic" resultType="com.casic.missiles.dto.meter.MeterWorkbenchResponse">
        SELECT
            DATE_FORMAT( work_date, '%Y-%m' ) AS `date`,
            COUNT( 1 ) AS `count`
        FROM
            meter_staff
        WHERE
            is_del = 0
          and DATE_FORMAT( work_date, '%Y-%m' ) &gt;= #{startTime}
          and DATE_FORMAT( work_date, '%Y-%m' ) &lt;= #{endTime}
        GROUP BY
            DATE_FORMAT( work_date, '%Y-%m' )
        ORDER BY
            DATE_FORMAT( work_date, '%Y-%m' )
    </select>

    <select id="getMaxNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(staff_no, 12)), 0) from meter_staff
    </select>
    <select id="staffCultivateLog" resultType="com.casic.missiles.model.meter.MeterTrainStaff">
        SELECT
            mts.staff_id,
            mts.plan_id,
            mts.name,
            mts.exam_result,
            mts.technology_job,
            mtp.train_time,
            mtp.plan_name,
            mtp.plan_no
        FROM
            meter_train_staff mts
                LEFT JOIN meter_train_plan mtp on mts.plan_id = mtp.id
        WHERE
            mts.staff_id = #{id} and mts.is_del = 0
    </select>
</mapper>
