package com.css.simulation.resource.server.adapter.controller.scene_library.standard;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.scene.SceneImportParam;
import api.common.pojo.param.scene.StandardsRegulationsParam;
import api.common.pojo.po.scene.*;
import api.common.pojo.po.system.SceneImportPO;
import api.common.pojo.vo.scene.StandardsRegulationsVO;
import api.common.util.CollectionUtil;
import com.css.simulation.resource.server.infra.util.PageUtil;
import com.css.simulation.resource.server.app.service.SceneStandardsService;
import com.css.simulation.resource.server.app.service.SceneImportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//标准法规
@RestController
@RequestMapping("/StandardsRegulations")
public class SceneStandardsController {

    @Resource
    private SceneStandardsService sceneStandardsService;
    @Resource
    private SceneImportService sceneImportService;

    //* -------------------------------- 场景上传 --------------------------------

    @PostMapping("/saveStandardsRegulationsBatch")
    public ResponseBodyVO<String> saveStandardsRegulationsBatch(@RequestBody SceneImportParam sceneImportParam) {
        SceneImportPO sceneImportPO = new SceneImportPO();
        sceneImportPO.setId(sceneImportParam.getId());
        sceneImportPO.setStatus(DictConstants.SCENE_IMPORT_STATUS_3);
        sceneImportService.updateStatus(sceneImportPO);
        // 解析参数
        sceneImportPO = sceneStandardsService.importMinio(sceneImportParam);
        sceneImportService.updateInfo(sceneImportPO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //* -------------------------------- Comment --------------------------------



    @PostMapping("/batchDeleteSceneStandards")
    public ResponseBodyVO<String> batchDeleteSceneStandards(@RequestBody StandardsRegulationsParam params) {
        List<String> sceneNameList = params.getSceneNameList();
        sceneStandardsService.batchDeleteSceneStandards(sceneNameList);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //查询列表
    @PostMapping("/queryStandardsRegulationsList")
    public ResponseBodyVO<PageInfo<StandardsRegulationsVO>> queryStandardsRegulationsList(@RequestBody StandardsRegulationsParam params) {
        PageUtil.setPageInfo(params);
        List<StandardsRegulationsPO> list = sceneStandardsService.queryStandardsRegulationsList(params);
        PageInfo<StandardsRegulationsPO> objectPageInfo = new PageInfo<>(list);
        PageHelper.clearPage();
        SceneEvaluationOperatePO sceneEvaluationOperatePO = sceneStandardsService.getSceneEvaluationFirst(params, list.stream().map(StandardsRegulationsPO::getRegulationsId).collect(Collectors.toList()));
        Map<String, SceneComplexityPO> complexityPOMap = sceneEvaluationOperatePO.getSceneComplexityPOMap();
        Map<String, SceneRiskPO> riskPOMap = sceneEvaluationOperatePO.getSceneRiskPOMap();
        List<StandardsRegulationsVO> list1 = new ArrayList<>();
        for (StandardsRegulationsPO po : list) {
            StandardsRegulationsVO vo = new StandardsRegulationsVO();
            BeanUtils.copyProperties(po, vo);
            // 赋值复杂度
            if (complexityPOMap.get(po.getRegulationsId()) != null) {
                vo.setComplexity(String.valueOf(complexityPOMap.get(po.getRegulationsId()).getComplexity()));
                vo.setComplexityLevel(String.valueOf(complexityPOMap.get(po.getRegulationsId()).getComplexityLevel()));
            }
            if (riskPOMap.get(po.getRegulationsId()) != null) {
                vo.setRisk(String.valueOf(riskPOMap.get(po.getRegulationsId()).getRisk()));
                vo.setRiskLevel(String.valueOf(riskPOMap.get(po.getRegulationsId()).getRiskLevel()));
            }
            list1.add(vo);
        }
        PageInfo<StandardsRegulationsVO> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(objectPageInfo.getTotal());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, pageInfo);
    }


    /**
     * 新增标准法规
     */
    @PostMapping("/saveStandardsRegulations")
    public ResponseBodyVO<String> saveStandardsRegulations(@RequestBody StandardsRegulationsParam params) {
        return sceneStandardsService.importBzFg(params);
    }


    /**
     * 删除
     */
    @PostMapping("/deleteStandardsRegulations")
    public ResponseBodyVO<String> deleteStandardsRegulations(@RequestBody StandardsRegulationsPO params) {
        sceneStandardsService.deleteStandardsRegulations(CollectionUtil.createArrayList(params.getSceneName()));
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }



    /**
     * 查詢法规类型
     */
    @PostMapping("/queryRegulationType")
    public ResponseBodyVO<List<String>> queryRegulationType() {
        return sceneStandardsService.queryRegulationType();
    }


    /**
     * 分享
     */
    @PostMapping("/instStandardsRegulation")
    public ResponseBodyVO<String> instStandardsRegulation(@RequestBody StandardsRegulationsPO po) {
        sceneStandardsService.instStandardsRegulation(po);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

}
