package com.ruoyi.logistics.controller;

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

import com.ruoyi.logistics.annotation.SimpleDictExportUtil;
import com.ruoyi.logistics.domain.RptFinancialMonthSummary;
import com.ruoyi.logistics.service.IRptFinancialMonthSummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.BizWaybillCostDetails;
import com.ruoyi.logistics.service.IBizWaybillCostDetailsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * PC-运单费用明细
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@RestController
@RequestMapping("/costDetails")
public class BizWaybillCostDetailsController extends BaseController
{
    @Autowired
    private IBizWaybillCostDetailsService bizWaybillCostDetailsService;
    @Autowired
    private IRptFinancialMonthSummaryService rptFinancialMonthSummaryService;



    //运单中的费用明细
    @GetMapping("/detaillist")
    public TableDataInfo detaillist(BizWaybillCostDetails bizWaybillCostDetails)
    {
        startPage();
        List<BizWaybillCostDetails> list = bizWaybillCostDetailsService.detaillist(bizWaybillCostDetails);
        return getDataTable(list);
    }








    /**
     * 查询运单明细列表
     * 这个在财务月度汇总需传入供应商id以及外部运单号  查询详情列表
     * 参数：  月份--月度账单中的月份 格式2026.01
     *        供应商--也叫部门id
     */
    @RequiresPermissions("system:costDetails:list")
    @GetMapping("/list")
    public TableDataInfo list(BizWaybillCostDetails bizWaybillCostDetails)
    {
        startPage();
        List<BizWaybillCostDetails> list = bizWaybillCostDetailsService.selectBizWaybillCostDetailsList(bizWaybillCostDetails);
        return getDataTable(list);
    }



    /**
     * 导出运单明细列表
     */
    @RequiresPermissions("system:costDetails:export")
    @Log(title = "运单明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizWaybillCostDetails bizWaybillCostDetails)
    {
        List<BizWaybillCostDetails> list = bizWaybillCostDetailsService.selectBizWaybillCostDetailsList(bizWaybillCostDetails);
        ExcelUtil<BizWaybillCostDetails> util = new ExcelUtil<BizWaybillCostDetails>(BizWaybillCostDetails.class);
        util.exportExcel(response, list, "运单明细数据");
    }



    /**
     * 运单明细详情查询
     * 参数：  详情id
     *
     */

   // @RequiresPermissions("system:costDetails:edit")
    @Log(title = "运单明细调整", businessType = BusinessType.UPDATE)
    @GetMapping ("/getAccountAdjustment")
    public AjaxResult getAccountAdjustment( BizWaybillCostDetails bizWaybillCostDetails)
    {
        try {
            RptFinancialMonthSummary rptFinancialMonthSummary = new RptFinancialMonthSummary();
            rptFinancialMonthSummary.setSummaryMonth(bizWaybillCostDetails.getSummaryMonth());
            rptFinancialMonthSummary.setStatus("1");
            rptFinancialMonthSummary.setDeptId(bizWaybillCostDetails.getDeptId());
            List<RptFinancialMonthSummary> list = rptFinancialMonthSummaryService.selectRptFinancialMonthSummaryListToCheck(rptFinancialMonthSummary);
            if (list.size() != 1) {
                return AjaxResult.success("月度汇总状态发生变化 无法进行调账！");
            }
            //拿着部门id和月份进行查询当前月度账单状态是否允许进行调账
            return AjaxResult.success(bizWaybillCostDetailsService.getAccountAdjustmentById(bizWaybillCostDetails));
        } catch (Exception e) {
            return AjaxResult.error("系统发生错误！");
        }
    }




    /**
     * 修改运单明细  也叫调账
     * 参数：  月份--月度账单中的月份 格式2026.01
     *      *        供应商--也叫部门id
     */

    @RequiresPermissions("system:costDetails:edit")
    @Log(title = "运单明细调整", businessType = BusinessType.UPDATE)
    @PutMapping("/accountAdjustment")
    public AjaxResult accountAdjustment(@RequestBody BizWaybillCostDetails bizWaybillCostDetails)
    {
        try {

            RptFinancialMonthSummary rptFinancialMonthSummary = new RptFinancialMonthSummary();
            rptFinancialMonthSummary.setSummaryMonth(bizWaybillCostDetails.getSummaryMonth());
            rptFinancialMonthSummary.setStatus("1");
            rptFinancialMonthSummary.setDeptId(bizWaybillCostDetails.getDeptId());
            List<RptFinancialMonthSummary> list = rptFinancialMonthSummaryService.selectRptFinancialMonthSummaryList(rptFinancialMonthSummary);
            if (list.size() != 1) {
                return AjaxResult.success("月度汇总状态发生变化 无法进行调账！");
            }
            //拿着部门id和月份进行查询当前月度账单状态是否允许进行调账
            return toAjax(bizWaybillCostDetailsService.accountAdjustment(bizWaybillCostDetails));
        } catch (Exception e) {
            return AjaxResult.error("系统发生错误！");
        }
    }










    /**
     * 获取运单明细详细信息
     */
    @RequiresPermissions("system:costDetails:query")
    @GetMapping(value = "/{waybillDetailId}")
    public AjaxResult getInfo(@PathVariable("waybillDetailId") Long waybillDetailId)
    {
        return success(bizWaybillCostDetailsService.selectBizWaybillCostDetailsByWaybillDetailId(waybillDetailId));
    }

    /**
     * 新增运单明细
     */
    @RequiresPermissions("system:costDetails:add")
    @Log(title = "运单明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizWaybillCostDetails bizWaybillCostDetails)
    {
        return toAjax(bizWaybillCostDetailsService.insertBizWaybillCostDetails(bizWaybillCostDetails));
    }

    /**
     *
     * 修改运单明细
     */
    @RequiresPermissions("system:costDetails:edit")
    @Log(title = "运单明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizWaybillCostDetails bizWaybillCostDetails)
    {
        return toAjax(bizWaybillCostDetailsService.updateBizWaybillCostDetails(bizWaybillCostDetails));
    }

    /**
     * 删除运单明细
     */
    @RequiresPermissions("system:costDetails:remove")
    @Log(title = "运单明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{waybillDetailIds}")
    public AjaxResult remove(@PathVariable Long[] waybillDetailIds)
    {
        return toAjax(bizWaybillCostDetailsService.deleteBizWaybillCostDetailsByWaybillDetailIds(waybillDetailIds));
    }
}
