<?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.willalp.integral.mapper.IntegralMapper">

    <sql id="columns">
        id, money_type, recharge_equipment_code, recharge_equipment_name, login_name, nick_name, card_number, card_name, recharge_time, recharge_integral, order_number, is_delete, organization_code, recharge_id, update_time
    </sql>

    <sql id="where">
        <where>
            <if test="@sqlt.Ognl@isNotEmpty(moneyType)">
                and money_type = #{moneyType}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(rechargeEquipmentCode)">
                and recharge_equipment_code = #{rechargeEquipmentCode}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(rechargeEquipmentName)">
                and recharge_equipment_name like concat('%', #{rechargeEquipmentName}, '%')
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(loginName)">
                and login_name like concat('%', #{loginName}, '%')
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(nickName)">
                and nick_name like concat('%', #{nickName}, '%')
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(cardNumber)">
                and card_number = #{cardNumber}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(cardName)">
                and card_name like concat('%', #{cardName}, '%')
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(rechargeTime)">
                and recharge_time = #{rechargeTime}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(rechargeIntegral)">
                and recharge_integral = #{rechargeIntegral}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(isDelete)">
                and is_delete = #{isDelete}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(organizationCode)">
                and organization_code = #{organizationCode}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(rechargeId)">
                and recharge_id = #{rechargeId}
            </if>
            <if test="@sqlt.Ognl@isNotEmpty(organizations)">
                and organization_code in
                <foreach collection="organizations" item="z" open="(" close=")">
                    #{z}
                </foreach>
            </if>
        </where>
    </sql>

    <select id="selectIntegralList" resultType="com.willalp.integral.domain.Integral">
        select a.id as id,
        a.money_type as moneyType,
        a.recharge_equipment_code as rechargeEquipmentCode,
        a.recharge_equipment_name as rechargeEquipmentName,
        a.login_name as loginName,
        a.nick_name as nickName,
        a.card_number as cardNumber,
        a.card_name as cardName,
        a.recharge_time as rechargeTime,
        a.recharge_integral as rechargeIntegral,
        a.order_number as orderNumber,
        a.is_delete as isDelete,
        a.organization_code as organizationCode,
        a.recharge_id as rechargeId,
        a.update_time as updateTime,
        b.nick_name as rechargeNickName
        from pns_integral a left join sys_user b on recharge_id = b.user_id
        where 1 = 1
        <if test="@sqlt.Ognl@isNotEmpty(moneyType)">
            and a.money_type = #{moneyType}
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(loginName)">
            and a.login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(nickName)">
            and a.nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(cardNumber)">
            and a.card_number = #{cardNumber}
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(cardName)">
            and a.card_name like concat('%', #{cardName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(organizations)">
            and a.organization_code in
            <foreach collection="organizations" item="z" open="(" close=")">
                #{z}
            </foreach>
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(organizationCode)">
            and a.organization_code = #{organizationCode}
        </if>
        order by a.recharge_time desc
    </select>

    <insert id="saveBatchIntegral">
        INSERT INTO pns_integral (
        id ,
        money_type ,
        recharge_equipment_code ,
        recharge_equipment_name ,
        login_name ,
        nick_name ,
        card_number ,
        card_name ,
        recharge_time ,
        recharge_integral ,
        is_delete ,
        order_number,
        organization_code ,
        recharge_id ,
        update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id} ,
            #{item.moneyType} ,
            #{item.rechargeEquipmentCode} ,
            #{item.rechargeEquipmentName} ,
            #{item.loginName} ,
            #{item.nickName} ,
            #{item.cardNumber} ,
            #{item.cardName} ,
            #{item.rechargeTime} ,
            #{item.rechargeIntegral} ,
            #{item.isDelete} ,
            #{item.orderNumber},
            #{item.organizationCode} ,
            #{item.rechargeId} ,
            #{item.updateTime}
            )
        </foreach>
    </insert>

    <select id="queryIntegralSum" resultType="com.willalp.integral.domain.Integral">
        select login_name as loginName,
        nick_name as nickName,
        card_name as cardName,
        card_number as cardNumber,
        recharge_equipment_code as rechargeEquipmentCode,
        recharge_equipment_name as rechargeEquipmentName,
        sum(recharge_integral) as rechargeIntegral,
        organization_code as organizationCode,
        case money_type when 1 then 1
        else 2 end as moneyType
        from pns_integral
        where is_delete = 0
        <if test="@sqlt.Ognl@isNotEmpty(moneyType) and moneyType == 1">
            and money_type = #{moneyType}
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(moneyType) and moneyType == 2">
            and money_type != 1
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(rechargeEquipmentCode)">
            and recharge_equipment_code = #{rechargeEquipmentCode}
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(rechargeEquipmentName)">
            and recharge_equipment_name like concat('%', #{rechargeEquipmentName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(loginName)">
            and login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(nickName)">
            and nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(cardNumber)">
            and card_number = #{cardNumber}
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(cardName)">
            and card_name like concat('%', #{cardName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(loginNameList)">
            and login_name in
            <foreach collection="loginNameList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(organizationCode)">
            and organization_code = #{organizationCode}
        </if>
        group by login_name,
        nick_name,
        card_name,
        card_number,
        recharge_equipment_code,
        recharge_equipment_name,
        organization_code,
        case money_type when 1 then 1
        else 2 end
        having sum(recharge_integral) > 0
    </select>

    <select id="getIntegralReportList" resultType="java.util.Map">
        SELECT b.nick_name                                                          as list_name,
               b.user_name                                                          as list_number,
               case
                   when a.money_type = 1 then '机构充值'
                   when a.money_type = 2 then '个人充值'
                   when a.money_type = 3 then '记账额度'
                   when a.money_type = 4 then '押金充值'
                   when a.money_type = 5 then '因错退还'
                   when a.money_type = 6 then '余额退还'
                   when a.money_type = 7 then '押金退还'
                   when a.money_type = 8 then '额度调整'
                   end                                                              as list_type,
               date_format(a.recharge_time, '%Y-%m-%d %T')                          as list_time,
               case when a.money_type in (1, 2) then a.recharge_integral else 0 end as list_czjf,
               case when a.money_type in (5, 6) then a.recharge_integral else 0 end as list_tkjf,
               case when a.money_type = 3 then a.recharge_integral else 0 end       as list_czed,
               case when a.money_type = 7 then a.recharge_integral else 0 end       as list_tked,
               case when a.money_type = 4 then a.recharge_integral else 0 end       as list_czyj,
               case when a.money_type = 8 then a.recharge_integral else 0 end       as list_tkyj
        FROM pns_integral a
                 JOIN sys_user b ON a.login_name = b.user_name
        where a.money_type in (1, 2, 3, 4, 5, 6, 7, 8)
          and date(a.recharge_time) >= #{begin}
          and date(a.recharge_time) &lt;= #{end}
          and a.recharge_id = #{recharge_id}
          and a.organization_code = #{organizationCode}
    </select>
    <select id="userBalances" resultType="com.willalp.integral.domain.excelVo.IntegralVo2">
        select suo.login_name,
        su.nick_name,
        su.phonenumber phonenumber,
        suo.integral rechargeIntegral
        from sys_user_organization suo
        left join sys_user su on su.user_name = suo.login_name
        where
        su.del_flag = 0
        and suo.organization_code = 'G00000007'
        <if test="@sqlt.Ognl@isNotEmpty(loginName)">
            and suo.login_name like concat('%', #{loginName}, '%')
        </if>
        <if test="@sqlt.Ognl@isNotEmpty(nickName)">
            and su.nick_name like concat('%', #{nickName}, '%')
        </if>
    </select>
</mapper>
