package com.ruoyi.logistics.service.impl;

import java.util.*;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.logistics.domain.BizFinancialAdjustmentRecord;
import com.ruoyi.logistics.mapper.BizFinancialAdjustmentRecordMapper;
import com.ruoyi.logistics.service.IRptFinancialMonthSummaryService;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.logistics.mapper.BizWaybillCostDetailsMapper;
import com.ruoyi.logistics.domain.BizWaybillCostDetails;
import com.ruoyi.logistics.service.IBizWaybillCostDetailsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运单明细Service业务层处理
 * 
 * @author RuiJing
 * @date 2026-01-29
 */
@Service
public class BizWaybillCostDetailsServiceImpl implements IBizWaybillCostDetailsService 
{
    @Autowired
    private BizWaybillCostDetailsMapper bizWaybillCostDetailsMapper;
    @Autowired
    private BizFinancialAdjustmentRecordMapper bizFinancialAdjustmentRecordMapper;
    @Autowired
    private IRptFinancialMonthSummaryService rptFinancialMonthSummaryService;

    /**
     * 查询运单明细
     * 
     * @param waybillDetailId 运单明细主键
     * @return 运单明细
     */
    @Override
    public BizWaybillCostDetails selectBizWaybillCostDetailsByWaybillDetailId(Long waybillDetailId)
    {
        return bizWaybillCostDetailsMapper.selectBizWaybillCostDetailsByWaybillDetailId(waybillDetailId);
    }

    /**
     * 查询运单明细列表
     * 
     * @param bizWaybillCostDetails 运单明细
     * @return 运单明细
     */
    @Override
    @DataScope(deptAlias = "a")
    public List<BizWaybillCostDetails> selectBizWaybillCostDetailsList(BizWaybillCostDetails bizWaybillCostDetails)
    {
        return bizWaybillCostDetailsMapper.selectBizWaybillCostDetailsList(bizWaybillCostDetails);
    }

    /**
     * 新增运单明细
     * 
     * @param bizWaybillCostDetails 运单明细
     * @return 结果
     */
    @Override
    public int insertBizWaybillCostDetails(BizWaybillCostDetails bizWaybillCostDetails)
    {
        bizWaybillCostDetails.setCreateTime(DateUtils.getNowDate());
        return bizWaybillCostDetailsMapper.insertBizWaybillCostDetails(bizWaybillCostDetails);
    }

    /**
     * 修改运单明细
     * 
     * @param bizWaybillCostDetails 运单明细
     * @return 结果
     */
    @Override
    public int updateBizWaybillCostDetails(BizWaybillCostDetails bizWaybillCostDetails)
    {
        bizWaybillCostDetails.setUpdateTime(DateUtils.getNowDate());
        return bizWaybillCostDetailsMapper.updateBizWaybillCostDetails(bizWaybillCostDetails);
    }

    /**
     * 批量删除运单明细
     * 
     * @param waybillDetailIds 需要删除的运单明细主键
     * @return 结果
     */
    @Override
    public int deleteBizWaybillCostDetailsByWaybillDetailIds(Long[] waybillDetailIds)
    {
        return bizWaybillCostDetailsMapper.deleteBizWaybillCostDetailsByWaybillDetailIds(waybillDetailIds);
    }

    /**
     * 删除运单明细信息
     * 
     * @param waybillDetailId 运单明细主键
     * @return 结果
     */
    @Override
    public int deleteBizWaybillCostDetailsByWaybillDetailId(Long waybillDetailId)
    {
        return bizWaybillCostDetailsMapper.deleteBizWaybillCostDetailsByWaybillDetailId(waybillDetailId);
    }

    @Override
    public List<BizWaybillCostDetails> selectBizWaybillCostDetailsListByWx(BizWaybillCostDetails bizWaybillCostDetails) {
        return bizWaybillCostDetailsMapper.selectBizWaybillCostDetailsListByWx(bizWaybillCostDetails);
    }


