package com.njzscloud.supervisory.order.controller;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.supervisory.order.contant.OrderCategory;
import com.njzscloud.supervisory.order.pojo.param.*;
import com.njzscloud.supervisory.order.pojo.result.OrderCertificateResult;
import com.njzscloud.supervisory.order.pojo.result.OrderPagingResult;
import com.njzscloud.supervisory.order.pojo.result.TrainBillResult;
import com.njzscloud.supervisory.order.service.OrderInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单信息
 */
@Slf4j
@RestController
@RequestMapping("/order_info")
@RequiredArgsConstructor
public class OrderInfoController {
    private final OrderInfoService orderInfoService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public R<?> add(@RequestBody AddOrderInfoParam addOrderInfoParam) {
        OrderCategory orderCategory = addOrderInfoParam.getOrderCategory();
        Assert.isTrue(orderCategory == OrderCategory.PuTong
                        || orderCategory == OrderCategory.DuanBoChu
                , "订单类型必须是普通订单、短驳出");
        orderInfoService.add(addOrderInfoParam);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/modify")
    public R<?> modify(@RequestBody ModifyOrderParam modifyOrderParam) {
        orderInfoService.modify(modifyOrderParam);
        return R.success();
    }

    /**
     * 修改订单产品
     */
    @PostMapping("/modify_goods")
    public R<?> modifyGoods(@RequestBody ModifyOrderGoodsParam modifyOrderGoodsParam) {
        orderInfoService.modifyGoods(modifyOrderGoodsParam);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/del")
    public R<?> del(@RequestBody List<Long> ids) {
        orderInfoService.del(ids);
        return R.success();
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<OrderPagingResult> detail(@RequestParam Long id) {
        return R.success(orderInfoService.detail(id));
    }

    /**
     * 查询准运证
     */
    @GetMapping("/certificate")
    public R<OrderCertificateResult> certificate(OrderCertificateParam orderCertificateParam) {
        return R.success(orderInfoService.certificate(orderCertificateParam));
    }

    /**
     * 查询联单
     */
    @GetMapping("/train_bill")
    public R<TrainBillResult> trainBill(@RequestParam("orderId") Long orderId) {
        return R.success(orderInfoService.trainBill(orderId));
    }

    /**
     * 分页查询
     */
    @GetMapping("/paging")
    public R<PageResult<OrderPagingResult>> paging(PageParam pageParam, OrderPagingSearchParam orderPagingSearchParam) {
        return R.success(orderInfoService
                .paging(pageParam, orderPagingSearchParam));
    }

    /**
     * 待派单列表
     */
   /*  @GetMapping("/dispatch_paging")
    public R<PageResult<OrderPagingResult>> dispatchPaging(PageParam pageParam, OrderPagingSearchParam orderPagingSearchParam) {
        return R.success(orderInfoService
                .dispatchPaging(pageParam, orderPagingSearchParam));
    }
 */

    /**
     * 审核分页查询
     */
    @GetMapping("/audit_paging")
    public R<PageResult<OrderPagingResult>> auditPaging(PageParam pageParam, OrderPagingSearchParam orderPagingSearchParam) {
        return R.success(orderInfoService.auditPaging(pageParam, orderPagingSearchParam));
    }

    /**
     * 审核
     */
    @GetMapping("/audit")
    public R<PageResult<OrderPagingResult>> audit(AuditOrderParam auditOrderParam) {
        orderInfoService.audit(auditOrderParam);
        return R.success();
    }

    /**
     * 看料
     */
    @PostMapping("/check")
    public R<?> check(@RequestBody CheckGoodsParam checkGoodsParam) {
        orderInfoService.check(checkGoodsParam);
        return R.success();
    }

    /**
     * 指派清运公司/司机
     */
    @GetMapping("/assignment")
    public R<?> assignment(AssignmentOrderParam assignmentOrderParam) {
        orderInfoService.assignment(assignmentOrderParam);
        return R.success();
    }

    /**
     * 司机确认接单
     *
     * @param orderId 订单Id
     */
    @GetMapping("/confirm")
    public R<?> confirm(@RequestParam("orderId") Long orderId,
                        @RequestParam("truckId") Long truckId) {
        orderInfoService.confirm(orderId, truckId);
        return R.success();
    }

    /**
     * 开始清运
     */
    @PostMapping("/start_trans")
    public R<?> startTrans(@RequestBody StartTransportOrderParam startTransportOrderParam) {
        orderInfoService.startTrans(startTransportOrderParam);
        return R.success();
    }

    /**
     * 取消订单
     *
     * @param orderId 订单Id
     */
    @GetMapping("/cancel")
    public R<?> cancel(@RequestParam("orderId") Long orderId) {
        orderInfoService.cancel(orderId);
        return R.success();
    }

    @GetMapping("/start_track")
    public R<?> startTrack(@RequestParam("orderId") String orderId) {
        orderInfoService.startTuqiangTrack(orderId);
        return R.success();
    }

    @GetMapping("/stop_track")
    public R<?> stopTrack(@RequestParam("orderId") String orderId) {
        orderInfoService.stopTrack(orderId);
        return R.success();
    }

    /**
     * 过磅单
     *
     * @param orderId 订单Id
     */
    @GetMapping("/obtain_weigh_bill")
    public R<?> obtainWeighBill(@RequestParam("orderId") Long orderId) {
        OrderPagingResult obtainWeighBill = orderInfoService.obtainWeighBill(orderId);
        return R.success(obtainWeighBill);
    }

    /**
     * 车辆进场
     */
    @PostMapping("/truck_coming")
    public R<?> truckComing(@RequestBody TruckComingOrderParam truckComingOrderParam) {
        orderInfoService.truckComing(truckComingOrderParam, 1);
        return R.success();
    }

    /**
     * 车辆出场
     */
    @PostMapping("/truck_leaving")
    public R<?> truckLeaving(@RequestBody TruckLeavingOrderParam truckLeavingOrderParam) {
        orderInfoService.truckLeaving(truckLeavingOrderParam, 1);
        return R.success();
    }

}
