package com.sckw.transport.controller;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.core.model.page.PageHelperUtil;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.core.web.model.ValiList;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.dto.*;
import com.sckw.transport.model.vo.WaybillOrderDriverVo;
import com.sckw.transport.service.DriverService;
import com.sckw.transport.service.KwtWaybillOrderService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lfq
 * @desc 车辆运单
 * @date 2023/7/19 0019
 */
@Slf4j
@RestController
@RequestMapping("/kwtWaybillOrder")
public class KwtWaybillOrderController {

    @Autowired
    KwtWaybillOrderService waybillOrderService;

    @Autowired
    private DriverService driverService;

    /**
     * @desc 统计司机任务数(待接单/出车/执行/完结)
     * @author zk
     * @date 2023/8/1
     **/
    @GetMapping("/findCountByDriver")
    public HttpResult findCountByDriver(){
        Long driverId = LoginUserHolder.getUserId();
        Map count = waybillOrderService.findStatisticsCountByDriver(driverId);
        return HttpResult.ok(count);
    }

    /**
     * @param params {driverId 司机ID, month月份}
     * @desc 统计司机任务数（任务数/委派量/装货量/卸货量/亏吨量/亏吨扣款）
     * @author zk
     * @date 2023/8/1
     **/
    @PostMapping("/findSumByDriver")
    public HttpResult findSumByDriver(@RequestBody Map<String, Object> params){
        try {
            params.put("driverId", LoginUserHolder.getUserId());
            Map<String, Object> count = waybillOrderService.findStatisticsSumByDriver(params);
            return HttpResult.ok(count);
        } catch (Exception e) {
            log.error("findSumByDriver error query:{},errorMessage:{}", JSONObject.toJSONString(params),e.getMessage(),e);
            return HttpResult.error(e.getMessage());
        }
    }

    /**
     * @param params {driverId 司机ID, busStatus  1待接单/2出车/3执行/4完结, month月份}
     * @desc 查询司机任务列表-待接单/出车/执行/完结
     * @author zk
     * @date 2023/8/1
     **/
    @PostMapping("/findWaybillOrderByDriver")
    public HttpResult findWaybillOrderByDriver(@RequestBody Map<String, Object> params){
        params.put("driverId", LoginUserHolder.getUserId());
        /**校验**/
        if (StringUtils.isBlank(params.get("busStatus"))) {
            return HttpResult.error("请选择业务状态！");
        }
        // 设置分页参数
        PageHelper.startPage(PageResult.getPage(params), PageResult.getPageSize(params));
        List<WaybillOrderDriverVo> list = waybillOrderService.findWaybillOrderByDriver(params);
        PageResult pageResult = PageHelperUtil.getPageResult(new PageInfo<>(list));
        return HttpResult.ok(pageResult);
    }

    /**
     * @param params
     * @desc 变更司机
     * @author zk
     * @date 2023/8/2
     **/
    @PostMapping("/changeDriver")
    public HttpResult changeDriver(@RequestBody @Valid ChangeDriverDto params){
        return waybillOrderService.changeDriver(params);
    }

    /**
     * @param params
     * @desc 变更车辆
     * @author zk
     * @date 2023/8/2
     **/
    @PostMapping("/changeTruck")
    public HttpResult changeTruck(@RequestBody @Valid ChangeTruckDto params){
        return waybillOrderService.changeTruck(params);
    }

