package com.dayou.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dayou.configuration.DfsConfig;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.mapper.MajorProductionMapper;
import com.dayou.mapper.PersonalTargetMapper;
import com.dayou.service.IBusinessProductionService;
import com.dayou.service.IPersonalTargetService;
import com.dayou.utils.FileUploadUtils;
import com.dayou.utils.PersonalUtils;
import com.dayou.utils.QRCodeUtil;
import com.dayou.vo.ProdValidateVO;
import com.google.zxing.WriterException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.*;

import static com.dayou.common.Constants.PNG;
import static com.dayou.common.Constants.PROD_VALIDATE_URI;
import static com.dayou.enums.MainBusinessEnum.PERSONAL_BUSINESS;

/**
 * 类说明：
 *
 * @author: wucl
 * @since: 2024/4/1
 * created with IntelliJ IDEA.
 */
@Slf4j
@Service
public class BusinessProductionServiceImpl implements IBusinessProductionService, ResourceLoaderAware {

    private ResourceLoader resourceLoader;

    @Autowired
    private PersonalTargetMapper personalTargetMapper;

    @Autowired
    private MajorProductionMapper majorProductionMapper;

    @Autowired
    private DfsConfig dfsConfig;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Override
    public Boolean addPublicStamp(String filePath) {
        try {
            if (StrUtil.isNotBlank(filePath)){
                InputStream inputStream = new FileInputStream(filePath);
                Resource resource = resourceLoader.getResource("classpath:static/image/stamp.png");
                PersonalUtils.addPublicStamp(inputStream,resource,filePath);
                inputStream.close();
                return Boolean.TRUE;
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Boolean.FALSE;
    }

    @Override
    public ProdValidateVO validateProd(String businessType, Long businessId) {
        ProdValidateVO prodValidateVO = null;
        if (MainBusinessEnum.PERSONAL_BUSINESS.name().equals(businessType)){
            prodValidateVO = personalTargetMapper.validateProd(businessId);
        }
        if (MainBusinessEnum.MAJOR_BUSINESS.name().equals(businessType)){
            prodValidateVO = majorProductionMapper.validateProd(businessId);
        }
        return prodValidateVO;
    }

    @Override
    public String doCreateValidateCode(MainBusinessEnum businessEnum, Long businessId,String reportNo) {
        try {
            String domainRoot = dfsConfig.getDomainRoot();
            String validateUrl = new StringBuffer(domainRoot).append(PROD_VALIDATE_URI).append("?").append("businessType=")
                    .append(businessEnum.name()).append("&").append("targetId=").append(businessId).toString();
            String baseDir = dfsConfig.getPath()+dfsConfig.getCode();
            String dateIndexFileName = FileUploadUtils.createDateIndexFileName(reportNo+"_validate" + PNG);
            File absoluteFile = FileUploadUtils.getAbsoluteFile(baseDir, dateIndexFileName);
            QRCodeUtil.write(validateUrl,200,absoluteFile);
            return dfsConfig.getDomain() + dfsConfig.getCode() + dateIndexFileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (WriterException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String doCreateNormalCode(String production,String reportNo) {
        try {
            String baseDir = dfsConfig.getPath()+dfsConfig.getCode();
            String dateIndexFileName = FileUploadUtils.createDateIndexFileName(reportNo + PNG);
            File absoluteFile = FileUploadUtils.getAbsoluteFile(baseDir, dateIndexFileName);
            String flog = production.substring(0, 1);
            QRCodeUtil.write(reportNo+flog,200,absoluteFile);
            return dfsConfig.getDomain() + dfsConfig.getCode() + dateIndexFileName;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (WriterException e) {
            throw new RuntimeException(e);
        }
    }
}
