package com.njyunzhi.pet_identity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njyunzhi.pet_identity.common.Constants;
import com.njyunzhi.pet_identity.common.StringUtils;
import com.njyunzhi.pet_identity.entity.TaCardNo;
import com.njyunzhi.pet_identity.entity.TaMiniappQrcode;
import com.njyunzhi.pet_identity.mapper.TaCardNoMapper;
import com.njyunzhi.pet_identity.mapper.TaSequenceMapper;
import com.njyunzhi.pet_identity.service.ITaCardNoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njyunzhi.pet_identity.service.ITaMiniappQrcodeService;
import com.njyunzhi.pet_identity.vo.BatchCardParams;
import com.njyunzhi.pet_identity.vo.MiniAppScene;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 卡号库 服务实现类
 * </p>
 *
 * @author yansen
 * @since 2022-05-28
 */
@Service
public class TaCardNoServiceImpl extends ServiceImpl<TaCardNoMapper, TaCardNo> implements ITaCardNoService {

    @Autowired
    TaSequenceMapper taSequenceMapper;

    @Autowired
    ITaMiniappQrcodeService iTaMiniappQrcodeService;

    @Value("${yz.miniapp.cardpath}")
    String cardpath;

    @Override
    public TaCardNo getUnuseCard(String cardNo) {
        if (StringUtils.isEmpty(cardNo)) return null;

        TaCardNo taCardNo = getById(cardNo);
        if (null == taCardNo) return null;

        // 已经关联的无效
        if (Constants.STATUS_READY != taCardNo.getStatus()) return null;

        // 没有小程序码的无效
        if (StringUtils.isEmpty(taCardNo.getQrId())) return null;

        return taCardNo;
    }

    @Async
    @Override
    public void createBatchCardNo(BatchCardParams batchCardParams) throws Exception {
        LocalDateTime now = LocalDateTime.now();

        for (int i = 0; i < batchCardParams.getNum(); i += 1) {
            Integer nextVal = taSequenceMapper.getNextVal("card-" + batchCardParams.getPrefix());
            String cardNo = getFormatNo(batchCardParams.getPrefix(), nextVal);
            MiniAppScene scene = new MiniAppScene().setPage(cardpath).setQueryStr("cardno=" + StringUtils.urlEncode(cardNo));

            // 生成小程序码
            TaMiniappQrcode qrCode = iTaMiniappQrcodeService.createQRCode(scene);
            // 生成模板图片
            // TODO

            TaCardNo taCardNo = new TaCardNo();
            taCardNo.setCardNo(cardNo);
            taCardNo.setQrId(qrCode.getQrId().toString());
            taCardNo.setQrImage(qrCode.getQrImage());
            taCardNo.setStatus(Constants.STATUS_READY);
            taCardNo.setCreateDate(now);

            if (!save(taCardNo)) {
                throw new Exception("生成卡号失败");
            }
        }
    }

    @Override
    public String getFormatNo(String prefix, Integer num) {
        if (StringUtils.isEmpty(prefix) || num == null) return null;

        int maxLen = 5;
        return String.format("%s%s", prefix, StringUtils.lpad(num.toString(), "0", maxLen));
    }
}
