package com.sckw.transport.controller;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.transport.model.dto.OrderFinishDTO;
import com.sckw.transport.model.param.LogisticsConsignmentParam;
import com.sckw.transport.service.LogisticsConsignmentService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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;

/**
 * @author lfdc
 * @description 采购订单/销售订单-物流托运Controller
 * @date 2023-06-28 11:06:06
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/kwtLogisticsConsignment")
public class LogisticsConsignmentController {

    @Autowired
    LogisticsConsignmentService logisticsConsignmentService;

    /**
     * 采购订单-物流托运生成托运订单
     *
     * @param logisticsConsignmentParam
     * @return
     */
    @Validated
    @RequestMapping(value = "/purchaseLogisticsConsignment", method = RequestMethod.POST)
    public HttpResult purchaseLogisticsConsignment(@Valid @RequestBody LogisticsConsignmentParam logisticsConsignmentParam) {
        log.info("采购订单传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentParam));
        try {
            return logisticsConsignmentService.purchaseLogisticsConsignment(logisticsConsignmentParam);
        } catch (Exception e) {
            log.error("采购订单-物流托运生成托运订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-销售订单id
     *
     * @param ids 销售订单id
     * @return
     */
    @RequestMapping(value = "/purchaseLogisticsOrder", method = RequestMethod.GET)
    public HttpResult purchaseLogisticsOrder(@RequestParam("ids") @NotBlank(message = "单据id不能为空") String ids,
                                             @RequestParam("page") @NotNull(message = "当前页不能为空") int page,
                                             @RequestParam("pageSize") @NotNull(message = "每页条数不能为空") int pageSize) {
        log.info("采购订单-托运订单列表 传递参数信息:{}", ids);
        try {
            return logisticsConsignmentService.logisticsOrder(ids, page, pageSize, "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-下方数据统计-贸易订单id
     *
     * @param id 贸易订单id
     * @return
     */
    @Validated
    @RequestMapping(value = "/purchaseLogisticsOrderCount", method = RequestMethod.GET)
    public HttpResult purchaseLogisticsOrderCount(@RequestParam("id") @NotBlank(message = "单据id不能为空") String id) {
        log.info("采购订单-托运订单列表 传递参数信息:{}", id);
        try {
            return logisticsConsignmentService.logisticsOrderCount(id, "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-撤销托运
     *
     * @param ids 物流运单id
     * @return
     */
    @Validated
    @RequestMapping(value = "/purchaseCancelConsign", method = RequestMethod.POST)
    @GlobalTransactional(name = "default_tx_group")
    public HttpResult purchaseCancelConsign(@RequestParam("ids") @NotBlank(message = "单据不能为空") List<String> ids) {
        log.info("采购订单-托运订单列表-撤销托运:{}", JSONObject.toJSONString(ids));
        try {
            return logisticsConsignmentService.cancelConsign(ids, "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表-撤销托运 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-车辆信息-销售订单id
     *
     * @param ids      销售订单ids
     * @param page
     * @param pageSize
     * @return
     */
    @Validated
    @RequestMapping(value = "/purchaseLogisticsCar", method = RequestMethod.GET)
    public HttpResult purchaseLogisticsCar(@RequestParam("ids") @NotBlank(message = "单据id不能为空") String ids,
                                           @RequestParam("page") @NotNull(message = "当前页不能为空") int page,
                                           @RequestParam("pageSize") @NotNull(message = "每页条数不能为空") int pageSize) {
        log.info("采购订单-车辆详情 传递参数信息:{}", ids);
        try {
            return logisticsConsignmentService.purchaseLogisticsCar(ids, page, pageSize);
        } catch (Exception e) {
            log.error("采购订单-车辆详情 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-订单完结-数据查询
     *
     * @param orderId 物流订单id
     * @return
     */
    @Validated
    @RequestMapping(value = "/selectPurchaseOrderFinish", method = RequestMethod.GET)
    public HttpResult selectPurchaseOrderFinish(@RequestParam("orderId") @NotBlank(message = "数据id不能为空") String orderId) {
        log.info("采购订单-托运订单列表-订单完结-数据查询 传递参数信息:{}", orderId);
        try {
            return logisticsConsignmentService.selectOrderFinishVo(orderId, "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表-订单完结-数据查询 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-完结订单
     *
     * @param OrderFinishDTO
     * @return
     */
    @Validated
    @RequestMapping(value = "/purchaseOrderFinish", method = RequestMethod.POST)
    public HttpResult purchaseOrderFinish(@RequestBody @Validated OrderFinishDTO OrderFinishDTO) {
        log.info("采购订单-托运订单列表-完结订单 传递参数信息:{}", JSONObject.toJSONString(OrderFinishDTO));
        try {
            return logisticsConsignmentService.orderFinish(OrderFinishDTO, "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表-完结订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }


    /**
     * 销售订单-物流托运生成托运订单
     *
     * @param logisticsConsignmentParam
     * @return
     */
    @RequestMapping(value = "/sellLogisticsConsignment", method = RequestMethod.POST)
    public HttpResult sellLogisticsConsignment(@Valid @RequestBody LogisticsConsignmentParam logisticsConsignmentParam) {
        log.info("销售订单-物流托运生成托运订单 传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentParam));
        try {
            return logisticsConsignmentService.sellLogisticsConsignment(logisticsConsignmentParam);
        } catch (Exception e) {
            log.error("物流托运 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-销售订单id
     *
     * @param id 销售订单id
     * @return
     */
    @RequestMapping(value = "/sellLogisticsOrder", method = RequestMethod.GET)
    public HttpResult sellLogisticsOrder(@RequestParam("id") @NotBlank(message = "单据id不能为空") String id,
                                         @RequestParam("page") @NotNull(message = "当前页不能为空") int page,
                                         @RequestParam("pageSize") @NotNull(message = "每页条数不能为空") int pageSize) {
        log.info("销售订单-托运订单列表 传递参数信息:{}", id);
        try {
            return logisticsConsignmentService.logisticsOrder(id, page, pageSize, "2");
        } catch (Exception e) {
            log.error("销售订单-托运订单列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-下方数据统计-贸易订单id
     *
     * @param id 贸易订单id
     * @return
     */
    @Validated
    @RequestMapping(value = "/sellLogisticsOrderCount", method = RequestMethod.GET)
    public HttpResult sellLogisticsOrderCount(@RequestParam("id") @NotBlank(message = "单据id不能为空") String id) {
        log.info("销售订单-托运订单列表 传递参数信息:{}", id);
        try {
            return logisticsConsignmentService.logisticsOrderCount(id, "2");
        } catch (Exception e) {
            log.error("销售订单-托运订单列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-车辆列表-销售订单id
     *
     * @param id 销售订单id
     * @return
     */
    @RequestMapping(value = "/sellLogisticsCar", method = RequestMethod.GET)
    public HttpResult sellLogisticsCar(@RequestParam("id") @NotBlank(message = "单据id不能为空") String id,
                                       @RequestParam("page") @NotNull(message = "当前页不能为空") int page,
                                       @RequestParam("pageSize") @NotNull(message = "每页条数不能为空") int pageSize) {
        log.info("销售订单-车辆列表 传递参数信息:{}", id);
        try {
            return logisticsConsignmentService.sellLogisticsCar(id, page, pageSize);
        } catch (Exception e) {
            log.error("销售订单-车辆列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-撤销托运
     *
     * @param ids 物流订单id
     * @return
     */
    @RequestMapping(value = "/sellCancelConsign", method = RequestMethod.GET)
    @GlobalTransactional(name = "default_tx_group")
    public HttpResult sellCancelConsign(@RequestParam("ids") @NotBlank(message = "单据id不能为空") List<String> ids) {
        log.info("销售订单-车辆列表 传递参数信息:{}", JSONObject.toJSONString(ids));
        try {
            return logisticsConsignmentService.cancelConsign(ids, "2");
        } catch (Exception e) {
            log.error("销售订单-车辆列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-订单完结-数据查询
     *
     * @param orderId 物流订单id
     * @return
     */
    @Validated
    @RequestMapping(value = "/selectSellOrderFinish", method = RequestMethod.GET)
    public HttpResult selectSellOrderFinish(@RequestParam("orderId") @NotBlank(message = "数据id不能为空") String orderId) {
        log.info("销售订单-托运订单列表-订单完结-数据查询 传递参数信息:{}", orderId);
        try {
            return logisticsConsignmentService.selectOrderFinishVo(orderId, "2");
        } catch (Exception e) {
            log.error("销售订单-托运订单列表-订单完结-数据查询 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-完结订单
     *
     * @param orderFinishDTO
     * @return
     */
    @Validated
    @RequestMapping(value = "/sellOrderFinish", method = RequestMethod.POST)
    public HttpResult sellOrderFinish(@RequestBody @Validated OrderFinishDTO orderFinishDTO) {
        log.info("销售订单-托运订单列表-完结订单 传递参数信息:{}", JSONObject.toJSONString(orderFinishDTO));
        try {
            return logisticsConsignmentService.orderFinish(orderFinishDTO, "2");
        } catch (Exception e) {
            log.error("销售订单-托运订单列表-完结订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }
}
