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.CollectionUtils;
import com.sckw.core.utils.IdWorker;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.payment.dao.KwpWalletRelationMapper;
import com.sckw.payment.model.KwpWalletRelation;
import com.sckw.payment.model.dto.WalletRelationDto;
import com.sckw.redis.constant.RedisConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author xucaiqin
 * @date 2023-07-10 16:38:36
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwpWalletRelationService {
    private final KwpWalletRelationMapper relationMapper;
    private final RedisTemplate<String, Object> redisTemplate;

    public List<String> getAll() {
        List<KwpWalletRelation> kwpWalletRelations = relationMapper.selectList(new LambdaQueryWrapper<KwpWalletRelation>()
                .eq(KwpWalletRelation::getDelFlag, 0));
        if (!CollectionUtils.isEmpty(kwpWalletRelations)) {
            return kwpWalletRelations.stream().map(KwpWalletRelation::getUid).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    /**
     * 根据企业id查询中台用户
     *
     * @param entId 企业id
     * @return
     */
    public String getRelation(Long entId) {
        if (Objects.isNull(entId)) {
            return null;
        }
        String uid = (String) redisTemplate.opsForHash().get(RedisConstant.WALLET_ENT, String.valueOf(entId));
        if (StringUtils.isNotBlank(uid)) {
            return uid;
        }
        KwpWalletRelation kwpWalletRelation = relationMapper.selectOne(new LambdaQueryWrapper<KwpWalletRelation>()
                .eq(KwpWalletRelation::getEntId, entId)
                .eq(KwpWalletRelation::getDelFlag, 0).last("limit 1"));
        if (Objects.isNull(kwpWalletRelation)) {
            return null;
        }
        uid = kwpWalletRelation.getUid();
        redisTemplate.opsForHash().put(RedisConstant.WALLET_ENT, String.valueOf(entId), uid);
        return uid;
    }

    /**
     * 通过中台uid反查关联的企业
     *
     * @param uid 中台uid
     * @return
     */
    public Long getEnt(String uid) {
        if (StringUtils.isBlank(uid)) {
            return null;
        }
        Long entId = (Long) redisTemplate.opsForHash().get(RedisConstant.WALLET_UID, uid);
        if (Objects.nonNull(entId)) {
            return entId;
        }
        KwpWalletRelation kwpWalletRelation = relationMapper.selectOne(new LambdaQueryWrapper<KwpWalletRelation>()
                .eq(KwpWalletRelation::getUid, uid)
                .eq(KwpWalletRelation::getDelFlag, 0).last("limit 1"));
        if (Objects.isNull(kwpWalletRelation)) {
            return null;
        }
        entId = kwpWalletRelation.getEntId();
        redisTemplate.opsForHash().put(RedisConstant.WALLET_UID, uid, entId);
        return entId;
    }

    /**
     * 保存关联信息
     *
     * @param walletRelationDto 企业关联钱包信息
     */
    public void save(WalletRelationDto walletRelationDto) {
        KwpWalletRelation kwpWalletRelation = new KwpWalletRelation();
        kwpWalletRelation.setId(new IdWorker(NumberConstant.ONE).nextId());
        kwpWalletRelation.setEntId(walletRelationDto.entId());
        kwpWalletRelation.setUid(walletRelationDto.uid());
        kwpWalletRelation.setStatus(0);
        kwpWalletRelation.setCreateBy(LoginUserHolder.getUserId());
        kwpWalletRelation.setCreateTime(new Date());
        kwpWalletRelation.setUpdateBy(LoginUserHolder.getUserId());
        kwpWalletRelation.setUpdateTime(new Date());
        kwpWalletRelation.setDelFlag(0);
        relationMapper.insert(kwpWalletRelation);
    }

    public KwpWalletRelation query(WalletRelationDto walletRelationDto) {
        LambdaQueryWrapper<KwpWalletRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwpWalletRelation::getEntId, walletRelationDto.entId())
                .eq(KwpWalletRelation::getUid, walletRelationDto.uid())
                .eq(KwpWalletRelation::getDelFlag, Global.UN_DELETED).last("limit 1");
        return relationMapper.selectOne(wrapper);
    }


}
