package com.sckw.payment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sckw.core.common.enums.NumberConstant;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.DateTimeUtil;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.payment.dao.KwpWalletMapper;
import com.sckw.payment.model.KwpWallet;
import com.sckw.redis.constant.RedisConstant;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @author xucaiqin
 * @date 2023-10-07 08:49:45
 */
@Service
@RequiredArgsConstructor
public class KwpWalletService {
    private final KwpWalletMapper kwpWalletMapper;
    private final RedisTemplate<String, Object> redisTemplate;


    /**
     * 更新或新增 相关操作
     *
     * @param kwpWallet
     */
    public void saveOrUpdate(KwpWallet kwpWallet) {
        LambdaQueryWrapper<KwpWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWallet::getUid, kwpWallet.getUid())
                .eq(KwpWallet::getFilter, kwpWallet.getFilter())
                .eq(KwpWallet::getChannel, kwpWallet.getChannel())
                .last("limit 1");
        KwpWallet kwpWalletCheck = kwpWalletMapper.selectOne(wrapper);
        if (Objects.nonNull(kwpWalletCheck)) {
            kwpWalletCheck.setAction(kwpWallet.getAction());
            kwpWalletCheck.setActionTime(kwpWallet.getActionTime());
            kwpWalletCheck.setRemark(kwpWallet.getRemark());
            kwpWalletCheck.setOrderNo(kwpWallet.getOrderNo());
            kwpWalletCheck.setStatus(kwpWallet.getStatus());
            kwpWalletCheck.setUpdateBy(LoginUserHolder.getUserId());
            kwpWalletCheck.setUpdateTime(LocalDateTime.now());
            kwpWalletMapper.updateById(kwpWalletCheck);
            return;
        }
        kwpWallet.setId(new IdWorker(NumberConstant.ONE).nextId());
        kwpWallet.setCreateBy(LoginUserHolder.getUserId());
        kwpWallet.setCreateTime(LocalDateTime.now());
        kwpWallet.setUpdateBy(LoginUserHolder.getUserId());
        kwpWallet.setUpdateTime(LocalDateTime.now());
        kwpWallet.setDelFlag(Global.UN_DELETED);
        kwpWalletMapper.insert(kwpWallet);
    }

    public KwpWallet queryOne(String uid, String filter, String channel) {
        LambdaQueryWrapper<KwpWallet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWallet::getUid, uid)
                .eq(KwpWallet::getFilter, filter)
                .eq(KwpWallet::getChannel, channel)
                .last("limit 1");
        return kwpWalletMapper.selectOne(wrapper);
    }

    /**
     * 记录预付业务的更新时间
     *
     * @param uid
     * @param filter
     * @param channel
     */
    public void saveTime(String uid, String filter, String channel) {
        redisTemplate.opsForValue().set(String.format(RedisConstant.WALLET_TIME, uid, filter, channel), DateTimeUtil.format(LocalDateTime.now()));
    }

    /**
     * 获取预付记录的更新时间
     *
     * @param uid
     * @param filter
     * @param channel
     * @return
     */
    public LocalDateTime queryTime(String uid, String filter, String channel) {
        String time = (String) redisTemplate.opsForValue().get(String.format(RedisConstant.WALLET_TIME, uid, filter, channel));
        if (StringUtils.isNotBlank(time)) {
            return DateTimeUtil.parse(time);
        }
        return null;
    }


}
