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

    <select id="pdaList" parameterType="com.jsh.erp.datasource.pda.dto.PDATaskStocktakingDTO" resultType="com.jsh.erp.datasource.pda.vo.PDATaskStocktakingVO">
        SELECT
            ts.id AS id,
            ts.task_name AS task_name,
            ts.number AS number,
            d.`name` AS depot_name,
            ju.username AS create_name,
            u.username AS creator_name,
            ts.category_count AS category_count,
            ts.material_count AS material_count,
            ts.position_range AS position_range,
            ts.create_time AS create_time,
            ts.oper_time AS oper_time,
            ou.username AS oper_name,
            ts.task_status AS task_status
        FROM
            task_stocktaking ts
                LEFT JOIN jsh_depot d ON ts.depot_id = d.id
                LEFT JOIN jsh_user ju ON ts.create_by = ju.id
                LEFT JOIN jsh_user ou ON ts.oper_by = ou.id
                LEFT JOIN jsh_user u ON ts.creator = u.id
        <where>
            ts.delete_flag = 0
            <if test="number != null and number != ''">
                AND (ts.task_name LIKE CONCAT('%',#{number},'%') OR ts.number LIKE CONCAT('%',#{number},'%'))
            </if>
            <if test="taskStatus != null and taskStatus != 0">
                AND ts.task_status = #{taskStatus}
            </if>
            <if test="depotId != null and depotId != 0">
                AND ts.depot_id = #{depotId}
            </if>
            ORDER BY
                ts.create_time DESC,
                ts.id DESC
        </where>
    </select>

    <select id="listBy" parameterType="com.jsh.erp.datasource.dto.TaskStocktakingQueryDTO" resultType="com.jsh.erp.datasource.vo.TaskStocktakingVO">
        SELECT
            ts.id AS id,
            ts.number AS number,
            ts.task_name AS task_name,
            ts.task_type AS task_type,
            d.`name` AS depot_name,
            ts.position_range AS position_range,
            ju.username AS create_by_name,
            ts.create_time AS create_time,
            u.username AS creator_name,
            ts.task_status AS task_status,
            ts.oper_time AS oper_time,
            count(tsi.id) AS item_count,
            count(tsi.id) AS material_count
        FROM
            task_stocktaking ts
                LEFT JOIN jsh_depot d ON ts.depot_id = d.id
                LEFT JOIN jsh_user ju ON ts.create_by = ju.id
                LEFT JOIN jsh_user ou ON ts.oper_by = ou.id
                LEFT JOIN jsh_user u ON ts.creator = u.id
                LEFT JOIN task_stocktaking_item tsi ON ts.id = tsi.task_stocktaking_id
        <where>
            ts.delete_flag = 0 AND tsi.delete_flag = 0
            <if test="number != null and number != ''">
                AND (ts.number LIKE CONCAT('%',#{number},'%') OR ts.task_name LIKE CONCAT('%',#{number},'%'))
            </if>
            <if test="taskStatus != null and taskStatus != 0">
                AND ts.task_status = #{taskStatus}
            </if>
            <if test="depotId != null and depotId !=''">
                AND ts.depot_id = #{depotId}
            </if>
            <if test="createBy != null and createBy !=''">
                AND ts.create_by = #{createBy}
            </if>
        GROUP BY
            ts.id
        ORDER BY
            ts.create_time DESC,
            ts.id DESC
        </where>
    </select>

</mapper>