<?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="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"/>
        <result property="treatingThicknessMin" column="treating_thickness_min"/>
        <result property="treatingThicknessMax" column="treating_thickness_max"/>
        <result property="treatingWidthMin" column="treating_width_min"/>
        <result property="treatingWidthMax" column="treating_width_max"/>
        <result property="dieSunkOffsetOilTankNum" column="die_sunk_offset_oil_tank_num"/>
        <result property="printingMethod" column="printing_method"/>
        <result property="sacmForEdgeOpening"    column="sacm_for_edge_opening"    />
        <result property="sacmForPasteComposite"    column="sacm_for_paste_composite"    />
        <result property="sacmForThermalComposite"    column="sacm_for_thermal_composite"    />
        <result property="sacmForLabeling"    column="sacm_for_labeling"    />
        <result property="sacmForBuckleUp"    column="sacm_for_buckle_up"    />
        <result property="sacmForCreasing"    column="sacm_for_creasing"    />
        <result property="sacmForOrgan"    column="sacm_for_organ"    />
        <result property="sacmForThermalCutting"    column="sacm_for_thermal_cutting"    />
        <result property="sacmForThermalSelfFusion"    column="sacm_for_thermal_self_fusion"    />
        <result property="sacmForMolarTooth"    column="sacm_for_molar_tooth"    />
        <result property="sacmForSlitting"    column="sacm_for_slitting"    />
    </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="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"/>
        <result property="treatingThicknessMin" column="treating_thickness_min"/>
        <result property="treatingThicknessMax" column="treating_thickness_max"/>
        <result property="treatingWidthMin" column="treating_width_min"/>
        <result property="treatingWidthMax" column="treating_width_max"/>
        <result property="dieSunkOffsetOilTankNum" column="die_sunk_offset_oil_tank_num"/>
        <result property="printingMethod" column="printing_method"/>
        <result property="sacmForEdgeOpening"    column="sacm_for_edge_opening"    />
        <result property="sacmForPasteComposite"    column="sacm_for_paste_composite"    />
        <result property="sacmForThermalComposite"    column="sacm_for_thermal_composite"    />
        <result property="sacmForLabeling"    column="sacm_for_labeling"    />
        <result property="sacmForBuckleUp"    column="sacm_for_buckle_up"    />
        <result property="sacmForCreasing"    column="sacm_for_creasing"    />
        <result property="sacmForOrgan"    column="sacm_for_organ"    />
        <result property="sacmForThermalCutting"    column="sacm_for_thermal_cutting"    />
        <result property="sacmForThermalSelfFusion"    column="sacm_for_thermal_self_fusion"    />
        <result property="sacmForMolarTooth"    column="sacm_for_molar_tooth"    />
        <result property="sacmForSlitting"    column="sacm_for_slitting"    />
    </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,
               fault_flag,
               enable_flag,
               remark,
               create_by,
               create_time,
               update_by,
               update_time,
               solidify_time,
               treating_thickness_min,
               treating_thickness_max,
               treating_width_min,
               treating_width_max,
               die_sunk_offset_oil_tank_num,
               printing_method,
               sacm_for_edge_opening,
               sacm_for_paste_composite,
               sacm_for_thermal_composite,
               sacm_for_labeling,
               sacm_for_buckle_up,
               sacm_for_creasing,
               sacm_for_organ,
               sacm_for_thermal_cutting,
               sacm_for_thermal_self_fusion,
               sacm_for_molar_tooth,
               sacm_for_slitting
        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,
        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.enable_flag,
        w.solidify_time,
        w.treating_thickness_min,
        w.treating_thickness_max,
        w.treating_width_min,
        w.treating_width_max,
        w.die_sunk_offset_oil_tank_num,
        w.printing_method,
        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>
        </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.enable_flag,
               w.remark,
               w.workstation_code,
               w.create_by,
               w.create_time,
               w.update_by,
               w.update_time,
               w.solidify_time,
               w.treating_thickness_min,
               w.treating_thickness_max,
               w.treating_width_min,
               w.treating_width_max,
               w.die_sunk_offset_oil_tank_num,
               w.printing_method
        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="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>
            <if test="treatingThicknessMin != null">treating_thickness_min,</if>
            <if test="treatingThicknessMax != null">treating_thickness_max,</if>
            <if test="treatingWidthMin != null">treating_width_min,</if>
            <if test="treatingWidthMax != null">treating_width_max,</if>
            <if test="dieSunkOffsetOilTankNum != null">die_sunk_offset_oil_tank_num,</if>
            <if test="printingMethod != null">printing_method,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="workunitName != null">#{workunitName},</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>
            <if test="treatingThicknessMin != null">#{treatingThicknessMin},</if>
            <if test="treatingThicknessMax != null">#{treatingThicknessMax},</if>
            <if test="treatingWidthMin != null">#{treatingWidthMin},</if>
            <if test="treatingWidthMax != null">#{treatingWidthMax},</if>
            <if test="dieSunkOffsetOilTankNum != null">#{dieSunkOffsetOilTankNum},</if>
            <if test="printingMethod != null">#{printingMethod},</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="workunitCode != null">workunit_code = #{workunitCode},</if>
            <if test="workunitName != null">workunit_name = #{workunitName},</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>
            <if test="treatingThicknessMin != null">treating_thickness_min = #{treatingThicknessMin},</if>
            <if test="treatingThicknessMax != null">treating_thickness_max = #{treatingThicknessMax},</if>
            <if test="treatingWidthMin != null">treating_width_min = #{treatingWidthMin},</if>
            <if test="treatingWidthMax != null">treating_width_max = #{treatingWidthMax},</if>
            <if test="dieSunkOffsetOilTankNum != null">die_sunk_offset_oil_tank_num = #{dieSunkOffsetOilTankNum},</if>
            <if test="printingMethod != null">printing_method = #{printingMethod},</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>
