<?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.yiqi.recharge.mapper.RechargePasswordCardMapper">

    <resultMap type="RechargePasswordCard" id="RechargePasswordCardResult">
        <result property="id" column="id"/>
        <result property="batchNo" column="batch_no"/>
        <result property="cardNo" column="card_no"/>
        <result property="password" column="password"/>
        <result property="useLevel" column="use_level"/>
        <result property="cardVersionId" column="card_version_id"/>
        <result property="rechargeAmount" column="recharge_amount"/>
        <result property="giveAmount" column="give_amount"/>
        <result property="rechargeBalance" column="recharge_balance"/>
        <result property="giveBalance" column="give_balance"/>
        <result property="rechargeStatus" column="recharge_status"/>
        <result property="rechargeTime" column="recharge_time"/>
        <result property="rechargeStoreId" column="recharge_store_id"/>
        <result property="rechargeUserId" column="recharge_user_id"/>
        <result property="expireTime" column="expire_time"/>
        <result property="orderNo" column="order_no"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectRechargePasswordCardVo">
        select * from recharge_password_card
    </sql>


    <sql id="selectRechargePasswordCardQueryVo">
        select c.*, v.title as card_version_title,v.code as card_version_code,
               u.real_name as rechargeUserName, s.name as rechargeStoreName
        from recharge_password_card as c
            left join recharge_card_version as v on v.id=c.card_version_id
            LEFT JOIN app_user u on c.recharge_user_id = u.id
            left join sys_store as s on s.id=c.recharge_store_id
    </sql>

    <select id="selectUserRechargePasswordCardList" resultType="com.yiqi.recharge.domain.vo.RechargePasswordCardQueryVO">
        <include refid="selectRechargePasswordCardQueryVo"/>
        <where>
            and c.del_flag='0'  and c.recharge_user_id=#{appUserId}
        </where>
        order by c.recharge_time desc
        limit #{pageVO.startRow},#{pageVO.pageSize}
    </select>

    <select id="countRechargePasswordCardList" resultType="java.lang.Integer">
        select count(1)
        from recharge_password_card as c
        left join recharge_card_version as v on v.id=c.card_version_id
        left join sys_store as s on s.id=c.recharge_store_id
        LEFT JOIN app_user u on c.recharge_user_id = u.id
        <where>
            and c.del_flag='0' and c.recharge_status = '1' and c.recharge_user_id=#{appUserId}
        </where>
    </select>

    <select id="selectRechargePasswordCardList" parameterType="com.yiqi.recharge.domain.dto.RechargePasswordCardQueryDTO" resultType="com.yiqi.recharge.domain.vo.RechargePasswordCardQueryVO">
        <include refid="selectRechargePasswordCardQueryVo"/>
        <where>
            and c.del_flag='0' and c.card_type = '0'
            <if test="cardNo != null  and cardNo != ''">
                and c.card_no like concat('%', #{cardNo}, '%')
            </if>
            <if test="rechargeUserId != null">
                and c.recharge_user_id = #{rechargeUserId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and c.order_no like concat('%', #{orderNo}, '%')
            </if>
            <if test="batchNo != null and batchNo != ''">
                and c.batch_no = #{batchNo}
            </if>
            <if test="rechargeStatus != null  and rechargeStatus != ''">
                and c.recharge_status = #{rechargeStatus}
            </if>
            <if test="beginGiveActiveTime != null and beginGiveActiveTime != ''"><!-- 开始时间检索 -->
                and date_format(c.give_active_time,'%y%m%d') &gt;= date_format(#{beginGiveActiveTime},'%y%m%d')
            </if>
            <if test="endGiveActiveTime != null and endGiveActiveTime != ''"><!-- 结束时间检索 -->
                and date_format(c.give_active_time,'%y%m%d') &lt;= date_format(#{endGiveActiveTime},'%y%m%d')
            </if>
            <if test="beginRechargeTime != null and beginRechargeTime != ''"><!-- 开始时间检索 -->
                and date_format(c.recharge_time,'%y%m%d') &gt;= date_format(#{beginRechargeTime},'%y%m%d')
            </if>
            <if test="endRechargeTime != null and endRechargeTime != ''"><!-- 结束时间检索 -->
                and date_format(c.recharge_time,'%y%m%d') &lt;= date_format(#{endRechargeTime},'%y%m%d')
            </if>
            <if test="beginExpireTime != null and beginExpireTime != ''"><!-- 开始时间检索 -->
                and date_format(c.expire_time,'%y%m%d') &gt;= date_format(#{beginExpireTime},'%y%m%d')
            </if>
            <if test="endExpireTime != null and endExpireTime != ''"><!-- 结束时间检索 -->
                and date_format(c.expire_time,'%y%m%d') &lt;= date_format(#{endExpireTime},'%y%m%d')
            </if>
            <if test="beginCreateTime != null and beginCreateTime != ''"><!-- 开始时间检索 -->
                and date_format(c.create_time,'%y%m%d') &gt;= date_format(#{beginCreateTime},'%y%m%d')
            </if>
            <if test="endCreateTime != null and endCreateTime != ''"><!-- 结束时间检索 -->
                and date_format(c.create_time,'%y%m%d') &lt;= date_format(#{endCreateTime},'%y%m%d')
            </if>
        </where>
        order by c.create_time desc
    </select>

    <select id="selectRechargePasswordCardById" parameterType="Long"
            resultMap="RechargePasswordCardResult">
        <include refid="selectRechargePasswordCardVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="selectTotalBalanceGroupByAppUser" resultType="com.yiqi.recharge.domain.RechargePasswordCard">
        select sum(recharge_balance) as rechargeBalance,
        sum(give_balance) as giveBalance,
        sum(welfare_balance) as welfareBalance
        from recharge_password_card
        where recharge_user_id = #{appUserId} and recharge_status = '1'
        and (expire_time &gt;= now() or expire_time IS NULL)
        and del_flag='0'
        <if test="orderType != null and orderType != ''">
            <if test="orderType == '0'.toString()">
                and is_support_cloth='Y'
            </if>
            <if test="orderType == '1'.toString()">
                and is_support_goods='Y'
            </if>
            <if test="orderType == '2'.toString()">
                and is_support_life='Y'
            </if>
            <if test="orderType == '3'.toString()">
                and is_support_car='Y'
            </if>
        </if>
    </select>
    <select id="getPasswordCardsInfoByAppUserId"  resultType="com.yiqi.recharge.domain.vo.weapp.WeAppCardsInfoVO">
        select a.id as id, a.recharge_store_id as rechargeStoreId, c.name as rechargeStoreName,
               a.expire_time as expireTime, a.recharge_amount as rechargeAmount, a.recharge_balance as rechargeBalance,
               a.give_balance as giveBalance
            from recharge_password_card a
            left join app_user b on a.recharge_user_id = b.id
            left join sys_store c on a.recharge_store_id = c.id
        where  a.recharge_user_id = #{appUserId}
            and a.del_flag='0'
        ORDER BY a.create_time DESC
            limit #{startRow},#{pageSize}
    </select>
    <select id="getUseRecordsByCardId" resultType="com.yiqi.recharge.domain.vo.weapp.WeAppCardsUseRecordsVO">
        select a.recharge_store_id as rechargeStoreId, c.name as rechargeStoreName,
               a.give_active_time as giveActiveTime, a.recharge_time as rechargeTime,
               a.expire_time as expireTime, a.recharge_amount as rechargeAmount, a.recharge_balance as rechargeBalance,
               a.give_amount as giveAmount,a.give_balance as giveBalance
        from recharge_password_card a
                 left join app_user b on a.recharge_user_id = b.id
                 left join sys_store c on a.recharge_store_id = c.id
        where  a.id = #{rechargePasswordCardId}
    </select>
    <select id="findUserPageByBalance" resultType="com.yiqi.recharge.domain.vo.RechargeBalanceCountVO">
        select a.user_id as userId, b.real_name as userName, a.create_time as createTime, b.phone_number as phoneNumber
        from app_org_user a
        left join app_user b on a.user_id = b.id
        where a.org_id = #{orgId} and a.source_type = #{sourceType} and b.status = '0'
        <if test="rechargeBalanceCountDTO.phoneNumber != null">
            and b.phone_number = #{rechargeBalanceCountDTO.phoneNumber}
        </if>
        <if test="rechargeBalanceCountDTO.userName != null">
            and b.real_name = #{rechargBalanceCountDTO.userName}
        </if>
        order by a.create_time desc
        limit #{rechargeBalanceCountDTO.startRow},#{rechargeBalanceCountDTO.pageSize}
    </select>
    <select id="findUserPageByBalanceCount" resultType="java.lang.Integer">
        select count(1)
        from app_org_user a
        left join app_user b on a.user_id = b.id
        where a.org_id = #{orgId} and a.source_type = #{sourceType} and b.status = '0'
        <if test="rechargeBalanceCountDTO.phoneNumber != null">
            and b.phone_number = #{rechargeBalanceCountDTO.phoneNumber}
        </if>
        <if test="rechargeBalanceCountDTO.userName != null">
            and b.real_name = #{rechargBalanceCountDTO.userName}
        </if>
    </select>
    <select id="getBalanceCountListByUserIdList"
            resultType="com.yiqi.recharge.domain.vo.RechargeBalanceCountVO">
        select recharge_user_id as userId, sum(recharge_balance) as rechargeBalance,
        sum(give_balance) as giveBalance, sum(welfare_balance) as welfareBalance       from recharge_password_card
        where del_flag='0' and recharge_status = '1'
        and (expire_time &gt;= #{expireDate} or expire_time IS NULL)
        and (recharge_balance > 0 or give_balance > 0 or welfare_balance > 0)
        <if test="userIdList != null and userIdList.size > 0">
            and recharge_user_id in
            <foreach collection="userIdList" item="userIdList" open="(" separator="," close=")">
                #{userIdList}
            </foreach>
        </if>
        group by recharge_user_id

    </select>
    <select id="getCardBillMstrInfoById" resultType="com.yiqi.recharge.domain.vo.RechargeCardBillMstrQueryVO">
        select aubm.id, aubm.title, aubm.content, aubm.order_id as orderId, aubm.order_no as orderNo,
        aubm.order_type as orderType, aubm.pay_type as payType,
        aubm.app_user_id  as appUserId, aubm. bill_type as billType, aubm.pay_total_amount  as payTotalAmount, aubm.cash_pay_amount  as cashPayAmount,
        aubm.recharge_pay_amount  as rechargePayAmount, aubm.give_pay_amount  as givePayAmount, aubm.welfare_pay_amount  as welfarePayAmount, aubm.receive_point  as receivePoint,
        aubm.use_point  as usePoint, aubm.is_refund  as isRefund, aubm.create_time  as createTime, aubm.recharge_card_id  as rechargeCardId,
        aubm.create_time as createTime,
        au.real_name  as userName, ss.name  as orgName
        from app_user_bill_mstr aubm
        left join app_user au on aubm.app_user_id = au.id
        left join sys_store ss on aubm.org_id = ss.id and aubm.source_type = '02'
        where aubm.recharge_card_id = #{rechargeCardId}
        <if test="orgId != null  and orgId != ''">
            and aubm.org_id = #{orgId}  and aubm.source_type = '02'
        </if>
        order by aubm.create_time desc
    </select>
    <select id="getBillMstrInfoList" resultType="com.yiqi.recharge.domain.vo.RechargeCardBillMstrQueryVO">
        select aubm.id, aubm.title, aubm.content, aubm.order_id as orderId, aubm.order_no as orderNo,
        aubm.order_type as orderType, aubm.pay_type as payType, aubm.org_id as orgId, aubm.source_type as sourceType,
        aubm.app_user_id  as appUserId, aubm. bill_type as billType, aubm.pay_total_amount  as payTotalAmount, aubm.cash_pay_amount  as cashPayAmount,
        aubm.recharge_pay_amount  as rechargePayAmount, aubm.give_pay_amount  as givePayAmount, aubm.welfare_pay_amount  as welfarePayAmount, aubm.receive_point  as receivePoint,
        aubm.use_point  as usePoint, aubm.is_refund  as isRefund, aubm.create_time  as createTime, aubm.recharge_card_id  as rechargeCardId,
        aubm.create_time as createTime,
        au.real_name  as userName, au.phone_number as phoneNumber, ss.name  as orgName
        from app_user_bill_mstr aubm
        left join app_user au on aubm.app_user_id = au.id
        left join sys_store ss on aubm.org_id = ss.id
        where 1=1
        <if test="orgId != null  and orgId != ''">
            and aubm.org_id = #{orgId}  and aubm.source_type = #{sourceType}
        </if>
        <if test="billMstrQueryDTO.beginCreateTime != null and billMstrQueryDTO.beginCreateTime != ''"><!-- 开始时间检索 -->
            and date_format(aubm.create_time,'%y%m%d') &gt;= date_format(#{billMstrQueryDTO.beginCreateTime},'%y%m%d')
        </if>
        <if test="billMstrQueryDTO.endCreateTime != null and billMstrQueryDTO.endCreateTime != ''"><!-- 结束时间检索 -->
            and date_format(aubm.create_time,'%y%m%d') &lt;= date_format(#{billMstrQueryDTO.endCreateTime},'%y%m%d')
        </if>
        order by aubm.create_time desc
    </select>
    <select id="countBillMstrInfoList" resultType="java.lang.Integer">
        select count(*)
        from app_user_bill_mstr aubm
        left join app_user au on aubm.app_user_id = au.id
        left join sys_store ss on aubm.org_id = ss.id
        where 1=1
        <if test="orgId != null  and orgId != ''">
            and aubm.org_id = #{orgId}  and aubm.source_type = #{sourceType}
        </if>
        <if test="billMstrQueryDTO.beginCreateTime != null and billMstrQueryDTO.beginCreateTime != ''"><!-- 开始时间检索 -->
            and date_format(aubm.create_time,'%y%m%d') &gt;= date_format(#{billMstrQueryDTO.beginCreateTime},'%y%m%d')
        </if>
        <if test="billMstrQueryDTO.endCreateTime != null and billMstrQueryDTO.endCreateTime != ''"><!-- 结束时间检索 -->
            and date_format(aubm.create_time,'%y%m%d') &lt;= date_format(#{billMstrQueryDTO.endCreateTime},'%y%m%d')
        </if>
    </select>

    <select id="selectRechargePasswordCardByOrderNo"
            resultType="com.yiqi.recharge.domain.RechargePasswordCard">
        select * from recharge_password_card where order_no = #{orderNo}
    </select>

    <select id="getCountyByAppUserId" resultType="java.lang.Long" parameterType="java.lang.Long">
        select count(*) from recharge_password_card where recharge_user_id = #{appUserId} and recharge_status !=-2 and del_flag = 0
    </select>

    <!--购买商品类型 1:洗衣 2：普通商品 3：生活服务 4：汽车美容-->
    <select id="selectRechargePasswordCardListByAppUserIdAndBuyGoodsType"
            resultType="com.yiqi.recharge.domain.RechargePasswordCard">
        select * from recharge_password_card where recharge_user_id = #{appUserId} and recharge_status = 1 and del_flag = 0
        <if test="buyGoodsType != null and buyGoodsType == '0'">
            and is_support_cloth = 'Y'
        </if>
        <if test="buyGoodsType != null and buyGoodsType == '1'">
            and is_support_goods = 'Y'
        </if>
        <if test="buyGoodsType != null and buyGoodsType == '2'">
            and is_support_life = 'Y'
        </if>
        <if test="buyGoodsType != null and buyGoodsType == '3'">
            and is_support_car = 'Y'
        </if>
        order by recharge_time desc
    </select>

    <select id="selectRechargePasswordCardBySn" resultType="com.yiqi.recharge.domain.RechargePasswordCard"
            parameterType="java.lang.String">
        select * from recharge_password_card where card_no = #{cardSn}
    </select>

    <delete id="deleteRechargePasswordCardById" parameterType="Long">
        update recharge_password_card set del_flag='2' where id = #{id}
    </delete>

    <delete id="deleteRechargePasswordCardByIds" parameterType="String">
        update recharge_password_card set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>