package com.njcn.energy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.OperateType;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.common.utils.LogUtil;
import com.njcn.energy.pojo.param.AirStrategyParam;
import com.njcn.energy.pojo.param.NetDevParam;
import com.njcn.energy.pojo.po.AirStrategy;
import com.njcn.energy.pojo.po.NetDev;
import com.njcn.energy.pojo.vo.AirStrategyVO;
import com.njcn.energy.pojo.vo.EnergyDeviceTreeVO;
import com.njcn.energy.pojo.vo.NetDevInfoVO;
import com.njcn.energy.service.EleAirStrategyService;
import com.njcn.energy.service.ILogicDeviceLineService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * pqs
 *
 * @author cdf
 * @date 2022/4/21
 */
@Slf4j
@RestController
@Api(tags = "空调控制策略")
@RequestMapping("/air")
@RequiredArgsConstructor
public class EleAirStrategyController extends BaseController {

    private final EleAirStrategyService eleAirStrategyService;

    private final ILogicDeviceLineService iLogicDeviceLineService;

    /**
     * 新增空调策略
     * @author cdf
     * @date 2022/2/22
     */
    @PostMapping("add")
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @ApiOperation("新增空调策略")
    @ApiImplicitParam(name = "airStrategyParam", value = "空调策略", required = true)
    public HttpResult<Object> add(@RequestBody  @Validated AirStrategyParam airStrategyParam){
        String methodDescribe = getMethodDescribe("add");
        LogUtil.njcnDebug(log, "{}，空调策略数据为：{}", methodDescribe, airStrategyParam);
        boolean result = eleAirStrategyService.addAirStrategy(airStrategyParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }

    }



    /**
     * 查询空调策略
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @PostMapping("/list")
    @ApiOperation("查询空调策略")
    @ApiImplicitParam(name = "queryParam", value = "查询参数", required = true)
    public HttpResult<Page<AirStrategy>> list(@RequestBody @Validated AirStrategyParam.AirStrategyQueryParam queryParam) {
        String methodDescribe = getMethodDescribe("list");
        LogUtil.njcnDebug(log, "{}，查询数据为：{}", methodDescribe, queryParam);
        Page<AirStrategy> result = eleAirStrategyService.listAirStrategy(queryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }


    /**
     * 修改空调策略
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改空调策略")
    @ApiImplicitParam(name = "updateParam", value = "空调策略设备数据", required = true)
    public HttpResult<Object> update(@RequestBody @Validated AirStrategyParam.AirUpdateParam updateParam) {
        String methodDescribe = getMethodDescribe("update");
        LogUtil.njcnDebug(log, "{}，空调策略数据为：{}", methodDescribe, updateParam);
        boolean result = eleAirStrategyService.updateAirStrategy(updateParam);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }

    }

    /**
     * 删除空调策略
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON, operateType = OperateType.DELETE)
    @GetMapping("/delete")
    @ApiOperation("删除空调策略")
    @ApiImplicitParam(name = "id", value = "策略索引", required = true)
    public HttpResult<Object> delete(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("delete");
        boolean result = eleAirStrategyService.deleteAirStrategy(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 停止空调策略
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/stop")
    @ApiOperation("停止空调策略")
    @ApiImplicitParam(name = "id", value = "策略索引", required = true)
    public HttpResult<Object> stopAirStrategy(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("stopAirStrategy");
        boolean result = eleAirStrategyService.stopAirStrategy(id);
        if (result) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 查询空调策略详情
     * @author cdf
     * @date 2022/3/28
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getAirStrategyInfo")
    @ApiOperation("查询空调策略")
    @ApiImplicitParam(name = "id", value = "查询参数", required = true)
    public HttpResult<AirStrategyVO> getAirStrategyInfo(@RequestParam("id")String id) {
        String methodDescribe = getMethodDescribe("getAirStrategyInfo");
        AirStrategyVO airStrategy  = eleAirStrategyService.findById(id);
        if(Objects.nonNull(airStrategy)){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, airStrategy, methodDescribe);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
    }



    /**
     * 查询空调策略详情
     * @author cdf
     * @date 2022/3/28
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/getAirLineTree")
    @ApiOperation("查询空调监测点树")
    public HttpResult<List<EnergyDeviceTreeVO>> getAirLineTree() {
        String methodDescribe = getMethodDescribe("getAirLineTree");
        List<EnergyDeviceTreeVO> list  = iLogicDeviceLineService.getAirLineTree();
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, list, methodDescribe);
    }


    /**
     * 执行按钮触发
     * @author cdf
     * @date 2022/4/22
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/startAirStrategy")
    @ApiOperation("执行空调策略任务")
    public HttpResult<Object> startAirStrategy(@RequestParam("id")String id) {
        String methodDescribe = getMethodDescribe("startAirStrategy");
        boolean res  = eleAirStrategyService.startAirStrategy(id);
        if(res){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        }else{
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 根据策略定时开启关闭空调
     * @author cdf
     * @date 2022/4/22
     */
    @OperateInfo(info = LogEnum.SYSTEM_COMMON)
    @GetMapping("/dealAirStrategyId")
    @ApiOperation("根据策略定时开启关闭空调")
    public void dealAirStrategyId(@RequestParam("operation")String operation) {
        eleAirStrategyService.dealAirStrategyId(operation);
    }







}