    /**
     * @param wOrderId
     * @desc 变更车辆/司机查询
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/changeWaybillOrder")
    public HttpResult changeWaybillOrder(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderService.changeWaybillOrder(wOrderId);
    }

    /**
     * 承运订单-更换司机
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateDriver", method = RequestMethod.POST)
    public HttpResult updateDriver(@Validated @RequestBody UpdateDriverCarDTO dto) {
        try {
            return driverService.updateDriver(dto);
        } catch (Exception e) {
            log.error("承运订单更换司机 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 承运订单-更换车辆
     * @param dto
     * @return
     */
    @RequestMapping(value = "/updateCarCommit", method = RequestMethod.POST)
    public HttpResult updateCarCommit(@Validated @RequestBody UpdateDriverCarDTO dto) {
        try {
            return driverService.updateCarCommit(dto);
        } catch (Exception e) {
            log.error("承运订单更换司机 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 派车-查询已派车量、待运量数据
     * @param orderId
     * @return
     */
    @RequestMapping(name = "查询已派车量、待运量数据",path = "/getLogisticsAmountByOrderId",method = RequestMethod.GET)
    public HttpResult getLogisticsAmountByOrderId(@NotBlank @RequestParam("orderId") String orderId){
        log.info("查询已排量、待运量数据 query :{}",orderId);
        try {
            return waybillOrderService.getLogisticsAmountByOrderId(orderId);
        } catch (Exception e) {
            log.error("查询已排量、待运量数据 error:{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * @param params {}
     * @desc 派车-趟次/循环
     * @author zk
     * @date 2023/5/30
     **/
    @PostMapping("/sendCar")
    public HttpResult sendCar(@RequestBody @Valid ValiList<SendCarDto1> params){
        if (CollectionUtils.isEmpty(params)) {
            return HttpResult.error("请选择委派车辆信息！");
        }
        return waybillOrderService.sendCar(params);
    }

    /**
     * @param ids 车辆运单主键ID，多个已逗号隔开
     * @desc 取消派车
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/cancelSendCar")
    public HttpResult cancelSendCar(@RequestBody List<Long> ids){
        return waybillOrderService.cancelSendCarV1(ids);
    }

    /**
     * @param params {}
     * @desc 确认接单
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/confirmSendCar")
    public HttpResult confirmSendCar(@RequestBody @Valid OperateSendCarDto params){
        return waybillOrderService.confirmSendCar(params);
    }

    /**
     * @param params {}
     * @desc 拒绝接单
     * @author zk
     * @date 2023/7/24
     **/
    @PostMapping("/refuseSendCar")
    public HttpResult refuseSendCar(@RequestBody @Valid OperateSendCarDto params){
        return waybillOrderService.refuseSendCar(params);
    }

    /**
     * @param params {}
     * @desc 确认出车
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/confirmDeparture")
    public HttpResult confirmDeparture(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.confirmDeparture(params);
    }

    /**
     * @param params {}
     * @desc 拒绝出车
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/refuseDeparture")
    public HttpResult refuseDeparture(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.refuseDeparture(params);
    }

    /**
     * @param params {}
     * @desc 到达装货地点
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/arriveLoadLocation")
    public HttpResult arriveLoadLocation(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.arriveLoadLocation(params);
    }

    /**
     * @param params {}
     * @desc 装货
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/loading")
    public HttpResult loading(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.loading(params);
    }

    /**
     * @param params {}
     * @desc 到达卸货地点
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/arriveUnloadLocation")
    public HttpResult arriveUnloadLocation(@RequestBody @Valid OperateWaybillDto params){
        return waybillOrderService.arriveUnloadLocation(params);
    }

    /**
     * @param params {}
     * @desc 卸货
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/unloading")
    public HttpResult unloading(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.unloading(params);
    }

    /**
     * @param params {}
     * @desc 修改单证
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/editTicket")
    public HttpResult editTicket(@RequestBody @Valid WaybillOrderTicketDto params){
        return waybillOrderService.editTicket(params);
    }

    /**
     * @param params {}
     * @desc 修改单证-装卸货一起修改
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/editTicketV1")
    public HttpResult editTicketV1(@RequestBody @Valid WaybillOrderTicketV1Dto params){
        return waybillOrderService.editTicket(params);
    }

    /**
     * @param wOrderId
     * @desc 修改单证查询运单
     * @author zk
     * @date 2023/8/2
     **/
    @GetMapping("/ticketWaybillOrder")
    public HttpResult ticketWaybillOrder(@RequestParam("wOrderId") Long wOrderId){
        return waybillOrderService.ticketWaybillOrder(wOrderId);
    }

    /**
     * @param params {}
     * @desc 单证审核
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/approvalTicket")
    public HttpResult approvalTicket(@RequestBody @Valid WaybillOrderApprovalDto params){
        return waybillOrderService.approvalTicket(params);
    }

    /**
     * @param params {}
     * @desc 单证审核（编辑单证）
     * @author zk
     * @date 2023/7/26
     **/
    @PostMapping("/approvalEditTicket")
    public HttpResult approvalEditTicket(@RequestBody @Valid WaybillOrderTicketApprovalDto params){
        return waybillOrderService.approvalEditTicket(params);
    }



    //取消派车（未接单前）
    //更换车辆（趟次派车(-出车前)/循环派车(接单后-出车前)）
    //更换司机（趟次派车(-卸货前)/循环派车(接单后-卸货前)）
    //确认接单-趟次/循环
    //拒绝接单-趟次/循环
    //确认/拒绝出车
    //到达装货地点
    //确认装货
    //到达卸货地点
    //确认卸货
    //修改单证
    //单证审核

}
