/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.gpsboot.service.emergency.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.gpsboot.common.Constant;
import com.tofly.gpsboot.common.system.FileTableEnum;
import com.tofly.gpsboot.mapper.emergency.EmergencyDisMapper;
import com.tofly.gpsboot.service.comm.IFileInfoService;
import com.tofly.gpsboot.service.emergency.EmergencyDisService;
import com.tofly.gpsboot.service.report.IReportEmergencyDisServer;
import com.tofly.pojo.emergency.EmergencyDis;
import com.tofly.pojo.plan.dto.ReportAuditDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 应急事件处理日志记录表
 *
 * @author admin
 * @date Tue Feb 22 00:00:00 CST 2022
 */
@Service("emergencyDisService")
public class EmergencyDisServiceImpl extends ServiceImpl<EmergencyDisMapper, EmergencyDis> implements EmergencyDisService {

    @Autowired
    private IFileInfoService iFileInfoService;
    @Value("${xrty.ftp.emergencyDisFilePath}")
    private String emergencyDisFilePath;

    @Autowired
    private IReportEmergencyDisServer iReportEmergencyDisServer;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveInfo(List<MultipartFile> fileList, EmergencyDis emergencyDis) {
        // 原文件
        String fileIdsOne = emergencyDis.getFileIdsOne();
        String fileIdsTow = emergencyDis.getFileIdsTow();
        String fileIdsThree = emergencyDis.getFileIdsThree();

        if (null != fileList && fileList.size() > 0) {
            Map<String, String> stringListMap = new HashMap<>(12);
            Map<String, List<MultipartFile>> fileMap = new HashMap<>(12);
            String key1 = "1";
            String key2 = "2";
            String key3 = "3";
            fileList.forEach(file -> {
                // 分层 前/中/后 文件名称前缀分别新增1 2 3
                String originalFilename = file.getOriginalFilename();
                assert originalFilename != null;
                String[] split = originalFilename.split(Constant.DATE_TILDE);
                String flag = split[0];
                if (key1.equals(flag)) {
                    disposeFile(fileMap, key1, file);
                } else if (key2.equals(flag)) {
                    disposeFile(fileMap, key2, file);
                } else {
                    disposeFile(fileMap, key3, file);
                }

            });
            fileMap.forEach((k, v) -> {
                StringBuilder builder = iFileInfoService.saveFileList(v, emergencyDisFilePath, FileTableEnum.TF_YWPN_EMERGENCY_DIS_W.getTableName());
                stringListMap.put(k, builder.toString());
            });
            String one = stringListMap.get(key1);
            String tow = stringListMap.get(key2);
            String three = stringListMap.get(key3);
            if(!StringUtils.isEmpty(one)){
                if(!StringUtils.isEmpty(fileIdsOne)){
                    fileIdsOne = fileIdsOne + Constant.CSV + one;
                }else {
                    fileIdsOne = one;
                }
            }
            if(!StringUtils.isEmpty(tow)){
                if(!StringUtils.isEmpty(fileIdsTow)){
                    fileIdsTow = fileIdsTow + Constant.CSV + tow;
                }else {
                    fileIdsTow = tow;
                }
            }
            if(!StringUtils.isEmpty(three)){
                if(!StringUtils.isEmpty(fileIdsThree)){
                    fileIdsThree = fileIdsThree + Constant.CSV + three;
                }else {
                    fileIdsThree = three;
                }
            }


        }
        emergencyDis.setFileIdsOne(fileIdsOne);
        emergencyDis.setFileIdsTow(fileIdsTow);
        emergencyDis.setFileIdsThree(fileIdsThree);

       return this.save(emergencyDis);
    }

    @Override
    public boolean updateInfo(List<MultipartFile> fileList, EmergencyDis emergencyDis) {
        String isHis = emergencyDis.getIsHis();
        // 判断是否为驳回后的数据
        if("1".equals(isHis)){
            // 设置无效
            emergencyDis.setFlag("0");
            this.updateById(emergencyDis);
            // id设置为null isHis设置为0 有效设置为1 审核信息清空

            emergencyDis.setIsHis("0");
            emergencyDis.setFlag("1");
            emergencyDis.setAuditResult(null);
            emergencyDis.setAuditNote(null);
            emergencyDis.setAuditTime(null);
            emergencyDis.setAuditUser(null);
        }else {
            this.removeById(emergencyDis);
        }
        emergencyDis.setId(null);
        this.saveInfo(fileList,emergencyDis);

        return true;
    }

    @Override
    public boolean reportInfo(Long emergencyId, Long auditUserId) {
        // 对当前计划派工人 发送审核消息/代办信息/当前计划状态设置为待审核
        ReportAuditDto dto = new ReportAuditDto();
        dto.setAuditId(auditUserId);
        dto.setId(emergencyId);
        iReportEmergencyDisServer.report(dto);
        return true;
    }

    private void disposeFile(Map<String, List<MultipartFile>> fileMap, String key, MultipartFile file) {
        List<MultipartFile> multipartFiles;
        if (fileMap.containsKey(key)) {
            multipartFiles = fileMap.get(key);
        } else {
            multipartFiles = new ArrayList<>();
        }
        multipartFiles.add(file);
        fileMap.put(key, multipartFiles);
    }
}
