<?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.CompanyWalletDetailMapper">


    <select id="selectList" resultType="com.xxfc.platform.order.pojo.vo.CompanyWalletDetailVo"  parameterType="com.xxfc.platform.order.pojo.dto.WalletDetailDTO">
        SELECT
            d.cono,
            d.itype,
            d.type,
            c.`name` as companyName,
            ci.`name` as branchName,
            SUM(d.amount) as amount,
            MAX(s_amount)as sAmount
        FROM company_wallet_detail d
        LEFT JOIN  branch_company c ON d.company_id=c.id
        LEFT JOIN company_info ci ON d.branch_id=ci.id
        <where>
            d.itype != 5
            <if test="companyId != null and companyId > 0">
               and  d.company_id=#{companyId}
            </if>
            <if test="branchId != null and branchId > 0">
                and  d.branch_id=#{branchId}
            </if>
            <if test="startTime != null and startTime > 0">
                and  d.crt_time >= #{startTime}
            </if>
            <if test="endTime != null and endTime > 0">
                and  d.crt_time &lt;= #{endTime}
            </if>
            <if test="dataCompanyIds != null and dataCompanyIds.size() > 0">
                and d.company_id= in
                <foreach collection="dataCompanyIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size() > 0">
                and d.branch_id  in
                <foreach collection="dataCorporationIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY d.cono,d.itype
        order  by  d.id desc
    </select>

    <select id="sumAmount" resultType="BigDecimal"  parameterType="com.xxfc.platform.order.pojo.dto.WalletDetailDTO">
        SELECT IFNULL(SUM(amount),0) as amount  FROM company_wallet_detail
        <where>
            itype in
            <foreach collection="itypes"  item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
            <if test="companyId != null and companyId > 0">
                and  company_id=#{companyId}
            </if>
            <if test="branchId != null and branchId > 0">
                and  branch_id=#{branchId}
            </if>
            <if test="dataCompanyIds != null and dataCompanyIds.size() > 0">
                and company_id  in
                <foreach collection="dataCompanyIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dataCorporationIds != null and dataCorporationIds.size() > 0">
                and branch_id  in
                <foreach collection="dataCorporationIds"  item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    
    


</mapper>