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


import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.model.VehicleParam;
import api.common.pojo.po.model.VehiclePO;
import api.common.pojo.vo.model.VehicleGSVO;
import api.common.pojo.vo.model.VehicleVO;
import api.common.util.FileUtil;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.VehicleService;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(value = "/vehicle", name = LogConstants.MODULE_VEHICLE)
public class VehicleController {

    @Resource
    private VehicleService vehicleService;

    //* -------------------------------- 删除动力学配置 --------------------------------
    @RequestMapping("/delVehicleById")
    public ResponseBodyVO<String> delVehicleById(@RequestBody VehiclePO vehiclePO) {
        vehicleService.delVehicleById(vehiclePO);
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }
    //* -------------------------------- Comment --------------------------------

    /**
     * 获取列表
     */
    @RequestMapping("/getVehicleList")
    public ResponseBodyVO<List<VehicleVO>> getVehicleList(@RequestBody VehicleParam vehicleParam) {
        ResponseBodyVO<List<VehicleVO>> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(vehicleService.getVehicleList(vehicleParam));
        return response;
    }

    /**
     * 获取私有列表
     */
    @RequestMapping("/getMyVehicleList")
    public ResponseBodyVO<List<VehicleVO>> getMyVehicleList() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, vehicleService.getMyVehicleList());
    }

    /**
     * 获取公私有列表
     */
    @RequestMapping("/getMyVehicleListNew")
    public ResponseBodyVO<List<VehicleGSVO>> getMyVehicleListNew() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, vehicleService.getMyVehicleListNew());
    }

    /**
     * 获取公私有列表
     */
    @RequestMapping("/getMyVehicleListNew1")
    public ResponseBodyVO<List> getMyVehicleListNew1() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, vehicleService.getMyVehicleListNew1());
    }

    /**
     * 根据id获取详情
     */
    @RequestMapping("/getVehicleInfo")
    public ResponseBodyVO<VehicleVO> getVehicleInfo(@RequestBody VehicleParam vehicleParam) {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, vehicleService.getVehicleInfo(vehicleParam));
    }

    /**
     * 新增、修改、另存为
     */
    @RequestMapping("/saveVehicle")
    public ResponseBodyVO<VehiclePO> saveVehicle(@RequestBody VehicleVO vehicleVO) {
        vehicleVO.setSaveOrShare("0");
        VehiclePO po = vehicleService.saveVehicle(vehicleVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "车辆名称重复！", null);
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, po);
    }

    /**
     * 分享
     */
    @RequestMapping("/shareVehicle")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<VehiclePO> shareVehicle(@RequestBody VehicleVO vehicleVO) {
        if (ObjectUtil.isNull(vehicleVO)) {
            return new ResponseBodyVO<>(false, 500, "参数必传！", null);
        }
        ResponseBodyVO<VehiclePO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        VehiclePO po = vehicleService.shareVehicle(vehicleVO);
        if (ObjectUtil.isNull(po.getId())) {
            return new ResponseBodyVO<>(false, 500, "车辆名称重复！", null);
        }
        response.setInfo(po);
        return response;
    }



    @PostMapping(value = "/parsePar", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseBodyVO<VehicleVO> parsePar(@RequestPart("par") MultipartFile par) {
        try {
            String read = FileUtil.read(par.getInputStream());
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, vehicleService.parsePar(read));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
