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 {

    /**
     * @Author WH
     * @Date 2021/9/8 15:55
     * @Description upload images to Server
     * @Param [xhpcTerminal, chargingPileId, environment, xhpcImgMapper]
     * @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("https://www.scxhua.cn?pNum=");
        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)
            );

            System.out.println("===================》完整二维码图片生成成功");

            //4.上传图片至服务器
            // 创建OSSClient实例
            OSSClient ossClient = new OSSClient(environment.getProperty("oss.endpoint"), environment.getProperty("oss.access-key"), environment.getProperty("oss.secret-key"));
            // 上传文件流
            ossClient.putObject(environment.getProperty("oss.bucket-name"), xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName, new File(environment.getProperty("destPath") + File.separatorChar + finallyImgFileName));
            ossClient.shutdown();

            System.out.println("===================》将完整二维码上传至阿里云成功");

            //5.将放在阿里云上的生成的图片的路径和图片所对应的终端的id放入数据库xhpc_img表中
            Long terminalId = xhpcTerminal.getTerminalId();
            xhpcImgMapper.insert(xhpcTerminal.getPileSerialNumber() + "/" + finallyImgFileName, terminalId);
            System.out.println("===================》将阿里云上的图片地址放入数据库");
            //6.删除生成的二维码图片
            File QrImg = new File(environment.getProperty("destPath") + File.separatorChar + qrFileName);
            QrImg.delete();
            //7.删除本地生成的完整图片
            File finallyImg = new File(environment.getProperty("fullImgDestPath") + finallyImgFileName);
            finallyImg.delete();
            System.out.println("==================》已删除本地生成的完成二维码图片");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            System.out.println("二维码生成图片失败，请检查生成二维码所需要的资源路径是否正确");
        }
    }
}
