<?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.shzg.project.worn.mapper.WornDeclareItemMapper">

    <resultMap id="WornDeclareItemResult" type="com.shzg.project.worn.domain.WornDeclareItem">

        <!-- ===== 主键必须最前 ===== -->
        <id property="id" column="id"/>

        <!-- ===== 主表（bill）===== -->
        <result property="billId" column="bill_id"/>
        <result property="billNo" column="bill_no"/>
        <result property="projectId" column="project_id"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="deliveryTime" column="delivery_time"/>
        <result property="address" column="address"/>
        <result property="billRemark" column="bill_remark"/>
        <result property="billStatus" column="bill_status"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>

        <!-- ===== 明细（item）===== -->
        <result property="materialId" column="material_id"/>
        <result property="quantity" column="quantity"/>
        <result property="uniqueCode" column="unique_code"/>
        <result property="remark" column="remark"/>
        <result property="isDelete" column="is_delete"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="status" column="status"/>

        <!-- ===== 物料 ===== -->
        <result property="materialName" column="material_name"/>
        <result property="materialCode" column="material_code"/>
        <result property="materialShortName" column="material_short_name"/>
        <result property="specification" column="specification"/>
        <result property="model" column="model"/>
        <result property="weight" column="weight"/>

        <!-- ===== 单位 ===== -->
        <result property="unitId" column="unit_id"/>
        <result property="unitName" column="unit_name"/>

        <!-- ===== 类型 ===== -->
        <result property="typeName" column="type_name"/>
        <result property="typeParentNames" column="type_parent_names"/>

    </resultMap>

    <sql id="selectWornDeclareItemVo">
        select id, bill_id, bill_no, material_id, material_name, material_code, material_short_name, specification, model, type_name, type_parent_names, unique_code, quantity, unit_id, unit_name, remark, status, create_by, create_time, update_by, update_time, is_delete from worn_declare_item
    </sql>

    <select id="selectWornDeclareItemList" parameterType="WornDeclareItem" resultMap="WornDeclareItemResult">
        <include refid="selectWornDeclareItemVo"/>
        <where>  
            <if test="billId != null "> and bill_id = #{billId}</if>
            <if test="billNo != null  and billNo != ''"> and bill_no = #{billNo}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialCode != null  and materialCode != ''"> and material_code = #{materialCode}</if>
            <if test="materialShortName != null  and materialShortName != ''"> and material_short_name like concat('%', #{materialShortName}, '%')</if>
            <if test="specification != null  and specification != ''"> and specification = #{specification}</if>
            <if test="model != null  and model != ''"> and model = #{model}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="typeParentNames != null  and typeParentNames != ''"> and type_parent_names = #{typeParentNames}</if>
            <if test="uniqueCode != null "> and unique_code = #{uniqueCode}</if>
            <if test="quantity != null "> and quantity = #{quantity}</if>
            <if test="unitId != null "> and unit_id = #{unitId}</if>
            <if test="unitName != null  and unitName != ''"> and unit_name like concat('%', #{unitName}, '%')</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="isDelete != null  and isDelete != ''"> and is_delete = #{isDelete}</if>
        </where>
    </select>
    
    <select id="selectWornDeclareItemById" parameterType="Long" resultMap="WornDeclareItemResult">
        <include refid="selectWornDeclareItemVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornDeclareItem" parameterType="WornDeclareItem" useGeneratedKeys="true" keyProperty="id">
        insert into worn_declare_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billId != null">bill_id,</if>
            <if test="billNo != null and billNo != ''">bill_no,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="materialCode != null and materialCode != ''">material_code,</if>
            <if test="materialShortName != null and materialShortName != ''">material_short_name,</if>
            <if test="specification != null and specification != ''">specification,</if>
            <if test="model != null and model != ''">model,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="typeParentNames != null and typeParentNames != ''">type_parent_names,</if>
            <if test="uniqueCode != null">unique_code,</if>
            <if test="quantity != null">quantity,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="unitName != null and unitName != ''">unit_name,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billId != null">#{billId},</if>
            <if test="billNo != null and billNo != ''">#{billNo},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="materialCode != null and materialCode != ''">#{materialCode},</if>
            <if test="materialShortName != null and materialShortName != ''">#{materialShortName},</if>
            <if test="specification != null and specification != ''">#{specification},</if>
            <if test="model != null and model != ''">#{model},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="typeParentNames != null and typeParentNames != ''">#{typeParentNames},</if>
            <if test="uniqueCode != null">#{uniqueCode},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="unitName != null and unitName != ''">#{unitName},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateWornDeclareItem" parameterType="WornDeclareItem">
        update worn_declare_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="billId != null">bill_id = #{billId},</if>
            <if test="billNo != null and billNo != ''">bill_no = #{billNo},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialShortName != null">material_short_name = #{materialShortName},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="model != null">model = #{model},</if>
            <if test="typeName != null">type_name = #{typeName},</if>
            <if test="typeParentNames != null">type_parent_names = #{typeParentNames},</if>
            <if test="uniqueCode != null">unique_code = #{uniqueCode},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="unitName != null">unit_name = #{unitName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</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>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWornDeclareItemById" parameterType="Long">
        delete from worn_declare_item where id = #{id}
    </delete>

    <delete id="deleteWornDeclareItemByIds" parameterType="String">
        delete from worn_declare_item where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectWornDeclareItemDetailByBillNo"
            parameterType="String"
            resultMap="WornDeclareItemResult">
        SELECT
            -- ===== 主表（bill）=====
            b.id AS bill_id,
            b.bill_no AS bill_no,
            b.project_id,
            b.contact_name,
            b.contact_phone,
            b.delivery_time,
            b.address,
            b.remark AS bill_remark,
            b.status AS bill_status,
            b.update_by,
            b.update_time,

            -- ===== 明细（item）=====
            i.id AS id,
            i.material_id,
            i.quantity,
            i.unique_code,
            i.remark,
            i.is_delete,
            i.create_by,
            i.create_time,
            i.status,

            -- ===== 物料（material）=====
            m.material_name,
            m.material_code,
            m.material_short_name,
            m.specification,
            m.model,
            m.weight,
            m.unit_id,
            mu.unit_name,

            -- ===== 类型（material_type）=====
            mt.type_name AS type_name,
            (
                SELECT GROUP_CONCAT(t2.type_name ORDER BY t2.id SEPARATOR '/')
                FROM worn_material_type t2
                WHERE FIND_IN_SET(t2.id, mt.ancestors) OR t2.id = mt.id
            ) AS type_parent_names

        FROM worn_declare_bill b
                 LEFT JOIN worn_declare_item i
                           ON b.id = i.bill_id
                 LEFT JOIN worn_material m
                           ON i.material_id = m.id
                 LEFT JOIN worn_material_type mt
                           ON m.type_id = mt.id
                 LEFT JOIN worn_material_unit mu
                           ON m.unit_id = mu.id
        WHERE b.bill_no = #{billNo}
          AND b.is_delete = '0'
          AND (i.is_delete = '0' OR i.is_delete IS NULL)
        ORDER BY i.id ASC
    </select>
    <update id="deleteWornDeclareItemByBillId">
        update worn_declare_item
        set is_delete = '1',
            status = '9',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where bill_id = #{billId}
          and is_delete = '0'
    </update>
    <update id="cancelWornDeclareItemByBillId">
        update worn_declare_item
        set status = '9',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where bill_id = #{billId}
          and is_delete = '0'
    </update>
</mapper>