package vip.xiaonuo.biz.api.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.biz.api.cloud.param.CloudDeviceParam;
import vip.xiaonuo.biz.api.cloud.param.DepotCloudAlarmAddParam;
import vip.xiaonuo.biz.api.cloud.param.TruckBookFlowParam;
import vip.xiaonuo.biz.api.cloud.param.TruckBookFlowQueryParam;
import vip.xiaonuo.biz.api.cloud.service.CloudService;
import vip.xiaonuo.biz.core.Const.BizStr;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.entity.DepotTruckAdmitFlow;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.service.DepotTruckAdmitFlowService;
import vip.xiaonuo.biz.modular.depotTruckBook.entity.DepotTruckBook;
import vip.xiaonuo.biz.modular.depotTruckBook.service.DepotTruckBookService;
import vip.xiaonuo.biz.modular.depotTruckPlateList.param.DepotTruckPlateListPushParam;
import vip.xiaonuo.biz.modular.depotTruckPlateList.service.DepotTruckPlateListService;
import vip.xiaonuo.biz.modular.depotWorkAlarm.entity.DepotWorkAlarm;
import vip.xiaonuo.biz.modular.depotWorkAlarm.param.DepotWorkAlarmAddParam;
import vip.xiaonuo.biz.modular.depotWorkAlarm.service.DepotWorkAlarmService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "云端控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class CloudController {

    @Resource
    private CloudService cloudService;
    @Resource
    private DepotWorkAlarmService depotWorkAlarmService;

    /**
     * 云端添加监控异常
     *
     * @author xiang
     * @date 2023/10/23 16:28
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端添加监控异常")
    @PostMapping("/api/cloud/depotWorkAlarm/add")
    public CommonResult<Object> add(@RequestBody DepotCloudAlarmAddParam param) {
        if (Strings.isBlank(param.getFlowType())
                || ("67".contains(param.getFlowType()) && Strings.isBlank(param.getDepotId()))
                || (!"67".contains(param.getFlowType()) && Strings.isBlank(param.getTruckBookId())))
            return CommonResult.error("缺少参数");
        cloudService.depotWorkAlarmAdd(param);
        if (Strings.isNotBlank(param.getTruckBookId()))
            return CommonResult.data(depotTruckBookService.getOneByBookId(param.getTruckBookId()));
        return CommonResult.ok();
    }

    /**
     * 云端查询出入库异常校验 弃用
     *
     * @author xiang
     * @date 2023/10/23 16:28
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端查询出入库异常校验")
    @CommonLog("云端查询出入库异常校验")
    @PostMapping("/api/cloud/depotWorkAlarm/check")
    public CommonResult<Object> check(@RequestBody DepotWorkAlarmAddParam param) {
        if (ArrayUtil.hasEmpty(param.getTruckBookId(), param.getFlowType()))
            return CommonResult.error("缺少参数");
        DepotWorkAlarm alarm = depotWorkAlarmService.getOne(new DepotWorkAlarm(param.getTruckBookId(), param.getFlowType()));
        //无异常
        if (alarm == null)
            return CommonResult.code(501);
        //有异常_仅提示
        if (BizStr.ALARM_TYPE_Y.equals(alarm.getAlarmType()))
            return CommonResult.code(502);
        //有异常_未处理
        if (!BizStr.STATE_TRUCK_Y.equals(alarm.getSaveState()))
            return CommonResult.code(500);
        //有异常_已处理
        return CommonResult.ok();
    }

    @Resource
    private DepotTruckBookService depotTruckBookService;

    /**
     * 云端获取车辆预约表
     *
     * @author xiang
     * @date 2023/09/13 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("云端获取车辆预约表")
    @GetMapping("/api/cloud/depotTruckBook/list")
    public CommonResult<List<DepotTruckBook>> list(DepotTruckBook param) {
        return CommonResult.data(depotTruckBookService.lst(param));
    }

    /**
     * 云端获取车辆预约待入场
     * 根据前车牌
     *
     * @author xiang
     * @date 2023/09/13 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("云端获取车辆预约详情")
    @RequestMapping("/api/cloud/depotTruckBook/onWait")
    public CommonResult<DepotTruckBook> onWaitTruckBook(DepotTruckBook param) {
        return CommonResult.data(depotTruckBookService.onWait(param.getDepotId(), param.getLicensePlate()));
    }

    /**
     * 云端获取车辆预约详情
     *
     * @author xiang
     * @date 2023/09/13 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("云端获取车辆预约详情")
    @GetMapping("/api/cloud/depotTruckBook/detail")
    public CommonResult<DepotTruckBook> detail(DepotTruckBook param) {
        return CommonResult.data(depotTruckBookService.getOneByBookId(param.getTruckBookId()));
    }

    /**
     * 云端获取车辆票单信息
     *
     * @author xiang
     * @date 2023/09/13 09:21
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("获取车辆票单信息")
    @GetMapping("/api/cloud/depotTruckBook/bill")
    public Object bill(DepotTruckBook param) {
        return CommonResult.data(depotTruckBookService.bill(param.getTruckBookId()));
    }

    @Resource
    private DepotTruckAdmitFlowService depotTruckAdmitFlowService;

    /**
     * 云端车辆接待流程表
     *
     * @author xiang
     * @date 2023/09/13 18:44
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端车辆接待流程表")
    @CommonLog("云端车辆接待流程表")
    @PostMapping("/api/cloud/depotTruckAdmitFlow/list")
    public CommonResult<List<DepotTruckAdmitFlow>> list(@RequestBody @Valid DepotTruckAdmitFlow param) {
        return CommonResult.data(depotTruckAdmitFlowService.lst(param));
    }

    /**
     * 云端添加车辆进程
     *
     * @author xiang
     * @date 2023/10/23 16:28
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端添加车辆进程")
    @CommonLog("云端添加车辆进程")
    @PostMapping("/api/cloud/truckBookFlow/push")
    public CommonResult<Object> push(@RequestBody @Valid TruckBookFlowQueryParam bookFlowParam) {
        return CommonResult.data(cloudService.truckBookFlowPush(BeanUtil.copyProperties(bookFlowParam, TruckBookFlowParam.class)));
    }

    /**
     * 云端添加设备同步
     *
     * @author xiang
     * @date 2023/10/23 16:28
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端添加设备同步")
    @CommonLog("云端添加设备同步")
    @PostMapping("/api/cloud/depotDevice/push")
    public CommonResult<Object> depotDevicePush(@RequestBody CloudDeviceParam param) {
        cloudService.depotDevicePush(param);
        return CommonResult.ok();
    }

    @Resource
    DepotTruckPlateListService plateListService;

    /**
     * 云端添加车辆名单同步
     *
     * @author xiang
     * @date 2023/10/23 16:28
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("云端添加车辆名单同步")
    @CommonLog("云端添加车辆名单同步")
    @PostMapping("/api/cloud/depotTruckPlateList/push")
    public CommonResult<Object> depotTruckPlateListPush(@RequestBody DepotTruckPlateListPushParam param) {
        plateListService.depotTruckPlateListPush(param);
        return CommonResult.ok();
    }

}
