package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.db.entity.RedPacketInfo;
import com.jeequan.jeepay.service.mapper.RedPacketInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 红包记录 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2024-01-18
 */
@Service
public class RedPacketInfoService extends ServiceImpl<RedPacketInfoMapper, RedPacketInfo>{

    /**
     * 根据门店商户号 支付用户id 获取 正常的红包记录
     * @param mchNo
     * @param storeId
     * @param payUserId
     * @return
     */
    public RedPacketInfo getRedPaketData(String mchNo, String storeId,String payUserId) {
        return baseMapper.getRedPaketData(mchNo,storeId,payUserId);
    }

    public Page<RedPacketInfo> pageList(Page<RedPacketInfo> iPage, LambdaQueryWrapper<RedPacketInfo> wrapper, RedPacketInfo info) {
        wrapper.eq(StringUtils.isNotEmpty(info.getMchNo()),RedPacketInfo::getMchNo,info.getMchNo());
        wrapper.eq(StringUtils.isNotEmpty(info.getType()),RedPacketInfo::getType,info.getType());
        wrapper.eq(StringUtils.isNotEmpty(info.getStoreId()),RedPacketInfo::getStoreId,info.getStoreId());
        wrapper.eq(StringUtils.isNotEmpty(info.getFlowNo()),RedPacketInfo::getFlowNo,info.getFlowNo());
        wrapper.eq(info.getState() != null,RedPacketInfo::getState,info.getState());
        Date[] dateRange = info.buildQueryDateRange();
        if (dateRange[0] != null) {
            wrapper.ge(RedPacketInfo::getUpdatedAt, dateRange[0]);
        }
        if (dateRange[1] != null) {
            wrapper.le(RedPacketInfo::getUpdatedAt, dateRange[1]);
        }
        wrapper.orderByDesc(RedPacketInfo::getUpdatedAt);
        return super.page(iPage,wrapper);
    }

    /**
     * 查询即将过期的红包
     * @return
     */
    public List<RedPacketInfo> listRedPacketByExpire() {
        return baseMapper.listRedPacketByExpire();
    }

    /**
     * 更新红包过期
     * @param flowNo
     * @param expire
     * @param remark
     * @return
     */
    public boolean updateRedExpire(String flowNo, byte expire,String remark) {
        int i = baseMapper.updateRedExpire(flowNo, expire,remark);
        return i > 0 ? true : false;
    }
}
