<?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.audit.mapper.IntBalanceDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="org.springblade.audit.entity.IntBalanceDetail">
        <result column="THEMONTH" property="themonth" />
        <result column="PROVINCE" property="province" />
        <result column="SPNAME" property="spname" />
        <result column="SPID" property="spid" />
        <result column="MONEY1" property="money1" />
        <result column="MONEY2" property="money2" />
        <result column="MONEY3" property="money3" />
        <result column="MONEY4" property="money4" />
        <result column="SPCODE" property="spcode" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        THEMONTH, PROVINCE, SPNAME, SPID, MONEY1, MONEY2, MONEY3, MONEY4, SPCODE
    </sql>

    <update id="giveSpidSoul">
        UPDATE INT_BALANCE_DETAIL a SET spid =( SELECT max( spid ) FROM dim_product_category b WHERE a.spname = b.spname )
        WHERE
        spid IS NULL
        AND themonth = #{theMonth}
    </update>

    <update id="giveSpidSoulTwo">
        UPDATE INT_BALANCE_DETAIL a
        SET spid = ( SELECT max( spid ) FROM DIM_CS_PRM_SPNAME b WHERE a.spname = b.PRM_SPNAME )
        WHERE
        themonth = #{theMonth}
        AND spid IS NULL
    </update>

    <update id="settingFileTag">
        UPDATE INT_IMPORT_FLAG
        SET FLAG_BALANCE_DETAIL = 1
        WHERE
            themonth = #{theMonth}
    </update>

    <delete id="deleteZeroMoney">
        DELETE
        FROM
            INT_BALANCE_DETAIL
        WHERE
            themonth = #{theMonth}
            AND MONEY1 = 0
            AND MONEY2 = 0
            AND MONEY3 = 0
            AND MONEY4 = 0
            AND FEE1 = 0
            AND FEE2 = 0
            AND FEE3 = 0
    </delete>

    <delete id="deleteNoSpid">
        DELETE
        FROM
            INT_BALANCE_DETAIL
        WHERE
            themonth = #{theMonth}
        AND spid IS NULL
    </delete>

</mapper>
