package com.xhpc.charging.station.utils.img;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.OSSClient;
import com.xhpc.charging.station.mapper.XhpcImgMapper;
import com.xhpc.common.domain.XhpcTerminal;
import org.springframework.core.env.Environment;

import java.io.File;
import java.util.Date;
import java.util.Map;

public class QrImgUtils {

    /**
     * @param xhpcTerminal        终端实体类
     * @param serialNumber        终端序列号
     * @param environment         nacos对象
     * @param xhpcImgMapper       xhpc_img表对象
     * @param chargingStationName 场站名称
     * @param forIndex            枪号，用于拿出letterMap中的字母
     * @param letterMap           字母集合
     * @param name                桩号（桩名称）
     * @Author WH
     * @Date 2021/9/8 15:55
     * @Description upload images to Server
     * @Return void
     * @Since version-1.0
     */
    public static void uploadImg(XhpcTerminal xhpcTerminal, String serialNumber, Environment environment, XhpcImgMapper xhpcImgMapper, String chargingStationName, int forIndex, Map<Integer,Character> letterMap, String name) {
        //生成桩的二维码图片
        //1.生成二维码链接内容：
        StringBuilder prefix = new StringBuilder(environment.getProperty("img.qrLinkContent"));
        String QrContent = prefix.append(xhpcTerminal.getSerialNumber()).toString();
        try {
            //2.生成二维码图片：
            //2.1生成指定规则的图片名
            String qrFileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + serialNumber + ".png";
            //2.2获取生成的二维码图片
            File QrCode = QRCodeUtil.encode(QrContent, environment.getProperty("imgPath"), environment.getProperty("destPath"), true, qrFileName);
            //3.拼接成完整图片
            //3.1生成指定规则的图片名
            String finallyImgFileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS") + serialNumber + ".png";
            new ImageUtil().fastCreateImg(
                    new File(environment.getProperty("bottomImg")),
                    QrCode,
                    serialNumber,
                    chargingStationName,
                    name,
                    String.valueOf(letterMap.get(forIndex)),
                    new File(environment.getProperty("fullImgDestPath") + finallyImgFileName)
            );

            //4.上传图片至服务器
            // 创建OSSClient实例
            OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
            // 上传文件流
            // 拼接阿里云文件上传路径
            String aLiYunUploadLocation = "QrCodeImg/" + xhpcTerminal.getChargingStationId() + "/" + xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName;
            ossClient.putObject(environment.getProperty("oss.bucket-name"), aLiYunUploadLocation, new File(environment.getProperty("destPath") + File.separatorChar + finallyImgFileName));
            ossClient.shutdown();
            //System.out.println("===================》将完整二维码上传至阿里云成功");
            //5.将放在阿里云上的生成的图片的路径和图片所对应的终端的id放入数据库xhpc_img表中
            Long terminalId = xhpcTerminal.getTerminalId();
            xhpcImgMapper.insert(aLiYunUploadLocation, terminalId);
            //6.删除生成的二维码图片
            File QrImg = new File(environment.getProperty("destPath") + File.separatorChar + qrFileName);
            QrImg.delete();
            //7.删除本地生成的完整图片
            File finallyImg = new File(environment.getProperty("fullImgDestPath") + finallyImgFileName);
            finallyImg.delete();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            //System.out.println("二维码生成图片失败，请检查生成二维码所需要的资源路径是否正确");
        }
    }
}
