<?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.shinsoft.sto.mapper.TransRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.shinsoft.sto.model.TransRecord">
        <result column="ID" property="id" />
        <result column="PROCESS_INSTANCE_ID" property="processInstanceId" />
        <result column="PROCESS_INSTANCE_STATUS" property="processInstanceStatus" />
        <result column="ADD_ID" property="addId" />
        <result column="ADD_CODE" property="addCode" />
        <result column="ADD_NAME" property="addName" />
        <result column="ADD_DEPT_ID" property="addDeptId" />
        <result column="ADD_DEPT_CODE" property="addDeptCode" />
        <result column="ADD_DEPT_NAME" property="addDeptName" />
        <result column="ADD_TIME" property="addTime" />
        <result column="MODIFY_ID" property="modifyId" />
        <result column="MODIFY_CODE" property="modifyCode" />
        <result column="MODIFY_NAME" property="modifyName" />
        <result column="MODIFY_TIME" property="modifyTime" />
        <result column="CANCEL_ID" property="cancelId" />
        <result column="CANCEL_CODE" property="cancelCode" />
        <result column="CANCEL_NAME" property="cancelName" />
        <result column="CANCEL_TIME" property="cancelTime" />
        <result column="CANCEL_FLAG" property="cancelFlag" />
        <result column="BELONG_ID" property="belongId" />
        <result column="COMP_CD" property="compCd" />
        <result column="DEPT_CD" property="deptCd" />
        <result column="ITEM_CD" property="itemCd" />
        <result column="ITEM_NM" property="itemNm" />
        <result column="ITEM_TYPE" property="itemType" />
        <result column="TRANSACTION_NO" property="transactionNo" />
        <result column="QTY" property="qty" />
        <result column="UOM" property="uom" />
        <result column="STORE_CD" property="storeCd" />
        <result column="AREA_CD" property="areaCd" />
        <result column="STACK_CD" property="stackCd" />
        <result column="TRANSACTION_TYPE" property="transactionType" />
        <result column="QTY_WET" property="qtyWet" />
        <result column="TRANSACTION_DATE" property="transactionDate" />
        <result column="TRANSACTION_TABLE" property="transactionTable" />
        <result column="TRANSACTION_ID" property="transactionId" />
        <result column="SAL_ORD_NO" property="salOrdNo" />
        <result column="SAL_ORD_LINE" property="salOrdLine" />
        <result column="PUR_ORD_NO" property="purOrdNo" />
        <result column="PUR_ORD_LN" property="purOrdLn" />
        <result column="PRD_PLAN_NO" property="prdPlanNo" />
        <result column="TRADE_PARTNER_ID" property="tradePartnerId" />
        <result column="TRADE_PARTNER_CODE" property="tradePartnerCode" />
        <result column="TRADE_PARTNER_NAME" property="tradePartnerName" />
        <result column="REMARKS" property="remarks" />
        <result column="ATTR1" property="attr1" />
        <result column="ATTR2" property="attr2" />
        <result column="ATTR3" property="attr3" />
        <result column="ATTR4" property="attr4" />
        <result column="ATTR5" property="attr5" />
        <result column="ATTR6" property="attr6" />
        <result column="ATTR7" property="attr7" />
        <result column="ATTR8" property="attr8" />
        <result column="ATTR9" property="attr9" />
        <result column="ATTR10" property="attr10" />
    </resultMap>
    
    <select id="selectAddInfoByTId" resultType="com.shinsoft.sto.model.TransRecordAddInfo">
        select add_id addId,add_code addCode,add_name addName,add_dept_id addDeptId,add_dept_code addDeptCode,add_dept_name addDeptName,add_time addTime,qty
        from ${tName} where id=#{tId}
    </select>
    
    <update id="updateOnHand">
        update ${tname} 
        <set>
            <if test="qtyNum!=0">
                qty=#{qtyNum},
            </if>
            <if test="modifyId!=null and modifyId!=''">
                modify_id=#{modifyId},
            </if>
            <if test="modifyCode!=null and modifyCode!=''">
                modify_code=#{modifyCode},
            </if>
            <if test="modifyName!=null and modifyName!=''">
                modify_name=#{modifyName},
                last_name=#{modifyName},
            </if>
            <if test="modifyTime!=null">
                modify_time=#{modifyTime},
                last_time=SYSDATE
            </if>
        </set>
        where id=#{tid}
    </update>
    
    <select id="queryOne" resultType="com.shinsoft.sto.model.OnHandVO">
        select store_cd storeCd,area_cd areaCd,stack_cd stackCd,item_cd itemCd,item_nm itemNm,item_type itemType
         from ${tname} 
        where id=#{tid}
    </select>
    <select id="selectList" resultType="com.shinsoft.sto.model.TransRecord">
        select * from sto_trans_record recode
        left join sto_config config on recode.store_cd = config.store_cd
        <where>
            <if test="storeNm!=null and storeNm!=''">
                and config.store_nm=#{storeNm}
            </if>
            <if test="itemNm!=null and itemNm!=''">
                and recode.item_nm=#{itemNm}
            </if>
            <if test="startTime!=null and endTime!=null">
                and recode.transaction_date between #{startTime} and #{endTime}
            </if>
            <if test="itemType!=null and itemType!=''">
                and recode.item_type=#{itemType}
            </if>
        </where>
        order by recode.ADD_TIME desc
    </select>

    <update id="updateModify">
        update ${transactionTable} 
        <set>
            <if test="addId!=null and addId!=''">
                modify_id=#{addId},
            </if>
            <if test="addCode!=null and addCode!=''">
                modify_code=#{addCode},
            </if>
            <if test="addName!=null and addName!=''">
                modify_name=#{addName},
            last_name=#{addName},
            </if>
            <if test="addTime!=null">
                modify_time=#{addTime},
                last_time=#{addTime}
            </if>
        </set>
        where id=#{transactionId}
    </update>


    <select id="queryWlsfc" resultType="com.shinsoft.sto.model.TransRecordWlsfc">
        SELECT TA.*,CON.STORE_NM FROM (SELECT SUM(addQty)      as addQty,
        SUM(subtractQty) as subtractQty,
        TABLEA.STORE_CD,
        TABLEA.ITEM_CD,
        TABLEA.ITEM_NM
        FROM (SELECT sum(qty) addQty, 0 AS subtractQty, STORE_CD, ITEM_CD, ITEM_NM
        FROM STO_TRANS_RECORD
        WHERE
        CANCEL_FLAG = '0'
        <if test="map.itemCd != null and map.itemCd !=''">
            AND ITEM_CD = #{map.itemCd}
        </if>
        <if test="map.startDate != null and map.startDate !='' and map.endDate != null and map.endDate !=''">
            AND TRANSACTION_DATE between TO_DATE(#{map.startDate}, 'YYYY-MM-DD HH24:MI:SS') and TO_DATE(#{map.endDate}, 'YYYY-MM-DD HH24:MI:SS')
        </if>

        AND TRANSACTION_TYPE in ('A1', 'A3', 'A5', 'A7', 'B2', 'B4', 'B5', 'C1')
        group by STORE_CD, ITEM_CD, ITEM_NM
        union all
        SELECT 0 AS addQty, sum(qty) subtractQty, STORE_CD, ITEM_CD, ITEM_NM
        FROM STO_TRANS_RECORD
        WHERE CANCEL_FLAG = '0'
        <if test="map.itemCd != null and map.itemCd !=''">
            ITEM_CD = #{map.itemCd}
        </if>

        <if test="map.startDate != null and map.startDate !='' and map.endDate != null and map.endDate !=''">
            AND TRANSACTION_DATE between TO_DATE(#{map.startDate}, 'YYYY-MM-DD HH24:MI:SS') and TO_DATE(#{map.endDate}, 'YYYY-MM-DD HH24:MI:SS')
        </if>
        AND TRANSACTION_TYPE not in ('A1', 'A3', 'A5', 'A7', 'B2', 'B4', 'B5', 'C1')
        group by STORE_CD, ITEM_CD, ITEM_NM) TABLEA
        GROUP BY TABLEA.STORE_CD, TABLEA.ITEM_CD, TABLEA.ITEM_NM) TA
        INNER JOIN STO_CONFIG CON ON TA.STORE_CD = CON.STORE_CD AND CON.STO_CONFIG_LEVEL = '1' AND CON.CANCEL_FLAG = '0'
        ORDER BY TA.ITEM_CD
    </select>
    <select id="queryWlsfcList" resultType="com.shinsoft.sto.model.TransRecordWlsfc">
        SELECT TA.*,CON.STORE_NM FROM (SELECT SUM(addQty)      as addQty,
        SUM(subtractQty) as subtractQty,
        TABLEA.STORE_CD,
        TABLEA.ITEM_CD,
        TABLEA.ITEM_NM
        FROM (SELECT sum(qty) addQty, 0 AS subtractQty, STORE_CD, ITEM_CD, ITEM_NM
        FROM STO_TRANS_RECORD
        WHERE
        CANCEL_FLAG = '0'
        <if test="map.itemCd != null and map.itemCd !=''">
            AND ITEM_CD = #{map.itemCd}
        </if>
        <if test="map.startDate != null and map.startDate !='' and map.endDate != null and map.endDate !=''">
            AND TRANSACTION_DATE between TO_DATE(#{map.startDate}, 'YYYY-MM-DD HH24:MI:SS') and TO_DATE(#{map.endDate}, 'YYYY-MM-DD HH24:MI:SS')
        </if>

        AND TRANSACTION_TYPE in ('A1', 'A3', 'A5', 'A7', 'B2', 'B4', 'B5', 'C1')
        group by STORE_CD, ITEM_CD, ITEM_NM
        union all
        SELECT 0 AS addQty, sum(qty) subtractQty, STORE_CD, ITEM_CD, ITEM_NM
        FROM STO_TRANS_RECORD
        WHERE CANCEL_FLAG = '0'
        <if test="map.itemCd != null and map.itemCd !=''">
            ITEM_CD = #{map.itemCd}
        </if>

        <if test="map.startDate != null and map.startDate !='' and map.endDate != null and map.endDate !=''">
            AND TRANSACTION_DATE between TO_DATE(#{map.startDate}, 'YYYY-MM-DD HH24:MI:SS') and TO_DATE(#{map.endDate}, 'YYYY-MM-DD HH24:MI:SS')
        </if>
        AND TRANSACTION_TYPE not in ('A1', 'A3', 'A5', 'A7', 'B2', 'B4', 'B5', 'C1')
        group by STORE_CD, ITEM_CD, ITEM_NM) TABLEA
        GROUP BY TABLEA.STORE_CD, TABLEA.ITEM_CD, TABLEA.ITEM_NM) TA
        INNER JOIN STO_CONFIG CON ON TA.STORE_CD = CON.STORE_CD AND CON.STO_CONFIG_LEVEL = '1' AND CON.CANCEL_FLAG = '0'
        ORDER BY TA.ITEM_CD
    </select>
    <select id="selectSum" parameterType="map" resultType="hashmap">
        SELECT cc.STORE_NM, rr.ITEM_NM, SUM(rr.QTY) AS TOTALQTY, rr.ITEM_CD, rr.STORE_CD, rr.AREA_CD, rr.STACK_CD
        FROM sto_trans_record rr
        LEFT JOIN sto_config cc
        ON rr.store_cd = cc.store_cd
        WHERE rr.cancel_flag = '0'
        <if test="transacationDate != null">
            AND rr.transaction_date BETWEEN #{transacationDate} AND #{todayDate}
        </if>
        <if test="storeNm != null and storeNm !=''">
            AND cc.store_nm = #{storeNm}
        </if>
        <if test="itemType != null and itemType !=''">
            AND rr.item_type = #{itemType}
        </if>
        <if test="areaCd != null and areaCd !=''">
            AND rr.area_cd = #{areaCd}
        </if>
        <if test="stackCd != null and stackCd !=''">
            AND rr.stack_cd = #{stackCd}
        </if>
        <if test="itemNm != null and itemNm !=''">
            AND rr.item_nm = #{itemNm}
        </if>
        GROUP BY rr.STORE_CD, rr.ITEM_NM, rr.ITEM_CD, cc.STORE_NM, rr.AREA_CD, rr.STACK_CD
    </select>


</mapper>
