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

    <resultMap type="CalWorkunit" id="CalWorkunitResult">
        <result property="id" column="id"/>
        <result property="planId" column="plan_id"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="remark" column="remark"/>
        <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="selectCalWorkunitVo">
        select plan_id,
               id,
               workunit_id,
               workunit_code,
               workunit_name,
               remark,
               create_by,
               create_time,
               update_by,
               update_time
        from cal_workunit
    </sql>

    <select id="selectCalWorkunitList" parameterType="CalWorkunit" resultMap="CalWorkunitResult">
        <include refid="selectCalWorkunitVo"/>
        <where>
            <if test="planId != null ">and plan_id = #{planId}</if>
            <if test="planCode != null ">and plan_code = #{planCode}</if>
            <if test="workunitId != null ">and workunit_id = #{workunitId}</if>
            <if test="workunitCode != null  and workunitCode != ''">and workunit_code = #{workunitCode}</if>
            <if test="workunitName != null  and workunitName != ''">and workunit_name like concat('%', #{workunitName},
                '%')
            </if>

        </where>
    </select>

    <select id="selectCalWorkunitById" parameterType="Long" resultMap="CalWorkunitResult">
        <include refid="selectCalWorkunitVo"/>
        where id = #{id}
    </select>

    <insert id="insertCalWorkunit" parameterType="CalWorkunit">
        insert into cal_workunit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="id != null">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="planCode != null">plan_code,</if>
            <if test="remark != null">remark,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="id != null">#{id},</if>
            <if test="workunitId != null">#{workunitId},</if>
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="workunitName != null">#{workunitName},</if>
            <if test="planCode != null">#{planCode},</if>
            <if test="remark != null">#{remark},</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>
        </trim>
    </insert>

    <update id="updateCalWorkunit" parameterType="CalWorkunit">
        update cal_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="remark != null">remark = #{remark},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCalWorkunitById" parameterType="Long">
        delete
        from cal_workunit
        where id = #{id}
    </delete>
    <delete id="deleteCalWorkunitByPlanId" parameterType="Long">
        delete
        from cal_workunit
        where plan_id = #{planId}
    </delete>

    <delete id="deleteCalWorkunitByPlanCode">
        delete
        from cal_workunit
        where plan_code = #{planCode}
    </delete>

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