<?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_name,device_id, pcde, tme, scan_type, status, created_at)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.taskName},#{item.deviceId}, #{item.pcde}, #{item.tme}, #{item.scanType}, #{item.status}, NOW())
        </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="getByTaskName" resultType="com.zg.project.Inventory.domain.vo.InventoryMatchScanVO">-->
<!--        SELECT-->
<!--        i.id,-->
<!--        i.device_id AS deviceId,-->
<!--        i.task_name AS taskName,-->
<!--        s.pcde,-->
<!--        i.tme,-->
<!--        i.scan_type AS scanType,-->
<!--        i.status,-->
<!--        i.created_at AS createdAt,-->
<!--        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-->
<!--        JOIN-->
<!--        sod s ON i.pcde = s.fycde_1-->
<!--        WHERE-->
<!--        i.pcde IS NOT NULL-->
<!--        AND s.rmn > 0-->
<!--        <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>-->
<!--    </select>-->

<!--    <select id="getByTaskName" resultType="com.zg.project.Inventory.domain.vo.InventoryMatchScanVO">-->
<!--        SELECT-->
<!--        id,-->
<!--        device_id AS deviceId,-->
<!--        task_name AS taskName,-->
<!--        pcde,-->
<!--        tme,-->
<!--        scan_type AS scanType,-->
<!--        status,-->
<!--        created_at AS createdAt-->
<!--        FROM-->
<!--        inventory_match_scan-->
<!--        WHERE-->
<!--        task_name LIKE CONCAT('%', #{taskName}, '%')-->
<!--        AND pcde IS NOT NULL-->
<!--        <if test="status != null">-->
<!--            AND status = #{status}-->
<!--        </if>-->
<!--    </select>-->
    <select id="getGroupedHistoryList" resultType="com.zg.project.Inventory.domain.vo.InventoryMatchScanSimpleVO">
        SELECT
        task_name AS taskName,
        MAX(created_at) AS createdAt,
        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(created_at) <![CDATA[ >= ]]> #{dto.tme[0]}
                AND DATE(created_at) <![CDATA[ <= ]]> #{dto.tme[1]}
            </if>
            <if test="dto.scanType != null">
                AND scan_type = #{dto.scanType}
            </if>
            <if test="dto.createdAt != null and dto.createdAt != ''">
                AND created_at = #{dto.createdAt}
            </if>
            <if test="dto.status != null">
                AND status = #{dto.status}
            </if>
        </where>
        GROUP BY task_name
        ORDER BY MAX(created_at) 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(created_at) <![CDATA[ >= ]]> #{dto.tme[0]}
                AND DATE(created_at) <![CDATA[ <= ]]> #{dto.tme[1]}
            </if>
            <if test="dto.scanType != null">
                AND scan_type = #{dto.scanType}
            </if>
            <if test="dto.createdAt != null and dto.createdAt != ''">
                AND created_at = #{dto.createdAt}
            </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,
        i.created_at AS createdAt,
        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>

</mapper>
