<?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.WornInboundItemMapper">
    
    <resultMap type="WornInboundItem" id="WornInboundItemResult">
        <result property="id"    column="id"    />
        <result property="billId"    column="bill_id"    />
        <result property="billNo"    column="bill_no"    />
        <result property="materialId"    column="material_id"    />
        <result property="quantity"    column="quantity"    />
        <result property="uniqueCode"    column="unique_code"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="inboundTime"    column="inbound_time"    />
        <!-- 扩展查询字段 -->
        <result property="warehouseCode"     column="warehouse_code"/>
        <result property="warehouseName"     column="warehouse_name"/>
        <result property="areaCode"          column="area_code"/>
        <result property="areaName"          column="area_name"/>
        <result property="materialName"      column="material_name"/>
        <result property="materialShortName" column="material_short_name"/>
        <result property="materialCode"      column="material_code"/>
        <result property="specification"     column="specification"/>
        <result property="model"             column="model"/>
        <result property="weight"            column="weight"/>
        <result property="billRemark"        column="bill_remark"/>
        <result property="billStatus"        column="bill_status"/>
        <result property="billType"        column="bill_type"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="typeName" column="type_name"/>
        <result property="typeParentNames" column="type_parent_names"/>
        <result property="unitId" column="unit_id"/>
        <result property="unitName" column="unit_name"/>
        <result property="operatorName" column="operator_name"/>
    </resultMap>

    <sql id="selectWornInboundItemVo">
        select id, bill_id, bill_no, material_id, quantity, unique_code, remark, create_by, create_time from worn_inbound_item
    </sql>

    <select id="selectWornInboundItemList" parameterType="com.shzg.project.worn.domain.WornInboundItem" resultMap="WornInboundItemResult">
        SELECT
            -- ===== 主表（bill）=====
            b.id      AS bill_id,
            b.bill_no AS bill_no,
            b.bill_type       ,
            b.warehouse_code   ,
            b.warehouse_name   ,
            b.area_code        ,
            b.area_name       ,
            b.remark AS bill_remark,
            b.inbound_time     ,
            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_inbound_bill b
                 LEFT JOIN worn_inbound_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>
    
    <select id="selectWornInboundItemById" parameterType="Long" resultMap="WornInboundItemResult">
        <include refid="selectWornInboundItemVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornInboundItem" parameterType="WornInboundItem" useGeneratedKeys="true" keyProperty="id">
        insert into worn_inbound_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billId != null">bill_id,</if>
            <if test="billNo != null">bill_no,</if>
            <if test="materialId != null">material_id,</if>
            <if test="quantity != null">quantity,</if>
            <if test="uniqueCode != null">unique_code,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isDelete != null">is_delete,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billId != null">#{billId},</if>
            <if test="billNo != null">#{billNo},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="uniqueCode != null">#{uniqueCode},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
         </trim>
    </insert>

    <update id="updateWornInboundItem" parameterType="WornInboundItem">
        update worn_inbound_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="billId != null">bill_id = #{billId},</if>
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="uniqueCode != null">unique_code = #{uniqueCode},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="isDelete != null and isDelete != ''">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWornInboundItemById" parameterType="Long">
        delete from worn_inbound_item where id = #{id}
    </delete>

    <delete id="deleteWornInboundItemByIds" parameterType="String">
        delete from worn_inbound_item where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="updateUniqueCodeByBillNoAndMaterialId">
        update worn_inbound_item
        set unique_code = #{uniqueCode}
        where bill_no = #{billNo}
          and material_id = #{materialId}
          and (is_delete = '0' or is_delete is null)
    </update>
    <update id="updateInboundItemStatus"
            parameterType="com.shzg.project.worn.domain.WornInboundItem">
        UPDATE worn_inbound_item
        SET status = #{status},
            remark = #{remark},
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE id = #{id}
          AND status = '0'
          AND (is_delete = '0' OR is_delete IS NULL)
    </update>

    <select id="countNotInboundByBillId"
            parameterType="Long"
            resultType="int">
        SELECT COUNT(1)
        FROM worn_inbound_item
        WHERE bill_id = #{billId}
          AND (is_delete = '0' OR is_delete IS NULL)
          AND (status IS NULL OR status != '1')
    </select>
    <update id="updateItemToVoid">
        update worn_inbound_item
        set status = '2',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where id = #{id}
          and (is_delete = '0' or is_delete is null)
    </update>
    <select id="selectWornInboundItemByBillId"
            parameterType="Long"
            resultMap="WornInboundItemResult">
        SELECT
            i.id,
            i.bill_id,
            i.bill_no,
            i.material_id,
            i.quantity,
            i.unique_code,
            i.remark,
            i.status,
            i.create_by,
            i.create_time,
            i.update_by,
            i.update_time,
            i.is_delete,
            b.inbound_time,
            b.warehouse_code,
            b.warehouse_name,
            b.area_code,
            b.area_name,
            b.remark AS bill_remark,
            b.status AS bill_status,
            b.bill_type,
            m.material_name,
            m.material_short_name,
            m.material_code,
            m.specification,
            m.model,
            m.weight,
            m.unit_id,
            mu.unit_name,
            u.nick_name AS operator_name
        FROM worn_inbound_item i
                 LEFT JOIN worn_inbound_bill b ON i.bill_id = b.id
                 LEFT JOIN worn_material m ON i.material_id = m.id
                 LEFT JOIN worn_material_unit mu ON m.unit_id = mu.id
                 LEFT JOIN sys_user u ON CAST(b.create_by AS UNSIGNED) = u.user_id
        WHERE i.bill_id = #{billId}
          AND (i.is_delete = '0' OR i.is_delete IS NULL)
          AND (b.is_delete = '0' OR b.is_delete IS NULL)
        ORDER BY i.id ASC
    </select>
</mapper>
