package com.sckw.transport.controller;

import com.alibaba.fastjson.JSONObject;
import com.sckw.core.annotation.RepeatSubmit;
import com.sckw.core.exception.CustomPromptException;
import com.sckw.core.model.constant.NumberConstant;
import com.sckw.core.model.enums.OrderRuleEnum;
import com.sckw.core.model.enums.RedisOrderGenerateEnum;
import com.sckw.core.model.vo.BaseList;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.OrderGenerateSeqNoUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.HttpResult;
import com.sckw.excel.utils.ExcelUtil;
import com.sckw.transport.model.dto.LogisticsConsignmentDto;
import com.sckw.transport.model.dto.OrderManualCompletionDto;
import com.sckw.transport.model.param.LogisticsConsignmentParam;
import com.sckw.transport.model.param.OrderQuery;
import com.sckw.transport.model.vo.PurchaseLogisticOrderExcelVo;
import com.sckw.transport.model.vo.SellLogisticOrderExcelVo;
import com.sckw.transport.service.KwtLogisticsConsignmentService;
import com.sckw.transport.service.KwtTransportCommonService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

    @Autowired
    KwtLogisticsConsignmentService logisticsConsignmentService;

    @Autowired
    KwtTransportCommonService transportCommonService;

    /**
     * 采购订单-物流托运生成托运订单
     *
     * @param logisticsConsignmentParam 页面请求参数
     * @return  响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @RequestMapping(value = "/purchaseLogisticsConsignment", method = RequestMethod.POST)
    public HttpResult purchaseLogisticsConsignment(@Valid @RequestBody LogisticsConsignmentParam logisticsConsignmentParam) {
        log.info("采购订单传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentParam));
        try {
            return logisticsConsignmentService.logisticsConsignment(logisticsConsignmentParam,NumberConstant.ONE);
        } catch (Exception e) {
            log.error("采购订单-物流托运生成托运订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-销售订单id
     *
     * @param orderQuery 销售订单id
     * @return  查询物流订单结果
     */
    @RequestMapping(value = "/purchaseLogisticsOrder", method = RequestMethod.POST)
    public HttpResult purchaseLogisticsOrder(@RequestBody @Valid OrderQuery orderQuery) {
        log.info("采购订单-托运订单列表 传递参数信息:{}", JSONObject.toJSONString(orderQuery));
        try {
            return logisticsConsignmentService.logisticsOrder(orderQuery.getIds(), orderQuery.getPage(), orderQuery.getPageSize(), "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 baseList 物流运单ids
     * @return  操作结果
     */
    @Validated
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @RequestMapping(value = "/purchaseCancelConsign", method = RequestMethod.POST)
    @GlobalTransactional(name = "default_tx_group")
    public HttpResult purchaseCancelConsign(@Valid @RequestBody BaseList baseList) {
        log.info("采购订单-托运订单列表-撤销托运:{}", JSONObject.toJSONString(baseList));
        try {
            List<String> stringList = StringUtils.splitStrToList(baseList.getIds(), String.class);
            return logisticsConsignmentService.cancelConsign(stringList, baseList.getRemark(), "1");
        } catch (Exception e) {
            log.error("采购订单-托运订单列表-撤销托运 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购购订单-车辆信息-销售订单ids
     *
     * @param orderQuery    销售订单请求数据
     * @return  响应结果
     */
    @Validated
    @RequestMapping(value = "/purchaseLogisticsCar", method = RequestMethod.POST)
    public HttpResult purchaseLogisticsCar(@Valid @RequestBody OrderQuery orderQuery) {
        log.info("采购订单-车辆详情 传递参数信息:{}", JSONObject.toJSONString(orderQuery));
        try {
            return HttpResult.ok(logisticsConsignmentService.sellLogisticsCar(orderQuery.getIds(), orderQuery.getPage(), orderQuery.getPageSize()));
        } catch (Exception e) {
            log.error("采购订单-车辆详情 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购/销售订单-完成车次统计
     *
     * @param tradeOrderId  贸易订单id
     * @return  响应结果
     */
    @Validated
    @RequestMapping(value = "/statisticsWaybillCount", method = RequestMethod.GET)
    public HttpResult statisticsWaybillCount(@NotBlank(message = "单据号不能为空") @RequestParam("tradeOrderId")  String tradeOrderId) {
        log.info("采购/销售订单-完成车次统计 传递参数信息:{}", tradeOrderId);
        try {
            return HttpResult.ok(logisticsConsignmentService.statisticsWaybillCount(tradeOrderId));
        } 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");
            return transportCommonService.orderFinishDetail(orderId);
        } catch (Exception e) {
            log.error("采购订单-托运订单列表-订单完结-数据查询 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 采购订单-托运订单列表-完结订单
     *
     * @param orderFinishDTO    物流订单完结-页面请求参数
     * @return  响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @RequestMapping(value = "/purchaseOrderFinish", method = RequestMethod.POST)
    public HttpResult purchaseOrderFinish(@RequestBody @Validated OrderManualCompletionDto orderFinishDTO) {
        log.info("采购订单-托运订单列表-完结订单 传递参数信息:{}", JSONObject.toJSONString(orderFinishDTO));
        try {
            //return logisticsConsignmentService.orderFinish(orderFinishDTO, "1");
            return transportCommonService.orderFinish(orderFinishDTO);
        } 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)
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    public HttpResult sellLogisticsConsignment(@Valid @RequestBody LogisticsConsignmentParam logisticsConsignmentParam) {
        log.info("销售订单-物流托运生成托运订单 传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentParam));
        try {
            return logisticsConsignmentService.logisticsConsignment(logisticsConsignmentParam, NumberConstant.TWO);
        } catch (Exception e) {
            OrderGenerateSeqNoUtils.getSeqNoMinusOne(RedisOrderGenerateEnum.LOGISTICS_ORDER,OrderRuleEnum.LOGISTICS_ORDER);
            log.error("物流托运 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-销售订单id
     *
     * @param query 订单查询
     * @return  响应结果
     */
    @RequestMapping(value = "/sellLogisticsOrder", method = RequestMethod.POST)
    public HttpResult sellLogisticsOrder(@Valid @RequestBody OrderQuery query) {
        log.info("销售订单-托运订单列表 传递参数信息:{}", JSONObject.toJSONString(query));
        try {
            return logisticsConsignmentService.logisticsOrder(query.getIds(), query.getPage(), query.getPageSize(), "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 orderQuery 销售订单ids
     * @return  响应结果
     */
    @RequestMapping(value = "/sellLogisticsCar", method = RequestMethod.POST)
    public HttpResult sellLogisticsCar(@Valid @RequestBody OrderQuery orderQuery) {
        log.info("销售订单-车辆列表 传递参数信息:{}", JSONObject.toJSONString(orderQuery));
        try {
            return logisticsConsignmentService.sellLogisticsCar(orderQuery.getIds(), orderQuery.getPage(), orderQuery.getPageSize());
        } catch (Exception e) {
            log.error("销售订单-车辆列表 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * 销售订单-托运订单列表-撤销托运
     *
     * @param baseList 物流订单id
     * @return  响应结果
     */
    @RequestMapping(value = "/sellCancelConsign", method = RequestMethod.POST)
    @GlobalTransactional(name = "default_tx_group")
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    public HttpResult sellCancelConsign(@Valid @RequestBody BaseList baseList) {
        log.info("销售订单-托运订单列表-撤销托运:{}", JSONObject.toJSONString(baseList));
        try {
            List<String> stringList = StringUtils.splitStrToList(baseList.getIds(), String.class);
            return logisticsConsignmentService.cancelConsign(stringList, baseList.getRemark(), "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");
            return transportCommonService.orderFinishDetail(orderId);
        } 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)
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    public HttpResult sellOrderFinish(@RequestBody @Validated OrderManualCompletionDto orderFinishDTO) {
        log.info("销售订单-托运订单列表-完结订单 传递参数信息:{}", JSONObject.toJSONString(orderFinishDTO));
        try {
            //return logisticsConsignmentService.orderFinish(orderFinishDTO, "2");
            return transportCommonService.orderFinish(orderFinishDTO);
        } catch (Exception e) {
            log.error("销售订单-托运订单列表-完结订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

    /**
     * @desc: 采购订单-托运运单列表导出
     * @author: jc
     * @date: 2023-08-09 20:16:22
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/logisticOrderByPurchaseOrderIdExport", produces = MediaType.APPLICATION_JSON_VALUE)
    public void logisticOrderByPurchaseOrderIdExport(@RequestParam("ids") @NotBlank(message = "单据id不能为空") String ids, HttpServletResponse response) {
        List<PurchaseLogisticOrderExcelVo> list = logisticsConsignmentService.logisticOrderByPurchaseOrderIdExport(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, PurchaseLogisticOrderExcelVo.class, list);
    }

    /**
     * @desc: 销售订单-托运运单列表导出
     * @author: jc
     * @date: 2023-08-09 20:16:22
     * @Param params:
     * @return: com.sckw.core.web.response.HttpResult
     */
    @PostMapping(value = "/logisticOrderBySellOrderIdExport", produces = MediaType.APPLICATION_JSON_VALUE)
    public void logisticOrderBySellOrderIdExport(@RequestParam("ids") @NotBlank(message = "单据id不能为空") String ids, HttpServletResponse response) {
        List<SellLogisticOrderExcelVo> list = logisticsConsignmentService.logisticOrderBySellOrderIdExport(ids);
        if (CollectionUtils.isEmpty(list)) {
            throw new CustomPromptException(HttpStatus.SUCCESS_CODE, "暂无数据，请确认");
        }
        ExcelUtil.downData(response, SellLogisticOrderExcelVo.class, list);
    }


    /**
     * 采购订单/销售订单-物流托运-V4.2
     *
     * @param logisticsConsignmentDto 页面请求参数
     * @return  响应结果
     */
    @Validated
    @RepeatSubmit(interval = 1000,message ="前方拥堵，请稍后尝试")
    @RequestMapping(value = "/logisticsOrderConsignment", method = RequestMethod.POST)
    public HttpResult logisticsOrderConsignment(@Valid @RequestBody LogisticsConsignmentDto logisticsConsignmentDto) {
        log.info("采购订单/销售订单-物流托运传递参数信息:{}", JSONObject.toJSONString(logisticsConsignmentDto));
        try {
            return logisticsConsignmentService.logisticsOrderConsignment(logisticsConsignmentDto);
        } catch (Exception e) {
            log.error("采购订单/销售订单-物流托运生成托运订单 error :{}", e.getMessage(), e);
            return HttpResult.error(HttpStatus.GLOBAL_EXCEPTION_CODE, e.getMessage());
        }
    }

}
