<?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.unique.mapper.WornUniqueCodeMaterialMapper">

    <!-- ================= 原有 resultMap ================= -->
    <resultMap type="WornUniqueCodeMaterial" id="WornUniqueCodeMaterialResult">
        <result property="id" column="id"/>
        <result property="uniqueCodeId" column="unique_code_id"/>
        <result property="materialId" column="material_id"/>
        <result property="unitId" column="unit_id"/>
        <result property="quantity" column="quantity"/>
        <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="isDelete" column="is_delete"/>
    </resultMap>

    <!-- ================= 联表 resultMap ================= -->
    <resultMap id="materialWithWornMaterial" type="com.shzg.project.unique.domain.WornUniqueCodeMaterial">
        <id property="id" column="id"/>
        <result property="uniqueCodeId" column="unique_code_id"/>
        <result property="materialId" column="material_id"/>
        <result property="unitId" column="unit_id"/>
        <result property="quantity" column="quantity"/>
        <result property="remark" column="remark"/>
        <result property="isDelete" column="is_delete"/>

        <!-- 嵌套物料 -->
        <association property="wornMaterial" javaType="com.shzg.project.worn.domain.WornMaterial">
            <id property="id" column="wm_id"/>
            <result property="materialName" column="material_name"/>
            <result property="materialShortName" column="material_short_name"/>
            <result property="materialCode" column="material_code"/>
            <result property="typeId" column="type_id"/>
            <result property="unitId" column="unit_id"/>
            <result property="materialCategory" column="material_category"/>
            <result property="kgFactor" column="kg_factor"/>
            <result property="specification" column="specification"/>
            <result property="model" column="model"/>
            <result property="barcode" column="barcode"/>
            <result property="weight" column="weight"/>
            <result property="description" column="description"/>

            <!-- 扩展字段 -->
            <result property="typeName" column="type_name"/>
            <result property="typeAncestors" column="type_ancestors"/>
            <result property="typeParentNames" column="type_parent_names"/>
            <result property="unitName" column="unit_name"/>
        </association>
    </resultMap>

    <sql id="selectWornUniqueCodeMaterialVo">
        select id, unique_code_id, material_id, unit_id, quantity, create_by, create_time, update_by, update_time, remark, is_delete from worn_unique_code_material
    </sql>

    <select id="selectWornUniqueCodeMaterialList" parameterType="WornUniqueCodeMaterial" resultMap="WornUniqueCodeMaterialResult">
        <include refid="selectWornUniqueCodeMaterialVo"/>
        <where>
            <if test="uniqueCodeId != null"> and unique_code_id = #{uniqueCodeId}</if>
            <if test="materialId != null"> and material_id = #{materialId}</if>
            <if test="unitId != null"> and unit_id = #{unitId}</if>
            <if test="quantity != null"> and quantity = #{quantity}</if>
            <if test="isDelete != null and isDelete != ''"> and is_delete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectWornUniqueCodeMaterialById" parameterType="Long" resultMap="WornUniqueCodeMaterialResult">
        <include refid="selectWornUniqueCodeMaterialVo"/>
        where id = #{id}
    </select>

    <!-- ================= 核心：联表查询 ================= -->
    <select id="selectWithMaterialByUniqueCodeId"
            parameterType="java.lang.Long"
            resultMap="materialWithWornMaterial">

        SELECT
            m.*,

            wm.id AS wm_id,
            wm.material_name,
            wm.material_short_name,
            wm.material_code,
            wm.type_id,
            wm.unit_id,
            wm.material_category,
            wm.kg_factor,
            wm.specification,
            wm.model,
            wm.barcode,
            wm.weight,
            wm.description,

            wt.type_name,
            wt.ancestors AS type_ancestors,

            wu.unit_name,

            GROUP_CONCAT(wt_parent.type_name ORDER BY wt_parent.id SEPARATOR '/') AS type_parent_names

        FROM worn_unique_code_material m

                 LEFT JOIN worn_material wm ON m.material_id = wm.id
                 LEFT JOIN worn_material_type wt ON wm.type_id = wt.id
                 LEFT JOIN worn_material_type wt_parent ON FIND_IN_SET(wt_parent.id, wt.ancestors)
                 LEFT JOIN worn_material_unit wu ON wm.unit_id = wu.id

        WHERE m.unique_code_id = #{uniqueCodeId}

        GROUP BY m.id
    </select>

    <insert id="insertWornUniqueCodeMaterial" parameterType="WornUniqueCodeMaterial" useGeneratedKeys="true" keyProperty="id">
        insert into worn_unique_code_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="uniqueCodeId != null">unique_code_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="quantity != null">quantity,</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="remark != null">remark,</if>
            <if test="isDelete != null">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="uniqueCodeId != null">#{uniqueCodeId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="quantity != null">#{quantity},</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="remark != null">#{remark},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateWornUniqueCodeMaterial" parameterType="WornUniqueCodeMaterial">
        update worn_unique_code_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="unitId != null">unit_id = #{unitId},</if>
            <if test="quantity != null">quantity = #{quantity},</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="remark != null">remark = #{remark},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where unique_code_id = #{uniqueCodeId}
    </update>

    <delete id="deleteWornUniqueCodeMaterialById" parameterType="Long">
        delete from worn_unique_code_material where id = #{id}
    </delete>

    <delete id="deleteWornUniqueCodeMaterialByIds" parameterType="Long">
        delete from worn_unique_code_material where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <update id="updateDeleteByUniqueCodeId">
        UPDATE worn_unique_code_material
        SET is_delete = '1',
            update_time = NOW()
        WHERE unique_code_id = #{uniqueCodeId}
    </update>
</mapper>