<?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.supplier.mapper.SupplierWalletMapper">
    <resultMap id="supplierWalletMap" type="com.tmerclub.cloud.supplier.model.SupplierWallet">
        <id column="supplier_wallet_id" property="supplierWalletId"/>
        <result column="pay_sys_type" property="paySysType"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="supplier_id" property="supplierId"/>
        <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="version" property="version"/>
    </resultMap>
    <sql id="Vo_Column_List">
        `supplier_wallet_id`,
        `pay_sys_type`,
        `create_time`,
        `update_time`,
        `supplier_id`,
        `unsettled_amount`,
        `settled_amount`,
        `freeze_amount`,
        `total_settled_amount`,
        `version`
    </sql>
    <insert id="save">
        insert into supplier_wallet (`pay_sys_type`,`supplier_id`, `unsettled_amount`, `settled_amount`, `freeze_amount`,
                                 `total_settled_amount`,  `version`)
        values (#{supplierWallet.paySysType},#{supplierWallet.supplierId}, #{supplierWallet.unsettledAmount}, #{supplierWallet.settledAmount},
                #{supplierWallet.freezeAmount}, #{supplierWallet.totalSettledAmount},
                #{supplierWallet.version});
    </insert>

    <select id="getBySupplierId" resultType="com.tmerclub.cloud.supplier.vo.SupplierWalletVO">
        select
        <include refid="Vo_Column_List"/>
        from supplier_wallet
        where supplier_id = #{supplierId} and pay_sys_type = #{paySysType}
    </select>

    <update id="addUnsettledAmount">
        update supplier_wallet
        set unsettled_amount = unsettled_amount + #{changeAmount}
        where supplier_id = #{supplierId} and pay_sys_type = #{paySysType}
        and unsettled_amount &gt;= -#{changeAmount}
    </update>

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

    <update id="subSettledAmount">
        update supplier_wallet
        set settled_amount       = settled_amount - #{settledAmount}
        where supplier_id = #{supplierId}
        and settled_amount &gt;= #{settledAmount} and pay_sys_type = #{paySysType}
    </update>

    <update id="subUnsettledAmount">
        update supplier_wallet
        set unsettled_amount       = unsettled_amount - #{supplierRealRefundAmount}
        where supplier_id = #{supplierId}
        and unsettled_amount &gt;= #{supplierRealRefundAmount} and pay_sys_type = #{paySysType}
    </update>

    <update id="addFreezeAmount">
        update supplier_wallet
        set freeze_amount = freeze_amount + #{changeAmount}
        where supplier_id = #{supplierId} and pay_sys_type = #{paySysType}
    </update>

    <update id="subFreezeAmount">
        update supplier_wallet
        set freeze_amount = freeze_amount - #{changeAmount}
        where supplier_id = #{supplierId} and pay_sys_type = #{paySysType}
        and freeze_amount &gt;= #{changeAmount}
    </update>

    <select id="getAllSupplier" resultType="com.tmerclub.cloud.supplier.vo.SupplierWalletVO">
        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 `supplier_wallet` sw
        join supplier_detail sd on sw.supplier_id = sd.supplier_id
        <where>
            sw.pay_sys_type = #{paySysType}
            <if test="supplierWalletLogSearchDTO.supplierName != null and supplierWalletLogSearchDTO.supplierName != ''">
                AND TRIM(REPLACE(sd.supplier_name,' ','')) LIKE TRIM(REPLACE(CONCAT('%',#{supplierWalletLogSearchDTO.supplierName},'%'),' ',''))
            </if>
        </where>
    </select>

    <select id="listAllSupplierWallet" resultType="com.tmerclub.cloud.supplier.vo.SupplierWalletVO">
        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.supplier_name, sw.supplier_id
        FROM `supplier_wallet` sw
        JOIN supplier_detail sd on sw.supplier_id = sd.supplier_id
        <where>
            sw.pay_sys_type = #{paySysType}
            AND sd.supplier_status != -1
            <if test="supplierWalletLogSearchDTO.supplierName != null and supplierWalletLogSearchDTO.supplierName != ''">
                AND TRIM(REPLACE(sd.supplier_name,' ','')) LIKE TRIM(REPLACE(CONCAT('%',#{supplierWalletLogSearchDTO.supplierName},'%'),' ',''))
            </if>
            <if test="paySysType == null or paySysType == 0">
                AND sd.supplier_status != 3 AND sd.supplier_status != 4
            </if>
        </where>
        GROUP BY sw.supplier_id
        <if test="supplierWalletLogSearchDTO.sortType != null">
            ORDER BY
            <choose>
                <when test="supplierWalletLogSearchDTO.sortType == 0">
                    sw.supplier_id
                </when>
                <otherwise>
                    settled_amount
                </otherwise>
            </choose>
            <choose>
                <when test="supplierWalletLogSearchDTO.sortType == 1">
                    ASC
                </when>
                <when test="supplierWalletLogSearchDTO.sortType == 2">
                    DESC
                </when>
                <otherwise>
                    ASC
                </otherwise>
            </choose>
        </if>
    </select>
</mapper>
