<?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.xxfc.platform.order.mapper.CompanyWalletMapper">
    
    
     <update id="updMyWater" parameterType="com.github.wxiaoqi.security.admin.entity.CompanyWallet">
        update  company_wallet
        <set>
        <if test=" companyId != null and companyId != '' ">
            company_id=#{companyId},
        </if>
            <if test="balance!=null ">
             balance=#{balance},
         </if>
         <if test="withdrawals!=null and withdrawals!='' ">
             withdrawals=#{withdrawals},
         </if>
         <if test="totalAmount!=null and totalAmount!='' ">
             total_amount=#{totalAmount},
         </if>
         <if test="todayAmount!=null and todayAmount!='' ">
             today_amount=#{todayAmount},
         </if>
         <if test="unbooked!=null and unbooked!='' ">
             unbooked=#{unbooked},
         </if>
         <if test="isFrozen!=null and isFrozen!='' ">
             is_frozen=#{isFrozen},
         </if>
        <if test="lastIntime!=null and lastIntime!='' ">
            last_intime=#{lastIntime},
        </if>
        <if test="crtTime!=null and crtTime!='' ">
            crt_time=#{crtTime},
        </if>
        <if test="updTime!=null and updTime!='' ">
            upd_time=#{updTime},
        </if>
        <if test="version!=null and version!='' ">
            version=version+1,
        </if>
        </set>
        <where>
             id=#{id} and version=#{version}
        </where>
     </update>


    <select id="sumAmount" resultType="com.github.wxiaoqi.security.admin.entity.CompanyWallet"  parameterType="com.github.wxiaoqi.security.admin.dto.WalletDetailDTO">
        SELECT IFNULL(SUM(w.balance),0) as balance,IFNULL(SUM(w.withdrawals),0) as withdrawals FROM company_wallet w
        LEFT JOIN branch_company c ON w.company_id=c.id
        <where>
            <if test="companyId != null and companyId > 0">
                and  w.company_id=#{companyId}
            </if>
            <if test="branchId != null and branchId > 0">
                and  c.company_id=#{branchId}
            </if>
            <if test="dataCompanyIds != null and dataCompanyIds.size() > 0">
                and w.company_id  in
                <foreach collection="dataCompanyIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size() > 0">
                and c.company_id  in
                <foreach collection="dataCorporationIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>



    <select id="selectList" resultType="com.github.wxiaoqi.security.admin.vo.CompanyWalletVo"  parameterType="com.github.wxiaoqi.security.admin.dto.WalletDTO">
        SELECT
        w.*,
        c.`name` as companyName,
        ci.`name` as branchName,
        ci.id as branchId
        FROM company_wallet w
        LEFT JOIN  branch_company c ON w.company_id =c.id
        LEFT JOIN company_info ci ON c.company_id=ci.id
        <where>
            <if test="companyId != null and companyId > 0">
                and  w.company_id=#{companyId}
            </if>
            <if test="branchId != null and branchId > 0">
                and  c.company_id=#{branchId}
            </if>
            <if test="dataCompanyIds != null and dataCompanyIds.size() > 0">
                and w.company_id= in
                <foreach collection="dataCompanyIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size() > 0">
                and c.company_id  in
                <foreach collection="dataCorporationIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        order  by  w.id desc
    </select>

</mapper>