<?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.zg.project.inventory.Task.mapper.InventoryTaskMapper">

    <!-- ========== 映射对象 ========== -->
    <resultMap type="InventoryTask" id="InventoryTaskResult">
        <result property="id" column="id"/>
        <result property="taskName" column="task_name"/>
        <result property="warehouseCode" column="warehouse_code"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="sceneId" column="scene_id"/>
        <result property="sceneName" column="scene_name"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="requireTime" column="require_time"/>
        <result property="status" column="status"/>
        <result property="taskType" column="task_type"/>
        <result property="remark" column="remark"/>
        <result property="createdBy" column="created_by"/>
        <result property="createdAt" column="created_at"/>
        <result property="updatedBy" column="updated_by"/>
        <result property="updatedAt" column="updated_at"/>
        <result property="isDelete" column="is_delete"/>
    </resultMap>

    <!-- ========== 公共 SELECT 语句 ========== -->
    <sql id="selectInventoryTaskVo">
        SELECT
            t.id,
            t.task_name,
            t.warehouse_code,
            w.warehouse_name AS warehouse_name,
            t.scene_id,
            s.scene_name AS scene_name,
            t.user_id,
            u.nick_name AS user_name,
            t.require_time,
            t.status,
            t.task_type,
            t.remark,
            t.created_by,
            t.created_at,
            t.updated_by,
            t.updated_at,
            t.is_delete
        FROM inventory_task t
                 LEFT JOIN warehouse_info w ON t.warehouse_code = w.warehouse_code
                 LEFT JOIN scene_mapping s ON t.scene_id = s.scene_code
                 LEFT JOIN sys_user u ON t.user_id = u.user_id
    </sql>

    <!-- ========== 列表查询 ========== -->
    <select id="selectInventoryTaskList" parameterType="InventoryTask" resultMap="InventoryTaskResult">
        <include refid="selectInventoryTaskVo"/>
        <where>
            <if test="taskName != null and taskName != ''">
                AND t.task_name LIKE concat('%', #{taskName}, '%')
            </if>
            <if test="warehouseCode != null and warehouseCode != ''">
                AND t.warehouse_code = #{warehouseCode}
            </if>
            <if test="sceneId != null and sceneId != ''">
                AND t.scene_id = #{sceneId}
            </if>
            <if test="userId != null">
                AND t.user_id = #{userId}
            </if>
            <if test="requireTime != null and requireTime != ''">
                AND t.require_time = #{requireTime}
            </if>
            <if test="status != null and status != ''">
                AND t.status = #{status}
            </if>
            <if test="taskType != null and taskType != ''">
                AND t.task_type = #{taskType}
            </if>
            <if test="createdBy != null and createdBy != ''">
                AND t.created_by = #{createdBy}
            </if>
            <if test="createdAt != null and createdAt != ''">
                AND t.created_at = #{createdAt}
            </if>
            <if test="updatedBy != null and updatedBy != ''">
                AND t.updated_by = #{updatedBy}
            </if>
            <if test="updatedAt != null and updatedAt != ''">
                AND t.updated_at = #{updatedAt}
            </if>
            <if test="isDelete != null and isDelete != ''">
                AND t.is_delete = #{isDelete}
            </if>
        </where>
        ORDER BY t.created_at DESC
    </select>

    <!-- ========== 根据 ID 查询 ========== -->
    <select id="selectInventoryTaskById" parameterType="Long" resultMap="InventoryTaskResult">
        <include refid="selectInventoryTaskVo"/>
        WHERE t.id = #{id}
    </select>

    <!-- ========== 查询用户未完成任务数量 ========== -->
    <select id="countPendingTaskByUserId" resultType="java.lang.Integer" parameterType="java.lang.Long">
        SELECT COUNT(*)
        FROM inventory_task
        WHERE user_id = #{userId}
          AND status = '0'
          AND is_delete = '0'
    </select>

    <!-- ========== 根据任务 ID 获取仓库（小仓编码） ========== -->
    <select id="getWhByTaskId" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT warehouse_code
        FROM inventory_task
        WHERE id = #{taskId}
    </select>

    <!-- ========== 根据任务 ID 获取场景编码 ========== -->
    <select id="getSceneByTaskId" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT scene_id
        FROM inventory_task
        WHERE id = #{taskId}
    </select>

    <select id="selectSceneIdById" parameterType="string" resultType="java.lang.String">
        SELECT scene_id
        FROM inventory_task
        WHERE id = #{id}
          AND is_delete = '0'
            LIMIT 1
    </select>

    <!-- ========== 新增 ========== -->
    <insert id="insertInventoryTask" parameterType="InventoryTask" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO inventory_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">task_name,</if>
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code,</if>
            <if test="sceneId != null and sceneId != ''">scene_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="requireTime != null and requireTime != ''">require_time,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="taskType != null and taskType != ''">task_type,</if>
            <if test="remark != null">remark,</if>
            <if test="createdBy != null and createdBy != ''">created_by,</if>
            <if test="createdAt != null and createdAt != ''">created_at,</if>
            <if test="updatedBy != null and updatedBy != ''">updated_by,</if>
            <if test="updatedAt != null and updatedAt != ''">updated_at,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
        </trim>
        <trim prefix="VALUES (" suffix=")" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">#{taskName},</if>
            <if test="warehouseCode != null and warehouseCode != ''">#{warehouseCode},</if>
            <if test="sceneId != null and sceneId != ''">#{sceneId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="requireTime != null and requireTime != ''">#{requireTime},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="taskType != null and taskType != ''">#{taskType},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createdBy != null and createdBy != ''">#{createdBy},</if>
            <if test="createdAt != null and createdAt != ''">#{createdAt},</if>
            <if test="updatedBy != null and updatedBy != ''">#{updatedBy},</if>
            <if test="updatedAt != null and updatedAt != ''">#{updatedAt},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
        </trim>
    </insert>

    <!-- ========== 修改 ========== -->
    <update id="updateInventoryTask" parameterType="InventoryTask">
        UPDATE inventory_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskName != null and taskName != ''">task_name = #{taskName},</if>
            <if test="warehouseCode != null and warehouseCode != ''">warehouse_code = #{warehouseCode},</if>
            <if test="sceneId != null and sceneId != ''">scene_id = #{sceneId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="requireTime != null and requireTime != ''">require_time = #{requireTime},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="taskType != null and taskType != ''">task_type = #{taskType},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createdBy != null and createdBy != ''">created_by = #{createdBy},</if>
            <if test="createdAt != null and createdAt != ''">created_at = #{createdAt},</if>
            <if test="updatedBy != null and updatedBy != ''">updated_by = #{updatedBy},</if>
            <if test="updatedAt != null and updatedAt != ''">updated_at = #{updatedAt},</if>
            <if test="isDelete != null and isDelete != ''">is_delete = #{isDelete},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <!-- ========== 更新状态 ========== -->
    <update id="updateStatus" parameterType="map">
        UPDATE inventory_task
        SET status = #{status},
            updated_at = NOW()
        WHERE id = #{taskId}
    </update>

    <!-- ========== 删除 ========== -->
    <delete id="deleteInventoryTaskById" parameterType="Long">
        DELETE FROM inventory_task WHERE id = #{id}
    </delete>

    <delete id="deleteInventoryTaskByIds" parameterType="String">
        DELETE FROM inventory_task WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
