package com.hfkj.service.agent.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsAgentApiParamMapper;
import com.hfkj.entity.BsAgentApiParam;
import com.hfkj.entity.BsAgentApiParamExample;
import com.hfkj.service.agent.BsAgentApiParamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Date;
import java.util.List;

/**
 * @className: BsAgentApiParamServiceImpl
 * @author: HuRui
 * @date: 2024/8/27
 **/
@Service("agentApiParamService")
public class BsAgentApiParamServiceImpl implements BsAgentApiParamService {
    @Resource
    private BsAgentApiParamMapper agentApiParamMapper;
    @Resource
    private RedisUtil redisUtil;
    /**
     * 参数缓存目录
     */
    private final static String CACHE_AGENT_API_PARAM = "CACHE_AGENT_API_PARAM";

    @Override
    public void create(Long agentId) {
        BsAgentApiParam data = new BsAgentApiParam();
        data.setAgentId(agentId);
        data.setStatus(1);
        data.setAppId(generateAppID());
        data.setAppSecret(generateAppSecret());
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        agentApiParamMapper.insert(data);
        // 缓存
        redisUtil.hset(CACHE_AGENT_API_PARAM, data.getAppId() ,data);
    }

    @Override
    public void update(BsAgentApiParam data) {
        data.setUpdateTime(new Date());
        agentApiParamMapper.updateByPrimaryKey(data);
        // 更新缓存
        redisUtil.hset(CACHE_AGENT_API_PARAM, data.getAppId() ,data);
    }

    @Override
    public BsAgentApiParam getParam(Long agentId) {
        BsAgentApiParamExample example = new BsAgentApiParamExample();
        example.createCriteria().andAgentIdEqualTo(agentId).andStatusNotEqualTo(0);
        List<BsAgentApiParam> params = agentApiParamMapper.selectByExample(example);
        if (params.isEmpty()) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未配置appid、appsecret");
        }
        return params.get(0);
    }

    @Override
    public List<BsAgentApiParam> getParamListByGasNo(String gasNo) {
        return agentApiParamMapper.selectParamListByGasNo(gasNo);
    }

    @Override
    public BsAgentApiParam getParamByAppId(String appId) {
        // 查询
        Object cacheObj = redisUtil.hget("CACHE_AGENT_API_PARAM", appId);
        if (cacheObj != null) {
            return (BsAgentApiParam) cacheObj;
        }
        BsAgentApiParamExample example = new BsAgentApiParamExample();
        example.createCriteria().andAppIdEqualTo(appId).andStatusNotEqualTo(0);
        List<BsAgentApiParam> params = agentApiParamMapper.selectByExample(example);
        if (params.isEmpty()) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无效的appid");
        }
        redisUtil.hset(CACHE_AGENT_API_PARAM, appId ,params.get(0));
        return params.get(0);
    }


    public static void main(String[] args) {
        System.out.println(generateAppID());
        System.out.println(generateAppSecret());
    }
    /**
     * 生成16位APPID
     * @return
     */
    public static String generateAppID() {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[7];
        random.nextBytes(bytes);
        return "hf"+new BigInteger(1, bytes).toString(16);
    }

    /**
     * 生成32位APPID
     * @return
     */
    public static String generateAppSecret() {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[16];
        random.nextBytes(bytes);
        return new BigInteger(1, bytes).toString(16);
    }

}
