package com.njcn.device.controller;

import cn.hutool.core.util.StrUtil;
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.exception.BusinessException;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.enums.DeviceResponseEnum;
import com.njcn.device.pojo.param.TerminalMainQueryParam;
import com.njcn.device.pojo.param.TerminalParam;
import com.njcn.device.pojo.vo.TerminalMaintainVO;
import com.njcn.device.service.TerminalMaintainService;
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 javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * pqs
 * 终端运维管理
 *
 * @author cdf
 * @date 2022/5/11
 */
@Slf4j
@Api(tags = "终端运维管理")
@RestController
@RequestMapping("maintain")
@RequiredArgsConstructor
@Validated
public class TerminalMaintainController extends BaseController {

    private final TerminalMaintainService terminalMaintainService;

    /**
     * 查询终端运维主界面列表
     *
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getTerminalMainList")
    @ApiOperation("获取终端运维主列表")
    public HttpResult<List<TerminalMaintainVO>> getTerminalMainList(TerminalMainQueryParam terminalMainQueryParam) {
        String methodDescribe = getMethodDescribe("getTerminalMainList");
        List<TerminalMaintainVO> resList = terminalMaintainService.getTerminalMainList(terminalMainQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    /**
     * 终端状态管理,流量管理，流量策略管理主界面
     *
     * @param ids 装置id集
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getStatusManageList")
    @ApiOperation("终端状态管理列表")
    public HttpResult<List<TerminalMaintainVO>> getStatusManageList(@RequestBody @NotEmpty(message = "id不可为空") List<String> ids) {
        String methodDescribe = getMethodDescribe("getStatusManageList");
        List<TerminalMaintainVO> resList = terminalMaintainService.getManageList(ids, 0);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    /**
     * 终端流量配置主界面
     *
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getFlowManageList")
    @ApiOperation("终端流量管理列表")
    public HttpResult<List<TerminalMaintainVO>> getFlowManageList(@RequestBody @NotEmpty(message = "id不可为空") List<String> ids) {
        String methodDescribe = getMethodDescribe("getFlowManageList");
        List<TerminalMaintainVO> resList = terminalMaintainService.getManageList(ids, 1);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    /**
     * 终端流量策略配置主界面
     *
     * @author cdf
     * @date 2022/5/11
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getFlowStrategyManageList")
    @ApiOperation("终端流量策略列表")
    public HttpResult<List<TerminalMaintainVO>> getFlowStrategyManageList(@RequestBody @NotEmpty(message = "id不可为空") List<String> ids) {
        String methodDescribe = getMethodDescribe("getFlowStrategyManageList");
        List<TerminalMaintainVO> resList = terminalMaintainService.getManageList(ids, 2);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    /**
     * 批量修改终端状态
     *
     * @author cdf
     * @date 2022/5/12
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateRunFlagManage")
    @ApiOperation("批量修改终端状态")
    @ApiImplicitParam(name = "terminalParam", value = "实体", required = true)
    public HttpResult<List<TerminalMaintainVO>> updateRunFlagManage(@RequestBody TerminalParam terminalParam) {
        String methodDescribe = getMethodDescribe("updateRunFlagManage");
        if(Objects.isNull(terminalParam.getRunFlag())){
            throw new BusinessException(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION);
        }
        boolean res = terminalMaintainService.updateRunFlagManage(terminalParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 批量修改终端流量
     *
     * @author cdf
     * @date 2022/5/12
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateDevFlowMeal")
    @ApiOperation("批量修改终端流量套餐")
    @ApiImplicitParam(name = "terminalParam", value = "实体", required = true)
    public HttpResult<Object> updateDevFlowMeal(@RequestBody TerminalParam terminalParam) {
        String methodDescribe = getMethodDescribe("updateDevFlowMeal");
        if(StrUtil.isBlank(terminalParam.getBaseMealId()) && StrUtil.isBlank(terminalParam.getExtendMealId())){
            throw new BusinessException(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION);
        }
        boolean res = terminalMaintainService.updateFlowMeal(terminalParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


    /**
     * 批量修改终端流量策略
     *
     * @author cdf
     * @date 2022/5/12
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON, operateType = OperateType.UPDATE)
    @PostMapping("/updateDevFlowStrategy")
    @ApiOperation("批量修改终端流量策略")
    @ApiImplicitParam(name = "terminalParam", value = "实体", required = true)
    public HttpResult<Object> updateDevFlowStrategy(@RequestBody TerminalParam terminalParam) {
        String methodDescribe = getMethodDescribe("updateDevFlowStrategy");
        if(StrUtil.isBlank(terminalParam.getFlowStrategyId())){
            throw new BusinessException(CommonResponseEnum.ILLEGAL_ARGUMENT_EXCEPTION);
        }
        boolean res = terminalMaintainService.updateFlowStrategy(terminalParam);
        if (res) {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, null, methodDescribe);
        }
    }


}
