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

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.LogConstants;
import api.common.pojo.param.model.OgtParam;
import api.common.pojo.po.model.OgtPO;
import api.common.pojo.vo.model.OgtVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.OgtService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

@Controller
@RequestMapping(value = "/ogt",name = LogConstants.MODULE_OGT)
public class SensorOgtController {

    @Resource
    private OgtService ogtService;

    /**
     * 获取列表
     */
    @RequestMapping("/getOgtList")
    @ResponseBody
    public ResponseBodyVO<List<OgtVO>> getOgtList(@RequestBody OgtParam ogtParam){
        ResponseBodyVO<List<OgtVO>> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(ogtService.getOgtList(ogtParam));
        return response;
    }

    /**
     * 获取私有列表
     */
    @RequestMapping("/getMyOgtList")
    @ResponseBody
    public ResponseBodyVO<List<OgtVO>> getMyOgtList(@RequestBody OgtParam ogtParam){
        ResponseBodyVO<List<OgtVO>> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(ogtService.getMyOgtList(ogtParam));
        return response;
    }
    /**
     * 获取私有列表
     */
    @RequestMapping("/getMyOgtListNew")
    @ResponseBody
    public ResponseBodyVO<List<OgtVO>> getMyOgtListNew(@RequestBody OgtParam ogtParam){
        ResponseBodyVO<List<OgtVO>> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(ogtService.getMyOgtList1(ogtParam));
        return response;
    }

    /**
     * 根据id获取详情
     */
    @RequestMapping("/getOgtInfo")
    @ResponseBody
    public ResponseBodyVO<OgtVO> getOgtInfo(@RequestBody OgtParam ogtParam){
        ResponseBodyVO<OgtVO> response = new ResponseBodyVO<OgtVO>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(ogtService.getOgtInfo(ogtParam));
        return response;
    }

    /**
     * 新增、修改、另存为
     */
    @RequestMapping("/saveOgt")
    @ResponseBody
    public ResponseBodyVO<OgtPO> saveOgt(@RequestBody OgtPO ogtPO){
        if(ObjectUtil.isNull(ogtPO)){
            return new ResponseBodyVO(false, 500, "参数必传！",null);
        }
        ResponseBodyVO<OgtPO> response = new ResponseBodyVO<OgtPO>(ResponseBodyVO.Response.SUCCESS);
        OgtPO po = ogtService.saveOgt(ogtPO);
        if(ObjectUtil.isNull(po.getId())){
            return new ResponseBodyVO(false, 500, "传感器名称重复！",null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 分享
     */
    @RequestMapping("/shareOgt")
    @ResponseBody
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<OgtPO> shareOgt(@RequestBody OgtPO ogtPO){
        if(ObjectUtil.isNull(ogtPO)){
            return new ResponseBodyVO(false, 500, "参数必传！",null);
        }
        ResponseBodyVO<OgtPO> response = new ResponseBodyVO<OgtPO>(ResponseBodyVO.Response.SUCCESS);
        OgtPO po = ogtService.shareOgt(ogtPO);
        if(ObjectUtil.isNull(po.getId())){
            return new ResponseBodyVO(false, 500, "传感器名称重复！",null);
        }
        response.setInfo(po);
        return response;
    }

    /**
     * 删除
     */
    @RequestMapping("/delOgtById")
    @ResponseBody
    public ResponseBodyVO delOgtById(@RequestBody OgtPO ogtPO){
        int i = ogtService.delOgtById(ogtPO);
        if(i>0){
            return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS);
        }else{
            return new ResponseBodyVO(false, 500, "删除失败！",null);
        }
    }
}
