package com.ho.business.controller;

import com.ho.business.constant.DeviceTypeConstant;
import com.ho.business.entity.Station;
import com.ho.business.entity.Topology;
import com.ho.business.service.StationService;
import com.ho.business.service.TopologyService;
import com.ho.business.vo.req.TopologyInReqVo;
import com.ho.business.vo.req.TopologyReqVo;
import com.ho.business.vo.req.TopologyType;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@RequestMapping(ContextConstant.BUSINESS + "topology")
@RestController
@Api(tags = "业务模块-拓扑管理")
@Slf4j
public class TopologyController {

    @Autowired
    private TopologyService topologyService;

    @Autowired
    private StationService stationService;

    @Autowired
    private RedisService redisService;


    //根据电站id查询
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询拓扑信息")
    //@LogAnnotation(title = "拓扑管理", action = "根据id查询拓扑信息")
    public DataResult<List<Topology>> selectByIds(@PathVariable @Valid Integer id) {
        DataResult<List<Topology>> result = DataResult.success();
        List<Topology> topology = topologyService.selectByStationId(id);
        result.setData(topology);
        return result;
    }

    //根据电站id查询
    @PostMapping
    @ApiOperation(value = "查询所有拓扑信息")
    public DataResult<List<Topology>> selectTopologys(@RequestBody @Valid TopologyType topologyType, HttpServletRequest request) {
        //获取该用户所有在部门下的所有电站
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        SimpleUser user = redisService.getSimpleUserByToken(token);
        //如果是超级管理员,提示 平台用户管理员不能直接查看监控页面,请使用集团用户登陆
        if (user.getPlatSuper()) {
            return DataResult.getResult(BaseResponseCode.PLAT_USER_CANNOT_MONITOR);
        }
        List<Station> list = stationService.selectByDeptIds(user.getDepts());
        log.info("station list:" + list);
        if (list == null || list.isEmpty()){
            return DataResult.getResult(BaseResponseCode.STATION_NO_FOUND);
        }
        List<Topology> topologyList = new ArrayList<>();
        //判断是否有参数
        if(topologyType.getType()==null){
         topologyType.setType(DeviceTypeConstant.PACK);
            topologyList = topologyService.selectByType(topologyType,list);
        }else{
            topologyList = topologyService.selectByType(topologyType,list);
        }

        log.info("topology list:" + topologyList);
        return DataResult.success(topologyList);
    }

    //新增
    @PostMapping("add")
    @ApiOperation(value = "新增拓扑接口")
    @LogAnnotation(title = "拓扑管理", action = "新增拓扑信息")
    public DataResult<Topology> add(@RequestBody @Valid TopologyReqVo vo) {
        DataResult<Topology> result = new DataResult<>();
        Topology topo = topologyService.selectBysev(vo);
        if (topo != null) {
            return result;
        }
        Topology topology = new Topology();
        BeanUtils.copyProperties(vo, topology);
        int num = topologyService.insertSelective(topology);
        result.setData(topology);
        return result;
    }

    //删除
    @DeleteMapping("del/{id}")
    @ApiOperation(value = "删除拓扑接口")
    @LogAnnotation(title = "拓扑管理", action = "删除拓扑信息")
    public DataResult deleteBytopologyId(@PathVariable @Valid Integer id) {
        topologyService.deleteBytopologyId(id);
        return DataResult.success();
    }

    //修改
    @PutMapping("update")
    @ApiOperation(value = "更新拓扑接口")
    @LogAnnotation(title = "拓扑管理", action = "更新拓扑信息")
    public DataResult update(@RequestBody @Valid TopologyInReqVo vo) {
        topologyService.updateByPrimaryKeySelective(vo);
        return DataResult.success();
    }




}
