<?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="org.springblade.imports.mapper.FctStandingBookMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="org.springblade.imports.entity.FctStandingBook">
        <id column="DID" property="dId" />
        <result column="THEMONTH" property="themonth" />
        <result column="SPNAME" property="spname" />
        <result column="IN_MONEY" property="inMoney" />
        <result column="IN_NUM" property="inNum" />
        <result column="IN_GIFTCARD_NUM" property="inGiftcardNum" />
        <result column="IN_PRICE" property="inPrice" />
        <result column="OUT_PRICE" property="outPrice" />
        <result column="SALE_PRICE" property="salePrice" />
        <result column="ACCOUNT_NUM" property="accountNum" />
        <result column="SALE_GIFTCARD_NUM" property="saleGiftcardNum" />
        <result column="ACCOUNT_MONEY" property="accountMoney" />
        <result column="BALANCE_MONEY" property="balanceMoney" />
        <result column="BALANCE_NORMAL_CARD" property="balanceNormalCard" />
        <result column="BALANCE_GIFTCARD" property="balanceGiftcard" />
        <result column="OUT_CHANNEL" property="outChannel" />
        <result column="COUT_TIME" property="coutTime" />
        <result column="MCOMMENT" property="mcomment" />
    </resultMap>


    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO FCT_STANDING_BOOK
        (
        DID,
        THEMONTH,
        SPNAME,
        IN_MONEY,
        IN_NUM,
        IN_GIFTCARD_NUM,
        IN_PRICE,
        OUT_PRICE,
        SALE_PRICE,
        ACCOUNT_NUM,
        SALE_GIFTCARD_NUM,
        ACCOUNT_MONEY,
        SYSTEM_NUM,
        BALANCE_MONEY,
        BALANCE_NORMAL_CARD,
        BALANCE_GIFTCARD,
        OUT_CHANNEL,
        COUT_TIME,
        MCOMMENT
        )
        SELECT SEQ_STANDING_BOOK.nextval,a.*
        FROM(
        <foreach item="item" index="index" collection="list" separator="union all">
            SELECT
            #{item.themonth,jdbcType=VARCHAR} as THEMONTH,
            #{item.spname,jdbcType=VARCHAR} as SPNAME,
            #{item.inMoney,jdbcType=DOUBLE} as IN_MONEY,
            #{item.inNum,jdbcType=DOUBLE} as IN_NUM,
            #{item.inGiftcardNum,jdbcType=DOUBLE} as IN_GIFTCARD_NUM,
            #{item.inPrice,jdbcType=DOUBLE} as IN_PRICE,
            #{item.outPrice,jdbcType=DOUBLE} as OUT_PRICE,
            #{item.salePrice,jdbcType=DOUBLE} as SALE_PRICE,
            #{item.accountNum,jdbcType=DOUBLE} as ACCOUNT_NUM,
            #{item.saleGiftcardNum,jdbcType=DOUBLE} as SALE_GIFTCARD_NUM,
            #{item.accountMoney,jdbcType=DOUBLE} as ACCOUNT_MONEY,
            #{item.systemNum,jdbcType=DOUBLE} as SYSTEM_NUM,
            #{item.balanceMoney,jdbcType=DOUBLE} as BALANCE_MONEY,
            #{item.balanceNormalCard,jdbcType=DOUBLE} as BALANCE_NORMAL_CARD,
            #{item.balanceGiftcard,jdbcType=DOUBLE} as BALANCE_GIFTCARD,
            #{item.outChannel,jdbcType=VARCHAR} as OUT_CHANNEL,
            #{item.coutTime,jdbcType=VARCHAR} as COUT_TIME,
            #{item.mcomment,jdbcType=VARCHAR} as MCOMMENT
            from dual
        </foreach>
        ) a
    </insert>


    <insert id="insertBatchHasDid" parameterType="java.util.List">
        INSERT INTO FCT_STANDING_BOOK
        (
        DID,
        THEMONTH,
        SPNAME,
        IN_MONEY,
        IN_NUM,
        IN_GIFTCARD_NUM,
        IN_PRICE,
        OUT_PRICE,
        SALE_PRICE,
        ACCOUNT_NUM,
        SALE_GIFTCARD_NUM,
        ACCOUNT_MONEY,
        SYSTEM_NUM,
        BALANCE_MONEY,
        BALANCE_NORMAL_CARD,
        BALANCE_GIFTCARD,
        OUT_CHANNEL,
        COUT_TIME,
        ACCOUNTID,
        ACCOUNTNAME,
        IMPORTTIME,
        MCOMMENT
        )
        SELECT a.*
        FROM(
        <foreach item="item" index="index" collection="list" separator="union all">
            SELECT
            #{item.dId,jdbcType=NUMERIC} as DID,
            #{item.themonth,jdbcType=VARCHAR} as THEMONTH,
            #{item.spname,jdbcType=VARCHAR} as SPNAME,
            #{item.inMoney,jdbcType=DOUBLE} as IN_MONEY,
            #{item.inNum,jdbcType=DOUBLE} as IN_NUM,
            #{item.inGiftcardNum,jdbcType=DOUBLE} as IN_GIFTCARD_NUM,
            #{item.inPrice,jdbcType=DOUBLE} as IN_PRICE,
            #{item.outPrice,jdbcType=DOUBLE} as OUT_PRICE,
            #{item.salePrice,jdbcType=DOUBLE} as SALE_PRICE,
            #{item.accountNum,jdbcType=DOUBLE} as ACCOUNT_NUM,
            #{item.saleGiftcardNum,jdbcType=DOUBLE} as SALE_GIFTCARD_NUM,
            #{item.accountMoney,jdbcType=DOUBLE} as ACCOUNT_MONEY,
            #{item.systemNum,jdbcType=DOUBLE} as SYSTEM_NUM,
            #{item.balanceMoney,jdbcType=DOUBLE} as BALANCE_MONEY,
            #{item.balanceNormalCard,jdbcType=DOUBLE} as BALANCE_NORMAL_CARD,
            #{item.balanceGiftcard,jdbcType=DOUBLE} as BALANCE_GIFTCARD,
            #{item.outChannel,jdbcType=VARCHAR} as OUT_CHANNEL,
            #{item.coutTime,jdbcType=VARCHAR} as COUT_TIME,
            #{item.accountId,jdbcType=VARCHAR} as ACCOUNTID,
            #{item.accountName,jdbcType=VARCHAR} as ACCOUNTNAME,
            #{item.importTime,jdbcType=VARCHAR} as IMPORTTIME,
            #{item.mcomment,jdbcType=VARCHAR} as MCOMMENT
            from dual
        </foreach>
        ) a
    </insert>


    <select id="getPageByQuery" resultMap="BaseResultMap">
        select
        DID,
        THEMONTH,
        SPNAME,
        NVL(IN_MONEY,0) as IN_MONEY,
        NVL(IN_NUM,0) as IN_NUM,
        NVL(IN_GIFTCARD_NUM,0) as IN_GIFTCARD_NUM,
        NVL(IN_PRICE,0) as IN_PRICE,
        NVL(OUT_PRICE,0) as OUT_PRICE,
        NVL(SALE_PRICE,0) as SALE_PRICE,
        NVL(ACCOUNT_NUM,0) as ACCOUNT_NUM,
        NVL(SALE_GIFTCARD_NUM,0) as SALE_GIFTCARD_NUM,
        NVL(ACCOUNT_MONEY,0) as ACCOUNT_MONEY,
        NVL(BALANCE_MONEY,0) as BALANCE_MONEY,
        NVL(BALANCE_NORMAL_CARD,0) BALANCE_NORMAL_CARD,
        NVL(BALANCE_GIFTCARD,0) BALANCE_GIFTCARD,
        NVL(SYSTEM_NUM,0) SYSTEM_NUM,
        OUT_CHANNEL,
        COUT_TIME,
        MCOMMENT,
        ACCOUNTID,
        ACCOUNTNAME,
        IMPORTTIME
        from FCT_STANDING_BOOK where 1=1
        <if test="query.channelName != null and query.channelName != ''">
            and regexp_like(OUT_CHANNEL,#{query.channelName})
        </if>
        <if test="query.productName != null and query.productName != ''">
            and regexp_like(SPNAME,#{query.productName})
        </if>
        <if test="query.accountName != null and query.accountName != ''">
            and regexp_like(ACCOUNTNAME,#{query.accountName})
        </if>
        <if test="query.inOut != null and query.inOut == 1">
            and (IN_MONEY <![CDATA[>]]> 0 or IN_NUM  <![CDATA[>]]> 0 or IN_GIFTCARD_NUM <![CDATA[>]]> 0 or IN_PRICE <![CDATA[>]]> 0)
            /*and (IN_MONEY is not null or IN_NUM is not null or IN_GIFTCARD_NUM is not null or IN_PRICE is not null)*/
        </if>
        <if test="query.inOut != null and query.inOut == 2">
            and (IN_MONEY is null or IN_MONEY = 0) and (IN_NUM is null or IN_NUM = 0) and (IN_GIFTCARD_NUM is null or IN_GIFTCARD_NUM = 0) and (IN_PRICE is null or IN_PRICE = 0)
            /*and IN_MONEY is null and IN_NUM is null and IN_GIFTCARD_NUM is null and IN_PRICE is null*/
        </if>
        <if test="query.startTime != null and query.startTime != ''">
            and THEMONTH <![CDATA[>=]]> #{query.startTime}
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and THEMONTH <![CDATA[<=]]> #{query.endTime}
        </if>
        order by DID
    </select>


    <select id="getListByQuery" resultMap="BaseResultMap">
        select * from FCT_STANDING_BOOK where 1=1
        <if test="query.channelName != null and query.channelName != ''">
            and OUT_CHANNEL IN #{query.channelName})
        </if>
        <if test="query.productName != null and query.productName != ''">
            and SPNAME IN #{query.productName})
        </if>
        <if test="query.startTime != null and query.startTime != ''">
            and THEMONTH <![CDATA[>=]]> #{query.startTime}
        </if>
        <if test="query.endTime != null and query.endTime != ''">
            and THEMONTH <![CDATA[<=]]> #{query.endTime}
        </if>
        order by DID
    </select>

</mapper>
