<?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.ruoyi.asset.mapper.AmsInventoryTaskMapper">

    <resultMap type="AmsInventoryTask" id="AmsInventoryTaskResult">
        <result property="taskId" column="task_id" />
        <result property="taskNo" column="task_no" />
        <result property="scopeType" column="scope_type" />
        <result property="warehouseId" column="warehouse_id" />
        <result property="warehouseCode" column="warehouse_code" />
        <result property="warehouseName" column="warehouse_name" />
        <result property="locationId" column="location_id" />
        <result property="locationCode" column="location_code" />
        <result property="locationName" column="location_name" />
        <result property="categoryId" column="category_id" />
        <result property="categoryCode" column="category_code" />
        <result property="categoryName" column="category_name" />
        <result property="ownerUserId" column="owner_user_id" />
        <result property="ownerUserName" column="owner_user_name" />
        <result property="taskStatus" column="task_status" />
        <result property="issueTime" column="issue_time" />
        <result property="finishTime" column="finish_time" />
        <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="delFlag" column="del_flag" />
    </resultMap>

    <resultMap id="AmsInventoryTaskItemCollectionResult" type="AmsInventoryTask" extends="AmsInventoryTaskResult">
        <collection property="amsInventoryTaskItemList" ofType="AmsInventoryTaskItem"
                    column="task_id" select="selectAmsInventoryTaskItemList" />
        <collection property="amsInventoryTaskSurplusList" ofType="AmsInventoryTaskSurplus"
                    column="task_id" select="selectAmsInventoryTaskSurplusList" />
    </resultMap>

    <resultMap type="AmsInventoryTaskItem" id="AmsInventoryTaskItemResult">
        <result property="itemId" column="item_id" />
        <result property="taskId" column="task_id" />
        <result property="taskNo" column="task_no" />
        <result property="assetId" column="asset_id" />
        <result property="assetCode" column="asset_code" />
        <result property="assetName" column="asset_name" />
        <result property="categoryId" column="category_id" />
        <result property="categoryCode" column="category_code" />
        <result property="categoryName" column="category_name" />
        <result property="specModel" column="spec_model" />
        <result property="brand" column="brand" />
        <result property="bookWarehouseId" column="book_warehouse_id" />
        <result property="bookWarehouseName" column="book_warehouse_name" />
        <result property="bookLocationId" column="book_location_id" />
        <result property="bookLocationName" column="book_location_name" />
        <result property="inventoryWarehouseId" column="inventory_warehouse_id" />
        <result property="inventoryWarehouseName" column="inventory_warehouse_name" />
        <result property="inventoryLocationId" column="inventory_location_id" />
        <result property="inventoryLocationName" column="inventory_location_name" />
        <result property="inventoryResult" column="inventory_result" />
        <result property="inventoryUserId" column="inventory_user_id" />
        <result property="inventoryUserName" column="inventory_user_name" />
        <result property="inventoryTime" column="inventory_time" />
        <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="delFlag" column="del_flag" />
    </resultMap>

    <resultMap type="AmsInventoryTaskSurplus" id="AmsInventoryTaskSurplusResult">
        <result property="surplusId" column="surplus_id" />
        <result property="taskId" column="task_id" />
        <result property="taskNo" column="task_no" />
        <result property="surplusType" column="surplus_type" />
        <result property="identifyMethod" column="identify_method" />
        <result property="assetId" column="asset_id" />
        <result property="assetCode" column="asset_code" />
        <result property="assetName" column="asset_name" />
        <result property="categoryId" column="category_id" />
        <result property="categoryCode" column="category_code" />
        <result property="categoryName" column="category_name" />
        <result property="specModel" column="spec_model" />
        <result property="brand" column="brand" />
        <result property="tagId" column="tag_id" />
        <result property="tagCode" column="tag_code" />
        <result property="epcCode" column="epc_code" />
        <result property="bookAssetStatus" column="book_asset_status" />
        <result property="bookWarehouseId" column="book_warehouse_id" />
        <result property="bookWarehouseCode" column="book_warehouse_code" />
        <result property="bookWarehouseName" column="book_warehouse_name" />
        <result property="bookLocationId" column="book_location_id" />
        <result property="bookLocationCode" column="book_location_code" />
        <result property="bookLocationName" column="book_location_name" />
        <result property="inventoryWarehouseId" column="inventory_warehouse_id" />
        <result property="inventoryWarehouseCode" column="inventory_warehouse_code" />
        <result property="inventoryWarehouseName" column="inventory_warehouse_name" />
        <result property="inventoryLocationId" column="inventory_location_id" />
        <result property="inventoryLocationCode" column="inventory_location_code" />
        <result property="inventoryLocationName" column="inventory_location_name" />
        <result property="inventoryUserId" column="inventory_user_id" />
        <result property="inventoryUserName" column="inventory_user_name" />
        <result property="inventoryTime" column="inventory_time" />
        <result property="confirmStatus" column="confirm_status" />
        <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="delFlag" column="del_flag" />
    </resultMap>

    <sql id="selectAmsInventoryTaskVo">
        select task_id, task_no, scope_type, warehouse_id, warehouse_code, warehouse_name,
               location_id, location_code, location_name, category_id, category_code, category_name,
               owner_user_id, owner_user_name, task_status, issue_time, finish_time,
               create_by, create_time, update_by, update_time, remark, del_flag
        from ams_inventory_task
    </sql>

    <select id="selectAmsInventoryTaskList" parameterType="AmsInventoryTask" resultMap="AmsInventoryTaskResult">
        <include refid="selectAmsInventoryTaskVo"/>
        <where>
            del_flag = '0'
            <if test="taskNo != null and taskNo != ''">
                and task_no like concat(#{taskNo}, '%')
            </if>
            <if test="scopeType != null and scopeType != ''">
                and scope_type = #{scopeType}
            </if>
            <if test="warehouseId != null">
                and warehouse_id = #{warehouseId}
            </if>
            <if test="locationId != null">
                and location_id = #{locationId}
            </if>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </if>
            <if test="ownerUserId != null">
                and owner_user_id = #{ownerUserId}
            </if>
            <if test="ownerUserName != null and ownerUserName != ''">
                and owner_user_name like concat('%', #{ownerUserName}, '%')
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and task_status = #{taskStatus}
            </if>
            <if test="params.beginIssueTime != null and params.beginIssueTime != ''">
                and issue_time &gt;= #{params.beginIssueTime}
            </if>
            <if test="params.endIssueTime != null and params.endIssueTime != ''">
                and issue_time &lt;= #{params.endIssueTime}
            </if>
        </where>
        order by create_time desc, task_id desc
    </select>

    <select id="selectAmsInventoryTaskByTaskId" parameterType="Long" resultMap="AmsInventoryTaskItemCollectionResult">
        <include refid="selectAmsInventoryTaskVo"/>
        where task_id = #{taskId} and del_flag = '0'
    </select>

    <select id="selectAmsInventoryTaskByTaskIdForUpdate" parameterType="Long"
            resultMap="AmsInventoryTaskItemCollectionResult">
        <include refid="selectAmsInventoryTaskVo"/>
        where task_id = #{taskId} and del_flag = '0'
        for update
    </select>

    <select id="selectAmsInventoryTaskItemList" resultMap="AmsInventoryTaskItemResult">
        select item_id, task_id, task_no, asset_id, asset_code, asset_name, category_id, category_code,
               category_name, spec_model, brand, book_warehouse_id, book_warehouse_name,
               book_location_id, book_location_name, inventory_warehouse_id, inventory_warehouse_name,
               inventory_location_id, inventory_location_name, inventory_result, inventory_user_id,
               inventory_user_name, inventory_time, create_by, create_time, update_by, update_time,
               remark, del_flag
        from ams_inventory_task_item
        where task_id = #{task_id} and del_flag = '0'
        order by item_id
    </select>

    <select id="selectAmsInventoryTaskSurplusList" resultMap="AmsInventoryTaskSurplusResult">
        select surplus_id, task_id, task_no, surplus_type, identify_method, asset_id, asset_code, asset_name,
               category_id, category_code, category_name, spec_model, brand, tag_id, tag_code, epc_code,
               book_asset_status, book_warehouse_id, book_warehouse_code, book_warehouse_name,
               book_location_id, book_location_code, book_location_name, inventory_warehouse_id,
               inventory_warehouse_code, inventory_warehouse_name, inventory_location_id,
               inventory_location_code, inventory_location_name, inventory_user_id, inventory_user_name,
               inventory_time, confirm_status, create_by, create_time, update_by, update_time, remark, del_flag
        from ams_inventory_task_surplus
        where task_id = #{task_id} and del_flag = '0'
        order by surplus_id
    </select>

    <select id="selectAmsInventoryTaskSurplusBySurplusIdForUpdate" parameterType="Long"
            resultMap="AmsInventoryTaskSurplusResult">
        select surplus_id, task_id, task_no, surplus_type, identify_method, asset_id, asset_code, asset_name,
               category_id, category_code, category_name, spec_model, brand, tag_id, tag_code, epc_code,
               book_asset_status, book_warehouse_id, book_warehouse_code, book_warehouse_name,
               book_location_id, book_location_code, book_location_name, inventory_warehouse_id,
               inventory_warehouse_code, inventory_warehouse_name, inventory_location_id,
               inventory_location_code, inventory_location_name, inventory_user_id, inventory_user_name,
               inventory_time, confirm_status, create_by, create_time, update_by, update_time, remark, del_flag
        from ams_inventory_task_surplus
        where surplus_id = #{surplusId} and del_flag = '0'
        for update
    </select>

    <select id="countInventoryTaskItemByTaskIdAndAssetId" resultType="int">
        select count(1)
        from ams_inventory_task_item
        where task_id = #{taskId}
          and asset_id = #{assetId}
          and del_flag = '0'
    </select>

    <select id="countInventoryTaskSurplusByAssetId" resultType="int">
        select count(1)
        from ams_inventory_task_surplus
        where task_id = #{taskId}
          and asset_id = #{assetId}
          and del_flag = '0'
    </select>

    <select id="countInventoryTaskSurplusByEpcCode" resultType="int">
        select count(1)
        from ams_inventory_task_surplus
        where task_id = #{taskId}
          and epc_code = #{epcCode}
          and del_flag = '0'
    </select>

    <!-- 盘点下发时使用真实资产台账生成明细快照，不信任前端传入明细。 -->
    <select id="selectInventoryAssetSnapshotList" resultMap="AmsInventoryTaskItemResult">
        select asset.asset_id, asset.asset_code, asset.asset_name, asset.category_id, asset.category_code,
               asset.category_name, asset.spec_model, asset.brand, asset.warehouse_id as book_warehouse_id,
               asset.warehouse_name as book_warehouse_name, asset.location_id as book_location_id,
               asset.location_name as book_location_name
        from ams_asset asset
        where asset.del_flag = '0'
          and asset.asset_status in
        <foreach item="assetStatus" collection="assetStatuses" open="(" separator="," close=")">
            #{assetStatus}
        </foreach>
        <if test="task.warehouseId != null">
            and asset.warehouse_id = #{task.warehouseId}
        </if>
        <if test="task.locationId != null">
            and asset.location_id = #{task.locationId}
        </if>
        <if test="task.categoryId != null">
            and asset.category_id = #{task.categoryId}
        </if>
        order by asset.asset_id
    </select>

    <insert id="insertAmsInventoryTask" parameterType="AmsInventoryTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into ams_inventory_task (
            task_no, scope_type, warehouse_id, warehouse_code, warehouse_name,
            location_id, location_code, location_name, category_id, category_code, category_name,
            owner_user_id, owner_user_name, task_status, create_by, create_time, remark, del_flag
        ) values (
            #{taskNo}, #{scopeType}, #{warehouseId}, #{warehouseCode}, #{warehouseName},
            #{locationId}, #{locationCode}, #{locationName}, #{categoryId}, #{categoryCode}, #{categoryName},
            #{ownerUserId}, #{ownerUserName}, #{taskStatus}, #{createBy}, #{createTime}, #{remark}, #{delFlag}
        )
    </insert>

    <update id="updateAmsInventoryTask" parameterType="AmsInventoryTask">
        update ams_inventory_task
        set scope_type = #{scopeType},
            warehouse_id = #{warehouseId},
            warehouse_code = #{warehouseCode},
            warehouse_name = #{warehouseName},
            location_id = #{locationId},
            location_code = #{locationCode},
            location_name = #{locationName},
            category_id = #{categoryId},
            category_code = #{categoryCode},
            category_name = #{categoryName},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where task_id = #{taskId} and del_flag = '0' and task_status = 'DRAFT'
    </update>

    <update id="updateAmsInventoryTaskStatus" parameterType="AmsInventoryTask">
        update ams_inventory_task
        <trim prefix="set" suffixOverrides=",">
            <if test="taskStatus != null and taskStatus != ''">task_status = #{taskStatus},</if>
            <if test="issueTime != null">issue_time = #{issueTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where task_id = #{taskId} and del_flag = '0'
        <if test="params.expectedStatus != null and params.expectedStatus != ''">
            and task_status = #{params.expectedStatus}
        </if>
        <if test="params.expectedStatuses != null">
            and task_status in
            <foreach item="status" collection="params.expectedStatuses" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
        <if test="params.excludedStatuses != null">
            and task_status not in
            <foreach item="status" collection="params.excludedStatuses" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </update>

    <update id="updateAmsInventoryTaskItemResult" parameterType="AmsInventoryTaskItem">
        update ams_inventory_task_item
        set inventory_warehouse_id = #{inventoryWarehouseId},
            inventory_warehouse_name = #{inventoryWarehouseName},
            inventory_location_id = #{inventoryLocationId},
            inventory_location_name = #{inventoryLocationName},
            inventory_result = #{inventoryResult},
            inventory_user_id = #{inventoryUserId},
            inventory_user_name = #{inventoryUserName},
            inventory_time = #{inventoryTime},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where item_id = #{itemId}
          and task_id = #{taskId}
          and del_flag = '0'
    </update>

    <insert id="insertAmsInventoryTaskSurplus" parameterType="AmsInventoryTaskSurplus"
            useGeneratedKeys="true" keyProperty="surplusId">
        insert into ams_inventory_task_surplus (
            task_id, task_no, surplus_type, identify_method, asset_id, asset_code, asset_name,
            category_id, category_code, category_name, spec_model, brand, tag_id, tag_code, epc_code,
            book_asset_status, book_warehouse_id, book_warehouse_code, book_warehouse_name,
            book_location_id, book_location_code, book_location_name, inventory_warehouse_id,
            inventory_warehouse_code, inventory_warehouse_name, inventory_location_id,
            inventory_location_code, inventory_location_name, inventory_user_id, inventory_user_name,
            inventory_time, confirm_status, create_by, create_time, remark, del_flag
        ) values (
            #{taskId}, #{taskNo}, #{surplusType}, #{identifyMethod}, #{assetId}, #{assetCode}, #{assetName},
            #{categoryId}, #{categoryCode}, #{categoryName}, #{specModel}, #{brand}, #{tagId}, #{tagCode}, #{epcCode},
            #{bookAssetStatus}, #{bookWarehouseId}, #{bookWarehouseCode}, #{bookWarehouseName},
            #{bookLocationId}, #{bookLocationCode}, #{bookLocationName}, #{inventoryWarehouseId},
            #{inventoryWarehouseCode}, #{inventoryWarehouseName}, #{inventoryLocationId},
            #{inventoryLocationCode}, #{inventoryLocationName}, #{inventoryUserId}, #{inventoryUserName},
            #{inventoryTime}, #{confirmStatus}, #{createBy}, #{createTime}, #{remark}, #{delFlag}
        )
    </insert>

    <update id="confirmAmsInventoryTaskSurplusByTaskId">
        update ams_inventory_task_surplus
        set confirm_status = #{confirmStatus},
            update_by = #{updateBy},
            update_time = now()
        where task_id = #{taskId}
          and confirm_status = 'RECORDED'
          and del_flag = '0'
    </update>

    <update id="deleteAmsInventoryTaskSurplusBySurplusId">
        update ams_inventory_task_surplus
        set del_flag = '1',
            update_by = #{updateBy},
            update_time = now()
        where surplus_id = #{surplusId}
          and del_flag = '0'
          and confirm_status = 'RECORDED'
    </update>

    <update id="deleteAmsInventoryTaskByTaskId" parameterType="Long">
        update ams_inventory_task
        set del_flag = '1'
        where task_id = #{taskId} and del_flag = '0' and task_status = 'DRAFT'
    </update>

    <update id="deleteAmsInventoryTaskByTaskIds" parameterType="String">
        update ams_inventory_task
        set del_flag = '1'
        where task_id in
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
        and del_flag = '0' and task_status = 'DRAFT'
    </update>

    <update id="deleteAmsInventoryTaskItemByTaskIds" parameterType="String">
        update ams_inventory_task_item
        set del_flag = '1'
        where task_id in
        <foreach item="taskId" collection="array" open="(" separator="," close=")">
            #{taskId}
        </foreach>
        and del_flag = '0'
    </update>

    <update id="deleteAmsInventoryTaskItemByTaskId" parameterType="Long">
        update ams_inventory_task_item
        set del_flag = '1'
        where task_id = #{taskId} and del_flag = '0'
    </update>

    <update id="deleteAmsInventoryTaskSurplusByTaskId" parameterType="Long">
        update ams_inventory_task_surplus
        set del_flag = '1'
        where task_id = #{taskId} and del_flag = '0'
    </update>

    <insert id="batchAmsInventoryTaskItem">
        insert into ams_inventory_task_item (
            task_id, task_no, asset_id, asset_code, asset_name, category_id, category_code,
            category_name, spec_model, brand, book_warehouse_id, book_warehouse_name,
            book_location_id, book_location_name, create_by, create_time, remark, del_flag
        ) values
        <foreach item="item" collection="list" separator=",">
            (
                #{item.taskId}, #{item.taskNo}, #{item.assetId}, #{item.assetCode}, #{item.assetName},
                #{item.categoryId}, #{item.categoryCode}, #{item.categoryName}, #{item.specModel}, #{item.brand},
                #{item.bookWarehouseId}, #{item.bookWarehouseName}, #{item.bookLocationId},
                #{item.bookLocationName}, #{item.createBy}, #{item.createTime}, #{item.remark}, #{item.delFlag}
            )
        </foreach>
    </insert>
</mapper>
