<?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.md.mapper.MdWorkunitMapper">

    <resultMap type="MdWorkunit" id="MdWorkunitResult">
        <result property="workunitId" column="workunit_id"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="serial" column="serial"/>
        <result property="lineName" column="line_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <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"/>
        <result property="faultFlag" column="fault_flag"/>
        <result property="workstationCode" column="workstation_code"/>
        <result property="workstationName" column="workstation_name"/>
        <result property="solidifyTime" column="solidify_time"/>
    </resultMap>
    <resultMap type="com.ximai.mes.md.vo.MdWorkunitVo" id="MdWorkunitVoResult">
        <result property="workunitId" column="workunit_id"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="serial" column="serial"/>
        <result property="lineName" column="line_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <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"/>
        <result property="workstationCode" column="workstation_code"/>
        <result property="faultFlag" column="fault_flag"/>
        <result property="solidifyTime" column="solidify_time"/>
    </resultMap>

    <resultMap type="com.ximai.mes.md.vo.MdItemVo" id="MdItemVoResult">
        <result property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="allQuantity" column="all_quantity"/>
        <result property="nextMaintenPeriod" column="next_mainten_period"/>
    </resultMap>

    <sql id="selectMdWorkunitVo">
        select workunit_id,
               workstation_id,
               workstation_code,
               workunit_code,
               workunit_name,
               serial,
               line_name,
               fault_flag,
               enable_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time,
               solidify_time
        from md_workunit
    </sql>

    <select id="selectMdWorkunitList" parameterType="MdWorkunit" resultMap="MdWorkunitVoResult">
        SELECT
        w.workunit_id,
        w.workstation_id,
        w.workunit_code,
        w.workunit_name,
        ws.workstation_code,
        ws.workstation_name,
        ws.line_name,
        ws.serial,
        w.fault_flag,
        w.enable_flag,
        w.remark,
        w.workstation_code,
        w.create_by,
        w.create_time,
        w.update_by,
        w.update_time
        FROM
        md_workunit w
        inner join pro_task pt on w.workstation_id = pt.workstation_id
        LEFT JOIN md_workstation ws ON w.workstation_id = ws.workstation_id
        <where>
            <if test="workstationId != null ">and w.workstation_id = #{workstationId}</if>
            <if test="taskId != null and taskId != ''">and pt.task_id = #{taskId}</if>
            <if test="workunitCode != null  and workunitCode != ''">and w.workunit_code = #{workunitCode}</if>
            <if test="workunitName != null  and workunitName != ''">and w.workunit_name like concat('%',
                #{workunitName}, '%')
            </if>
            <if test="workstationName != null  and workstationName != ''">and ws.workstation_name like concat('%',
                #{workstationName}, '%')
            </if>

            <!--            <if test="wsName != null  and wsName != ''">and ws.workstation_name = #{wsName}-->
            <!--            </if>-->

            <if test="enableFlag != null  and enableFlag != ''">and w.enable_flag = #{enableFlag}</if>
            <if test="faultFlag != null  and faultFlag != ''">and w.fault_flag = #{faultFlag};</if>
        </where>
    </select>

    <select id="selectMdWorkunitLists" parameterType="MdWorkunit" resultMap="MdWorkunitVoResult">
        SELECT
        w.workunit_id,
        w.workstation_id,
        w.workunit_code,
        w.workunit_name,
        ws.workstation_code,
        ws.workstation_name,
        w.fault_flag,
        w.line_name,
        w.serial,
        w.enable_flag,
        w.solidify_time,
        w.remark,
        w.workstation_code,
        w.create_by,
        w.create_time,
        w.update_by,
        w.update_time,
        s.workshop_name
        FROM
        md_workunit w
        LEFT JOIN md_workstation ws ON w.workstation_id = ws.workstation_id
        left join md_workshop s on s.workshop_id=ws.workshop_id
        <where>
            <if test="workstationId != null ">and w.workstation_id = #{workstationId}</if>
            <if test="workstationCode != null ">and ws.workstation_code = #{workstationCode}</if>
            <if test="workunitCode != null  and workunitCode != ''">and w.workunit_code = #{workunitCode}</if>
            <if test="workunitName != null  and workunitName != ''">and w.workunit_name like concat('%',
                #{workunitName}, '%')
            </if>
            <if test="workstationName != null  and workstationName != ''">and ws.workstation_name like concat('%',
                #{workstationName}, '%')
            </if>
            <if test="enableFlag != null  and enableFlag != ''">and w.enable_flag = #{enableFlag}</if>
            <if test="faultFlag != null  and faultFlag != ''">and w.fault_flag = #{faultFlag}</if>
            <if test="lineName != null  and lineName != ''">and w.line_name = #{lineName}</if>
        </where>
    </select>

    <select id="selectMdWorkunitByWorkunitId" parameterType="Long" resultMap="MdWorkunitVoResult">
        select w.workunit_id,
               w.workstation_id,
               ws.workstation_name,
               ws.workstation_code,
               w.workunit_code,
               w.workunit_name,
               w.fault_flag,
               w.line_name,
               w.serial,
               w.enable_flag,
               w.remark,
               w.workstation_code,
               w.create_by,
               w.create_time,
               w.update_by,
               w.update_time,
               w.solidify_time
        from md_workunit w
                 left join md_workstation ws on w.workstation_id = ws.workstation_id
        where w.workunit_id = #{workunitId}
    </select>


    <insert id="insertMdWorkunit" parameterType="MdWorkunit" useGeneratedKeys="true" keyProperty="workunitId">
        insert into md_workunit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="workunitCode != null">workunit_code,</if>
            <if test="workunitName != null">workunit_name,</if>
            <if test="serial != null">serial,</if>
            <if test="lineName != null">line_name,</if>
            <if test="enableFlag != null">enable_flag,</if>
            <if test="faultFlag != null">fault_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="workstationCode != null">workstation_code,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="solidifyTime != null">solidify_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="workunitName != null">#{workunitName},</if>
            <if test="serial != null">#{serial},</if>
            <if test="lineName != null">#{lineName},</if>
            <if test="enableFlag != null">#{enableFlag},</if>
            <if test="faultFlag != null">#{faultFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="workstationCode != null">#{workstationCode},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="solidifyTime != null">#{solidifyTime},</if>
        </trim>
    </insert>

    <update id="updateMdWorkunit" parameterType="MdWorkunit">
        update md_workunit
        <trim prefix="SET" suffixOverrides=",">
            <if test="workstationCode != null">workstation_code = #{workstationCode},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="workunitCode != null">workunit_code = #{workunitCode},</if>
            <if test="workunitName != null">workunit_name = #{workunitName},</if>
            <if test="serial != null">serial = #{serial},</if>
            <if test="lineName != null">line_name = #{lineName},</if>
            <if test="enableFlag != null">enable_flag = #{enableFlag},</if>
            <if test="faultFlag != null">fault_flag=#{faultFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="solidifyTime != null">solidify_time = #{solidifyTime},</if>
        </trim>
        where workunit_id = #{workunitId}
    </update>

    <delete id="deleteMdWorkunitByWorkunitId" parameterType="Long">
        delete
        from md_workunit
        where workunit_id = #{workunitId}
    </delete>

    <delete id="deleteMdWorkunitByWorkunitIds" parameterType="String">
        delete from md_workunit where workunit_id in
        <foreach item="workunitId" collection="array" open="(" separator="," close=")">
            #{workunitId}
        </foreach>
    </delete>

</mapper>
