<?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.bonus.core.account.v3.mapper.AccInfoMapper">


    <sql id="queryPageAccInfoWhereSql">
        <where>
            t1.acc_status in
            <foreach collection="infoParam.accStatusList" item="accStatus" separator="," open="(" close=")">
                #{accStatus}
            </foreach>
            <if test="infoParam.keyword != null and infoParam.keyword != ''">
                <include refid="cust_fuzzy_query"></include>
            </if>
            <if test="infoParam.psnType != null">
                and t2.psn_type = #{infoParam.psnType}
            </if>
            <if test="infoParam.custId != null">
                and t2.cust_id = #{infoParam.custId}
            </if>
            <if test="infoParam.orgIdList != null and infoParam.orgIdList.size() > 0">
                and t2.org_id in
                <foreach collection="infoParam.orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="infoParam.psnTypeList != null and infoParam.psnTypeList.size() > 0">
                and t2.psn_type in
                <foreach collection="infoParam.psnTypeList" item="psnType" separator="," open="(" close=")">
                    #{psnType}
                </foreach>
            </if>
            <if test="infoParam.walletType != null and infoParam.walletMinAmount != null">
                <if test="infoParam.walletType == 0">
                    and  t4.accBalTotal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                </if>
                <if test="infoParam.walletType == 1">
                    and  t4.walletBal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                </if>
                <if test="infoParam.walletType == 2">
                    and  t4.subsidyBal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                </if>
                <if test="infoParam.walletType == 4">
                    and  t4.redEnvelope <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                </if>
            </if>
            <if test="infoParam.walletType != null and infoParam.walletMaxAmount != null">
                <if test="infoParam.walletType == 0">
                    and  t4.accBalTotal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                </if>
                <if test="infoParam.walletType == 1">
                    and  t4.walletBal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                </if>
                <if test="infoParam.walletType == 2">
                    and  t4.subsidyBal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                </if>
                <if test="infoParam.walletType == 4">
                    and  t4.redEnvelope <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                </if>
            </if>
            <if test="infoParam.startDateTime != null">
                and t1.uptime <![CDATA[ >= ]]> #{infoParam.startDateTime}
            </if>
            <if test="infoParam.endDateTime != null">
                and t1.uptime <![CDATA[ <= ]]> #{infoParam.endDateTime}
            </if>
        </where>
    </sql>

    <!--    查询账户信息-list-->
    <select id="queryListAccInfo" resultType="com.bonus.core.account.v3.web.vo.AccInfoPageVO">
        SELECT
        t1.uptime,
        t1.acc_id,
        t2.cust_id,
        t2.cust_num,
        t2.cust_name,
        t2.mobile,
        t6.org_full_name,
        t2.psn_type,
        t5.psn_type_name,
        t1.scope,
        t1.acc_status,
        t1.end_date
        FROM acc_info t1
        INNER JOIN cust_info t2 ON t1.cust_id = t2.cust_id
        LEFT JOIN cust_psn_type t5 on t5.psn_type = t2.psn_type
        LEFT JOIN cust_org t6 on t6.org_id = t2.org_id
        <include refid="queryPageAccInfoWhereSqlV2">
        </include>
        <choose>
            <when test="infoParam.startDateTime != null or infoParam.endDateTime != null">
                order by t1.uptime desc
            </when>
            <otherwise>
                order by t2.id desc
            </otherwise>
        </choose>
    </select>

    <sql id="queryPageAccInfoWhereSqlV2">
        <where>
            t1.acc_status in
            <foreach collection="infoParam.accStatusList" item="accStatus" separator="," open="(" close=")">
                #{accStatus}
            </foreach>
            <if test="infoParam.keyword != null and infoParam.keyword != ''">
                <include refid="cust_fuzzy_query"></include>
            </if>
            <if test="infoParam.psnType != null">
                and t2.psn_type = #{infoParam.psnType}
            </if>
            <if test="infoParam.custId != null">
                and t2.cust_id = #{infoParam.custId}
            </if>
            <if test="infoParam.orgIdList != null and infoParam.orgIdList.size() > 0">
                and t2.org_id in
                <foreach collection="infoParam.orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="infoParam.psnTypeList != null and infoParam.psnTypeList.size() > 0">
                and t2.psn_type in
                <foreach collection="infoParam.psnTypeList" item="psnType" separator="," open="(" close=")">
                    #{psnType}
                </foreach>
            </if>
            <if test="infoParam.startDateTime != null">
                and t1.uptime <![CDATA[ >= ]]> #{infoParam.startDateTime}
            </if>
            <if test="infoParam.endDateTime != null">
                and t1.uptime <![CDATA[ <= ]]> #{infoParam.endDateTime}
            </if>
            <if test="infoParam.walletType != null">
                AND EXISTS(
                SELECT null FROM
                (
                SELECT
                cust_id,
                SUM( CASE WHEN wallet_id = 1 THEN wallet_bal ELSE 0 END ) AS walletBal,
                SUM( CASE WHEN wallet_id = 2 THEN wallet_bal ELSE 0 END ) AS subsidyBal,
                SUM( CASE WHEN wallet_id = 4 THEN wallet_bal ELSE 0 END ) AS redEnvelope,
                SUM( a.wallet_bal ) + SUM( a.frozen_balance ) accBalTotal
                FROM
                acc_wallet_info a
                GROUP BY
                cust_id
                ) AS t4
                <where>
                    t4.cust_id = t1.cust_id
                    <if test="infoParam.walletMinAmount != null">
                        <if test="infoParam.walletType == 0">
                            and  t4.accBalTotal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                        </if>
                        <if test="infoParam.walletType == 1">
                            and  t4.walletBal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                        </if>
                        <if test="infoParam.walletType == 2">
                            and  t4.subsidyBal <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                        </if>
                        <if test="infoParam.walletType == 4">
                            and  t4.redEnvelope <![CDATA[ >= ]]> #{infoParam.walletMinAmount}
                        </if>
                    </if>
                    <if test="infoParam.walletMaxAmount != null">
                        <if test="infoParam.walletType == 0">
                            and  t4.accBalTotal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                        </if>
                        <if test="infoParam.walletType == 1">
                            and  t4.walletBal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                        </if>
                        <if test="infoParam.walletType == 2">
                            and  t4.subsidyBal <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                        </if>
                        <if test="infoParam.walletType == 4">
                            and  t4.redEnvelope <![CDATA[ <= ]]> #{infoParam.walletMaxAmount}
                        </if>
                    </if>
                </where>
                )
            </if>
        </where>
    </sql>

    <!--    分页查询账户信息-金额汇总数据-->
    <select id="queryPageAccInfoSum" resultType="com.bonus.core.account.v3.web.vo.AccInfoPageVO">
        SELECT
        SUM( t4.accBalTotal ) AS accBalTotal,
        SUM( t4.walletBal ) AS walletBal,
        SUM( t4.subsidyBal ) AS subsidyBal,
        SUM( t4.redEnvelope ) AS redEnvelope,
        SUM( t4.accFreezeBalTotal ) AS accFreezeBalTotal
        FROM acc_info t1
        INNER JOIN cust_info t2 ON t1.cust_id = t2.cust_id
        LEFT JOIN
        (
        SELECT
        cust_id,
        SUM( CASE WHEN wallet_id = 1 THEN wallet_bal ELSE 0 END ) AS walletBal,
        SUM( CASE WHEN wallet_id = 2 THEN wallet_bal ELSE 0 END ) AS subsidyBal,
        SUM( CASE WHEN wallet_id = 4 THEN wallet_bal ELSE 0 END ) AS redEnvelope,
        SUM( a.wallet_bal ) AS accAvailableBal,
        SUM( a.frozen_balance ) AS accFreezeBalTotal,
        SUM( a.wallet_bal ) + SUM( a.frozen_balance ) AS accBalTotal
        FROM acc_wallet_info a
        GROUP BY cust_id
        ) AS t4 ON t4.cust_id = t1.cust_id
        <include refid="queryPageAccInfoWhereSql">
        </include>
    </select>

    <select id="queryAccIdByParam" resultType="java.lang.Long">
        SELECT t1.acc_id
        FROM acc_info t1
        LEFT JOIN cust_info t2 ON t1.cust_id = t2.cust_id
        <include refid="queryPageAccInfoWhereSql">
        </include>
    </select>

    <update id="updateStateByOverdue">
        UPDATE acc_info
        SET acc_status = #{changeStatus}
        WHERE end_date <![CDATA[<]]> #{overdueDate}
        AND acc_status in
        <foreach collection="oldStatusList" item="accStatus" separator="," open="(" close=")">
            #{accStatus}
        </foreach>
    </update>



    <select id="listAccInfoVOByCustIdList" resultType="com.bonus.core.account.v3.web.vo.AccInfoVO">
        <include refid="base_acc_infoVO_sql">
        </include>
        WHERE ai.cust_id in
        <foreach collection="custIdList" item="custId" separator="," open="(" close=")">
            #{custId}
        </foreach>
    </select>
    <!--    查询存在冻结余额的账户列表-->
    <select id="queryFrozenAccPage" resultType="com.bonus.core.account.v3.web.vo.AccExistFrozenListVO">
        SELECT  ai.cust_id,
        t2.cust_num,
        t2.cust_name,
        t2.mobile,
        t2.org_full_name,
        t2.psn_type,
        cpt.psn_type_name
        FROM acc_info ai
        INNER JOIN cust_info t2 ON t2.cust_id = ai.cust_id
        LEFT JOIN cust_psn_type cpt ON cpt.psn_type = t2.psn_type
        <where>
            <if test="infoParam.custState != null">
                t2.cust_state = #{infoParam.custState}
            </if>
            <if test="infoParam.accStatus != null">
                AND ai.acc_status = #{infoParam.accStatus}
            </if>
            <if test="infoParam.psnType != null">
                AND t2.psn_type = #{infoParam.psnType}
            </if>
            <if test="infoParam.orgIdList != null and infoParam.orgIdList.size() > 0">
                and t2.org_id in
                <foreach collection="infoParam.orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="infoParam.keyword != null and infoParam.keyword != ''">
                <include refid="cust_fuzzy_query"></include>
            </if>
            AND EXISTS(
            SELECT awi.wallet_id
            FROM acc_wallet_info awi
            WHERE awi.acc_id = ai.acc_id
            AND awi.frozen_balance <![CDATA[>]]> 0
            )
        </where>
        ORDER BY t2.cust_num
    </select>

    <select id="getSumScopeByAccIdList" resultType="java.lang.Long">
        SELECT SUM(scope)
        FROM acc_info
        WHERE
        acc_id in
        <foreach collection="accIdList" item="accId" separator="," open="(" close=")">
            #{accId}
        </foreach>
    </select>

    <select id="getAccInfoDetailVOV2" resultType="com.bonus.core.account.v3.api.vo.AccInfoDetailVO">
        SELECT
        a.cust_id,
        SUM(a.wallet_bal) accBalTotal,
        MIN(t2.cust_name) AS custName,
        MIN(t1.acc_id) AS accId
        FROM
        acc_wallet_info a
        INNER JOIN acc_info t1 ON a.cust_id = t1.cust_id
        INNER JOIN cust_info t2 ON a.cust_id = t2.cust_id
        WHERE
        t1.acc_status = 1
        AND t2.cust_state = 1
        GROUP BY a.cust_id
        ORDER BY a.cust_id
    </select>


    <!--   账户批量操作查询账户信息 -->
    <select id="queryPageAccInfoForBatch" resultType="com.bonus.core.account.v3.web.vo.AccInfoPageVO">
        SELECT  t1.acc_id,
        t2.cust_id,
        t2.cust_num,
        t2.cust_name,
        t2.mobile,
        t2.org_full_name,
        t2.psn_type,
        t7.psn_type_name,
        t1.acc_status,
        t1.end_date
        FROM cust_info t2
        INNER JOIN acc_info t1 ON t1.cust_id = t2.cust_id
        LEFT JOIN cust_psn_type t7 on t7.psn_type = t2.psn_type
        <include refid="queryPageAccInfoWhereSql">
        </include>
        order by t2.id desc
    </select>
    <!--    通过人员id查询账户信息-->
    <select id="getAccInfoOpenPOByCustId" resultType="com.bonus.core.account.v3.po.AccInfoOpenPO">
        <include refid="query_accInfo_open_sql"></include>
        WHERE t1.cust_id = #{custId}
    </select>
    <!--    通过人员id集合查询账户信息-->
    <select id="queryPageAccInfoOpenPO" resultType="com.bonus.core.account.v3.po.AccInfoOpenPO">
        <include refid="query_accInfo_open_sql"></include>
        <where>
            <if test="custIdList != null and custIdList.size() > 0">
                and t1.cust_id in
                <foreach collection="custIdList" item="custId" separator="," open="(" close=")">
                    #{custId}
                </foreach>
            </if>
        </where>
    </select>

    <!--    查询账户信息-open-->
    <sql id="query_accInfo_open_sql">
        SELECT t1.acc_id,
        t2.cust_name AS accName,
        t1.cust_id,
        t1.acc_status,
        t2.cust_third_id,
        t3.org_id,
        t3.org_name
        FROM acc_info AS t1
        LEFT JOIN cust_info AS t2 ON t1.cust_id = t2.cust_id
        LEFT JOIN cust_org AS t3 ON t2.org_id = t3.org_id
    </sql>

    <!--    获取账户状态和卡片信息-->
    <select id="queryAccStatusAndCard" resultType="com.bonus.core.account.v3.web.vo.AccInfoVO">
        SELECT t1.cust_id,
        t1.acc_status
        WHERE t1.cust_id in
        <foreach collection="custIdList" item="custId" separator="," open="(" close=")">
            #{custId}
        </foreach>
    </select>
    <!--    通过用户Id集合获取账户状态,用户基本信息-->
    <select id="queryAccBaseInfoList" resultType="com.bonus.core.account.v3.web.vo.AccInfoVO">
        SELECT
        t1.cust_id,
        t1.acc_status,
        t2.cust_num,
        t2.cust_name,
        t2.mobile,
        t3.psn_type_name,
        t4.org_full_name
        FROM
        acc_info t1
        LEFT JOIN cust_info AS t2 ON t2.cust_id = t1.cust_id
        LEFT JOIN cust_psn_type t3 ON t3.psn_type = t2.psn_type
        LEFT JOIN cust_org t4 ON t4.org_id = t2.org_id
        WHERE t1.cust_id in
        <foreach collection="custIdList" item="custId" separator="," open="(" close=")">
            #{custId}
        </foreach>
    </select>


    <select id="listAccStateOverdue" resultType="java.lang.Long">
        SELECT cust_id
        FROM acc_info
        WHERE end_date <![CDATA[<]]> #{overdueDate}
        AND acc_status in
        <foreach collection="oldStatusList" item="accStatus" separator="," open="(" close=")">
            #{accStatus}
        </foreach>
    </select>

    <select id="accOpenCountStatistics" resultType="com.bonus.core.account.v3.api.vo.AccCountVO">
        SELECT A.accNums,B.depositTotal from (
        SELECT count(1) as accNums  FROM `acc_info` WHERE crtime BETWEEN #{accCountParam.startTime} and  #{accCountParam.endTime} ) A,
        (SELECT  SUM(deposit) as depositTotal FROM `acc_card` WHERE  card_status in (1, 4)  and crtime BETWEEN #{accCountParam.startTime} and  #{accCountParam.endTime} ) B
    </select>

    <!--    销户统计账户数和押金总额-->
    <select id="accCloseCountStatistics" resultType="com.bonus.core.account.v3.api.vo.AccCountVO">
        SELECT count(a.id) as accNums ,sum(b.deposit) as depositTotal from acc_info a
        JOIN acc_card_hist b on  a.cust_id=b.cust_id
        WHERE a.acc_status=3  and b.record_type=2 and a.uptime BETWEEN #{accCountParam.startTime} and  #{accCountParam.endTime}
    </select>

    <!--    查询账户数统计-->
    <select id="accCountStatistics" resultType="com.bonus.core.account.v3.api.vo.AccCountStatisticsVO">
        SELECT a.*,b.*,c.*,d.*
        FROM
        (
        SELECT
        ifnull(SUM( CASE WHEN acc_status = 1 THEN 1 ELSE 0 END ),0) AS normalCount,
        ifnull(SUM( CASE WHEN acc_status = 2 THEN 1 ELSE 0 END ),0) AS deactivateCount,
        ifnull(SUM( CASE WHEN acc_status = 4 THEN 1 ELSE 0 END ),0) AS expireCount
        FROM acc_info ai
        LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.uptime <![CDATA[<=]]> #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) a,
        (   SELECT count(*) AS startCount
        FROM acc_info ai
        LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.crtime <![CDATA[<=]]> #{param.startTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) b,
        (   SELECT count(*) AS closedCount
        FROM acc_info ai
        LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.acc_status = 3 and ai.uptime BETWEEN #{param.startTime} AND #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) c,
        (   SELECT count(*) AS openCount
        FROM acc_info ai
        LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.crtime BETWEEN #{param.startTime} AND #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) d
    </select>

    <!--    查询账户数统计-按照组织分组-->
    <select id="accCountStatisticsV2" resultType="com.bonus.core.account.v3.api.vo.AccCountStatisticsVO">
        SELECT
        g.org_id,
        IFNULL(t2.startCount,0) AS startCount,
        IFNULL(t1.endCount,0) AS endCount,
        IFNULL(t1.normalCount,0) AS normalCount,
        IFNULL(t1.deactivateCount,0) AS deactivateCount,
        IFNULL(t1.expireCount,0) AS expireCount,
        IFNULL(t3.openCount,0) AS openCount,
        IFNULL(t4.closedCount,0) AS closedCount
        FROM
        cust_org g
        LEFT JOIN (
        SELECT
        ci.org_id,
        count(*) AS endCount,
        IFNULL( SUM( CASE WHEN acc_status = 1 THEN 1 ELSE 0 END ), 0 ) AS normalCount,
        IFNULL( SUM( CASE WHEN acc_status = 2 THEN 1 ELSE 0 END ), 0 ) AS deactivateCount,
        IFNULL( SUM( CASE WHEN acc_status = 4 THEN 1 ELSE 0 END ), 0 ) AS expireCount
        FROM
        acc_info ai
        INNER JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.uptime <![CDATA[<=]]> #{param.endTime}
        AND ai.acc_status IN (1,2,4)
        GROUP BY ci.org_id
        ) t1 ON t1.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS startCount
        FROM acc_info ai
        INNER JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.crtime <![CDATA[<=]]> #{param.startTime}
        GROUP BY ci.org_id
        ) t2 ON t2.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS openCount
        FROM acc_info ai
        INNER JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.crtime BETWEEN #{param.startTime} AND #{param.endTime}
        GROUP BY ci.org_id
        ) t3 ON t3.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS closedCount
        FROM acc_info ai
        INNER JOIN cust_info ci ON ci.cust_id = ai.cust_id
        WHERE ai.acc_status = 3
        and ai.uptime BETWEEN #{param.startTime} AND #{param.endTime}
        GROUP BY ci.org_id
        ) t4 ON t4.org_id = g.org_id
    </select>

    <!--    查询账户卡片统计-->
    <select id="accCardStatistics" resultType="com.bonus.core.account.v3.api.vo.AccCardStatisticsVO">
        SELECT a.*,b.*,c.*,d.*
        FROM
        (
        SELECT
        ifnull(SUM( CASE WHEN card_status = 1 THEN 1 ELSE 0 END ),0) AS normalCount,
        ifnull(SUM( CASE WHEN card_status = 4 THEN 1 ELSE 0 END ),0) AS lossCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.uptime <![CDATA[<=]]> #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) a,
        (   SELECT count(*) AS startCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.crtime <![CDATA[<=]]> #{param.startTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) b,
        (   SELECT count(*) AS intervalLossCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.card_status = 4 and ac.uptime BETWEEN #{param.startTime} AND #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) c,
        (   SELECT count(*) AS intervalOpenCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.crtime BETWEEN #{param.startTime} AND #{param.endTime}
        <include refid="query_acc_org_auth_sql"></include>
        ) d
    </select>

    <!--    查询账户卡片统计-按照组织分组-->
    <select id="accCardStatisticsV2" resultType="com.bonus.core.account.v3.api.vo.AccCardStatisticsVO">
        SELECT
        g.org_id,
        IFNULL(t2.startCount,0) AS startCount,
        IFNULL(t1.endCount,0) AS endCount,
        IFNULL(t1.normalCount,0) AS normalCount,
        IFNULL(t1.lossCount,0) AS lossCount,
        IFNULL(t3.intervalLossCount,0) AS intervalLossCount,
        IFNULL(t4.intervalOpenCount,0) AS intervalOpenCount
        FROM
        cust_org g
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS endCount,
        IFNULL(SUM( CASE WHEN card_status = 1 THEN 1 ELSE 0 END ),0) AS normalCount,
        IFNULL(SUM( CASE WHEN card_status = 4 THEN 1 ELSE 0 END ),0) AS lossCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.card_status IN (1,4) AND ac.uptime <![CDATA[<=]]> #{param.endTime}
        GROUP BY ci.org_id
        ) t1 ON t1.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS startCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.crtime  <![CDATA[<=]]> #{param.startTime}
        GROUP BY ci.org_id
        ) t2 ON t2.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS intervalLossCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.card_status = 4 and ac.uptime BETWEEN #{param.startTime} AND #{param.endTime}
        GROUP BY ci.org_id
        ) t3 ON t3.org_id = g.org_id
        LEFT JOIN
        (
        SELECT
        ci.org_id,
        count(*) AS intervalOpenCount
        FROM acc_card ac
        LEFT JOIN cust_info ci ON ci.cust_id = ac.cust_id
        WHERE ac.crtime BETWEEN #{param.startTime} AND #{param.endTime}
        GROUP BY ci.org_id
        ) t4 ON t4.org_id = g.org_id
    </select>


    <!--    查询账户统计-组织权限sql-->
    <sql id="query_acc_org_auth_sql">
        <if test="'2'.toString() == authPO.roleType.toString()">
            and EXISTS (
            SELECT null
            FROM mgr_role_org it1
            LEFT JOIN mgr_role it2 ON it2.role_id = it1.role_id
            LEFT JOIN mgr_user_role it3 ON it1.role_id = it3.role_id
            WHERE it2.del_flag = 2
            AND it2.role_code = #{authPO.roleCode}
            AND it1.half_select = 1
            AND it3.user_id = #{authPO.userId}
            AND it1.org_id = ci.org_id
            )
        </if>
    </sql>

    <!--    移动端工作台：获取账户列表-->
    <select id="queryPageAccInfoForAppWork" resultType="com.bonus.core.account.v3.app.vo.AppWorkAccInfoVO">
        SELECT
        t2.cust_id,
        t2.cust_num,
        t2.cust_name,
        t2.mobile,
        t2.org_full_name,
        t2.psn_type,
        t5.psn_type_name,
        t1.acc_status,
        t1.end_date,
        t4.accAvailableBal,
        t4.accFreezeBalTotal,
        t4.accAvailableBal + t4.accFreezeBalTotal as accBalTotal,
        t4.walletBal,
        t4.frozenWallet,
        t4.subsidyBal,
        t4.frozenSub,
        t4.redEnvelope
        FROM acc_info t1
        LEFT JOIN cust_info t2 ON t1.cust_id = t2.cust_id
        LEFT JOIN
        (
        SELECT
        cust_id,
        SUM( CASE WHEN wallet_id = 1 THEN wallet_bal ELSE 0 END ) AS walletBal,
        SUM( CASE WHEN wallet_id = 1 THEN frozen_balance ELSE 0 END ) AS frozenWallet,
        SUM( CASE WHEN wallet_id = 2 THEN wallet_bal ELSE 0 END ) AS subsidyBal,
        SUM( CASE WHEN wallet_id = 2 THEN frozen_balance ELSE 0 END ) AS frozenSub,
        SUM( CASE WHEN wallet_id = 4 THEN wallet_bal ELSE 0 END ) AS redEnvelope,
        SUM( a.wallet_bal ) AS accAvailableBal,
        SUM( a.frozen_balance ) AS accFreezeBalTotal
        FROM  acc_wallet_info a
        GROUP BY cust_id
        ) AS t4 ON t4.cust_id = t1.cust_id
        LEFT JOIN cust_psn_type t5 on t5.psn_type = t2.psn_type
        <where>
            t1.acc_status in
            <foreach collection="infoParam.accStatusList" item="accStatus" separator="," open="(" close=")">
                #{accStatus}
            </foreach>
            <if test="infoParam.psnTypeList != null and infoParam.psnTypeList.size() > 0">
                and t2.psn_type in
                <foreach collection="infoParam.psnTypeList" item="psnType" separator="," open="(" close=")">
                    #{psnType}
                </foreach>
            </if>
            <if test="infoParam.orgIdList != null and infoParam.orgIdList.size() > 0">
                and t2.org_id in
                <foreach collection="infoParam.orgIdList" item="orgId" separator="," open="(" close=")">
                    #{orgId}
                </foreach>
            </if>
            <if test="infoParam.keyword != null and infoParam.keyword != ''">
                <include refid="cust_fuzzy_query"></include>
            </if>
        </where>
        ORDER BY t2.id desc

    </select>

    <!--    查询对应时间内账户数量-->
    <select id="queryAccInfoExpiredCount" resultType="java.lang.Integer">
        SELECT count( a.id ) FROM acc_info a
        WHERE a.acc_status = #{accStatus} AND a.uptime BETWEEN #{startDateTime} and  #{endDateTime}
    </select>



    <!--    关联用户模糊查询-->
    <sql id="cust_fuzzy_query">
        and (t2.cust_num LIKE CONCAT('%', #{infoParam.keyword},'%')
        or t2.cust_name = #{infoParam.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
        or t2.mobile = #{infoParam.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
        or t2.mobile_suffix = #{infoParam.keyword,typeHandler=net.xnzn.core.common.encrypt.SM4EncDecHandler}
        or t2.cust_name_like LIKE CONCAT('%',#{infoParam.keyword,typeHandler=net.xnzn.core.common.encrypt.CipherQueryLikeHandler},'%')
        )
    </sql>




    <!--    查询账户信息-->
    <!--    查询账户信息-->
    <select id="queryAccStatusByCustIdList" resultType="com.bonus.core.account.v3.mq.send.model.AccStatusChangeModel">
        SELECT cust_name,
        cust_id
        FROM cust_info
        WHERE cust_id in
        <foreach collection="custIdList" item="custId" separator="," open="(" close=")">
            #{custId}
        </foreach>
    </select>


    <select id="getAccInfoVOByCustId" resultType="com.bonus.core.account.v3.web.vo.AccInfoVO">
        <include refid="base_acc_infoVO_sql">
        </include>
        WHERE ai.cust_id = #{custId}
    </select>

    <sql id="base_acc_infoVO_sql">
        SELECT ai.id,
        ai.acc_id,
        ai.cust_id,
        ai.end_date,
        ai.acc_status,
        ci.cust_num,
        ci.cust_name,
        ci.mobile,
        ci.psn_type,
        cpt.psn_type_name,
        ci.org_id,
        co.org_full_name,
        ci.cust_third_id
        FROM acc_info ai
        LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id
        LEFT JOIN cust_psn_type cpt ON cpt.psn_type = ci.psn_type
        LEFT JOIN cust_org co ON co.org_id = ci.org_id
    </sql>



<!--    <select id="getAccInfoVOByCustId" resultType="com.bonus.core.account.v3.web.vo.AccInfoVO">-->
<!--        SELECT ai.id,-->
<!--               ai.acc_id,-->
<!--               ai.cust_id,-->
<!--               ai.end_date,-->
<!--               ai.acc_status,-->
<!--               ci.cust_num,-->
<!--               ci.cust_name,-->
<!--               ci.mobile,-->
<!--               ci.psn_type,-->
<!--               cpt.psn_type_name,-->
<!--               ci.org_id,-->
<!--               co.org_full_name,-->
<!--               ci.cust_third_id-->
<!--          FROM acc_info ai-->
<!--                   LEFT JOIN cust_info ci ON ci.cust_id = ai.cust_id-->
<!--                   LEFT JOIN cust_psn_type cpt ON cpt.psn_type = ci.psn_type-->
<!--                   LEFT JOIN cust_org co ON co.org_id = ci.org_id-->
<!--        WHERE ai.cust_id = #{custId}-->
<!--    </select>-->

<!--    &lt;!&ndash;    查询账户信息&ndash;&gt;-->
<!--    <select id="queryAccStatusByCustIdList" resultType="com.bonus.core.account.v3.mq.send.model.AccStatusChangeModel">-->
<!--        SELECT cust_name,-->
<!--        cust_id-->
<!--        FROM cust_info-->
<!--        WHERE cust_id in-->
<!--        <foreach collection="custIdList" item="custId" separator="," open="(" close=")">-->
<!--            #{custId}-->
<!--        </foreach>-->
<!--    </select>-->
</mapper>
