package com.casic.missiles.controller.business;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.core.page.PageFactory;
import com.casic.missiles.core.page.PageInfoBT;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.dispatch.*;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessOrder;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessDispatchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.BindingResult;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author cz
 */

@RestController
@Api(tags = "设备收发模块")
@RequestMapping("/device/dispatch")
@AllArgsConstructor
public class BusinessDispatchController extends ExportController {

    private IBusinessDispatchService bizDispatchService;

    /**
     * 除了ui界面要展示的列在DeviceDispatchDTO模型中体现以外，还需覆盖所有按钮的权限操作，终止、回退、收入、归还、催办按钮
     */
    @ApiOperation("设备收发列表-分页")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<DeviceDispatchVO>> listPage(@RequestBody @Valid DeviceDispatchDTO request, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        Page<DeviceDispatchVO> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(bizDispatchService.listPage(page, request)));
    }

    /**
     * 列表的导出
     */
    @ApiOperation("样品导出")
    @PostMapping("/export")
    public void exportSampleDispatchList(@RequestBody @Valid DeviceDispatchDTO request, HttpServletResponse response) {
        bizDispatchService.exportSampleDispatchList(request, response);
    }

    /**
     * cancelMeasure：无需检测需要检定人在填入错误的情况下，不必进行检测
     * receive：设备收入(只有待收入的设备具有收入操作)
     * back：回填：置为前置状态，已收入、待归还、已归还
     */
    @ApiOperation("( 回退、收入、归还、无需检测 按钮)")
    @PostMapping("/status/change")
    public ReturnDTO statusChange(@RequestBody @Valid List<DeviceStatusChangeDTO> deviceReceiveDTOList, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return bizDispatchService.batchStatusChange(deviceReceiveDTOList);
    }

    @ApiOperation("设备批量改变状态")
    @PostMapping("/status/batchChange")
    public ReturnDTO statusBatchChange(@RequestBody @Valid DeviceBatchStatusChangeDTO deviceBatchStatusChangeDTO, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(bizDispatchService.statusBatchChange(deviceBatchStatusChangeDTO));
    }

    @ApiOperation("标签绑定")
    @PostMapping("/label/bind")
    public ReturnDTO<List<DeviceScanVO>> bindLabel(@RequestBody @Valid LabelBindDTO labelBindDTO) throws Exception {
        Assert.isFalse(ObjectUtils.isEmpty(labelBindDTO.getLabel()), () -> {
            throw new BusinessException(BusinessExceptionEnum.LABEL_IS_EMPTY);
        });
        return bizDispatchService.bindLabel(labelBindDTO);
    }

    /**
     * 设备终止，，同时要进行终止原因的回填
     */
    @ApiOperation("设备终止")
    @PostMapping("/terminate")
    public ReturnDTO forcedTerminate(@RequestBody @Valid List<DeviceStatusChangeDTO> deviceReceiveDTO, BindingResult bindingResult) {
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return bizDispatchService.forcedTerminate(deviceReceiveDTO);
    }


    /**
     * 催办会产生催办消息，对相关操作人员进行消息提示
     */
    @ApiOperation("催办")
    @PostMapping("/remindWorker")
    public ReturnDTO<BusinessOrder> remindWorker(@RequestBody @Valid DeviceStatusChangeDTO deviceReceiveDTO) {
        Assert.isFalse(Objects.isNull(deviceReceiveDTO.getOrderId()) || Objects.isNull(deviceReceiveDTO.getSampleId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return bizDispatchService.remindWorker(deviceReceiveDTO);
    }

}