    /**
     * 调账功能
     * 调账+调账记录生成
     * 参数：BizWaybillCostDetails
     *  1.供应商id   费用清单明细的deptId
     *  2.账单月份   月度报表的月份
     *  3.原金额     rateAmount
     *  4.调账金额   adjustAmount
     *  5.调账原因  字典获取value值
     *  6.运单号    费用明细的运单号
     *  7.费用明细id
     *
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int accountAdjustment(BizWaybillCostDetails bizWaybillCostDetails) {

    try {
        LoginUser loginUser = SecurityUtils.getLoginUser();
    //调账流水记录
    BizFinancialAdjustmentRecord bizFinancialAdjustmentRecord = new BizFinancialAdjustmentRecord();
    bizFinancialAdjustmentRecord.setWaybillNo(bizWaybillCostDetails.getExternalWaybillNo());
    bizFinancialAdjustmentRecord.setWaybillCostDetailId(bizWaybillCostDetails.getWaybillDetailId());
    bizFinancialAdjustmentRecord.setAdjustmentType(bizWaybillCostDetails.getAdjustmentType());
    bizFinancialAdjustmentRecord.setOriginalAmount(bizWaybillCostDetails.getRateAmount());
    bizFinancialAdjustmentRecord.setAdjustedAmount(bizWaybillCostDetails.getAdjustAmount());
    //调整金额 由调整后金额-原金额 subtract
    bizFinancialAdjustmentRecord.setAdjustmentAmount(bizWaybillCostDetails.getAdjustAmount().subtract(bizWaybillCostDetails.getRateAmount()));
    bizFinancialAdjustmentRecord.setAdjustmentReason(bizWaybillCostDetails.getRemark());
    bizFinancialAdjustmentRecord.setCreateTime(new Date());
    bizFinancialAdjustmentRecord.setCreateBy(loginUser.getSysUser().getNickName());
    bizFinancialAdjustmentRecord.setUserId(loginUser.getUserid());
    bizFinancialAdjustmentRecord.setDeptId(bizWaybillCostDetails.getDeptId());
        bizFinancialAdjustmentRecord.setAdjustmentReason(bizWaybillCostDetails.getAdjustmentReason());
        bizFinancialAdjustmentRecord.setSummaryMonth(bizWaybillCostDetails.getSummaryMonth());
    bizFinancialAdjustmentRecordMapper.insertBizFinancialAdjustmentRecord(bizFinancialAdjustmentRecord);
    bizWaybillCostDetails.setUpdateTime(DateUtils.getNowDate());
    bizWaybillCostDetails.setUpdateBy(loginUser.getSysUser().getNickName());
    bizWaybillCostDetails.setRateAmount(null);
    bizWaybillCostDetailsMapper.updateBizWaybillCostDetails(bizWaybillCostDetails);
    //还要重新生成该供应商的月度账单报表
        Map param= new HashMap();
        param.put("deptId",bizWaybillCostDetails.getDeptId());
        param.put("monthCode",bizWaybillCostDetails.getSummaryMonth());
        return  rptFinancialMonthSummaryService.creatMontSummaryAccountAdjustment(param);


    }
catch(Exception e)
          {
    throw new RuntimeException("调账失败！", e);
      }

    }

    @Override
    public BizWaybillCostDetails getAccountAdjustmentById(BizWaybillCostDetails bizWaybillCostDetails) {
        return bizWaybillCostDetailsMapper.getAccountAdjustmentById(bizWaybillCostDetails);
    }

    @Override
    public List<BizWaybillCostDetails> detaillist(BizWaybillCostDetails bizWaybillCostDetails) {
        return bizWaybillCostDetailsMapper.detaillist(bizWaybillCostDetails);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upsertBizWaybillCostDetails(BizWaybillCostDetails bizWaybillCostDetails) {
        // 设置更新时间
        bizWaybillCostDetails.setUpdateTime(DateUtils.getNowDate());
        // 如果创建时间未设置，则设置为当前时间
        if (bizWaybillCostDetails.getCreateTime() == null) {
            bizWaybillCostDetails.setCreateTime(DateUtils.getNowDate());
            bizWaybillCostDetails.setDelFlag("0");
        }
        return bizWaybillCostDetailsMapper.upsertBizWaybillCostDetails(bizWaybillCostDetails);
    }
}
