<?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.WornOutboundItemMapper">
    
    <resultMap type="WornOutboundItem" id="WornOutboundItemResult">
        <result property="id"    column="id"    />
        <result property="billId" column="bill_id"/>
        <result property="billNo" column="bill_no"/>
        <result property="billType" column="bill_type"/>

        <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="billRemark" column="bill_remark"/>
        <result property="outboundTime" column="outbound_time"/>
        <result property="billStatus" column="bill_status"/>

        <result property="materialId" column="material_id"/>
        <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="quantity" column="quantity"/>
        <result property="uniqueCode" column="unique_code"/>
        <result property="remark" column="remark"/>
        <result property="isDelete" column="is_delete"/>

        <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="status" column="status"/>

        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectWornOutboundItemVo">
        select id, bill_id, bill_no, material_id, quantity, unique_code, remark, status, is_delete, create_by, create_time, update_by, update_time from worn_outbound_item
    </sql>

    <select id="selectWornOutboundItemList" parameterType="com.shzg.project.worn.domain.WornOutboundItem"
            resultMap="WornOutboundItemResult">
        SELECT
            oi.id,
            oi.bill_id,
            oi.bill_no,
            ob.bill_type,
            ob.warehouse_code,
            ob.warehouse_name,
            ob.area_code,
            ob.area_name,
            ob.remark AS bill_remark,
            ob.outbound_time,
            ob.status AS bill_status,
            ob.create_time,

            oi.material_id,
            oi.quantity,
            oi.unique_code,
            oi.remark,
            COALESCE(oi.is_delete, '0') AS is_delete,

            m.material_name,
            m.material_code,
            m.material_short_name,
            m.specification,
            m.model,
            m.unit_id,
            mu.unit_name,

            uc.status,
            mt.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_outbound_item oi
                 LEFT JOIN worn_outbound_bill ob ON oi.bill_id = ob.id
                 LEFT JOIN worn_material m ON oi.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
                 LEFT JOIN worn_unique_code uc ON oi.unique_code = uc.code

        WHERE (oi.is_delete = '0' OR oi.is_delete IS NULL)
          AND (ob.is_delete = '0' OR ob.is_delete IS NULL)
          AND (m.is_delete = '0' OR m.is_delete IS NULL)
          AND (mu.is_delete = '0' OR mu.is_delete IS NULL)
          AND oi.bill_no = #{billNo}

        ORDER BY oi.id ASC
    </select>


    <select id="selectWornOutboundItemById" parameterType="Long" resultMap="WornOutboundItemResult">
        <include refid="selectWornOutboundItemVo"/>
        where id = #{id}
    </select>

    <insert id="insertWornOutboundItem" parameterType="WornOutboundItem" useGeneratedKeys="true" keyProperty="id">
        insert into worn_outbound_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="status != null">status,</if>
            <if test="isDelete != null">is_delete,</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="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="status != null">#{status},</if>
            <if test="isDelete != null">#{isDelete},</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="updateWornOutboundItem" parameterType="WornOutboundItem">
        update worn_outbound_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="status != null">status = #{status},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</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 id = #{id}
    </update>

    <delete id="deleteWornOutboundItemById" parameterType="Long">
        delete from worn_outbound_item where id = #{id}
    </delete>

    <delete id="deleteWornOutboundItemByIds">
        delete from worn_outbound_item where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="countNotOutboundByBillId" resultType="int">
        select count(1)
        from worn_outbound_item oi
                 left join worn_unique_code uc on oi.unique_code = uc.code
        where oi.bill_id = #{billId}
          and (oi.is_delete = '0' or oi.is_delete is null)
          and (uc.is_delete = '0' or uc.is_delete is null)
          and uc.status != '4'
    </select>
</mapper>