package com.ruoyi.logistics.controller;

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

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.logistics.annotation.SimpleDictExportUtil;
import com.ruoyi.logistics.util.TimeUtil;
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.RptFinancialMonthSummary;
import com.ruoyi.logistics.service.IRptFinancialMonthSummaryService;
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("/monthSummary")
public class RptFinancialMonthSummaryController extends BaseController
{
    @Autowired
    private IRptFinancialMonthSummaryService rptFinancialMonthSummaryService;
    @Resource
    SimpleDictExportUtil simpleDictExportUtil;
    /**
     * 查询财务月度汇总列表
     */
    @RequiresPermissions("system:monthSummary:list")
    @GetMapping("/list")
    public TableDataInfo list(RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        startPage();
        List<RptFinancialMonthSummary> list = rptFinancialMonthSummaryService.selectRptFinancialMonthSummaryList(rptFinancialMonthSummary);
        return getDataTable(list);
    }

    /**
     * 导出财务月度汇总列表
     */
    @RequiresPermissions("system:monthSummary:export")
    @Log(title = "财务月度汇总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        List<RptFinancialMonthSummary> list = rptFinancialMonthSummaryService.selectRptFinancialMonthSummaryList(rptFinancialMonthSummary);
        simpleDictExportUtil.handleDictList(list);
        ExcelUtil<RptFinancialMonthSummary> util = new ExcelUtil<RptFinancialMonthSummary>(RptFinancialMonthSummary.class);
        util.exportExcel(response, list, "财务月度汇总数据");
    }

    /**
     * 获取财务月度汇总详细信息
     */
    @RequiresPermissions("system:monthSummary:query")
    @GetMapping(value = "/{summaryId}")
    public AjaxResult getInfo(@PathVariable("summaryId") Long summaryId)
    {
        return success(rptFinancialMonthSummaryService.selectRptFinancialMonthSummaryBySummaryId(summaryId));
    }

    /**
     * 新增财务月度汇总
     */
    @RequiresPermissions("system:monthSummary:add")
    @Log(title = "财务月度汇总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        return toAjax(rptFinancialMonthSummaryService.insertRptFinancialMonthSummary(rptFinancialMonthSummary));
    }

    /**
     * 月度账单汇总生成  定时任务定时生成
     */

    @PostMapping("/creatMontSummary")
    public AjaxResult creatMontSummary(@RequestBody Map param)
    {   //先将这个月份没有参与运单的进行处理
        rptFinancialMonthSummaryService.creatMontSummaryZero(param);
        return toAjax(rptFinancialMonthSummaryService.creatMontSummary(param));
    }




    /**
     * 重新计算按钮
     */

    @RequiresPermissions("logistics:rate:reCalc")
    @PostMapping("/recalculateMontSummary")
    public AjaxResult recalculateMontSummary(@RequestBody Map param)
    {
        Object deptIdObj = param.get("deptId");
        String deptId = (deptIdObj == null) ? null : deptIdObj.toString();
        Object monthCodeObj = param.get("monthCode");
        String monthCode = (monthCodeObj == null) ? null : monthCodeObj.toString();
        if(StringUtils.isBlank(deptId)||StringUtils.isBlank(monthCode)){
         return    AjaxResult.error("供应商id或账单月份不能为空!");
        }
        if(!TimeUtil.isBeforeCurrentMonth(monthCode)){
            return    AjaxResult.error("重新生成月份必须为当月之前！");
        }


        boolean result=rptFinancialMonthSummaryService.verification( monthCode, deptId );
        if(result){
            rptFinancialMonthSummaryService.creatMontSummary(param);
            return  AjaxResult.success("重新计算成功！");
        }
        return AjaxResult.error(" 该供应商在重新计算月份内 存在调账或月度账单状态非待对账状态，无法重新计算！");


    }

    /**
     * 修改财务月度汇总
     */
    @RequiresPermissions("system:monthSummary:edit")
    @Log(title = "财务月度汇总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        return toAjax(rptFinancialMonthSummaryService.updateRptFinancialMonthSummary(rptFinancialMonthSummary));
    }

    /**
     * 删除财务月度汇总
     */
    @RequiresPermissions("system:monthSummary:remove")
    @Log(title = "财务月度汇总", businessType = BusinessType.DELETE)
	@DeleteMapping("/{summaryIds}")
    public AjaxResult remove(@PathVariable Long[] summaryIds)
    {
        return toAjax(rptFinancialMonthSummaryService.deleteRptFinancialMonthSummaryBySummaryIds(summaryIds));
    }
}
