package com.css.simulation.resource.server.adapter.controller.model_library.vehicle_configuration;


import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.model.ConfigPageParam;
import api.common.pojo.po.model.ConfigPO;
import api.common.pojo.vo.model.ConfigSensorVO;
import api.common.pojo.vo.model.ConfigVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.ConfigService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
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.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/config", name = LogConstants.MODULE_CONFIG)
public class ModelConfigController {

    @Resource
    private ConfigService configService;

    /**
     * 获取列表
     */
    @RequestMapping("/getConfigPageList")
    public ResponseBodyVO<PageInfo<ConfigVO>> getConfigPageList(@RequestBody @Validated ConfigPageParam pageParam) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, configService.getConfigPageList(pageParam));
    }


    /**
     * 根据id获取详情
     */
    @RequestMapping("/getConfigInfo")
    public ResponseBodyVO<ConfigVO> getConfigInfo(@RequestBody ConfigVO configVO) {
        ResponseBodyVO<ConfigVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(configService.getConfigInfo(configVO));
        return response;
    }
    /**
     * 根据id获取详情
     */
    @RequestMapping("/getConfigInfoNew")
    public ResponseBodyVO<ConfigVO> getConfigInfoNew(@RequestBody ConfigVO configVO) {
        ResponseBodyVO<ConfigVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(configService.getConfigInfoNew(configVO));
        return response;
    }


    /**
     * 新增、修改
     */
    @RequestMapping("/saveConfig")
    public ResponseBodyVO<ConfigPO> saveConfig(@RequestBody ConfigVO configVO) {
        configVO.setIsUnavailable("0");
        // 端口重复校验
        Map<String, List<ConfigSensorVO>> configSensors = configVO.getConfigSensors();
        if (ObjectUtil.isNotNull(configSensors)) {
            List<ConfigSensorVO> configSensorVOS = configSensors.get("ogt");//完美传感器
            if (ObjectUtil.isNotNull(configSensorVOS)) {
                long count = configSensorVOS.stream().mapToInt(ConfigSensorVO::getSensorPort).distinct().count();
                if (configSensorVOS.size() != count) {
                    return new ResponseBodyVO<>(false, 500, "端口重复。", null);
                }
            }
        }
        ResponseBodyVO<ConfigPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ConfigPO po = configService.saveConfig(configVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "配置名称重复。", null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 另存为
     */
    @RequestMapping("/saveAnotherConfig")
    public ResponseBodyVO<ConfigPO> saveAnotherConfig(@RequestBody ConfigVO configVO) {
        configVO.setIsUnavailable("0");
        //端口重复校验
        Map<String, List<ConfigSensorVO>> configSensors = configVO.getConfigSensors();
        if (ObjectUtil.isNotNull(configSensors)) {
            List<ConfigSensorVO> configSensorVOS = configSensors.get("ogt");//完美传感器
            if (ObjectUtil.isNotNull(configSensorVOS)) {
                long count = configSensorVOS.stream().mapToInt(ConfigSensorVO::getSensorPort).distinct().count();
                if (configSensorVOS.size() != count) {
                    return new ResponseBodyVO<>(false, 500, "端口重复！", null);
                }
            }
        }
        ResponseBodyVO<ConfigPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ConfigPO po = configService.saveAnotherConfig(configVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "配置名称重复！", null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 详情分享
     */
    @RequestMapping("/shareConfigByInfo")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<ConfigPO> shareConfigByInfo(@RequestBody ConfigVO configVO) {
        if (ObjectUtil.isNull(configVO)) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        ResponseBodyVO<ConfigPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ConfigPO po = configService.shareConfigByInfo(configVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "配置名称重复！", null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 列表分享
     */
    @RequestMapping("/shareConfigById")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<ConfigPO> shareConfigById(@RequestBody ConfigVO configVO) {
        ResponseBodyVO<ConfigPO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        ConfigPO po = configService.shareConfigById(configVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "配置名称重复！", null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 删除
     */
    @RequestMapping("/delConfigById")
    public ResponseBodyVO<String> delConfigById(@RequestBody ConfigVO configVO) {
        int i = configService.delConfigById(configVO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }
}
