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

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.scene.ScenePackageParam;
import api.common.pojo.po.scene.ScenePackagePO;
import api.common.pojo.vo.scene.ScenePackageSublistVO;
import api.common.pojo.vo.scene.ScenePackageVO;
import api.common.util.ObjectUtil;
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.db.mysql.mapper.ScenePackageMapper;
import com.css.simulation.resource.server.app.service.ScenePackageService;
import com.css.simulation.resource.server.app.service.ParameterService;
import com.css.simulation.resource.server.infra.util.SceneUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
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.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping(value = "/ScenePackage", name = LogConstants.MODULE_SCENE_TEST_PACKAGE)
public class SceneTestPackageController {

    @Resource
    private ScenePackageService scenePackageService;
    @Resource
    private ScenePackageMapper scenePackageMapper;
    @Resource
    private ParameterService parameterService;
    @Resource
    private SceneUtil sceneUtil;

    //查询主表列表
    @PostMapping("/queryScenePackageList")
    public ResponseBodyVO<PageInfo<ScenePackageVO>> queryScenePackageList(@RequestBody ScenePackageParam parms) {
        PageUtil.setPageInfo(parms);
        List<ScenePackagePO> list = scenePackageService.queryScenePackageList(parms);
        PageInfo<ScenePackagePO> objectPageInfo = new PageInfo<>(list);
        List<ScenePackageVO> list1 = new ArrayList<>();
        for (ScenePackagePO po : list) {
            ScenePackageVO vo = new ScenePackageVO();
            BeanUtils.copyProperties(po, vo);
            list1.add(vo);
        }
        PageInfo<ScenePackageVO> pageInfo = new PageInfo<>(list1);
        pageInfo.setTotal(objectPageInfo.getTotal());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, pageInfo);
    }


    /**
     * 1. 新增场景测试包
     * 2. 修改保存
     * 3. 私有另存为成私有
     * 4. 公有另存为成私有
     */
    @PostMapping("/saveScenePackage")
    public ResponseBodyVO<String> saveScenePackage(@RequestBody Map<String, List<ScenePackageSublistVO>> map) {
        List<ScenePackageSublistVO> params = map.get("params");
        ScenePackageSublistVO scenePackageSublistVO = params.get(0);
        String packageId = scenePackageSublistVO.getId();
        int packageLevel = scenePackageSublistVO.getPackageLevel();
        // 检查场景测试包中的评分规则是否被删除
        sceneUtil.checkRulesByScenePackageSublistVO(scenePackageSublistVO);
        if ("1".equals(packageId)) {
            log.info("新增场景测试包（传 id 为 1）");
            params.get(0).setId("");
            scenePackageService.saveScenePackage(scenePackageSublistVO, packageLevel);
        } else if (0 == packageLevel) {
            log.info("修改保存。");
            scenePackageService.updateScenePackage(params);
        } else if (1 == packageLevel) {
            log.info("私有另存为成私有 或 公有另存为成私有");
            params.get(0).setId("");
            scenePackageService.saveScenePackage(scenePackageSublistVO, packageLevel);
        }
        // 将场景测试包修改成未禁用
        ScenePackagePO scenePackagePO = new ScenePackagePO();
        scenePackagePO.setPackageId(packageId);
        scenePackagePO.setModifyTime(TimeUtil.getNowForMysql());
        scenePackagePO.setModifyUserId(AuthUtil.getCurrentUserId());
        scenePackageMapper.updateIsUnavailable2(scenePackagePO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //修改
    @PostMapping("/updateScenePackage")
    public ResponseBodyVO<String> updateScenePackage(@RequestBody Map<String, List<ScenePackageSublistVO>> map) {
        List<ScenePackageSublistVO> params = map.get("params");
        return scenePackageService.updateScenePackage(params);
    }

    //查询一个测试包列表
    @PostMapping("/queryScenePackageSublistList")
    public ResponseBodyVO<List<ScenePackageSublistVO>> queryScenePackageSublistList(@RequestBody ScenePackageParam parms) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, scenePackageService.queryScenePackageSublistList(parms));
    }


    /**
     * 测试包分享
     */
    @PostMapping("/instShareList")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<String> instShareList(@RequestBody ScenePackageParam parms) {
        return scenePackageService.instShareList(parms);
    }

    //测试包删除
    @PostMapping("/deleteList")
    public ResponseBodyVO<String> deleteList(@RequestBody ScenePackageParam parms) {
        scenePackageService.deleteList(parms);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    //查询测试包数量
    @PostMapping("/queryPackageCount")
    public ResponseBodyVO<Map<String, Integer>> queryPackageCount() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, scenePackageService.queryPackageCount());
    }

    /**
     * 查询场景分配配置信息
     */
    @PostMapping("/getSceneParam")
    public ResponseBodyVO<Map<String, Integer>> getSceneParam() {
        Map<String, Integer> map = parameterService.getSceneParam();
        ScenePackageParam parms = new ScenePackageParam();
        parms.setShare("0");
        List<ScenePackagePO> list = scenePackageService.queryScenePackageList(parms);
        int creatNum = 0;
        if (ObjectUtil.isNotNull(list)) {
            creatNum = list.size();
        }
        if (ObjectUtil.isNotNull(map)) {
            map.put("creatNum", creatNum);
        } else {
            Map<String, Integer> map1 = new HashMap<>();
            map1.put("creatNum", 0);
            map1.put("numCreateScenePackage", 0);
            map1.put("numScenePerPackage", 0);
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, map1);
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, map);
    }

//    /**
//     * 1. 新增场景测试包
//     * 2. 修改保存
//     * 3. 私有另存为成私有
//     * 4. 公有另存为成私有
//     */
//    @PostMapping("/saveScenePackageNew")
//    public ResponseBodyVO<String> saveScenePackageNew(@RequestBody Map<String, List<ScenePackageSublistVO>> map) {
//        List<ScenePackageSublistVO> params = map.get("params");
//        ScenePackageSublistVO scenePackageSublistVO = params.get(0);
//        String packageId = scenePackageSublistVO.getId();
//        int packageLevel = scenePackageSublistVO.getPackageLevel();
//        sceneUtil.checkRules(packageId);
//        if ("1".equals(packageId)) {
//            log.info("新增场景测试包（传 id 为 1）");
//            params.get(0).setId("");
//            scenePackageService.saveScenePackage(scenePackageSublistVO, packageLevel);
//        } else if (0 == packageLevel) {
//            log.info("修改保存。");
//            scenePackageService.updateScenePackage(params);
//        } else if (1 == packageLevel) {
//            log.info("私有另存为成私有 或 公有另存为成私有");
//            params.get(0).setId("");
//            scenePackageService.saveScenePackage(scenePackageSublistVO, packageLevel);
//        }
//        // 将场景测试包修改成未禁用
//        ScenePackagePO scenePackagePO = new ScenePackagePO();
//        scenePackagePO.setPackageId(packageId);
//        scenePackagePO.setModifyTime(TimeUtil.getNowForMysql());
//        scenePackagePO.setModifyUserId(AuthUtil.getCurrentUserId());
//        scenePackageMapper.updateIsUnavailable2(scenePackagePO);
//        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
//    }

}
