package com.ruoyi.logistics.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.logistics.mapper.RptFinancialMonthSummaryMapper;
import com.ruoyi.logistics.domain.RptFinancialMonthSummary;
import com.ruoyi.logistics.service.IRptFinancialMonthSummaryService;

/**
 * 财务月度汇总Service业务层处理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@Service
public class RptFinancialMonthSummaryServiceImpl implements IRptFinancialMonthSummaryService 
{
    @Autowired
    private RptFinancialMonthSummaryMapper rptFinancialMonthSummaryMapper;

    /**
     * 查询财务月度汇总
     * 
     * @param summaryId 财务月度汇总主键
     * @return 财务月度汇总
     */
    @Override
    public RptFinancialMonthSummary selectRptFinancialMonthSummaryBySummaryId(Long summaryId)
    {
        return rptFinancialMonthSummaryMapper.selectRptFinancialMonthSummaryBySummaryId(summaryId);
    }

    /**
     * 查询财务月度汇总列表
     * 
     * @param rptFinancialMonthSummary 财务月度汇总
     * @return 财务月度汇总
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<RptFinancialMonthSummary> selectRptFinancialMonthSummaryList(RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        return rptFinancialMonthSummaryMapper.selectRptFinancialMonthSummaryList(rptFinancialMonthSummary);
    }


    @Override
    public int insertRptFinancialMonthSummary(RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        LocalDate today = LocalDate.now();
        LocalDate lastMonth = today.minusMonths(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM");
        // 格式化上个月日期为字符串
        String formattedDate = lastMonth.format(formatter);
        //按照传值条件查询统计账单情况
        Map param = new HashMap<>();
        param.put("deptId",rptFinancialMonthSummary.getDeptId());
        param.put("monthCode",rptFinancialMonthSummary.getSummaryMonth()!=null?rptFinancialMonthSummary.getSummaryMonth():formattedDate);
        List<Map<String,Object>> monthSummaryList=rptFinancialMonthSummaryMapper.selectMonthSummaryList(param);
        return rptFinancialMonthSummaryMapper.insertBachRptFinancialMonthSummary(monthSummaryList);

    }

    /**
     * 修改财务月度汇总
     * 
     * @param rptFinancialMonthSummary 财务月度汇总
     * @return 结果
     */
    @Override
    public int updateRptFinancialMonthSummary(RptFinancialMonthSummary rptFinancialMonthSummary)
    {
        rptFinancialMonthSummary.setUpdateTime(DateUtils.getNowDate());
        return rptFinancialMonthSummaryMapper.updateRptFinancialMonthSummary(rptFinancialMonthSummary);
    }

    /**
     * 批量删除财务月度汇总
     * 
     * @param summaryIds 需要删除的财务月度汇总主键
     * @return 结果
     */
    @Override
    public int deleteRptFinancialMonthSummaryBySummaryIds(Long[] summaryIds)
    {
        return rptFinancialMonthSummaryMapper.deleteRptFinancialMonthSummaryBySummaryIds(summaryIds);
    }

    /**
     * 删除财务月度汇总信息
     * 
     * @param summaryId 财务月度汇总主键
     * @return 结果
     */
    @Override
    public int deleteRptFinancialMonthSummaryBySummaryId(Long summaryId)
    {
        return rptFinancialMonthSummaryMapper.deleteRptFinancialMonthSummaryBySummaryId(summaryId);
    }



    /**
     * 新增财务月度汇总
     * 1.定时任务执行所有供应商生成财务月度汇总    传值汇总月份  monthCode
     * 2.每次调整流水后需要对供应商进行重新生成汇总覆盖   传值部门id    deptId
     *  部门Id和 账单月份必传其一  分别代表以上两种情况
     */
    @Override
    public int creatMontSummary(@NotNull Map param) {
        LocalDate today = LocalDate.now();
        LocalDate lastMonth = today.minusMonths(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM");
        // 格式化上个月日期为字符串
        String formattedDate = lastMonth.format(formatter);
        //按照传值条件查询统计账单情况
        param.put("monthCode",formattedDate);
        param.put("deptId",null);
        List<Map<String,Object>> monthSummaryList=rptFinancialMonthSummaryMapper.selectMonthSummaryList(param);
        return rptFinancialMonthSummaryMapper.insertBachRptFinancialMonthSummary(monthSummaryList);
    }
}
