package com.zcxk.rmcp.web.excel.resolver.service;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSONObject;
import com.zcxk.core.common.pojo.Message;
import com.zcxk.rmcp.core.entity.ImportFile;
import com.zcxk.rmcp.web.excel.download.adapter.DeviceExcelFillAdapter;
import com.zcxk.rmcp.web.excel.download.adapter.InstallPlanExcelFillAdapter;
import com.zcxk.rmcp.web.excel.model.*;
import com.zcxk.rmcp.web.excel.resolver.HandleExcelService;
import com.zcxk.rmcp.web.excel.resolver.listener.CommDataListener;
import com.zcxk.rmcp.web.excel.resolver.listener.InstallPlanListener;
import com.zcxk.rmcp.web.service.CommonService;
import com.zcxk.rmcp.web.service.DeviceService;
import com.zcxk.rmcp.web.service.ImportFileService;
import com.zcxk.rmcp.web.service.impl.ImportFileServiceImpl;
import com.zcxk.rmcp.web.service.install.InstallPlanService;
import com.zcxk.rmcp.web.service.install.impl.InstallPlanServiceImpl;
import com.zcxk.rmcp.web.util.MessageSendUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author linqingwei
 * @date 2021-08-02 15:23
 */
@Slf4j
@Service
public class InstallPlanHandleExcelService implements HandleExcelService {

    @Autowired
    private InstallPlanServiceImpl installPlanService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private ImportFileServiceImpl importFileService;
    @Autowired
    private MessageSendUtil messageSendUtil;


    @Override
    public void handle(ExcelData data) {
        ExcelReader excelReader = EasyExcel.read(data.getImportFile().getImportFilePath()).build();
        // 这里为了简单 所以注册了 同样的head 和Listener 自己使用功能必须不同的Listener
        InstallPlanListener listener = new InstallPlanListener(installPlanService,data);
        CommDataListener deviceTypeListener = new CommDataListener();
        CommDataListener communityListener = new CommDataListener();
        CommDataListener orgListener = new CommDataListener();

        ReadSheet readSheet1 = EasyExcel.readSheet(0).head(InstallPlanExcelData.class).headRowNumber(9).registerReadListener(listener).build();
        ReadSheet readSheet2 = EasyExcel.readSheet("公司").head(CommImportData.class).registerReadListener(orgListener).build();
        ReadSheet readSheet3 = EasyExcel.readSheet("小区").head(CommImportData.class).registerReadListener(communityListener).build();
        ReadSheet readSheet4 = EasyExcel.readSheet("设备型号").head(CommImportData.class).registerReadListener(deviceTypeListener).build();
        // 这里注意 一定要把sheet1 sheet2 一起传进去，不然有个问题就是03版的excel 会读取多次，浪费性能
        excelReader.read(readSheet1, readSheet2, readSheet3, readSheet4);
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();
        List<InstallPlanExcelData> list = listener.getData();
        List<CommImportData> deviceTypelist = deviceTypeListener.getData();
        List<CommImportData> communityList = communityListener.getData();
        List<CommImportData> orgList = orgListener.getData();

        //生成失败列表
        if(data.getFailNum().get() > 0){
            DownloadExcelData downloadExcelData = new DownloadExcelData();
            downloadExcelData.setInstallPlanExcelDataList(list);
            downloadExcelData.setProductDataList(deviceTypelist);
            downloadExcelData.setCommunityDataList(communityList);
            downloadExcelData.setOrgDataList(orgList);
            downloadExcelData.setInstallPlanInfoData(data.getInstallPlanInfoData());
            downloadExcelData.setFilePath(commonService.getExcelFilePath("template"));

            InstallPlanExcelFillAdapter template = new InstallPlanExcelFillAdapter();
            template.build(downloadExcelData).fillData();

            data.getImportFile().setFailFilePath(downloadExcelData.getFilePath());
        }


        //  更新状态生成消息
        ImportFile updateImportFile = new ImportFile();
        updateImportFile.setId(data.getImportFile().getId());
        updateImportFile.setImportStatus(1);
        updateImportFile.setSuccessNum(data.getSuccessNum().get());
        updateImportFile.setFailNum(data.getFailNum().get());
        updateImportFile.setFailFilePath(data.getImportFile().getFailFilePath());
        importFileService.updateByPrimaryKeySelective(updateImportFile);

        sendMessage(data);

    }

    private void sendMessage(ExcelData data){

        Map<String,String> variables = new HashMap<>();
        variables.put("文档名称",data.getImportFile().getImportFileName());
        variables.put("success", String.valueOf(data.getSuccessNum().get()));
        variables.put("fail", String.valueOf(data.getFailNum().get()));

        Message message=new Message();
        message.setStatus(1);
        message.setCreateBy("system");
        message.setTenantId(data.getImportFile().getTenantId());
        message.setMessageContent(JSONObject.toJSONString(variables));
        message.setMessageId(UUID.randomUUID().toString());
        message.setMessageType(9);
        if(data.getFailNum().get() > 0) message.setUrl("/import/downloadFailFile?objId="+data.getImportFile().getId());
        message.setMessageTemplateId(9);
        message.setChannel(0);
        message.setUserId(data.getImportFile().getUserId());
        messageSendUtil.send(message);
    }


}
