<?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="org.sky.scientific.mapper.TechnicianMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.TechnicianEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="unicode" property="unicode"/>
        <result column="name" property="name"/>
        <result column="number" property="number"/>
        <result column="id_card" property="idCard"/>
        <result column="gender" property="gender"/>
        <result column="birthday" property="birthday"/>
        <result column="ethnic" property="ethnic"/>
        <result column="department_id" property="departmentId"/>
        <result column="department" property="department"/>
        <result column="post" property="post"/>
        <result column="post_location" property="postLocation"/>
        <result column="nature" property="nature"/>
        <result column="hire_date" property="hireDate"/>
        <result column="education" property="education"/>
        <result column="degree" property="degree"/>
        <result column="school_name" property="schoolName"/>
        <result column="major" property="major"/>
        <result column="rank" property="rank"/>
        <result column="practicing_requirement" property="practicingRequirement"/>
        <result column="situation" property="situation"/>
        <result column="personnel_type" property="personnelType"/>
        <result column="year_and_month" property="yearAndMonth"/>
        <result column="remark" property="remark"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.TechnicianVO" extends="ResultMap">

    </resultMap>

    <delete id="batchDelete">
        DELETE FROM kd_technician
        WHERE year_and_month <![CDATA[>=]]> #{yearAndMonth}
        and unicode IN
        <foreach item="unicode" collection="unicodeList" separator="," close=")" open="(" index="">
            #{unicode}
        </foreach>
    </delete>

    <select id="selectTechnicianPage" resultMap="ResultVoMap"
            parameterType="org.sky.scientific.pojo.dto.TechnicianDTO">
        select *
        from kd_technician
        where is_deleted = 0 and year_and_month = #{ew.yearAndMonth}
        <if test="ew.name!=null and ew.name!=''">and name like concat(concat('%', #{ew.name}),
            '%')
        </if>
        <if test="ew.number!=null and ew.number!=''">and number like concat(concat('%',#{ew.number}), '%')
        </if>
        <if test="ew.idCard!=null and ew.idCard!=''">and id_card like concat(concat('%', #{ew.idCard}), '%')
        </if>
        <if test="ew.department!=null and ew.department!=''">and department like concat(concat('%',#{ew.department}), '%')
        </if>
        <if test="ew.post!=null and ew.post!=''">and post like concat(concat('%', #{ew.post}),'%')</if>
        <if test="ew.postLocation!=null and ew.postLocation!=''">and post_location like concat(concat('%', #{ew.postLocation}), '%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and nature = #{ew.nature}</if>
        <if test="ew.education!=null and ew.education!=''">and education = #{ew.education}</if>
        <if test="ew.degree!=null and ew.degree!=''">and degree = #{ew.degree}</if>
        <if test="ew.rank!=null and ew.rank!=''">and rank = #{ew.rank}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and personnel_type = #{ew.personnelType}</if>
        <if test="ew.situation !=null and ew.situation!=''">and situation = #{ew.situation}</if>
        <if test="ew.practicingRequirement !=null and ew.practicingRequirement!=''">and practicing_requirement like concat(concat('%', #{ew.practicingRequirement}), '%')</if>
        <if test="ew.hireDateMin!=null and ew.hireDateMax!=''">and hire_Date between #{ew.hireDateMin} and #{ew.hireDateMax}</if>
        order by year_and_month asc, id asc
    </select>

    <select id="selectByYear" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.TechnicianDTO">
        select t1.*
        from kd_technician t1
        JOIN
        (
            SELECT unicode,MAX(year_and_month) as maxMonth
            from kd_technician
            where left(year_and_month,4) = #{ew.yearAndMonth}
            GROUP BY unicode
        ) s on t1.year_and_month = s.maxMonth and t1.unicode=s.unicode
        where left(t1.year_and_month,4) = #{ew.yearAndMonth}
        <if test="ew.name!=null and ew.name!=''">and name like concat(concat('%', #{ew.name}),'%')</if>
        <if test="ew.number!=null and ew.number!=''">and number like concat(concat('%',#{ew.number}), '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and id_card like concat(concat('%', #{ew.idCard}), '%')</if>
        <if test="ew.department!=null and ew.department!=''">and department like concat(concat('%',#{ew.department}), '%')</if>
        <if test="ew.post!=null and ew.post!=''">and post like concat(concat('%', #{ew.post}),'%')</if>
        <if test="ew.postLocation!=null and ew.postLocation!=''">and post_location like concat(concat('%', #{ew.postLocation}), '%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and nature = #{ew.nature}</if>
        <if test="ew.education!=null and ew.education!=''">and education = #{ew.education}</if>
        <if test="ew.degree!=null and ew.degree!=''">and degree = #{ew.degree}</if>
        <if test="ew.rank!=null and ew.rank!=''">and rank = #{ew.rank}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and personnel_type = #{ew.personnelType}</if>
        <if test="ew.situation !=null and ew.situation!=''">and situation = #{ew.situation}</if>
        <if test="ew.practicingRequirement !=null and ew.practicingRequirement!=''">and practicing_requirement like concat(concat('%', #{ew.practicingRequirement}), '%')</if>
        <if test="ew.hireDateMin!=null and ew.hireDateMax!=''">and hire_Date between #{ew.hireDateMin} and #{ew.hireDateMax}</if>
    </select>

    <select id="selectByDateInterval" resultMap="ResultMap" parameterType="java.util.Map">
        select t1.id, t1.unicode, t1.name, t1.number, t1.id_card, year_and_month ,gender, birthday,ethnic,department_id,department,post,post_location,nature,hire_date,education,degree,school_name,major,rank,practicing_requirement,situation,personnel_type
        from kd_technician t1
        JOIN
        (
            SELECT unicode, MAX(year_and_month) as latest_month
            from kd_technician
            where year_and_month <![CDATA[>=]]> #{param.startDate} and year_and_month <![CDATA[<=]]> #{param.endDate}
            GROUP BY unicode
        ) s2 on t1.unicode = s2.unicode and t1.year_and_month = s2.latest_month
        where t1.is_deleted = 0
        order by year_and_month asc, t1.id asc
    </select>

    <select id="selectByUnicodeAndYearAndMonth"
            resultType="org.sky.scientific.pojo.entity.TechnicianEntity">
        select *
        from kd_technician
        where unicode = #{unicode} and year_and_month = #{yearAndMonth}
    </select>

    <select id="getUnicodeByNumberAndIdentityCard" resultType="java.lang.String">
        select distinct(unicode)
        from kd_technician
        <where>
            <if test="number!=null and number!=''">and number = #{number}</if>
            <if test="number==null || number==''">and (number is null or number = '')</if>
            <if test="idCard!=null and idCard!=''">and id_card = #{idCard}</if>
            <if test="idCard==null || idCard==''">and (id_card is null or id_card = '')</if>
        </where>
    </select>

    <select id="getIdByNumberAndIdentityCard">
        select id
        from kd_technician
        where year_and_month = #{yearAndMonth}
        <if test="number!=null and number!=''">and number = #{number}</if>
        <if test="number==null || number==''">and (number is null or number = '')</if>
        <if test="idCard!=null and idCard!=''">and id_card = #{idCard}</if>
        <if test="idCard==null || idCard==''">and (id_card is null or id_card = '')</if>
    </select>

    <select id="niandugaokejirenyuanhuizongbiao" resultMap="ResultVoMap" parameterType="org.sky.scientific.pojo.dto.TechnicianDTO">
        SELECT COUNT(DISTINCT h.work_date) as workDaysByYear,t.*
        FROM kd_xm_technician_hours h
        LEFT JOIN
        (
            SELECT
                t.unicode,t.`name`,t.gender,t.number, t.id_card,t.birthday, t.ethnic, t.department,t.post,t.post_location,t.nature, t.hire_date, t.education, t.degree, t.school_name, t.major, t.rank, t.practicing_requirement,t.situation,t.personnel_type, t.remark
            FROM kd_technician t
            WHERE LEFT(t.year_and_month, 4) = #{ew.yearAndMonth}
            GROUP BY unicode
            ORDER BY t.year_and_month desc
        )t on t.unicode = h.unicode
        WHERE h.tenant_id = #{ew.tenantId} and LEFT(work_date, 4) = #{ew.yearAndMonth} and work_hours > 0
        <if test="ew.name!=null and ew.name!=''">and t.name like concat('%', #{ew.name},'%')</if>
        <if test="ew.number!=null and ew.number!=''">and t.number like concat('%',#{ew.number}, '%')</if>
        <if test="ew.idCard!=null and ew.idCard!=''">and t.id_card like concat('%', #{ew.idCard}, '%')</if>
        <if test="ew.department!=null and ew.department!=''">and t.department like concat('%',#{ew.department}, '%')</if>
        <if test="ew.post!=null and ew.post!=''">and t.post like concat('%', #{ew.post},'%')</if>
        <if test="ew.nature!=null and ew.nature!=''">and t.nature = #{ew.nature}</if>
        <if test="ew.personnelType!=null and ew.personnelType!=''">and t.personnel_type = #{ew.personnelType}</if>
        <if test="ew.situation !=null and ew.situation!=''">and t.situation = #{ew.situation}</if>
        <if test="ew.education !=null and ew.education!=''">and t.education = #{ew.education}</if>
        <if test="ew.degree !=null and ew.degree!=''">and t.degree = #{ew.degree}</if>
        <if test="ew.rank !=null and ew.rank!=''">and t.rank like concat('%',#{ew.rank}, '%')</if>
        <if test="ew.practicingRequirement !=null and ew.practicingRequirement!=''">and t.practicing_requirement like concat('%',#{ew.practicingRequirement}, '%')</if>
        <if test="ew.hireDateMin!=null and ew.hireDateMax!=''">and hire_Date between #{ew.hireDateMin} and #{ew.hireDateMax}</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">
            and h.xm_id in
            <foreach item="item" index="index" collection="ew.xmIdList" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        GROUP BY h.unicode
        HAVING workDaysByYear >= 183
    </select>

    <select id="getNameById" resultType="java.lang.String">
        select name from kd_technician where id = #{technicianId}
    </select>

    <select id="getMaxUnicode" resultType="java.lang.String">
        SELECT max(unicode) from kd_technician
    </select>

</mapper>
