package com.persagy.dmp.rwd.qrcode.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.dmp.common.model.response.CommonResult;
import com.persagy.dmp.digital.client.DigitalObjectFacade;
import com.persagy.dmp.file.client.FileClientFacade;
import com.persagy.dmp.file.model.FileRequestData;
import com.persagy.dmp.file.model.UploadMes;
import com.persagy.dmp.rwd.qrcode.service.QrCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * 二维码管理 Service
 * @author:linhuili
 * @date:2021/11/8
 */
@Service
@Slf4j
public class QrCodeServiceImpl implements QrCodeService {

    /**
     * 生成默认二维码
     * @param objectDigitals
     */
    @Override
    public void createDefaultQrCode(List<ObjectNode> objectDigitals) {
        if(CollUtil.isEmpty(objectDigitals)){
            return;
        }
        //获取基础参数
        String projectId = AppContext.getContext().getProjectId();
        String groupCode = AppContext.getContext().getGroupCode();
        for (ObjectNode objectDigital : objectDigitals) {
            //生成二维码图片，并上传到文件服务器
            UploadMes objectQrCode = createObjectQrCode(groupCode,projectId,objectDigital);
            if(objectQrCode == null){
                log.info("生成二维码失败");
                continue;
            }
            //设置对象二维码信息
            setObjectQrCode(objectDigital,objectQrCode);
        }
        //更新对象二维码信息
        DigitalObjectFacade.update(groupCode,projectId,null,null,objectDigitals);
    }

    /**
     * 设置二维码图片
     * @param objectDigital
     */
    public void setObjectQrCode(ObjectNode objectDigital, UploadMes objectQrCode){
        //设置二维码信息
        ArrayNode qrCodePics = JsonNodeFactory.instance.arrayNode();
        ObjectNode qrCodePic = JsonNodeFactory.instance.objectNode();
        qrCodePic.put("key", objectQrCode.getId());
        qrCodePics.add(qrCodePic);
        objectDigital.put(CommonConstant.QR_CODE_PIC_INFO,qrCodePics);
    }

    /**
     * 生成二维码信息并上传到文件服务器
     * @param objectDigital
     */
    public UploadMes createObjectQrCode(String groupCode,String projectId,ObjectNode objectDigital){
        //获取基础参数
        String objId =objectDigital.get("id").textValue();
        String key = CommonConstant.QRCODE_KEY_PREFIX +"_"+ objId;
        //生成二维码
        QrConfig qrConfig = new QrConfig(300, 300);
        File qrCodeFile = QrCodeUtil.generate(objId, qrConfig, FileUtil.file("/image"));
        //上传到文件服务器
        FileRequestData fileRequestData = new FileRequestData();
        fileRequestData.setFileName(key);
        fileRequestData.setFileMd5(SecureUtil.md5(qrCodeFile));
        fileRequestData.setGroupCode(groupCode);
        fileRequestData.setFileBucket(CommonConstant.FILE_BUCKET_NAME);
        fileRequestData.setFileSize(qrCodeFile.length());
        CommonResult<UploadMes> uploadMesCommonResult = FileClientFacade.initSingleUploadPrototype(groupCode,projectId,null,null,fileRequestData);
        if(!CommonConstant.QUERY_SUCCESS.equals(uploadMesCommonResult.getResult())){
            log.info("上传文件失败:"+uploadMesCommonResult.getMessage());
            return new UploadMes();
        }
        log.info("生成二维码信息"+uploadMesCommonResult.getMessage());
        return uploadMesCommonResult.getData();
    }
}
