<?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.jsh.erp.datasource.mappers.TaskStocktakingItemMapper">

    <select id="pdaList" parameterType="com.jsh.erp.datasource.pda.dto.PDATaskStocktakingItemDTO" resultType="com.jsh.erp.datasource.pda.vo.PDATaskStocktakingItemVO">
        SELECT
        tsi.id AS id,
        tsi.material_item_id AS material_item_id,
        me.position AS position,
        mc.`name` AS category_name,
        m.`name` AS material_name,
        m.standard AS standard,
        u.username AS create_name,
        tsi.oper_time AS oper_time,
        me.inventory AS inventory,
        me.commodity_unit AS commodity_unit,
        tsi.new_inventory AS new_inventory,
        tsi.new_position AS new_position,
        m.img_name AS img_name,
        tsi.status AS status
        FROM
        task_stocktaking_item tsi
        LEFT JOIN material_batch me ON tsi.material_item_id = me.id
        LEFT JOIN jsh_material m ON me.material_id = m.id
        LEFT JOIN jsh_material_category mc ON m.category_id = mc.id
        LEFT JOIN jsh_user u ON u.id = tsi.creator
        <where>
            tsi.delete_flag = 0
            AND tsi.task_stocktaking_id = #{taskId}
            <if test="number != null and number != ''">
                AND (m.name LIKE CONCAT('%',#{number},'%')
                OR me.batch_number LIKE CONCAT('%',#{number},'%')
                OR me.position LIKE CONCAT('%',#{number},'%')
                OR m.system_sku LIKE CONCAT('%',#{number},'%'))
            </if>
            <if test="userIdList != null and userIdList.size > 0">
                AND tsi.creator IN
                <foreach collection="userIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="categoryIdList != null and categoryIdList.size > 0">
                AND m.category_id IN
                <foreach collection="categoryIdList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="positionList != null and positionList.size > 0">
                AND
                <foreach collection="positionList" item="item" open="(" separator="OR" close=")">
                     me.position LIKE CONCAT('%',#{item},'%')
                </foreach>
            </if>
            <if test="position != null and position != ''">
                AND me.position LIKE CONCAT('%',#{position},'%')
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND tsi.status IN
                <foreach collection="statusList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        tsi.oper_time DESC,
        tsi.id DESC
    </select>

    <select id="listByTaskStocktakingId" parameterType="com.jsh.erp.datasource.dto.TaskStocktakingItemQueryDTO" resultType="com.jsh.erp.datasource.vo.TaskStocktakingItemVO">
        SELECT
            tsi.id AS id,
            mc.`name` AS category_name,
            m.`name` AS material_name,
            m.standard AS standard,
            m.model AS model,
            m.color AS color,
            m.brand AS brand,
            me.position AS position,
            me.batch_number AS batch_number,
            m.system_sku AS system_sku,
            me.sku AS sku,
            me.commodity_unit AS commodity_unit,
            me.production_date AS production_date,
            s.supplier AS supplier_name,
            me.bar_code AS bar_code,
            me.inventory AS inventory,
            me.expiry_num AS expiry_num,
            d.`name` AS depot_name,
            tsi.new_position AS new_position,
            tsi.new_inventory AS new_inventory,
            tsi.difference_count AS difference_count,
            tsi.difference_reason AS difference_reason
        FROM
            task_stocktaking_item tsi
                LEFT JOIN material_batch me ON tsi.material_item_id = me.id
                LEFT JOIN jsh_material m ON me.material_id = m.id
                LEFT JOIN jsh_material_category mc ON mc.id = m.category_id
                LEFT JOIN jsh_supplier s ON me.supplier_id = s.id
                LEFT JOIN jsh_depot d ON me.depot_id = d.id
        <where>
            tsi.delete_flag = 0
            AND tsi.task_stocktaking_id = #{taskStocktakingId}
            <if test="categoryId != null and categoryId != ''">
                AND m.category_id = #{categoryId}
            </if>
            <if test="materialName != null and materialName != ''">
                AND m.`name` LIKE CONCAT('%',#{materialName},'%')
            </if>
            <if test="batchNumber != null and batchNumber != ''">
                AND me.batch_number = #{batchNumber}
            </if>
            <if test="position != null and position != ''">
                AND me.position LIKE  CONCAT('%',#{position},'%')
            </if>
            <if test="isDifference != null and isDifference == '1'.toString()">
                AND tsi.new_inventory = me.inventory
            </if>
            <if test="isDifference != null and isDifference == '2'.toString()">
                AND tsi.new_inventory != 0
                AND tsi.new_inventory != me.inventory
            </if>
        </where>
        ORDER BY
            tsi.oper_time DESC,
            tsi.id DESC
    </select>

    <select id="creatorSpinnerList" resultType="com.jsh.erp.datasource.vo.SpinnerVO">
        SELECT
            tsi.creator AS value,
            u.username AS label
        FROM
            task_stocktaking_item tsi
            LEFT JOIN jsh_user u ON tsi.creator = u.id
        WHERE
            tsi.delete_flag = 0
            AND tsi.creator IS NOT NULL
            AND tsi.task_stocktaking_id = #{taskId}
        GROUP BY
            tsi.creator
    </select>

</mapper>