/*
 * Copyright ©2023 CJB-CNIT Team. All rights reserved
 */
package com.openhis.financial.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.openhis.administration.dto.ChargeItemDefInfo;
import com.openhis.common.enums.PaymentStatisticalMethod;
import com.openhis.common.enums.PaymentType;
import com.openhis.financial.domain.PaymentRecStatic;
import com.openhis.financial.mapper.PaymentRecStaticMapper;
import com.openhis.financial.service.IPaymentRecStaticService;

/**
 * 付款管理Service业务层处理
 *
 * @author SunJQ
 * @date 2025-08-08
 */
@Service
public class PaymentRecStaticServiceImpl extends ServiceImpl<PaymentRecStaticMapper, PaymentRecStatic>
    implements IPaymentRecStaticService {

    /**
     * 批量保存付款快照信息
     * 
     * @param paymentId 付款id
     * @param paymentType 付款/退款
     * @param paymentStatisticalMethod 统计方式：字典/医保分类
     * @param chargeItemDefInfoList chargeItem基础信息
     */
    @Override
    public void savePaymentRecStatics(Long paymentId, PaymentType paymentType,
        PaymentStatisticalMethod paymentStatisticalMethod, List<ChargeItemDefInfo> chargeItemDefInfoList) {
        if (chargeItemDefInfoList.isEmpty()) {
            return;
        }
        Map<String, List<ChargeItemDefInfo>> collect;
        List<PaymentRecStatic> paymentRecStatics = new ArrayList<>();
        if (paymentStatisticalMethod == PaymentStatisticalMethod.FIN_TYPE_CODE) {
            collect = chargeItemDefInfoList.stream().collect(Collectors.groupingBy(ChargeItemDefInfo::getTypeCode));
            getPaymentRecStaticList(paymentId, paymentType, paymentStatisticalMethod, collect, paymentRecStatics);
        } else if (paymentStatisticalMethod == PaymentStatisticalMethod.MED_CHRGITM_TYPE) {
            collect = chargeItemDefInfoList.stream().collect(Collectors.groupingBy(ChargeItemDefInfo::getYbType));
            getPaymentRecStaticList(paymentId, paymentType, paymentStatisticalMethod, collect, paymentRecStatics);
        } else {
            collect = chargeItemDefInfoList.stream().collect(Collectors.groupingBy(ChargeItemDefInfo::getTypeCode));
            getPaymentRecStaticList(paymentId, paymentType, PaymentStatisticalMethod.FIN_TYPE_CODE, collect,
                paymentRecStatics);
            collect = chargeItemDefInfoList.stream().collect(Collectors.groupingBy(ChargeItemDefInfo::getYbType));
            getPaymentRecStaticList(paymentId, paymentType, PaymentStatisticalMethod.MED_CHRGITM_TYPE, collect,
                paymentRecStatics);
        }
        saveBatch(paymentRecStatics);
    }

    /**
     *
     * @param paymentId 付款id
     * @param paymentType 付款/退款
     * @param paymentStatisticalMethod 统计方式：字典/医保分类
     * @param collect map
     * @param paymentRecStatics 集合
     */
    public void getPaymentRecStaticList(Long paymentId, PaymentType paymentType,
        PaymentStatisticalMethod paymentStatisticalMethod, Map<String, List<ChargeItemDefInfo>> collect,
        List<PaymentRecStatic> paymentRecStatics) {
        PaymentRecStatic paymentRecStatic;
        BigDecimal bigDecimal;
        for (Map.Entry<String, List<ChargeItemDefInfo>> stringListEntry : collect.entrySet()) {
            paymentRecStatic = new PaymentRecStatic();
            bigDecimal = BigDecimal.ZERO;
            for (ChargeItemDefInfo chargeItemDefInfo : stringListEntry.getValue()) {
                bigDecimal = bigDecimal.add(chargeItemDefInfo.getTotalPrice().multiply(paymentType.getMultiplier()));
            }
            paymentRecStatic.setAmount(bigDecimal).setReconciliationId(paymentId).setTypeCode(stringListEntry.getKey())
                .setChargeItemIds(JSON.toJSONString(
                    stringListEntry.getValue().stream().map(ChargeItemDefInfo::getId).collect(Collectors.toList())))
                .setCountBy(paymentStatisticalMethod.getValue());
            paymentRecStatics.add(paymentRecStatic);
        }
        // return paymentRecStatics;
    }
}
