package com.jeequan.jeepay.agent.ctrl.qrcode;

import com.jeequan.jeepay.agent.ctrl.CommonCtrl;
import com.jeequan.jeepay.bizcommons.manage.qrshell.AbstractGenerator;
import com.jeequan.jeepay.bizcommons.manage.qrshell.ShellQRGenerator;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import com.jeequan.jeepay.db.entity.MchApplyment;
import com.jeequan.jeepay.db.entity.MchQrcShell;
import com.jeequan.jeepay.db.entity.MchQrcodeCard;
import com.jeequan.jeepay.db.entity.MchStore;
import com.jeequan.jeepay.service.impl.MchApplymentService;
import com.jeequan.jeepay.service.impl.MchQrcShellService;
import com.jeequan.jeepay.service.impl.MchQrcodeCardService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.image.BufferedImage;


/**
 * 二维码模板管理
 *
 * @author terrfly
 * @date 2022/1/17 14:22
 */
@RestController
@RequestMapping("/api/mchQrcShells")
public class MchQrcShellController extends CommonCtrl {

    @Autowired private MchQrcodeCardService mchQrcodeCardService;
    @Autowired private MchQrcShellService mchQrcShellService;
    @Autowired private SysConfigService sysConfigService;
    @Autowired private MchApplymentService mchApplymentService;

    /** 根据qrcId获取图片base64 **/
    @GetMapping("/viewByQrc/{qrcId}")
    public ApiRes viewByQrc(@PathVariable("qrcId") Long qrcId) throws Exception {

        MchQrcodeCard dbRecord = mchQrcodeCardService.getById(qrcId);

        String mchStoreName = "";
        if(dbRecord.getBindState() == CS.YES && dbRecord.getStoreId() != null && !"-1".equals(dbRecord.getStoreId())){
            MchStore mchStore = mchStoreService.getById(dbRecord.getStoreId());
            mchStoreName = mchStore != null ? mchStore.getStoreName() : "";
        }

        AbstractGenerator shellGenerator = SpringBeansUtil.getBean(ShellQRGenerator.class);

        MchApplyment applyment = mchApplymentService.getById(dbRecord.getMchApplyId());

        // 封装URL
        String qrUrlContent = sysConfigService.getDBApplicationConfig().genUniJsapiPayUrl(QRCodeParams.TYPE_QRC, dbRecord.getEntryPage(), qrcId + "", applyment.getIsvNo());

        if(dbRecord.getQrcShellId() == null || dbRecord.getQrcShellId() <= 0){
            //给前端拼接上 【 data:image/jpg;base64 】
            return ApiRes.ok("data:image/jpg;base64,"+ AbstractGenerator.convertImgBase64(shellGenerator.genQrImgBuffer(null, qrUrlContent, qrcId, mchStoreName, false)));
        }

        MchQrcShell mchQrcShell = mchQrcShellService.getById(dbRecord.getQrcShellId());

        BufferedImage bufferedImage = getAbstractGenerator(mchQrcShell.getStyleCode()).genQrImgBuffer(mchQrcShell.getConfigInfo(), qrUrlContent, qrcId, mchStoreName, false);

        //给前端拼接上 【 data:image/jpg;base64 】
        return ApiRes.ok("data:image/jpg;base64,"+ AbstractGenerator.convertImgBase64(bufferedImage));
    }


    private AbstractGenerator getAbstractGenerator(String code){

        AbstractGenerator generator = SpringBeansUtil.getBean(code + "Generator", AbstractGenerator.class);
        if(generator == null){
            throw new BizException("选择模板[" + code + "]不存在！");
        }
        return generator;
    }

}
