package com.ho.business.controller;

import com.ho.business.entity.StationRemoteControl;
import com.ho.business.service.StationRemoteControlService;
import com.ho.business.service.StationService;
import com.ho.business.vo.req.StationRemoteControlPageVo;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

@RequestMapping(ContextConstant.BUSINESS + "stationRemote")
@RestController
@Api(tags = "业务模块-电站远控管理")
public class StationRemoteController {

    @Autowired
    private StationRemoteControlService stationRemoteControlService;

    @Autowired
    private StationService stationService;

    @PostMapping("saveOrUpdate")
    @ApiOperation(value = "新增/修改远控电站信息")
    @LogAnnotation(title = "电站远控管理", action = "新增/修改新增远控电站信息")
    public DataResult saveOrUpdate(@RequestBody @Valid StationRemoteControl entity) {
        if(entity==null){
            throw new BusinessException(BaseResponseCode.DATA_NOT_EXISTS);
        }
        if(entity.getId()==null){
            //新增
            StationRemoteControl stationRemoteControl = stationRemoteControlService.search(entity.getStationId());
            if (stationRemoteControl != null) {
                throw new BusinessException(BaseResponseCode.STATION_ALREADY_EXISTS);
            }
            entity.setCreateTime(new Date());
            stationRemoteControlService.insert(entity);
        }else{
            //修改
            entity.setUpdateTime(new Date());
            stationRemoteControlService.update(entity);
        }
        return DataResult.success();
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除远控电站信息")
    @LogAnnotation(title = "电站远控管理", action = "删除远控电站信息")
    public DataResult delete(@RequestBody Integer id) {
        stationRemoteControlService.delete(id);
        return DataResult.success();
    }

    @PostMapping("search")
    @ApiOperation(value = "查询远控电站信息")
    public DataResult<StationRemoteControl> search(@RequestBody Integer StationId) {
        StationRemoteControl stationRemoteControl=stationRemoteControlService.search(StationId);
        return DataResult.success(stationRemoteControl);
    }

    @PostMapping("pageList")
    @ApiOperation(value = "查询远控电站信息")
    public DataResult<PageResult<StationRemoteControl>> pageList(StationRemoteControlPageVo vo) {
        PageResult<StationRemoteControl> page = stationRemoteControlService.selectPageList(vo);
        return DataResult.success(page);
    }
}
