package com.clx.performance.controller.pc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.clx.performance.enums.OrderChildEnum;
import com.clx.performance.param.pc.OrderChildCarrierBatchCancelParam;
import com.clx.performance.param.pc.OrderChildCarrierCancelParam;
import com.clx.performance.param.pc.PageCarrierOrderChildParam;
import com.clx.performance.param.pc.PageMonitorOrderChildQCParam;
import com.clx.performance.service.OrderChildService;
import com.clx.performance.vo.pc.*;
import com.clx.performance.vo.pc.child.UpdateCarrierBatchCancelVO;
import com.msl.common.base.PageData;
import com.msl.common.convertor.aspect.UnitCovert;
import com.msl.common.result.Result;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


@Slf4j
@RestController
@RequestMapping(value="/pc/carrier/orderChild")
@Validated
@Tag(name = "承运端-运单列表")
@AllArgsConstructor
public class CarrierOrderChildController {

    private final OrderChildService orderChildService;

    @Operation(summary = "运单列表",description = "<br>By:李瑞新")
    @PostMapping("/pageCarrierOrderChildList")
    @UnitCovert(param = false)
    public Result<PageData<PageCarrierOrderChildVO>> pageCarrierOrderChildList(@RequestBody PageCarrierOrderChildParam param) {
        IPage<PageCarrierOrderChildVO> page = orderChildService.pageCarrierOrderChildList(param);
        return Result.page(page.getRecords(), page.getTotal(), page.getPages());
    }

    @Operation(summary = "导出",description = "<br>By:李瑞新")
    @PostMapping("/exportCarrierOrderChildList")
    public Result exportCarrierOrderChildList(@RequestBody PageCarrierOrderChildParam param, HttpServletResponse response) throws Exception {

        SXSSFWorkbook workbook = orderChildService.exportCarrierOrderChildList(param);
        response.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
        workbook.write(response.getOutputStream());
        return null;
    }


    @Operation(summary = "运单详情详情",description = "<br>By:李瑞新")
    @GetMapping("/getCarrierOrderChildDetail")
    @UnitCovert(param = false)
    public Result<CarrierOrderChildDetailVO> getCarrierOrderChildDetail(@NotBlank(message = "运单编号不能为空") String childNo) {
        return Result.ok(orderChildService.getCarrierOrderChildDetail(childNo));
    }


    @Operation(summary = "运单状态列表",description = "<br>By:李瑞新")
    @GetMapping("/getOrderChildStatusList")
    public Result<List<OrderChildStatusVO>> getOrderChildStatusList() {
        ConcurrentHashMap<Integer, String> map = OrderChildEnum.map;
        List<OrderChildStatusVO> result = map.entrySet().stream().map(c -> new OrderChildStatusVO(c.getKey(), c.getValue())).collect(Collectors.toList());
        Collections.sort(result, new Comparator<OrderChildStatusVO>() {
            @Override
            public int compare(OrderChildStatusVO o1, OrderChildStatusVO o2) {
                Integer status1= o1.getStatus();
                Integer status2= o2.getStatus();
                return  status1.compareTo(status2);
            }
        });
        return Result.ok(result);
    }

    @Operation(summary = "取消运单", description = "<br>By:艾庆国")
    @PostMapping("/updateCancel")
    public Result<Void> updateCarrierCancel(@RequestBody @Validated OrderChildCarrierCancelParam param) {
        orderChildService.updateCarrierCancel(param);

        return Result.ok();
    }

    @Operation(summary = "业务信息", description = "<br>By:胡宇帆")
    @GetMapping("/orderChildBussInfo")
    public Result<OrderChildBussInfoVO> getOrderChildBussInfo(@NotNull(message = "车辆id不能为空") Integer truckId) {
        return Result.ok(orderChildService.getOrderChildBussInfo(truckId));
    }


    @Operation(summary = "全量运单监控预警列表", description = "<br>By:刘海泉")
    @RequestMapping(value = "/getMonitorOrerChildListsByParam", method = RequestMethod.POST)
    public Result<PageData<OutputMonitorOrderChildVO>> getMonitorOrerChildListsByParam(@RequestBody @Validated PageMonitorOrderChildQCParam param) {
        IPage<OutputMonitorOrderChildVO> page = orderChildService.getMonitorOrerChildListsByParam(param);
        return Result.page(page.getRecords(), page.getTotal(), page.getPages());
    }

    @Operation(summary = "司机取消运单-运单详情", description = "<br>By:胡宇帆")
    @GetMapping("/driverCancelOrderChildInfo")
    public Result<DriverCancelOrderChildInfo> driverCancelOrderChildInfo(@NotBlank(message = "运单编号不能为空") String orderChildNo) {
        return Result.ok(orderChildService.driverCancelOrderChildInfo(orderChildNo));
    }

    //模拟
    @GetMapping("/orderChildExpect")
    public void getMonitorOrerChildListsByParam() {
        orderChildService.orderChildExpect();
    }



    @Operation(summary = "履约进度表-历史线路拉运量", description = "<br>By:l刘海泉")
    @GetMapping("/getHistoryLineTransportVolume")
    @UnitCovert(param = false)
    public Result<List<HistoryLineTranVolumeChartVO>> getHistoryLineTransportVolume(@NotBlank(message = "订单编号不能为空") String orderNo) {
        return Result.ok(orderChildService.getHistoryLineTransportVolume(orderNo));
    }



    @Operation(summary = "批量取消运单", description = "<br>By:刘海泉")
    @PostMapping("/updateCarrierBatchCancel")
    public Result<UpdateCarrierBatchCancelVO> updateCarrierBatchCancel(@RequestBody @Validated OrderChildCarrierBatchCancelParam param) {
        return Result.ok(orderChildService.updateCarrierBatchCancel(param));
    }

}
