package com.ho.business.common;

import com.alibaba.druid.util.StringUtils;
import com.ho.business.entity.BusiIdControl;
import com.ho.business.mapper.BusiIdControlMapper;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.UserDetailRespVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.CodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author fancl
 * @desc: 业务模块工具类
 * @date 2022/10/14
 */
@Component
@Slf4j
public class BusiTool {

    @Autowired
    RedisService redisService;

    @Autowired
    BusiIdControlMapper busiMapper;

    @Autowired
    RedissonClient redissonClient;


    //根据token获取当前人缓存信息
    public SimpleUser getSimpleUser(String token) {
        UserDetailRespVO userDetailRespVO = getUserDetailRespVO(token);
        String userNameKey = redisService.getUserNameKey(userDetailRespVO,userDetailRespVO.getUsername() );
        SimpleUser user = (SimpleUser) redisService.get(userNameKey);
        return user;
    }

    //根据token获取当前人缓存token信息
    public UserDetailRespVO getUserDetailRespVO(String token) {
        if (StringUtils.isEmpty(token)) {
            throw new BusinessException(BaseResponseCode.TOKEN_PARSE_ERROR);
        }
        String userTokenKey = RedisKeyConstant.User.TOKEN + token;
        UserDetailRespVO userDetailRespVO = (UserDetailRespVO) redisService.get(userTokenKey);
        return userDetailRespVO;
    }

    //获取电价模板号
    public String getNextPriceTemplateId(String type) {
        //类型必须为已经定义的
        if (!CommonConstant.IdTypePriceTemplate.equals(type)) {
            throw new BusinessException(BaseResponseCode.PRICE_TEMPLATE_ID_TYPE_NOT_RIGHT);
        }
        //定义锁
        RLock lock = null;
        //返回的电价模板号
        Long id = null;
        try {
            lock = redissonClient.getLock(RedisKeyConstant.LOCK_KEY_PRICE_TEMPLATE);
            //获取锁,没有获取到锁的阻塞在lock.lock() 这行
            lock.lock();
            BusiIdControl busiIdControl = busiMapper.selectByType(type);
            //首次直接变为2,因为1已经返回被使用了
            if (busiIdControl != null) {
                Long currentValue = busiIdControl.getCurrentValue();
                id = currentValue;
                //每次返回数值后+1
                busiIdControl.setCurrentValue(++currentValue);
                busiMapper.update(busiIdControl);
            } else {
                id = 1L;
                //没有对象，创建一条记录
                BusiIdControl busi = new BusiIdControl();
                busi.setIdType(type);
                busi.setCurrentValue(2L);
                busiMapper.insert(busi);
            }
        } finally {
            if (lock != null && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
        log.info("电价模板id: {}", id);
        String workSn = CodeUtil.getCode(CodeUtil.PRICE_TEMPLATE_PREFIX, String.valueOf(id), 10, "0");
        log.info("电价模板号: {}", workSn);
        return workSn;
    }



}
