<?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.ximai.mes.cal.mapper.CalPlanWorkunitMapper">

    <resultMap type="CalPlanWorkunit" id="CalPlanWorkunitResult">
        <result property="planId" column="plan_id"/>
        <result property="id" column="id"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="restFlag" column="rest_flag"/>
        <result property="shiftType" column="shift_type"/>
        <result property="shiftId" column="shift_id"/>
        <result property="shiftName" column="shift_name"/>
        <result property="theDay" column="the_day"/>
        <result property="isWork" column="is_work"/>
        <result property="startDate" column="start_Date"/>
        <result property="endDate" column="end_Date"/>
        <result property="remark" column="remark"/>
        <result property="attr1" column="attr1"/>
        <result property="attr2" column="attr2"/>
        <result property="attr3" column="attr3"/>
        <result property="attr4" column="attr4"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectCalPlanWorkunitVo">
        select plan_id,
               id,
               workunit_id,
               workunit_code,
               workunit_name,
               rest_flag,
               shift_id,
               shift_name,
               the_day,
               shift_type,
               is_work,
               start_Date,
               end_Date,
               remark,
               attr1,
               attr2,
               attr3,
               attr4,
               create_by,
               create_time,
               update_by,
               update_time
        from cal_plan_workunit
    </sql>

    <select id="selectCalPlanWorkunitList" parameterType="CalPlanWorkunit" resultMap="CalPlanWorkunitResult">
        <include refid="selectCalPlanWorkunitVo"/>
        <where>
            <if test="planId != null ">and plan_id = #{planId}</if>
            <if test="workunitId != null ">and workunit_id = #{workunitId}</if>
            <if test="workunitCode != null  and workunitCode != ''">and workunit_code = #{workunitCode}</if>
            <if test="shiftName != null  and shiftName != ''">and shift_name = #{shiftName}</if>
            <if test="workunitName != null  and workunitName != ''">and workunit_name like concat('%', #{workunitName},
                '%')
            </if>

            <if test="isWork != null  and isWork != ''">and is_work = #{isWork}</if>
            <if test="attr1 != null  and attr1 != ''">and attr1 = #{attr1}</if>
            <if test="attr2 != null  and attr2 != ''">and attr2 = #{attr2}</if>
            <if test="attr3 != null ">and attr3 = #{attr3}</if>
            <if test="attr4 != null ">and attr4 = #{attr4}</if>
        </where>
    </select>


    <select id="selectCalPlanWorkunitListV2" parameterType="CalPlanWorkunit" resultMap="CalPlanWorkunitResult">
        select *
        from cal_plan_workunit
        <where>
            <if test="theDay != null  and theDay != ''">and the_day = #{theDay}</if>
            <!--            <if test="theDay != null  and theDay != ''">and STR_TO_DATE(start_date, '%Y-%m-%d') = #{theDay}</if>-->
            <if test="isWork != null  and isWork != ''">and is_work = #{isWork}</if>
            <if test="workunitName != null  and workunitName != ''">and workunit_name like concat('%', #{workunitName},
                '%')
            </if>

        </where>
    </select>

    <select id="selectCalPlanWorkunitByid" parameterType="Long" resultMap="CalPlanWorkunitResult">
        <include refid="selectCalPlanWorkunitVo"/>
        where id = #{id}
    </select>

    <insert id="insertCalPlanWorkunit" parameterType="CalPlanWorkunit">
        insert into cal_plan_workunit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="workunitId != null">workunit_id,</if>
            <if test="workunitCode != null">workunit_code,</if>
            <if test="workunitName != null">workunit_name,</if>
            <if test="restFlag != null">rest_flag,</if>
            <if test="shiftType != null">shift_type,</if>
            <if test="shiftId != null">shift_id,</if>
            <if test="shiftName != null">shift_name,</if>
            <if test="theDay != null">the_day,</if>
            <if test="isWork != null">is_work,</if>
            <if test="remark != null">remark,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="attr2 != null">attr2,</if>
            <if test="attr3 != null">attr3,</if>
            <if test="attr4 != null">attr4,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="workunitId != null">#{workunitId},</if>
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="workunitName != null">#{workunitName},</if>
            <if test="restFlag != null">#{restFlag},</if>
            <if test="shiftType != null">#{shiftType},</if>
            <if test="shiftId != null">#{shiftId},</if>
            <if test="shiftName != null">#{shiftName},</if>
            <if test="theDay != null">#{theDay},</if>
            <if test="isWork != null">#{isWork},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="attr2 != null">#{attr2},</if>
            <if test="attr3 != null">#{attr3},</if>
            <if test="attr4 != null">#{attr4},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
        </trim>
    </insert>


    <insert id="insertBatchCalPlanWorkunits" parameterType="CalPlanWorkunit">
        <if test="calPlanWorkunits != null and calPlanWorkunits.size() > 0">
            insert into cal_plan_workunit
            <trim prefix="(" suffix=")">
                plan_id,workunit_id,workunit_code,workunit_name,rest_flag,shift_type,shift_id,shift_name,the_day,is_work,remark,create_by,create_time,update_by,update_time,start_date,end_date
            </trim>
            values
            <foreach collection="calPlanWorkunits" item="item" separator=",">
                (#{item.planId},#{item.workunitId},#{item.workunitCode},#{item.workunitName},#{item.restFlag},#{item.shiftType},#{item.shiftId},#{item.shiftName},#{item.theDay},#{item.isWork},#{item.remark},#{item.createBy},#{item.createTime},#{item.updateBy},#{item.updateTime},#{item.startDate},#{item.endDate})
            </foreach>
        </if>

    </insert>

    <update id="updateCalPlanWorkunit" parameterType="CalPlanWorkunit">
        update cal_plan_workunit
        <trim prefix="SET" suffixOverrides=",">
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="workunitCode != null">workunit_code = #{workunitCode},</if>
            <if test="workunitName != null">workunit_name = #{workunitName},</if>
            <if test="restFlag != null">rest_flag=#{restFlag},</if>
            <if test="shiftType != null">shift_type = #{shiftType},</if>
            <if test="shiftId != null">shift_id = #{shiftId},</if>
            <if test="shiftName != null">shift_name = #{shiftName},</if>
            <if test="theDay != null">the_day = #{theDay},</if>
            <if test="isWork != null">is_work = #{isWork},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="attr2 != null">attr2 = #{attr2},</if>
            <if test="attr3 != null">attr3 = #{attr3},</if>
            <if test="attr4 != null">attr4 = #{attr4},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="startTime != null">start_time =#{startTime},</if>
            <if test="startDate != null">start_date =#{startDate},</if>
            <if test="endDate != null">end_date =#{endDate},</if>
            <if test="endTime != null">end_time =#{endTime},</if>
        </trim>

        where id = #{id}
    </update>

    <delete id="deleteCalPlanWorkunitByid" parameterType="Long">
        delete
        from cal_plan_workunit
        where id = #{id}
    </delete>

    <delete id="deleteCalPlanWorkunitByids" parameterType="String">
        delete from cal_plan_workunit where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>