package com.css.simulation.resource.server.app.service;

import api.common.pojo.constants.DictConstants;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.system.SceneImportPageParam;
import api.common.pojo.po.scene.SceneGeneralTemplatePO;
import api.common.pojo.po.system.SceneImportPO;
import api.common.util.CollectionUtil;
import api.common.util.ObjectUtil;
import api.common.util.StringUtil;
import api.common.util.TimeUtil;
import com.css.simulation.resource.server.infra.util.AuthUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.infra.util.PoUtil;
import com.css.simulation.resource.server.infra.db.mysql.mapper.SceneImportMapper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SceneImportService {

    @Resource
    private DictService dictService;
    @Resource
    private LogService logService;
    @Resource
    private SceneImportMapper sceneImportMapper;
    @Resource
    private SceneNaturalService sceneNaturalService;
    @Resource
    private SceneStandardsService sceneStandardsService;
    @Resource
    private SceneAccidentService sceneAccidentService;
    @Resource
    private SceneReferenceLibService sceneReferenceLibService;
    @Resource
    private SceneGeneralTemplateService sceneGeneralTemplateService;

    public SceneImportPO saveTask(SceneImportPO po) {
        // 名称校验
        List<SceneImportPO> list = sceneImportMapper.checkName(po);
        if (ObjectUtil.isNotNull(list)) {
            po.setId(null);
            return po;
        }
        // 常规字段赋值
        PoUtil.initAddPo(po);
        String id = po.getId();
        po.setStatus(DictConstants.SCENE_IMPORT_STATUS_0); //重置状态
        if (ObjectUtil.isNull(id)) { // 新增场景导入任务
            po.setId(StringUtil.getRandomUUID());
            sceneImportMapper.insert(po);
            // 记录日志
            Map<String, String> dictMap = dictService.getDictMapByType(DictConstants.SCENE_TYPE);
            String type = dictMap.get(po.getSceneType());
            String content = "创建" + type + "上传任务:" + po.getName() + "(ID:" + po.getId() + ")";
            logService.logSceneUpload(LogConstants.SYS_LOG_SCENE_TASK_INSERT, content);
            log.info(content);
        } else {
            sceneImportMapper.update(po);
        }
        return po;
    }


    public void updateStatus(SceneImportPO params) {
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        sceneImportMapper.updateStatus(params);
    }

    public void update(SceneImportPO params) {
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        sceneImportMapper.update(params);
    }

    public void updateInfo(SceneImportPO params) {
        params.setModifyTime(TimeUtil.getNowForMysql());
        params.setModifyUserId(AuthUtil.getCurrentUserId());
        sceneImportMapper.updateInfo(params);
    }

    public PageInfo<SceneImportPO> getSceneImporPagetList(SceneImportPageParam param) {
        PageUtil.setPageInfo(param);
        List<SceneImportPO> list = sceneImportMapper.getSceneImporPagetList(param);
        //字典翻译
        Map<String, Map<String, String>> dictMaps = dictService.getDictMapsByTypes(DictConstants.SCENE_TYPE + "," + DictConstants.SCENE_IMPORT_STATUS);
        list.forEach(po -> {
            po.setSceneType(dictMaps.get(DictConstants.SCENE_TYPE).get(po.getSceneType()));
            po.setStatus(dictMaps.get(DictConstants.SCENE_IMPORT_STATUS).get(po.getStatus()));
        });
        return new PageInfo<>(list);
    }

    @Transactional
    @SneakyThrows
    public void deleteTask(String taskId) {
        // 查询任务相关的场景类型和场景列表
        SceneImportPO sceneImportPO = sceneImportMapper.querySceneImportByTaskId(taskId);
        sceneImportPO.setModifyTime(TimeUtil.getNowForMysql());
        sceneImportPO.setModifyUserId(AuthUtil.getCreateUserId());
        // 获取各个参数
        String sceneType = sceneImportPO.getSceneType();    // 场景类型
        String sceneNameSequenceString = sceneImportPO.getSceneNames(); // 场景名称
        sceneImportMapper.deleteImportTask(sceneImportPO);
        if (StringUtil.isNotEmpty(sceneNameSequenceString)) {
            List<String> sceneNames = CollectionUtil.sequenceStringToList(sceneNameSequenceString, ",");
            //2 根据任务id删除相关联的所有同名任务
            if (DictConstants.SCENE_NATURAL.equals(sceneType)) {
                sceneNaturalService.deleteSceneNatural(sceneNames);
            } else if (DictConstants.SCENE_STANDARD.equals(sceneType)) {
                sceneStandardsService.deleteStandardsRegulations(sceneNames);
            } else if (DictConstants.SCENE_ACCIDENT.equals(sceneType)) {
                sceneAccidentService.deleteSceneAccidentList(sceneNames);
            } else if (DictConstants.SCENE_REFERENCE_LIB.equals(sceneType)) {
                sceneReferenceLibService.batchDeleteSceneReference(sceneNames);
            } else if (DictConstants.SCENE_GENERAL.equals(sceneType)) {
                SceneGeneralTemplatePO po = new SceneGeneralTemplatePO();
                for (String id : sceneNames) {
                    po.setSceneId(id);
                    sceneGeneralTemplateService.deleteSceneGeneralTemplateById(po);
                }
            } else {
                throw new RuntimeException("未知场景类型：" + sceneType);
            }
        }
    }
}
