package com.njzscloud.supervisory.order.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.njzscloud.common.core.ex.Exceptions;
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.AccountRegulationService;
import com.njzscloud.supervisory.order.service.OrderInfoService;
import com.njzscloud.supervisory.sys.log.annotation.Log;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

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

    private final AccountRegulationService accountRegulationService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public synchronized R<?> add(@RequestBody AddOrderInfoParam addOrderInfoParam) {
        OrderCategory orderCategory = addOrderInfoParam.getOrderCategory();
        Assert.isTrue(orderCategory == OrderCategory.PuTong
                        || orderCategory == OrderCategory.DuanBoChu
                , "订单类型必须是普通订单、短驳出");
        Integer truckCount = addOrderInfoParam.getTruckCount();
        Assert.isTrue(truckCount != null && truckCount > 0, "车辆数量必须大于0");
        Integer estimatedQuantity = addOrderInfoParam.getEstimatedQuantity();
        if (estimatedQuantity != null && estimatedQuantity > 0) {
            addOrderInfoParam.setEstimatedQuantity(estimatedQuantity / truckCount);
        }
        orderInfoService.batchAdd(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("/certificatePaging")
    public R<PageResult<OrderPagingResult>> certificatePaging(PageParam pageParam, OrderPagingSearchParam orderPagingSearchParam) {
        orderPagingSearchParam.setIsCertificatePaging(Boolean.TRUE);
        return R.success(orderInfoService.auditPaging(pageParam, orderPagingSearchParam));
    }


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

    /**
     * 批量审核
     */
    @PostMapping("/batch_audit")
    public R<?> batchAudit(@RequestBody BatchAuditOrderParam param) {
        orderInfoService.batchAudit(param);
        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 param 批量指派参数
     */
    @PostMapping("/batch_assignment")
    public R<?> batchAssignment(@RequestBody BatchAssignmentOrderParam param) {
        orderInfoService.batchAssignment(param);
        return R.success();
    }

    /**
     * 批量确认接单
     *
     * @param param 批量确认接单参数
     */
    @PostMapping("/batch_confirm")
    public R<?> batchConfirm(@RequestBody BatchConfirmOrderParam param) {
        orderInfoService.batchConfirm(param);
        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) {
        List<String> cargoPhoto = startTransportOrderParam.getCargoPhoto();
        Assert.notEmpty(cargoPhoto, () -> Exceptions.clierr("装车照片不能为空"));
        Assert.isFalse(cargoPhoto.stream().anyMatch(StrUtil::isBlank), () -> Exceptions.clierr("装车照片不能为空"));
        orderInfoService.startTrans(startTransportOrderParam);
        return R.success();
    }

    @GetMapping("/gps_test")
    public R<?> gpsTest(@RequestParam("sn") String sn, @RequestParam(value = "count", required = false, defaultValue = "1000") int count) {
        orderInfoService.gpsTest(sn, count);
        return R.success();
    }

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

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

    @GetMapping("/stop_track")
    public R<?> stopTrack(@RequestParam("orderId") Long orderId) {
        orderInfoService.stopTrackOrder(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();
    }

    /**
     * 导出历史数据
     */
    @GetMapping(value = "/export")
    public void export(HttpServletResponse response, OrderPagingSearchParam param) throws IOException {
        orderInfoService.export(response, param);
    }

    /**
     * 转办
     *
     * @param orderId 订单Id
     */
    @GetMapping("/forwarded")
    public R<?> forwarded(@RequestParam("orderId") Long orderId, @RequestParam("area") String area,
                          @RequestParam("areaName") String areaName) {
        orderInfoService.forwarded(orderId, area, areaName);
        return R.success();
    }

    /**
     * 改价
     */
    @Log("订单改价")
    @PostMapping("/change_price")
    public R<?> changePrice(@RequestBody ChangePriceParam param) {
        orderInfoService.changePrice(param);
        return R.success();
    }

    /**
     * 调账
     */
    @Log("业务调账")
    @PostMapping("/account_regulation")
    public R<?> accountRegulation(@RequestBody RegulationParam param) {
        accountRegulationService.accountRegulation(param);
        return R.success();
    }

    @PostMapping("/hsoa/push_provincial")
    public R<?> pushProvincial(@RequestBody List<String> orderSns) {
        return R.success(orderInfoService.pushProvincial(orderSns));
    }

    /**
     * 订单统计
     */
    @GetMapping("/statistics")
    public R<?> statistics(OrderStatisticsParam param) {
        return R.success(orderInfoService.statistics(param));
    }

    /**
     * 订单统计详情
     */
    @GetMapping("/statistics/detail")
    public R<?> statisticsDetail(PageParam pageParam, OrderPagingSearchParam param) {
        return R.success(orderInfoService.statisticsDetail(pageParam, param));
    }

}
