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.IdDTO;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.order.OrderCancelRequest;
import com.casic.missiles.dto.business.order.OrderListResponse;
import com.casic.missiles.dto.business.order.OrderListRequest;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.business.OrderStatusEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.business.BusinessDealRecord;
import com.casic.missiles.model.business.BusinessOrder;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
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;

/**
 * <p>
 * 业务管理-委托书 前端控制器
 * </p>
 *
 * @author cz
 * @since 2023-02-02
 */
@RestController
@Api(tags = "委托书模块")
@RequestMapping("/business/order")
@AllArgsConstructor
public class BusinessOrderController extends ExportController implements OrderStatusEnum {

    private final IBusinessOrderService businessOrderService;

    @ApiOperation("委托书查询")
    @PostMapping("/listPage")
    public ReturnDTO<PageInfoBT<OrderListResponse>> orderListPage(@RequestBody @Valid OrderListRequest request, BindingResult bindingResult) throws Exception {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        Page<OrderListResponse> page = PageFactory.defaultPage();
        return ReturnUtil.success(super.packForBT(businessOrderService.orderListPage(page, request)));
    }

    @ApiOperation("委托书详情")
    @PostMapping("/detail")
    public ReturnDTO<BusinessOrder> adviceDetail(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(businessOrderService.orderDetail(idDTO.getId()));
    }

    @ApiOperation("委托书更新")
    @PostMapping("/update")
    public ReturnDTO updateAdvice(@RequestBody @Valid BusinessOrder businessOrder, BindingResult bindingResult)throws  Exception {
        Assert.isFalse(Objects.isNull(businessOrder.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessOrderService.updateOrder(businessOrder));
    }

    @ApiOperation("委托书新增")
    @PostMapping("/add")
    public ReturnDTO addAdvice(@RequestBody @Valid BusinessOrder businessOrder, BindingResult bindingResult) throws Exception{
        Assert.isFalse(bindingResult.hasErrors(), () -> {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        });
        return ReturnUtil.success(businessOrderService.addOrder(businessOrder));
    }

    @ApiOperation("委托书导出")
    @PostMapping("/export")
    public void exportAdvice(@RequestBody @Valid OrderListRequest request, HttpServletResponse response) {
        businessOrderService.orderExport(request, response);
    }

    @ApiOperation("委托书取消")
    @PostMapping("/cancel")
    public ReturnDTO cancelOrder(@RequestBody @Valid OrderCancelRequest orderCancelRequest) {
        Assert.isFalse(Objects.isNull(orderCancelRequest.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return businessOrderService.cancelOrder(orderCancelRequest);
    }

    @ApiOperation("操作记录")
    @PostMapping("/optionRecord")
    public ReturnDTO<List<BusinessDealRecord>> optionRecord(@RequestBody @Valid IdDTO idDTO) {
        Assert.isFalse(Objects.isNull(idDTO.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return ReturnUtil.success(businessOrderService.optionRecord(idDTO.getId()));
    }


    @ApiOperation("委托书接收")
    @PostMapping("/receive")
    public ReturnDTO receiveOrder(@RequestBody @Valid  OrderCancelRequest orderCancelRequest) {
        Assert.isFalse(Objects.isNull(orderCancelRequest.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return businessOrderService.updateStatusById(orderCancelRequest, ALREADY_RECEIVED);
    }

    @ApiOperation("委托书退回")
    @PostMapping("/back")
    public ReturnDTO backOrder(@RequestBody @Valid OrderCancelRequest orderCancelRequest) {
        Assert.isFalse(Objects.isNull(orderCancelRequest.getId()), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        return businessOrderService.updateStatusById(orderCancelRequest, ALREADY_CANCEL);
    }

}
