<?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.tmerclub.cloud.multishop.mapper.ShopWalletMapper">
    <resultMap id="shopWalletMap" type="com.tmerclub.cloud.multishop.model.ShopWallet">
        <id column="shop_wallet_id" property="shopWalletId"/>
        <result column="pay_sys_type" property="paySysType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="shop_id" property="shopId"/>
        <result column="unsettled_amount" property="unsettledAmount"/>
        <result column="settled_amount" property="settledAmount"/>
        <result column="freeze_amount" property="freezeAmount"/>
        <result column="total_settled_amount" property="totalSettledAmount"/>
        <result column="sms_num" property="smsNum"/>
        <result column="version" property="version"/>
    </resultMap>
    <sql id="Vo_Column_List">
        `shop_wallet_id`,
        `pay_sys_type`,
        `create_time`,
        `update_time`,
        `shop_id`,
        `unsettled_amount`,
        `settled_amount`,
        `freeze_amount`,
        `total_settled_amount`,
        `sms_num`,
        `version`
    </sql>

    <select id="listByShopIds" resultType="com.tmerclub.cloud.multishop.vo.ShopWalletVO">
        select sw.shop_id, sw.freeze_amount, sw.sms_num, sw.create_time, sw.update_time, sw.sms_num,
        sw.unsettled_amount as unsettled_amount,
        sw.settled_amount as settled_amount,
        sw.total_settled_amount as total_settled_amount
        from shop_wallet sw
        where sw.pay_sys_type = #{paySysType} and sw.shop_id in
        <foreach collection="shopIds" item="shopId" open="(" separator="," close=")">
            #{shopId}
        </foreach>
    </select>
    <insert id="save">
        insert into shop_wallet (`pay_sys_type`,`shop_id`, `unsettled_amount`, `settled_amount`, `freeze_amount`,
                                 `total_settled_amount`, `sms_num`,`version`)
        values (#{shopWallet.paySysType},#{shopWallet.shopId}, #{shopWallet.unsettledAmount}, #{shopWallet.settledAmount},
                #{shopWallet.freezeAmount}, #{shopWallet.totalSettledAmount}, #{shopWallet.smsNum},
                #{shopWallet.version});
    </insert>
    <update id="update">
        update shop_wallet
        <set>
            <if test="shopWallet.shopId != null">
                `shop_id` = #{shopWallet.shopId},
            </if>
            <if test="shopWallet.unsettledAmount != null">
                `unsettled_amount` = #{shopWallet.unsettledAmount},
            </if>
            <if test="shopWallet.settledAmount != null">
                `settled_amount` = #{shopWallet.settledAmount},
            </if>
            <if test="shopWallet.freezeAmount != null">
                `freeze_amount` = #{shopWallet.freezeAmount},
            </if>
            <if test="shopWallet.totalSettledAmount != null">
                `total_settled_amount` = #{shopWallet.totalSettledAmount},
            </if>
            <if test="shopWallet.smsNum != null">
                `sms_num` = #{shopWallet.smsNum},
            </if>
            <if test="shopWallet.version != null">
                `version` = #{shopWallet.version} + 1,
            </if>
        </set>
        where shop_wallet_id = #{shopWallet.shopWalletId}
          and `version` = #{shopWallet.version}
    </update>

    <select id="getByShopId" resultType="com.tmerclub.cloud.multishop.vo.ShopWalletVO">
        select
        <include refid="Vo_Column_List"/>
        from shop_wallet
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
    </select>

    <update id="addUnsettledAmount">
        update shop_wallet
        set unsettled_amount = unsettled_amount + #{changeAmount}
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
        and unsettled_amount &gt;= -#{changeAmount}
    </update>

    <update id="subUnsettledAmount">
        update shop_wallet
        set unsettled_amount       = unsettled_amount - #{shopRealRefundAmount}
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
        and unsettled_amount &gt;= #{shopRealRefundAmount}
    </update>

    <update id="addSettledAmount">
        update shop_wallet
        set settled_amount = settled_amount + #{settledAmount}
        <if test="isUpdateTotal == true">
            , total_settled_amount = total_settled_amount + #{settledAmount}
        </if>
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
    </update>

    <update id="subSettledAmount">
        update shop_wallet
        set settled_amount = settled_amount - #{changeAmount}
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
        and settled_amount &gt;= #{changeAmount}
    </update>

    <update id="addFreezeAmount">
        update shop_wallet
        set freeze_amount = freeze_amount + #{changeAmount}
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
    </update>

    <update id="subFreezeAmount">
        update shop_wallet
        set freeze_amount = freeze_amount - #{changeAmount}
        where shop_id = #{shopId} and pay_sys_type = #{paySysType}
        and freeze_amount &gt;= #{changeAmount}
    </update>

    <select id="getAllShop" resultType="com.tmerclub.cloud.multishop.vo.ShopWalletVO">
        SELECT SUM(sw.unsettled_amount) AS unsettled_amount,
        SUM(sw.settled_amount) AS settled_amount,
        SUM(sw.freeze_amount) AS freeze_amount,
        SUM(sw.total_settled_amount) AS total_settled_amount
        FROM `shop_wallet` sw
        join shop_detail sd on sw.shop_id = sd.shop_id
        <where>
            sw.pay_sys_type = #{paySysType}
            <if test="shopWalletLogSearchDTO.shopName != null and shopWalletLogSearchDTO.shopName != ''">
                AND TRIM(REPLACE(sd.shop_name,' ','')) LIKE TRIM(REPLACE(CONCAT('%',#{shopWalletLogSearchDTO.shopName},'%'),' ',''))
            </if>
        </where>
    </select>

    <select id="listAllShopWallet" resultType="com.tmerclub.cloud.multishop.vo.ShopWalletVO">
        SELECT sw.`unsettled_amount` as unsettled_amount,
        sw.`settled_amount` as settled_amount,
        sw.`freeze_amount` as freeze_amount,
        sw.`total_settled_amount` as total_settled_amount,
        sd.shop_name, sw.shop_id
        FROM `shop_wallet` sw
        JOIN shop_detail sd on sw.shop_id = sd.shop_id
        WHERE sw.shop_id != 0 and sw.pay_sys_type = #{paySysType}
        AND sd.shop_status != -1
        <if test="shopWalletLogSearchDTO.shopName != null and shopWalletLogSearchDTO.shopName != ''">
            AND TRIM(REPLACE(sd.shop_name,' ','')) LIKE TRIM(REPLACE(CONCAT('%',#{shopWalletLogSearchDTO.shopName},'%'),' ',''))
        </if>
        <if test="paySysType == null or paySysType == 0">
            AND sd.shop_status != 3 AND shop_status != 4
        </if>
        GROUP BY sw.shop_id
        <if test="shopWalletLogSearchDTO.sortType != null">
            ORDER BY
            <choose>
                <when test="shopWalletLogSearchDTO.sortType == 0">
                    sw.shop_id
                </when>
                <otherwise>
                    settled_amount
                </otherwise>
            </choose>
            <choose>
                <when test="shopWalletLogSearchDTO.sortType == 1">
                    ASC
                </when>
                <when test="shopWalletLogSearchDTO.sortType == 2">
                    DESC
                </when>
                <otherwise>
                    ASC
                </otherwise>
            </choose>
        </if>
    </select>

    <select id="getSettlementAmountByShopId" resultType="long">
        SELECT settled_amount
        FROM `shop_wallet`
        WHERE shop_id = #{shopId} and pay_sys_type = #{paySysType}
    </select>
</mapper>
