package com.ruoyi.logistics.controller;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
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 com.ruoyi.logistics.service.LogisticsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * PC-运单管理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@RestController
@RequestMapping("/order")
public class BizWaybillOrderController extends BaseController
{
    @Autowired
    private IBizWaybillOrderService bizWaybillOrderService;

    /**
     * 查询运单管理列表
     */
    @RequiresPermissions("system:order:list")
    @GetMapping("/list")

    public TableDataInfo list(BizWaybillOrder bizWaybillOrder)
    {
        startPage();
        List<BizWaybillOrder> list = bizWaybillOrderService.selectBizWaybillOrderList(bizWaybillOrder);
        return getDataTable(list);
    }

    /**
     * 导出运单管理列表
     */
    @RequiresPermissions("system:order:export")
    @Log(title = "运单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWaybillOrder bizWaybillOrder)
    {
        List<BizWaybillOrder> list = bizWaybillOrderService.selectBizWaybillOrderList(bizWaybillOrder);
        ExcelUtil<BizWaybillOrder> util = new ExcelUtil<BizWaybillOrder>(BizWaybillOrder.class);
        util.exportExcel(response, list, "运单管理数据");
    }

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

    /**
     * 新增运单管理
     */
    @RequiresPermissions("system:order:add")
    @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);
        }

    }








    /**
     * 取消运单
     */
    @RequiresPermissions("system:order:edit")
    @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);
        }
    }

    /**
     * 删除运单管理
     */
    @RequiresPermissions("system:order:remove")
    @Log(title = "运单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{waybillIds}")
    public AjaxResult remove(@PathVariable Long[] waybillIds)
    {
        return toAjax(bizWaybillOrderService.deleteBizWaybillOrderByWaybillIds(waybillIds));
    }



    /**
     * 获取运单管理详细信息
     */
    @RequiresPermissions("system:order:query")
    @GetMapping(value = "/getFeeInfo")
    public AjaxResult getFeeInfo(@RequestParam("waybillCode") String waybillCode) {
        return success(bizWaybillOrderService.queryOrderFeeInfo(1, waybillCode));
    }


    /**
     * 获取运单pdf
     */
    @RequiresPermissions("system:order:query")
    @GetMapping(value = "/getWaybillPdf")
    public AjaxResult getWaybillPdf(@RequestParam("waybillId") Long waybillId) {
        BizWaybillOrder bizWaybillOrder = bizWaybillOrderService.selectBizWaybillOrderByWaybillId(waybillId);
        return success(bizWaybillOrderService.queryWaybillPdf(bizWaybillOrder));
    }
}
