<?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.AutoInventory.mapper.InventoryMatchScanMapper">

    <insert id="insertBatch">
        INSERT INTO inventory_match_scan (task_id, device_id, pcode, tme, scan_type, status)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.taskId}, #{item.deviceId}, #{item.pcode}, #{item.tme}, #{item.scanType}, #{item.status})
        </foreach>
    </insert>

    <select id="getAll" resultType="com.zg.project.inventory.domain.entity.InventoryMatchScan">
        SELECT * FROM inventory_match_scan
    </select>

    <select id="getAllSod" resultType="com.zg.project.inventory.domain.entity.Sod">
        SELECT * FROM inventory_match_scan i
                          LEFT JOIN sod s ON i.pcde = s.fycde_1
        WHERE s.rmn > 0
    </select>

    <select id="getPcdeCountByTask" resultType="com.zg.project.inventory.domain.vo.PcdeCountVO">
        SELECT
        s.pcde,
        COUNT(*) AS count
        FROM
        (
        SELECT pcde
        FROM inventory_match_scan
        WHERE pcde IS NOT NULL
        <if test="taskName != null and taskName != ''">
            AND task_name LIKE CONCAT('%', #{taskName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        ) i
        JOIN sod s ON i.pcde = s.fycde_1
        WHERE s.rmn > 0
        GROUP BY s.pcde
    </select>

    <select id="getGroupedHistoryList" resultType="com.zg.project.inventory.domain.vo.InventoryMatchScanSimpleVO">
        SELECT
        task_name AS taskName,
        MAX(tme) AS tme,
        MAX(scan_type) AS scanType
        FROM inventory_match_scan
        <where>
            <if test="dto.deviceId != null and dto.deviceId != ''">
                AND device_id = #{dto.deviceId}
            </if>
            <if test="dto.taskName != null and dto.taskName != ''">
                AND task_name LIKE CONCAT('%', #{dto.taskName}, '%')
            </if>
            <if test="dto.pcde != null and dto.pcde != ''">
                AND pcde = #{dto.pcde}
            </if>
            <if test="dto.tme != null and dto.tme.size() == 2">
                AND DATE(tme) <![CDATA[ >= ]]> #{dto.tme[0]}
                AND DATE(tme) <![CDATA[ <= ]]> #{dto.tme[1]}
            </if>
            <if test="dto.scanType != null">
                AND scan_type = #{dto.scanType}
            </if>
            <if test="dto.status != null">
                AND status = #{dto.status}
            </if>
        </where>
        GROUP BY task_name
        ORDER BY MAX(tme) DESC
        LIMIT #{limit} OFFSET #{offset}
    </select>

    <select id="countGroupedHistory" resultType="int">
        SELECT COUNT(DISTINCT task_name)
        FROM inventory_match_scan
        <where>
            <if test="dto.deviceId != null and dto.deviceId != ''">
                AND device_id = #{dto.deviceId}
            </if>
            <if test="dto.taskName != null and dto.taskName != ''">
                AND task_name LIKE CONCAT('%', #{dto.taskName}, '%')
            </if>
            <if test="dto.pcde != null and dto.pcde != ''">
                AND pcde = #{dto.pcde}
            </if>
            <if test="dto.tme != null and dto.tme.size() == 2">
                AND DATE(tme) <![CDATA[ >= ]]> #{dto.tme[0]}
                AND DATE(tme) <![CDATA[ <= ]]> #{dto.tme[1]}
            </if>
            <if test="dto.scanType != null">
                AND scan_type = #{dto.scanType}
            </if>
            <if test="dto.status != null">
                AND status = #{dto.status}
            </if>
        </where>
    </select>

    <select id="getByTaskName" resultType="com.zg.project.inventory.domain.vo.InventoryMatchScanVO">
        SELECT
        i.id,
        i.device_id AS deviceId,
        i.task_name AS taskName,
        s.pcde as fycde_1,
        i.pcde,
        i.tme,
        i.scan_type AS scanType,
        i.status,
        s.mid,
        s.des_mat AS desMat,
        s.rmn,
        s.unt,
        s.des_pro AS desPro,
        s.bnum,
        s.tcde
        FROM inventory_match_scan i
        LEFT JOIN sod s ON i.pcde = s.fycde_1
        WHERE i.pcde IS NOT NULL
        <if test="taskName != null and taskName != ''">
            AND i.task_name LIKE CONCAT('%', #{taskName}, '%')
        </if>
        <if test="status != null and status != ''">
            AND i.status = #{status}
        </if>
        <if test="status != 2">
            AND s.rmn > 0
        </if>
    </select>

    <select id="selectInventoryMatchScanList"
            resultType="com.zg.project.inventory.domain.entity.InventoryMatchScan"
            parameterType="com.zg.project.inventory.domain.entity.InventoryMatchScan">
        SELECT
        i.id AS id,
        i.task_id AS taskId,
        i.device_id AS deviceId,
        i.pcode AS pcode,
        i.tme AS tme,
        i.scan_type AS scanType,
        i.status AS status,
        r.pcode AS rkPcode,
        t.task_name AS taskName
        FROM inventory_match_scan i
        LEFT JOIN rk_info r ON i.pcode = r.pcode_id
        LEFT JOIN inventory_task t ON i.task_id = t.id
        <where>
            <if test="taskId != null and taskId != ''">
                AND i.task_id = #{taskId}
            </if>
            <if test="deviceId != null and deviceId != ''">
                AND i.device_id = #{deviceId}
            </if>
            <if test="pcode != null and pcode != ''">
                AND i.pcode LIKE CONCAT('%', #{pcode}, '%')
            </if>
            <if test="status != null and status != ''">
                AND i.status = #{status}
            </if>
            <if test="scanType != null">
                AND i.scan_type = #{scanType}
            </if>
            <if test="tme != null">
                AND i.tme >= #{tme}
            </if>
        </where>
        ORDER BY i.tme DESC
    </select>

    <select id="selectOnlyFromMatchScan"
            parameterType="InventoryMatchScan"
            resultType="com.zg.project.inventory.domain.vo.RkInfoMatchVO">
        SELECT
        i.pcode AS rkPcode,
        NULL AS realQty
        FROM inventory_match_scan i
        <where>
            <if test="taskId != null and taskId != ''">
                AND i.task_id = #{taskId}
            </if>
            <if test="deviceId != null and deviceId != ''">
                AND i.device_id = #{deviceId}
            </if>
            <if test="status != null and status != ''">
                AND i.status = #{status}
            </if>
        </where>
        ORDER BY i.tme DESC
    </select>

    <select id="selectJoinRkInfo"
            parameterType="InventoryMatchScan"
            resultType="com.zg.project.inventory.domain.vo.RkInfoMatchVO">
        SELECT
        r.pcode                      AS rkPcode,
        COALESCE(SUM(r.real_qty), 0) AS realQty
        FROM rk_info r
        WHERE r.pcode IN (
        SELECT DISTINCT i.pcode
        FROM inventory_match_scan i
        <where>
            <if test="taskId != null and taskId != ''">AND i.task_id = #{taskId}</if>
            <if test="deviceId != null and deviceId != ''">AND i.device_id = #{deviceId}</if>
            <if test="status != null and status != ''">AND i.status = #{status}</if>
        </where>
        )
        GROUP BY r.pcode
        ORDER BY MAX(r.create_time) DESC
    </select>

</mapper>
