<?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.pro.mapper.ProCombinationMapper">


    <resultMap type="ProCombination" id="ProCombinationResult">
        <result property="combinationId" column="combination_id"/>
        <result property="combinationCode" column="combination_code"/>
        <result property="combinationSort" column="combination_sort"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="salesOrderId" column="sales_order_id"/>
        <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="selectProCombinationVo">
        select combination_id,
               combination_code,
               combination_sort,
               workorder_id,
               workorder_code,
               sales_order_id,
               create_by,
               create_time,
               update_by,
               update_time
        from pro_combination
    </sql>

    <select id="selectProCombinationList" parameterType="ProCombination" resultMap="ProCombinationResult">
        <include refid="selectProCombinationVo"/>
        <where>
            <if test="combinationCode != null  and combinationCode != ''">and combination_code = #{combinationCode}</if>
            <if test="combinationSort != null ">and combination_sort = #{combinationSort}</if>
            <if test="workorderId != null ">and workorder_id = #{workorderId}</if>
            <if test="workorderCode != null  and workorderCode != ''">and workorder_code = #{workorderCode}</if>
            <if test="salesOrderId != null ">and sales_order_id = #{salesOrderId}</if>
        </where>
    </select>


    <select id="selectProCombList" parameterType="ProCombination" resultMap="ProCombinationResult">
        SELECT
        combination_code ,
        GROUP_CONCAT(workorder_code SEPARATOR ',') AS workorder_code,
        create_by,
        create_time
        FROM
        pro_combination
        <where>
            <if test="combinationCode != null  and combinationCode != ''">and combination_code = #{combinationCode}</if>
            <if test="combinationSort != null ">and combination_sort = #{combinationSort}</if>
            <if test="workorderId != null ">and workorder_id = #{workorderId}</if>
            <if test="salesOrderId != null ">and sales_order_id = #{salesOrderId}</if>
        </where>
        GROUP BY
        combination_code,
        create_by,
        create_time
    </select>


    <select id="selectProCombinationByCombinationId" parameterType="Long" resultMap="ProCombinationResult">
        <include refid="selectProCombinationVo"/>
        where combination_id = #{combinationId}
    </select>


    <insert id="insertProCombination" parameterType="ProCombination" useGeneratedKeys="true"
            keyProperty="combinationId">
        insert into pro_combination
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="combinationCode != null and combinationCode != ''">combination_code,</if>
            <if test="combinationSort != null">combination_sort,</if>
            <if test="workorderId != null">workorder_id,</if>
            <if test="workorderCode != null">workorder_code,</if>
            <if test="salesOrderId != null">sales_order_id,</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="combinationCode != null and combinationCode != ''">#{combinationCode},</if>
            <if test="combinationSort != null">#{combinationSort},</if>
            <if test="workorderId != null">#{workorderId},</if>
            <if test="workorderCode != null">#{workorderCode},</if>
            <if test="salesOrderId != null">#{salesOrderId},</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="updateProCombination" parameterType="ProCombination">
        update pro_combination
        <trim prefix="SET" suffixOverrides=",">
            <if test="combinationCode != null and combinationCode != ''">combination_code = #{combinationCode},</if>
            <if test="combinationSort != null">combination_sort = #{combinationSort},</if>
            <if test="workorderId != null">workorder_id = #{workorderId},</if>
            <if test="workorderCode != null">workorder_code = #{workorderCode},</if>
            <if test="salesOrderId != null">sales_order_id = #{salesOrderId},</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 combination_id = #{combinationId}
    </update>

    <delete id="deleteProCombinationByCombinationId" parameterType="Long">
        delete
        from pro_combination
        where combination_id = #{combinationId}
    </delete>

    <delete id="deleteProCombinationByCombinationIds" parameterType="String">
        delete from pro_combination where combination_id in
        <foreach item="combinationId" collection="array" open="(" separator="," close=")">
            #{combinationId}
        </foreach>
    </delete>


    <select id="selectProCombinationInWorkorderGroup" parameterType="String" resultType="String">
        SELECT workorder_ids from (
        SELECT
        t2.size_group,
        t2.client_size_unit,
        t2.client_cmd,
        t2.type_no,
        t2.color_no,
        t2.color,
        t2.sale_path,
        t2.segment,
        t2.`QUARTER`,
        t2.whole_size,
        t2.spec_size,
        t2.po_no,
        t2.wip_no,
        t2.packing_num,
        t2.region,
        t2.cycle,
        t2.sk_no,
        t2.patent_no,
        t2.material_desc,
        t2.signer,
        t2.ship_to_id,
        t2.prod_spec,
        t2.prod_usage,
        t2.thickness,
        t2.pack_mode,
        t2.printing_direction,
        t2.plant_code,
        t3.item_type_id,
        GROUP_CONCAT(t1.workorder_id) AS workorder_ids
        FROM
        pro_workorder t1
        LEFT JOIN md_item t3 ON t1.product_id = t3.item_code
        INNER JOIN pro_work_order_so_directive t2 ON t1.workorder_id = t2.workorder_id
        WHERE
        t1.workorder_id IN
        <foreach item="workorder_id" collection="array" open="(" separator="," close=")">
            #{workorder_id}
        </foreach>
        GROUP BY
        t2.size_group,
        t2.client_size_unit,
        t2.client_cmd,
        t2.type_no,
        t2.color_no,
        t2.color,
        t2.sale_path,
        t2.segment,
        t2.`QUARTER`,
        t2.whole_size,
        t2.spec_size,
        t2.po_no,
        t2.wip_no,
        t2.packing_num,
        t2.region,
        t2.cycle,
        t2.sk_no,
        t2.patent_no,
        t2.material_desc,
        t2.signer,
        t2.ship_to_id,
        t2.prod_spec,
        t2.prod_usage,
        t2.thickness,
        t2.pack_mode,
        t2.printing_direction,
        t2.plant_code,
        t3.item_type_id) t
    </select>

</mapper>
