package com.changhu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changhu.mapper.AccessKeysMapper;
import com.changhu.module.superManagement.pojo.params.GeneratedAccessKeyParams;
import com.changhu.pojo.entity.AccessKeys;
import com.changhu.service.AccessKeysService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * access_keys (开放接口的授权)  服务实现类
 * author: luozhun
 * desc 由groovy脚本自动生成
 */
@Service
public class AccessKeysServiceImpl extends ServiceImpl<AccessKeysMapper, AccessKeys> implements AccessKeysService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void generatedAccessKey(GeneratedAccessKeyParams params) {
        AccessKeys accessKeys = BeanUtil.copyProperties(params, AccessKeys.class);
        if (accessKeys.getSnowFlakeId() == null) {
            // 生成一个32位的随机字符串，包含大小写字母和数字
            String accessKey = RandomUtil.randomString(RandomUtil.BASE_CHAR + RandomUtil.BASE_NUMBER, 32);
            String secretKey = IdUtil.randomUUID();
            accessKeys.setAccessKey(accessKey);
            accessKeys.setSecretKey(secretKey);
        }
        this.saveOrUpdate(accessKeys);
    }

    public static void main(String[] args) {
        String randomPart = RandomUtil.randomString(RandomUtil.BASE_CHAR + RandomUtil.BASE_NUMBER, 32);
        System.out.println(randomPart);
        System.out.println(IdUtil.randomUUID());
    }
}
