package com.njcn.device.terminal.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.common.utils.LogUtil;
import com.njcn.device.pq.pojo.param.DeviceInfoParam;
import com.njcn.device.pq.pojo.param.PqsParam;
import com.njcn.device.pq.pojo.param.TerminalMainQueryParam;
import com.njcn.device.pq.pojo.param.TerminalParam;
import com.njcn.device.pq.pojo.vo.LineFlowMealDetailVO;
import com.njcn.device.pq.pojo.vo.TerminalDaliyFlowVO;
import com.njcn.device.pq.pojo.vo.TerminalMaintainVO;
import com.njcn.device.terminal.service.TerminalMaintainService;
import com.njcn.web.controller.BaseController;
import com.njcn.web.utils.RequestUtil;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
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("获取终端运维主列表")
    @ApiImplicitParam(name = "terminalMainQueryParam",required = true)
    public HttpResult<List<TerminalMaintainVO>> getTerminalMainList(@RequestBody TerminalMainQueryParam terminalMainQueryParam) {
        String methodDescribe = getMethodDescribe("getTerminalMainList");
        List<TerminalMaintainVO> resList = terminalMaintainService.getTerminalMainList(terminalMainQueryParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, resList, methodDescribe);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getMonthFlow")
    @ApiOperation("获取每月流量")
    @ApiImplicitParam(name = "deviceInfoParam", value = "实体", required = true)
    public HttpResult<List<LineFlowMealDetailVO>> getMonthFlow (@RequestBody PqsParam deviceInfoParam){
        String methodDescribe = getMethodDescribe("getMonthFlow");
        LogUtil.njcnDebug(log, "{}，参数集合：{}", methodDescribe, deviceInfoParam);
        if (deviceInfoParam.getDeptIndex() == null) {
            DeviceInfoParam param = new DeviceInfoParam(RequestUtil.getDeptIndex(),null);
            deviceInfoParam.setDeptIndex(param.getDeptIndex());
        }
        List<LineFlowMealDetailVO> result = terminalMaintainService.getMonthFlow(deviceInfoParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, 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);
    }

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/getFlowManageDetail")
    @ApiOperation("终端流量管理详情")
    public HttpResult<List<TerminalDaliyFlowVO>> getFlowManageDetail(@RequestBody TerminalMainQueryParam param) {
        String methodDescribe = getMethodDescribe("getFlowManageDetail");
        List<TerminalDaliyFlowVO> res = terminalMaintainService.getFlowManageDetail(param);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, res, 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);
        }
    }


}
