package jnpf.personnels.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.model.personnels.po.FtbPersonnelsShortchain;
import jnpf.personnels.mapper.FtbPersonnelsShortchainMapper;
import jnpf.personnels.service.FtbPersonnelsShortchainService;
import jnpf.util.excel.ShortChainUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Random;

@Service
public class FtbPersonnelsShortchainServiceImpl extends ServiceImpl<FtbPersonnelsShortchainMapper, FtbPersonnelsShortchain> implements FtbPersonnelsShortchainService {

    @Override
    public String generateShortChain(String longChainAddress) {
        String[] resUrl = ShortChainUtils.shortUrl(longChainAddress);
        Random random = new Random();
        int nextInt = random.nextInt(4);
        String shortUrl = resUrl[nextInt];
        LambdaQueryWrapper<FtbPersonnelsShortchain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsShortchain::getShortChainAddr, shortUrl);
        if (this.baseMapper.selectCount(queryWrapper) > 0) {
            return shortUrl + nextInt;
        }
        return shortUrl;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveShortChain(String shortChainAddress, String longChainAddress, String requestUrl) {
        LambdaQueryWrapper<FtbPersonnelsShortchain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsShortchain::getShortChainAddr, shortChainAddress);
        if (this.baseMapper.selectCount(queryWrapper) == 0) {
            FtbPersonnelsShortchain ftbPersonnelsShortchain = new FtbPersonnelsShortchain();
            ftbPersonnelsShortchain.setShortChainAddr(shortChainAddress);
            ftbPersonnelsShortchain.setLongChainAddr(longChainAddress);
            ftbPersonnelsShortchain.setRequestUrl(requestUrl);
            this.baseMapper.insert(ftbPersonnelsShortchain);
        }
    }

    @Override
    public String redirect(String url) {
        LambdaQueryWrapper<FtbPersonnelsShortchain> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(FtbPersonnelsShortchain::getRequestUrl, FtbPersonnelsShortchain::getLongChainAddr);
        queryWrapper.eq(FtbPersonnelsShortchain::getShortChainAddr, url);
        FtbPersonnelsShortchain ftbPersonnelsShortchain = this.baseMapper.selectOne(queryWrapper);
        if (ftbPersonnelsShortchain != null) {
            String chinesePath = ShortChainUtils.reverseLongChain(ftbPersonnelsShortchain.getLongChainAddr());
            chinesePath = chinesePath.substring(chinesePath.indexOf("?"));
            // 使用UTF-8编码将中文转换为URL编码格式
            String encodedChinesePath = URLEncoder.encode(chinesePath, StandardCharsets.UTF_8);
            return ftbPersonnelsShortchain.getRequestUrl() + "?" + encodedChinesePath;
        }
        return null;
    }

}
