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.InstallMeterExcelFillAdapter;
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.DeviceImportListener;
import com.zcxk.rmcp.web.excel.resolver.listener.InstallImportListener;
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.InstallListService;
import com.zcxk.rmcp.web.service.install.impl.InstallListServiceImpl;
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.Map;
import java.util.UUID;

/**
 * @author linqingwei
 * @date 2021-07-28 15:27
 */
@Slf4j
@Service
public class InstallHandleExcelServiceImpl implements HandleExcelService {

    @Autowired
    private InstallListServiceImpl installListService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private MessageSendUtil messageSendUtil;
    @Autowired
    private ImportFileServiceImpl importFileService;

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


        ReadSheet readSheet1 = EasyExcel.readSheet(0).head(InstallMeterData.class).headRowNumber(7).registerReadListener(listener).build();
        // 这里注意 一定要把sheet1 sheet2 一起传进去，不然有个问题就是03版的excel 会读取多次，浪费性能
        excelReader.read(readSheet1);
        // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
        excelReader.finish();

        //生成失败列表
        if(data.getFailNum().get() > 0){
            DownloadExcelData downloadExcelData = new DownloadExcelData();
            downloadExcelData.setInstallMeterDataList(listener.getData());

            downloadExcelData.setFilePath(commonService.getExcelFilePath("template"));

            InstallMeterExcelFillAdapter template = new InstallMeterExcelFillAdapter();
            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);
    }
}
