package com.bjtdba.poster.service.Impl;

import com.bjtdba.entity.poster.SystemAttachment;
import com.bjtdba.poster.dao.SystemAttachmentDao;
import com.bjtdba.poster.service.QrCodeService;
import com.bjtdba.utils.QRCodeUtil;
import com.bjtdba.utils.uploadDownload;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class QrCodeServiceImpl implements QrCodeService {

    @Autowired
    private SystemAttachmentDao systemAttachmentDao;

    /**
     * 生产二维码的方法
     */
    public String insterQrCode(int uid,String text)throws Exception{
        /**
         * 1、生成时间戳   充当二维码的code以及名称
         * 2、将本地的二维码上传到远程数据库
         * 3、将二维码的地址以及uid、mer_id、code、二维码的url 保存到数据库中  与商户产生绑定
         * 3、将本的二维码删除
         */

        // 存放在二维码中的内容    http://app.bjtdba.com/offlinepay/index.html?id=164888564678229691#/
        long timeNew = System.currentTimeMillis(); // 13位数的时间戳    这个也是code
        // 生成的二维码的路径及名称
        String destPath = "/Users/hanyuxing/qrcode/"+uid+"/"+timeNew+".jpg";//
        //String picPath = ResourceUtils.getURL("classpath:").getPath() + "file/20221025172815531.png";
        File directory = new File("classes");
        String reportPath = directory.getCanonicalPath();
        String resource =reportPath + "/file/20221025172815531.png";



//        String resource ="https://cdn.bjtdba.com/vod/image/2025-01-20/20250120125206881.png";
        System.out.println("-----------------二维码----------------------------"+resource);
        //生成二维码
        QRCodeUtil.encode(text, resource, destPath, true);
        System.out.println("-----------------二维码1----------------------------");
        // 解析二维码
        //String str = QRCodeUtil.decode(destPath);
        System.out.println("-----------------二维码2----------------------------");
        // 打印出解析出的内容
        //System.out.println(str);
        //上传二维码到cdn
        File file = new File(destPath);
        FileInputStream input = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));
        Map<String, String> qrCode = uploadDownload.qrCode(multipartFile);
        String url = qrCode.get("url");

        System.out.println("-----------------二维码s----------------------------"+url);
        //deleteDir(new File("/Users/hanyuxing/qrcode"));
        //将邀请二维码添加到 rrx_system_attachment
        SystemAttachment systemAttachment = new SystemAttachment();
        systemAttachment.setAttachment_category_id(8);
        systemAttachment.setAttachment_name(""+timeNew+".jpg");
        systemAttachment.setAttachment_src(url);
        systemAttachment.setUser_type(1);
        systemAttachment.setUser_id(uid);
        systemAttachment.setShare_image_id(0);
        systemAttachmentDao.insterSystemAttachment(systemAttachment);
        return url;
    }
    public boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            //递归删除目录中的子目录下
            for (int i=0;i<children.length;i++){
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
    }
}
