package com.jianshui.platform.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jianshui.common.constant.WebServiceConstant;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.core.redis.RedisCache;
import com.jianshui.common.utils.spring.SpringUtils;
import com.jianshui.system.domain.InvoiceAllYhdj;
import com.jianshui.system.mapper.InvoiceAllYhdjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: kane
 * @Description: 获取用户登记信息工具类
 * @CreateTime: 2023-06-07  09:08
 * @Version: 1.0
 **/
@Component
public class InvoiceAllYhdjUtils {

    @Autowired
    private InvoiceAllYhdjMapper invoiceAllYhdjMapper;

    public InvoiceAllYhdjUtils() {
    }

    /**
     * 获取登记用户信息
     */
    public InvoiceAllYhdj getUserInfo(Companyservice companyservice) {

        String identity = companyservice.getIdentity();
        String sellertax = companyservice.getSellertax();

        String key = WebServiceConstant.TOKEN_KEY + sellertax + "_"+ identity;

        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        cn.hutool.json.JSONObject yhdjObj = redisCache.getCacheObject(key);

        if(JSONUtil.isNull(yhdjObj)){
            // 查询用户登记参数
            QueryWrapper<InvoiceAllYhdj> yhdjQueryWrapper = new QueryWrapper<>();
            yhdjQueryWrapper.eq("nsrsbh", sellertax);
            yhdjQueryWrapper.eq("identity", identity);
            InvoiceAllYhdj invoiceAllYhdj = invoiceAllYhdjMapper.selectOne(yhdjQueryWrapper);

            cn.hutool.json.JSONObject obj = JSONUtil.parseObj(invoiceAllYhdj);
            Long exprieTime = System.currentTimeMillis(); // 过期时间
            obj.put("exprieTime", exprieTime);
            redisCache.setCacheObject(key, obj);

            yhdjObj = obj;
        }

        return BeanUtil.copyProperties(yhdjObj,InvoiceAllYhdj.class);
    }
}
