package com.ruoyi.front.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.logistics.domain.BizWaybillOrder;
import com.ruoyi.logistics.service.IBizWaybillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * WX-运单管理
 *
 * @author RuiJing
 * @date 2026-01-29
 */
@RestController
@RequestMapping("/front/order")
public class WaybillOrderController extends BaseController {

    @Autowired
    private IBizWaybillOrderService bizWaybillOrderService;

    /**
     * 查询运单管理列表
     */
    @GetMapping("/list")
    @RequiresPermissions("system:order:list")
    public TableDataInfo list(BizWaybillOrder bizWaybillOrder)
    {
        startPage();
        List<BizWaybillOrder> list = bizWaybillOrderService.selectBizWaybillOrderList(bizWaybillOrder);
        return getDataTable(list);
    }

    /**
     * 获取运单管理详细信息
     */
    @GetMapping(value = "/{waybillId}")
    public AjaxResult getInfo(@PathVariable("waybillId") Long waybillId)
    {
        return success(bizWaybillOrderService.selectBizWaybillOrderByWaybillId(waybillId));
    }

    /**
     * 新增运单管理
     */
    @Log(title = "运单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWaybillOrder bizWaybillOrder) {
        try{
            return toAjax(bizWaybillOrderService.insertBizWaybillOrder(bizWaybillOrder));
        }
        catch(ServiceException e){
            return AjaxResult.error("下单失败！ 原因:"+e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 取消运单
     */
    @Log(title = "运单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWaybillOrder bizWaybillOrder)
    {

        try{
            bizWaybillOrderService.canceleBizWaybillOrder(bizWaybillOrder);
            return AjaxResult.success("运单取消成功！");
        }
        catch(ServiceException e){
            return AjaxResult.error("取消运单失败！ 原因:"+e.getMessage());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
