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

    <resultMap id="SodResultMap" type="com.zg.project.Inventory.domain.entity.Sod">
        <id     column="Id"         property="Id" />
        <result column="mid"        property="mid"/>
        <result column="des_mat"    property="desMat"/>
        <result column="rmn"        property="rmn"/>
        <result column="unt"        property="unt"/>
        <result column="pcde"       property="pcde"/>
        <result column="erpid"      property="erpid"/>
        <result column="des_pro"    property="desPro"/>
        <result column="bnum"       property="bnum"/>
        <result column="tcde"       property="tcde"/>
        <result column="mrk"        property="mrk"/>
        <result column="rdid"       property="rdid"/>
        <result column="prc"        property="prc"/>
        <result column="tme"        property="tme"/>
        <result column="dmrk"       property="dmrk"/>
        <result column="wtpe"       property="wtpe"/>
        <result column="up"         property="up"/>
        <result column="wh"         property="wh"/>
        <result column="gys"        property="gys"/>
        <result column="hcde"       property="hcde"/>
        <result column="del"        property="del"/>
        <result column="drsn"       property="drsn"/>
        <result column="amt"        property="amt"/>
        <result column="sid"        property="sid"/>
        <result column="intme"      property="intme"/>
        <result column="ste"        property="ste"/>
        <result column="vlid"       property="vlid"/>
        <result column="oprf"       property="oprf"/>
        <result column="itme"       property="itme"/>
        <result column="trc"        property="trc"/>
        <result column="lck"        property="lck"/>
        <result column="lckrsn"     property="lckrsn"/>
        <result column="mtme"       property="mtme"/>
        <result column="bzqz"       property="bzqz"/>
        <result column="fycde_1"    property="fycde1"/>
        <result column="fycde_2"    property="fycde2"/>
        <result column="uptim"      property="uptim"/>
    </resultMap>

    <select id="selectByPcdeList" resultType="com.zg.project.Inventory.domain.vo.SodVO">
        SELECT
        pcde,
        des_mat AS desMat,
        rmn,
        unt,
        mid,
        des_pro AS desPro,
        bnum,
        gys
        FROM sod
        WHERE rmn != 0
        <if test="query.tme != null and query.tme != ''">
            AND DATE_FORMAT(tme, '%Y-%m-%d') = #{query.tme}
        </if>
    </select>

    <select id="getAll" resultMap="SodResultMap">
        SELECT * FROM sod WHERE rmn != 0
    </select>

    <select id="getByFycde1List" resultMap="SodResultMap">
        SELECT *
        FROM sod
        WHERE fycde_1 IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND rmn != 0
    </select>

    <select id="getMissedFycde1s" resultMap="SodResultMap">
        SELECT * FROM sod
        WHERE fycde_1 NOT IN
        <foreach item="id" collection="list" open="(" separator="," close=")">#{id}</foreach>
        AND rmn != 0
    </select>

    <select id="existsByFycde1" resultType="boolean">
        SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END
        FROM sod
        WHERE fycde_1 = #{fycde1}
        AND rmn != 0
    </select>

    <select id="selectPcdeCntByFycde1" resultType="com.zg.project.Inventory.domain.vo.PcdeCntVO">
        SELECT pcde,
        COUNT(*) AS cnt
        FROM   sod
        WHERE  fycde_1 IN
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        GROUP BY pcde
    </select>


</mapper>