package com.zfire.jiasm.syncdata.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zfire.jiasm.syncdata.base.BaseService;
import com.zfire.jiasm.syncdata.gree.GreeLogic;
import com.zfire.jiasm.syncdata.plus.entity.OrderRepairOperatingLog;
import com.zfire.jiasm.syncdata.plus.entity.WebsitWorker;
import com.zfire.jiasm.syncdata.plus.service.OrderRepairOperatingLogService;
import com.zfire.jiasm.syncdata.service.OrderOperatingLogFileService;
import com.zfire.jiasm.syncdata.service.PublicService;
import com.zfire.jiasm.syncdata.utils.oss.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderOperatingLogFileServiceImpl implements OrderOperatingLogFileService {
    @Resource
    OrderRepairOperatingLogService orderRepairOperatingLogService;
    @Resource
    PublicService publicService;
    @Resource
    BaseService baseService;
    @Resource
    GreeLogic greeLogic;
    @Resource
    OSSUtil ossUtil;


    @Override
    public void extractedRepairFile(){
        List<OrderRepairOperatingLog> orolsUp = new ArrayList<>();
        List<OrderRepairOperatingLog> orols = new ArrayList<>();

        // 提取需要处理的证件照数据
        orols = orderRepairOperatingLogService.lambdaQuery()
                .eq(OrderRepairOperatingLog::getOutZjljProcess,0)
                .isNotNull(OrderRepairOperatingLog::getOutZjlj).list();
        for (OrderRepairOperatingLog item :orols){
            try{
                // 生成图片
                processInfo(item);
                orolsUp.add(item);


                updateInfo(orolsUp, 10);
            } catch (Exception ex) {
                ex.printStackTrace();
                updateInfo(orolsUp, 0);
                log.info("生成生成维修工单同步的附件信息结束失败 order_repair_operating_log.websit_worker_id= {}",item.getId());
            }
        }

        updateInfo(orolsUp, 0);

    }

    private void updateInfo(List<OrderRepairOperatingLog> orolsUp, int i) {
        if (orolsUp.size() > i) {
            orderRepairOperatingLogService.updateBatchById(orolsUp);
            orolsUp.clear();
        }
    }



    private void processInfo(OrderRepairOperatingLog orol) throws IOException {
        if (StringUtils.isNotEmpty(orol.getOutZjlj())){
            // 多个附件处理
            if (orol.getOutZjlj().contains(",")){
                Boolean flag = Boolean.TRUE;
                int num=0;
                String insureFiles = "";
                String outInsureCert = "";

                List<String> result = Arrays.asList(orol.getOutZjlj().split(","));

                // 上传图片
                for (String p:result){
                    String id = putCert(p);
                    if (StringUtils.isNotEmpty(id)){
                        if (num==0){
                            insureFiles=id;
                            outInsureCert=p;
                        } else {
                            insureFiles=insureFiles+","+id;
                            outInsureCert=outInsureCert+","+p;
                        }
                        orol.setFileUrl(insureFiles);
                        orol.setBrandFileId(outInsureCert);
                        new File(id).delete();
                    } else {
                        flag = Boolean.FALSE;
                    }
                    num = num +1 ;
                }
                if (flag){
                    orol.setOutZjljProcess(1);
                }
            } else {
                String id = putCert(orol.getOutZjlj());
                if (StringUtils.isNotEmpty(id)){
                    orol.setFileUrl(id);
                    orol.setBrandFileId(orol.getOutZjlj());
                    orol.setOutZjljProcess(1);
                    new File(id).delete();
                }
            }
        }
    }



    private String putCert(String image) throws IOException {
        String fileId = "";
        String id = publicService.path(image);
        File newFile = publicService.createf(id);

        try{
            baseService.saveToFile(greeLogic.greeFileUrl(image), newFile);
            ossUtil.uploadFile(id, newFile);
            fileId = id;
            return fileId;
        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("生成师傅证件照信息失败");
        }
        return fileId;
    }
}
