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

    <resultMap type="WornUniqueCode" id="WornUniqueCodeResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="billNo"    column="bill_no"    />
        <result property="projectId"    column="project_id"    />
        <result property="status"    column="status"    />
        <result property="rfidCode"    column="rfid_code"    />
        <result property="printCount"    column="print_count"    />
        <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 id="WornInboundItemDTOResult" type="com.shzg.project.worn.domain.dto.WornInboundItemDTO">
        <result property="id" column="id"/>
        <result property="materialId" column="materialId"/>
        <result property="materialName" column="materialName"/>
        <result property="quantity" column="quantity"/>
        <result property="remark" column="remark"/>
        <result property="uniqueCode" column="uniqueCode"/>
        <result property="isDelete" column="isDelete"/>
        <result property="status"    column="status"    />
        <result property="materialCode" column="materialCode"/>
        <result property="materialShortName" column="materialShortName"/>
        <result property="specification" column="specification"/>
        <result property="model" column="model"/>
        <result property="unitId" column="unitId"/>
        <result property="unitName" column="unitName"/>
        <result property="typeName" column="typeName"/>
        <result property="typeParentNames" column="typeParentNames"/>
    </resultMap>
    <sql id="selectWornUniqueCodeVo">
        select id, code, bill_no, project_id, status, rfid_code, print_count, create_by, create_time, update_by, update_time, remark, is_delete
        from worn_unique_code
    </sql>

    <select id="selectWornUniqueCodeList" parameterType="WornUniqueCode" resultMap="WornUniqueCodeResult">
        SELECT t.*
        FROM worn_unique_code t
        LEFT JOIN sys_dept d ON t.project_id = d.dept_id
        <where>
            AND (t.is_delete = '0' OR t.is_delete IS NULL)

            <if test="dataScopeDeptIds != null and dataScopeDeptIds.size() > 0">
                AND t.project_id IN
                <foreach collection="dataScopeDeptIds" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>

            <if test="code != null and code != ''">
                AND t.code = #{code}
            </if>

            <if test="billNo != null and billNo != ''">
                AND t.bill_no = #{billNo}
            </if>

            <if test="status != null">
                AND t.status = #{status}
            </if>

            <if test="rfidCode != null and rfidCode != ''">
                AND t.rfid_code = #{rfidCode}
            </if>
        </where>
        order by t.create_time desc, t.id desc
    </select>

    <select id="selectWornUniqueCodeById" parameterType="Long" resultMap="WornUniqueCodeResult">
        <include refid="selectWornUniqueCodeVo"/>
        where id = #{id}
        AND (is_delete = '0' OR is_delete IS NULL)
    </select>

    <select id="selectIdByCode" resultType="java.lang.Long">
        select id
        from worn_unique_code
        where code = #{code}
          AND (is_delete = '0' OR is_delete IS NULL)
    </select>

    <insert id="insertWornUniqueCode" parameterType="WornUniqueCode" useGeneratedKeys="true" keyProperty="id">
        insert into worn_unique_code
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="billNo != null">bill_no,</if>
            <if test="projectId != null">project_id,</if>
            <if test="status != null">status,</if>
            <if test="rfidCode != null">rfid_code,</if>
            <if test="printCount != null">print_count,</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="code != null">#{code},</if>
            <if test="billNo != null">#{billNo},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="status != null">#{status},</if>
            <if test="rfidCode != null">#{rfidCode},</if>
            <if test="printCount != null">#{printCount},</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="updateWornUniqueCode" parameterType="WornUniqueCode">
        update worn_unique_code
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="billNo != null">bill_no = #{billNo},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="rfidCode != null">rfid_code = #{rfidCode},</if>
            <if test="printCount != null">print_count = #{printCount},</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 id = #{id}
    </update>

    <update id="updateByCode" parameterType="WornUniqueCode">
        update worn_unique_code
        set status = #{status},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where code = #{code}
          AND (is_delete = '0' OR is_delete IS NULL)
    </update>

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

    <select id="selectByCode" resultType="com.shzg.project.unique.domain.WornUniqueCode">
        SELECT
            id,
            code,
            bill_no,
            project_id,
            status,
            rfid_code,
            print_count,
            create_by,
            create_time,
            update_by,
            update_time,
            remark,
            is_delete
        FROM worn_unique_code
        WHERE code = #{code}
          AND (is_delete = '0' OR is_delete IS NULL)
            LIMIT 1
    </select>
    <select id="selectMaterialInfoByCode" resultMap="WornInboundItemDTOResult">
        SELECT
            ucm.id AS id,
            ucm.material_id AS materialId,
            ucm.quantity AS quantity,
            ucm.remark AS remark,
            uc.code AS uniqueCode,
            COALESCE(ucm.is_delete, '0') AS isDelete,
            m.unit_id AS unitId,
            mu.unit_name AS unitName,
            m.material_name AS materialName,
            m.material_code AS materialCode,
            m.material_short_name AS materialShortName,
            m.specification AS specification,
            m.model AS model,
            uc.status,
            mt.type_name AS typeName,
            (
                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 typeParentNames

        FROM worn_unique_code uc
                 LEFT JOIN worn_unique_code_material ucm
                           ON uc.id = ucm.unique_code_id
                 LEFT JOIN worn_material m
                           ON ucm.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 uc.code = #{code}
          AND (uc.is_delete = '0' OR uc.is_delete IS NULL)
          AND (ucm.id IS NULL OR ucm.is_delete = '0')
          AND (m.is_delete = '0' OR m.is_delete IS NULL)

        ORDER BY ucm.id ASC
    </select>

    <update id="updateInboundStatusByCode"
            parameterType="com.shzg.project.unique.domain.WornUniqueCode">
        UPDATE worn_unique_code
        SET status = #{status},
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE code = #{code}
          AND status = '1'
          AND (is_delete = '0' OR is_delete IS NULL)
    </update>
    <update id="updateVoidStatusByCode">
        update worn_unique_code
        set status = '9',
            update_by = #{updateBy},
            update_time = #{updateTime}
        where code = #{code}
          and (is_delete = '0' or is_delete is null)
    </update>
</mapper>