package com.hfkj.service.merchant.impl;

import com.hfkj.common.QRCodeGenerator;
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.config.CommonSysConst;
import com.hfkj.dao.BsMerchantQrCodeMapper;
import com.hfkj.entity.BsMerchant;
import com.hfkj.entity.BsMerchantQrCode;
import com.hfkj.entity.BsMerchantQrCodeExample;
import com.hfkj.service.merchant.BsMerchantQrCodeService;
import com.hfkj.sysenum.merchant.MerchantQrCodeStatusEnum;
import com.hfkj.sysenum.merchant.MerchantQrCodeTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @className: BsMerchantQrCodeServiceImpl
 * @author: HuRui
 * @date: 2024/3/4
 **/
@Service("merchantQrCodeService")
public class BsMerchantQrCodeServiceImpl implements BsMerchantQrCodeService {

    @Resource
    private BsMerchantQrCodeMapper merchantQrCodeMapper;
    @Resource
    private RedisUtil redisUtil;
    private final static String CACHE_MER_QR_CODE_KEY = "MERCHANT_QR_CODE:";

    @Override
    public void editData(BsMerchantQrCode data) {
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            data.setUpdateTime(new Date());
            merchantQrCodeMapper.insert(data);
        } else {
            data.setUpdateTime(new Date());
            merchantQrCodeMapper.updateByPrimaryKey(data);
        }
    }

    @Override
    public void generateMerQrCode(BsMerchant merchant) throws Exception {
/*        *//** 综合二维码 **//*
        BsMerchantQrCode compositeQrCode = new BsMerchantQrCode();
        compositeQrCode.setMerchantId(merchant.getId());
        compositeQrCode.setMerchantNo(merchant.getMerNo());
        compositeQrCode.setMerchantNo(merchant.getMerNo());
        compositeQrCode.setCodeType(MerchantQrCodeTypeEnum.type1.getNumber());
        compositeQrCode.setCodeContent("https://gratia-pay.dctpay.com?sn="+merchant.getMerNo());

        // 二维码
        String compositeQrCodeFile = "/merOilQrCode/" + merchant.getMerNo() + "/" + System.currentTimeMillis() + ".png";
        QRCodeGenerator.generateQRCodeImage(
                compositeQrCode.getCodeContent(),
                350,
                350,
                CommonSysConst.getSysConfig().getFilesystem() + compositeQrCodeFile);
        compositeQrCode.setCodeImg(compositeQrCodeFile);
        compositeQrCode.setStatus(MerchantQrCodeStatusEnum.status1.getNumber());
        compositeQrCode.setCreateTime(new Date());
        compositeQrCode.setUpdateTime(new Date());
        merchantQrCodeMapper.insert(compositeQrCode);*/

        /** 加油二维码 **/
        BsMerchantQrCode oilQrCode = new BsMerchantQrCode();
        oilQrCode.setMerchantId(merchant.getId());
        oilQrCode.setMerchantNo(merchant.getMerNo());
        oilQrCode.setMerchantNo(merchant.getMerNo());
        oilQrCode.setCodeType(MerchantQrCodeTypeEnum.type2.getNumber());
        oilQrCode.setCodeContent(CommonSysConst.getSysConfig().getDomainName()+"/cwebh5/#/petrolStation/comfirmOrder/index?merNo="+merchant.getMerNo());

        // 二维码
        String oilQrCodeFile = "/merOilQrCode/" + merchant.getMerNo() + "/" + System.currentTimeMillis() + ".png";
        QRCodeGenerator.generateQRCodeImage(
                oilQrCode.getCodeContent(),
                350,
                350,
                CommonSysConst.getSysConfig().getFilesystem() + oilQrCodeFile);
        oilQrCode.setCodeImg(oilQrCodeFile);
        oilQrCode.setStatus(MerchantQrCodeStatusEnum.status1.getNumber());
        oilQrCode.setCreateTime(new Date());
        oilQrCode.setUpdateTime(new Date());
        merchantQrCodeMapper.insert(oilQrCode);


        /** 商城二维码 **/
        /*BsMerchantQrCode shopQrCode = new BsMerchantQrCode();
        shopQrCode.setMerchantId(merchant.getId());
        shopQrCode.setMerchantNo(merchant.getMerNo());
        shopQrCode.setMerchantNo(merchant.getMerNo());
        shopQrCode.setCodeType(MerchantQrCodeTypeEnum.type3.getNumber());
        shopQrCode.setCodeContent("https://gratia-pay.dctpay.com?sn="+merchant.getMerNo());

        // 二维码
        String shopQrCodeFile = "/merOilQrCode/" + merchant.getMerNo() + "/" + System.currentTimeMillis() + ".png";
        QRCodeGenerator.generateQRCodeImage(
                compositeQrCode.getCodeContent(),
                350,
                350,
                CommonSysConst.getSysConfig().getFilesystem() + shopQrCodeFile);
        shopQrCode.setCodeImg(shopQrCodeFile);
        shopQrCode.setStatus(MerchantQrCodeStatusEnum.status1.getNumber());
        shopQrCode.setCreateTime(new Date());
        shopQrCode.setUpdateTime(new Date());
        merchantQrCodeMapper.insert(shopQrCode);*/
    }

    @Override
    public void updateQrCodeStatus(Long qrCodeId, MerchantQrCodeStatusEnum qrCodeStatus) {
        // 查询二维码
        BsMerchantQrCode qrCode = getMerQrCode(qrCodeId);
        if (qrCode == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知二维码");
        }
        qrCode.setStatus(qrCodeStatus.getNumber());
        editData(qrCode);

        redisUtil.set(CACHE_MER_QR_CODE_KEY + (qrCode.getMerchantNo() + "_" + qrCode.getCodeType()), qrCode);
    }

    @Override
    public BsMerchantQrCode getMerQrCode(Long id) {
        return merchantQrCodeMapper.selectByPrimaryKey(id);
    }

    @Override
    public BsMerchantQrCode getMerQrCode(String merNo, MerchantQrCodeTypeEnum qrCodeType) {
        Object obj = redisUtil.get(CACHE_MER_QR_CODE_KEY + (merNo + "_" + qrCodeType));
        if (obj == null) {
            return (BsMerchantQrCode) obj;
        }
        BsMerchantQrCodeExample example = new BsMerchantQrCodeExample();
        example.createCriteria()
                .andMerchantNoEqualTo(merNo)
                .andCodeTypeEqualTo(qrCodeType.getNumber())
                .andStatusNotEqualTo(MerchantQrCodeStatusEnum.status0.getNumber());
        List<BsMerchantQrCode> list = merchantQrCodeMapper.selectByExample(example);
        if (!list.isEmpty()) {
            BsMerchantQrCode qrCode = list.get(0);
            redisUtil.set(CACHE_MER_QR_CODE_KEY + (merNo + "_" + qrCodeType), qrCode);
            return qrCode;
        }
        return null;
    }

    @Override
    public List<BsMerchantQrCode> getMerQrCodeList(String merNo) {
        BsMerchantQrCodeExample example = new BsMerchantQrCodeExample();
        example.createCriteria().andMerchantNoEqualTo(merNo).andStatusNotEqualTo(MerchantQrCodeStatusEnum.status0.getNumber());
        example.setOrderByClause("code_type");
        return merchantQrCodeMapper.selectByExample(example);
    }
}
