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

    <resultMap type="MdItem" id="MdItemResult">
        <id property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="specification" column="specification"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="itemOrProduct" column="item_or_product"/>
        <result property="itemTypeId" column="item_type_id"/>
        <result property="itemTypeCode" column="item_type_code"/>
        <result property="itemTypeName" column="item_type_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="safeStockFlag" column="safe_stock_flag"/>
        <result property="minStock" column="min_stock"/>
        <result property="maxStock" column="max_stock"/>
        <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"/>
        <result property="remark" column="remark"/>
        <result property="isFormal" column="is_formal"/>
        <result property="brandId" column="brand_id"/>
        <result property="brandCode" column="brand_code"/>
        <result property="brandName" column="brand_name"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="message" column="message"/>
        <result property="sapFactoryInfo" column="sap_factory_info"/>
        <result property="sapEnglishName" column="sap_english_name"/>
        <result property="itemEnglishName" column="item_english_name"/>
        <result property="itemNo" column="item_no"/>
        <result property="dta" column="dta"/>


    </resultMap>

    <resultMap type="com.ximai.mes.md.vo.MdItemVo" id="MdItemResultVo">
        <id property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="productEnglishName" column="product_english_name"/>
        <result property="specification" column="specification"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="itemOrProduct" column="item_or_product"/>
        <result property="itemTypeId" column="item_type_id"/>
        <result property="itemTypeCode" column="item_type_code"/>
        <result property="itemTypeName" column="item_type_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="safeStockFlag" column="safe_stock_flag"/>
        <result property="minStock" column="min_stock"/>
        <result property="maxStock" column="max_stock"/>
        <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"/>
        <result property="remark" column="remark"/>
        <result property="isFormal" column="is_formal"/>
        <result property="brandId" column="brand_id"/>
        <result property="brandCode" column="brand_code"/>
        <result property="brandName" column="brand_name"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="message" column="message"/>
        <result property="length" column="length"/>
        <result property="width" column="width"/>
        <result property="height" column="height"/>
        <result property="thick" column="thick"/>
        <result property="weight" column="weight"/>
        <result property="angleType" column="angle_type"/>
        <result property="isAllotype" column="is_allotype"/>
        <result property="isRightAngle" column="is_right_angle"/>
        <result property="fillet" column="fillet"/>
        <result property="angle" column="angle"/>
        <result property="aperture" column="aperture"/>
        <result property="singleLayerThick" column="single_layer_thick"/>
        <result property="singleWeight" column="single_weight"/>
        <result property="layerNum" column="layer_num"/>
        <result property="isMould" column="is_mould"/>
        <result property="isCoding" column="is_coding"/>
        <result property="certification" column="certification"/>
        <result property="shape" column="shape"/>
        <result property="shapeSize" column="shape_size"/>
        <result property="sizeGroupId" column="size_group_id"/>
        <result property="sizeGroupName" column="size_group_name"/>
        <result property="sheetLength" column="sheet_length"/>
        <result property="sheetWidth" column="sheet_width"/>
        <result property="dispatchMultiple" column="dispatch_multiple"/>
        <result property="rowNum" column="row_num"/>
        <result property="itemEnglishName" column="item_english_name"/>
        <result property="itemNo" column="item_no"/>
        <result property="dta" column="dta"/>
    </resultMap>

    <sql id="selectMdItemVo">
        select mi.item_id,
               mi.item_code,
               mi.sap_item_code,
               mi.item_name,
               mi.dta,
               mi.item_english_name,
               mi.item_no,
               mi.message,
               mi.specification,
               mi.unit_of_measure,
               mi.item_or_product,
               mi.item_type_id,
               mi.item_type_code,
               mi.item_type_name,
               mi.enable_flag,
               mi.safe_stock_flag,
               mi.sap_factory_info,
               mi.sap_english_name,
               mi.min_stock,
               mi.max_stock,
               mi.create_by,
               mi.create_time,
               mi.remark,
               mi.is_formal,
               mbi.brand_id,
               mbi.product_english_name,
               msg.size_group_name,
               mbi.size_group_id,
               mb.brand_name,
               mb.brand_code,
               mpl.length,
               mpl.width,
               mpl.height,
               mpl.thick,
               mpl.weight,
               mpl.angle_type,
               mpl.is_allotype,
               mpl.is_right_angle,
               mpl.fillet,
               mpl.angle,
               mpl.aperture,
               mpl.single_layer_thick,
               mpl.single_weight,
               mpl.layer_num,
               mpl.is_mould,
               mpl.is_coding,
               mpl.certification,
               mpl.shape,
               mpl.shape_size,
               mppc.sheet_length,
               mppc.sheet_width,
               mppc.dispatch_multiple,
               mppc.row_num
        from md_item mi
                 left join md_base_info mbi on mi.item_id = mbi.item_id
                 left join md_brand mb on mbi.brand_id = mb.brand_id
                 LEFT JOIN md_product_look mpl ON mpl.item_id = mi.item_id
                 left join md_product_published_conf mppc on mppc.item_id = mi.item_id
                 left join md_size_group msg on msg.size_group_id = mbi.size_group_id
    </sql>

    <select id="selectMdItemList" parameterType="MdItem" resultMap="MdItemResultVo">
        <include refid="selectMdItemVo"/>
        <where>
            <if test="itemCode != null and itemCode != ''">
                AND mi.item_code like concat('%', #{itemCode}, '%')
            </if>
            <if test="sapItemCode != null and sapItemCode != ''">
                AND mi.sap_item_code like concat('%', #{sapItemCode}, '%')
            </if>
            <if test="sapItemCodeList != null and sapItemCodeList.size() > 0">
                AND mi.sap_item_code in
                <foreach collection="sapItemCodeList" item="sapItemCode" open="(" separator="," close=")">
                    #{sapItemCode}
                </foreach>
            </if>
            <if test="itemTypeId != null and itemTypeId != 0 ">
                AND (mi.item_type_id = #{itemTypeId} OR mi.item_type_id in (select item_type_id from md_item_type where
                find_in_set(#{itemTypeId},ancestors)))
            </if>
            <if test="itemName != null and itemName != ''">
                AND mi.item_name like concat('%', #{itemName}, '%')
            </if>
            <if test="itemId != null and itemId != ''">
                AND mi.item_id = #{itemId}
            </if>
            <if test="enableFlag != null and enableFlag != ''">
                AND mi.enable_flag = #{enableFlag}
            </if>
            <if test="brandName != null and brandName != ''">
                AND mb.brand_name like concat('%', #{brandName}, '%')
            </if>
            <if test="brandCode != null and brandCode != ''">
                AND mb.brand_code like concat('%', #{brandCode}, '%')
            </if>
        </where>
    </select>

    <select id="selectMdItemLists" parameterType="MdItem" resultMap="MdItemResultVo">
        <include refid="selectMdItemVo"/>
        <where>
            <if test="itemName != null and itemName != ''">
                AND mi.item_name = #{itemName}
            </if>
        </where>
    </select>


    <select id="selectMdItemById" parameterType="Long" resultMap="MdItemResult">
        <include refid="selectMdItemVo"></include>
        where mi.item_id = #{itemId}
    </select>

    <select id="checkItemCodeUnique" parameterType="MdItem" resultMap="MdItemResult">
        <include refid="selectMdItemVo"/>
        where mi.item_code = #{itemCode} limit 1
    </select>

    <select id="checkItemNameUnique" parameterType="MdItem" resultMap="MdItemResult">
        <include refid="selectMdItemVo"/>
        where mi.item_code = #{itemName} limit 1
    </select>
    <select id="selectMdItemByItemName" resultType="com.ximai.mes.md.domain.MdItem"
            parameterType="java.lang.String">
        select *
        from md_item
        where item_name = #{itemName}
    </select>
    <select id="selectMdItemBySapItemCode" resultType="com.ximai.mes.md.domain.MdItem" parameterType="java.lang.String">
        <include refid="selectMdItemVo"/>
        where mi.sap_item_code = #{sapItemCode} limit 1
    </select>

    <insert id="insertMdItem" parameterType="MdItem" useGeneratedKeys="true" keyProperty="itemId">
        insert into md_item(
        item_code,
        item_name,
        <if test="specification !=null and specification !=''">specification,</if>

        unit_of_measure,
        item_or_product,
        item_type_id,
        <if test="itemTypeCode !=null and itemTypeCode !=''">item_type_code,</if>
        <if test="itemTypeName !=null and itemTypeName !=''">item_type_name,</if>
        <if test="enableFlag !=null and enableFlag !=''">enable_flag,</if>
        safe_stock_flag,
        <if test="safeStockFlag =='Y'.toString()">min_stock,</if>
        <if test="safeStockFlag =='Y'.toString()">max_stock,</if>
        <if test="remark !=null and remark !=''">remark,</if>
        <if test="sapFactoryInfo !=null and sapFactoryInfo !=''">sap_factory_info,</if>
        <if test="sapEnglishName !=null and sapEnglishName !=''">sap_english_name,</if>
        <if test="sapItemCode !=null and sapItemCode !=''">sap_item_code,</if>
        <if test="attr2 !=null and attr2 !=''">attr2,</if>
        <if test="attr3 !=null and attr3 !=0">attr3,</if>
        <if test="attr4 !=null and attr4 !=0">attr4,</if>
        is_formal,
        <if test="createBy !=null and createBy !=''">create_by,</if>
        <if test="itemEnglishName !=null and itemEnglishName !=''">item_english_name,</if>
        <if test="itemNo !=null and itemNo !=''">item_no,</if>
        create_time

        )
        values (
        #{itemCode},
        #{itemName},
        <if test="specification !=null and specification !=''">#{specification},</if>
        #{unitOfMeasure},
        #{itemOrProduct},
        #{itemTypeId},
        <if test="itemTypeCode !=null and itemTypeCode !=''">#{itemTypeCode},</if>
        <if test="itemTypeName !=null and itemTypeName !=''">#{itemTypeName},</if>
        <if test="enableFlag !=null and enableFlag !=''">#{enableFlag},</if>
        #{safeStockFlag},
        <if test="sapFactoryInfo !=null and sapFactoryInfo !=''">#{sapFactoryInfo},</if>
        <if test="sapEnglishName !=null and sapEnglishName !=''">#{sapEnglishName},</if>
        <if test="sapItemCode !=null and sapItemCode !=''">#{sapItemCode},</if>
        <if test="safeStockFlag =='Y'.toString() and minStock !=null ">#{minStock},</if>
        <if test="safeStockFlag =='Y'.toString() and safeStockFlag !=null ">#{maxStock},</if>
        <if test="remark !=null and remark !=''">#{remark},</if>
        <if test="attr1 !=null and attr1 !=''">#{attr1},</if>
        <if test="attr2 !=null and attr2 !=''">#{attr2},</if>
        <if test="attr3 !=null and attr3 !=0">#{attr3},</if>
        <if test="attr4 !=null and attr4 !=0">#{attr4},</if>
        0,
        <if test="createBy !=null and createBy !=''">#{createBy},</if>
        <if test="itemEnglishName !=null and itemEnglishName !=''">#{itemEnglishName},</if>
        <if test="itemNo !=null and itemNo !=''">#{itemNo},</if>
        sysdate()
        )
    </insert>


    <insert id="insertMdItems" parameterType="MdItem" useGeneratedKeys="true" keyProperty="itemId">
        insert into md_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="itemCode != null and itemCode != ''">item_code,</if>
            <if test="sapItemCode != null">sap_item_code,</if>
            <if test="itemName != null and itemName != ''">item_name,</if>
            <if test="specification != null">specification,</if>
            <if test="unitOfMeasure != null and unitOfMeasure != ''">unit_of_measure,</if>
            <if test="itemOrProduct != null">item_or_product,</if>
            <if test="itemTypeId != null">item_type_id,</if>
            <if test="itemTypeCode != null">item_type_code,</if>
            <if test="itemTypeName != null">item_type_name,</if>
            <if test="enableFlag != null and enableFlag != ''">enable_flag,</if>
            <if test="safeStockFlag != null">safe_stock_flag,</if>
            <if test="sapEnglishName != null">sap_english_name,</if>
            <if test="sapFactoryInfo != null">sap_factory_info,</if>
            <if test="minStock != null">min_stock,</if>
            <if test="maxStock != null">max_stock,</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="isFormal != null">is_formal,</if>
            <if test="message != null">message,</if>
            <if test="itemEnglishName !=null and itemEnglishName !=''">item_english_name,</if>
            <if test="itemNo !=null and itemNo !=''">item_no,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="itemCode != null and itemCode != ''">#{itemCode},</if>
            <if test="sapItemCode != null">#{sapItemCode},</if>
            <if test="itemName != null and itemName != ''">#{itemName},</if>
            <if test="specification != null">#{specification},</if>
            <if test="unitOfMeasure != null and unitOfMeasure != ''">#{unitOfMeasure},</if>
            <if test="itemOrProduct != null">#{itemOrProduct},</if>
            <if test="itemTypeId != null">#{itemTypeId},</if>
            <if test="itemTypeCode != null">#{itemTypeCode},</if>
            <if test="itemTypeName != null">#{itemTypeName},</if>
            <if test="enableFlag != null and enableFlag != ''">#{enableFlag},</if>
            <if test="safeStockFlag != null">#{safeStockFlag},</if>
            <if test="sapEnglishName != null">#{sapEnglishName},</if>
            <if test="sapFactoryInfo != null">#{sapFactoryInfo},</if>
            <if test="minStock != null">#{minStock},</if>
            <if test="maxStock != null">#{maxStock},</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="isFormal != null">#{isFormal},</if>
            <if test="message != null">#{message},</if>
            <if test="itemEnglishName !=null and itemEnglishName !=''">#{itemEnglishName},</if>
            <if test="itemNo !=null and itemNo !=''">#{itemNo},</if>
        </trim>
    </insert>

    <update id="updateMdItem" parameterType="MdItem">
        update md_item
        <set>
            <if test="itemName !=null and itemName !=''">item_name = #{itemName},</if>
            <if test="sapItemCode !=null and sapItemCode !=''">sap_item_code = #{sapItemCode},</if>
            <if test="message !=null and message !=''">message = #{message},</if>
            <if test="itemTypeId != null and itemTypeId != 0 ">item_type_id = #{itemTypeId},</if>
            <if test="itemTypeCode !=null and itemTypeCode !=''">item_type_code = #{itemTypeCode},</if>
            <if test="itemTypeName !=null and itemTypeName !=''">item_type_name = #{itemTypeName},</if>
            <if test="specification !=null and specification !=''">specification = #{specification},</if>
            <if test="sapFactoryInfo !=null and sapFactoryInfo !=''">sap_factory_info = #{sapFactoryInfo},</if>
            <if test="unitOfMeasure !=null and unitOfMeasure !=''">unit_of_measure = #{unitOfMeasure},</if>
            <if test="itemOrProduct !=null and itemOrProduct !=''">item_or_product = #{itemOrProduct},</if>
            <if test="sapEnglishName !=null and sapEnglishName !=''">sap_english_name = #{sapEnglishName},</if>
            <if test="enableFlag !=null and enableFlag !=''">enable_flag = #{enableFlag},</if>
            <if test="safeStockFlag !=null and safeStockFlag !=''">safe_stock_flag = #{safeStockFlag},</if>
            <if test="minStock !=null and minStock !=''">min_stock = #{minStock},</if>
            <if test="maxStock !=null and maxStock !=''">max_stock = #{maxStock},</if>
            <if test="remark !=null and remark !=''">remark = #{remark},</if>
            <if test="attr1 !=null and attr1 !=''">attr1=#{attr1},</if>
            <if test="attr2 !=null and attr2 !=''">attr2=#{attr2},</if>
            <if test="attr3 !=null and attr3 !=0">attr3=#{attr3},</if>
            <if test="attr4 !=null and attr4 !=0">attr4=#{attr4},</if>
            <if test="updateBy !=null and updateBy !=''">update_by=#{updateBy},</if>
            <if test="itemEnglishName !=null and itemEnglishName !=''">item_english_name = #{itemEnglishName},</if>
            <if test="itemNo !=null and itemNo !=''">item_no = #{itemNo},</if>
            update_time = sysdate()
        </set>
        where item_id = #{itemId}
    </update>


    <update id="becomeFormalProduct" parameterType="Long">
        update md_item set is_formal = 1 where item_id in
        <foreach collection="array" item="itemId" open="(" separator="," close=")">
            #{itemId}
        </foreach>
    </update>

    <delete id="deleteMdItemById" parameterType="Long">
        delete
        from md_item
        where item_id = #{itemId}
    </delete>

    <delete id="deleteMdItemByIds" parameterType="Long">
        delete from md_item where item_id in
        <foreach collection="array" item="itemId" open="(" separator="," close=")">
            #{itemId}
        </foreach>
    </delete>

</mapper>
