<?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.tofly.xrtygis.mapper.DutyrosterMapper">

    <resultMap id="dutyrosterMap" type="com.tofly.xrtygis.entity.Dutyroster">
        <id property="id" column="ID"/>
        <result property="dutyUser" column="DUTY_USER"/>
        <result property="workTime" column="WORK_TIME"/>
        <result property="dutyDate" column="DUTY_DATE"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="deptId" column="DEPT_ID"/>
        <association property="dutyUserName" column="DUTY_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="workName" column="WORK_TIME" select="getWorkName"/>
    </resultMap>

    <select id="getWorkName" resultType="string">
        select decode(#{workTime}, '1', '白班', '2', '夜班', '3', '休息')
        from dual
    </select>
    <select id="getByMonth" resultMap="dutyrosterMap">
        select * from TF_YWPN_DUTYROSTER_W where TO_CHAR(duty_date,'yyyy-MM') like concat(concat('%',#{month}),'%')
        <if test="user != null ">
            and DUTY_USER = #{user}
        </if>
    </select>

    <select id="getMaxId" resultType="Long">
        select Max(id) id from TF_YWPN_DUTYROSTER_W
    </select>

    <insert id="saveDutyrosters" parameterType="java.util.List">
        INSERT INTO TF_YWPN_DUTYROSTER_W (ID, DUTY_USER, WORK_TIME, DUTY_DATE, CREATE_TIME, CREATE_USER,  DEPT_ID)
        <foreach collection="list" item="info" index="index" separator="union all">
            SELECT #{info.id},#{info.dutyUser},#{info.workTime},#{info.dutyDate},#{info.createTime},#{info.createUser}
                 <choose>
                     <when test="info.deptId!=null">,#{info.deptId}</when>
                     <otherwise> ,null</otherwise>
                 </choose>
            FROM DUAL
        </foreach>

    </insert>

</mapper>
