package com.openhis.nenu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.enums.DeleteFlag;
import com.openhis.common.enums.DelFlag;
import com.openhis.common.enums.GfRatioType;
import com.openhis.common.enums.ItemType;
import com.openhis.common.enums.ybenums.YbChrgitmLv;
import com.openhis.common.enums.ybenums.YbMedChrgItmType;
import com.openhis.financial.model.PaymentedItemModel;
import com.openhis.financial.model.StudentPaymentItemModel;
import com.openhis.nenu.domain.GfRatio;
import com.openhis.nenu.dto.GfTypeRatioDto;
import com.openhis.nenu.mapper.GfRatioMapper;
import com.openhis.nenu.service.IGfRatioService;

/**
 * 公费医疗自付比例管理表Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class GfRatioServiceImpl extends ServiceImpl<GfRatioMapper, GfRatio> implements IGfRatioService {

    /**
     * 查询自付比例（按照小项查询自付比例，如果没有的话，返回值不一定会有自付比例，还要查大项）
     * 
     * @param itemType
     * @param paymentItemList
     * @return
     */
    public List<StudentPaymentItemModel> getGfRatioList(ItemType itemType, List<PaymentedItemModel> paymentItemList) {

        ArrayList<StudentPaymentItemModel> list = new ArrayList<>();

        List<Long> serviceIdList =
            paymentItemList.stream().map(PaymentedItemModel::getServiceId).collect(Collectors.toList());

        Date date = new Date();
        // 查询自付比例
        List<GfRatio> gfRatios =
            baseMapper.selectList(new LambdaQueryWrapper<GfRatio>().in(GfRatio::getDefinitionId, serviceIdList)
                .eq(GfRatio::getDeleteFlag, DelFlag.NO.getCode()).eq(GfRatio::getItemType, itemType.getValue())
                .ge(GfRatio::getStartDate, date).le(GfRatio::getEndDate, date));

        Map<Long, List<GfRatio>> gfRatiosMap =
            gfRatios.stream().collect(Collectors.groupingBy(GfRatio::getDefinitionId));

        StudentPaymentItemModel studentPaymentItemModel;
        for (PaymentedItemModel paymentedItemModel : paymentItemList) {
            // copy字段
            studentPaymentItemModel = new StudentPaymentItemModel();
            BeanUtils.copyProperties(paymentedItemModel, studentPaymentItemModel);

            List<GfRatio> gfRatiosList = gfRatiosMap.get(paymentedItemModel.getServiceId());
            if (gfRatiosList != null && !gfRatiosList.isEmpty()) {
                // 赋值
                studentPaymentItemModel.setSelfRatio(gfRatiosList.get(0).getSelfRatio());
                studentPaymentItemModel.setTwiceRatio(gfRatiosList.get(0).getTwiceRatio());

            }
            list.add(studentPaymentItemModel);
        }

        return list;
    }

    /**
     * 查询大项比例列表
     *
     * @return 大项比例列表
     */
    @Override
    public List<GfTypeRatioDto> getTypeRatioList() {
        // 查询DB中的所有大项比例
        List<GfRatio> gfRatioList =
            this.list(new LambdaQueryWrapper<GfRatio>().eq(GfRatio::getRatioType, GfRatioType.TYPE_RATIO.getValue())
                .eq(GfRatio::getDeleteFlag, DeleteFlag.NOT_DELETED.getCode()));
        // 做成医保分项+医保等级拼接的Map
        Map<String, GfRatio> gfRatioMap =
            gfRatioList.stream().collect(Collectors.toMap(e -> e.getYbClass() + e.getYbLv(), Function.identity()));
        List<GfTypeRatioDto> gfTypeRatioDtoList = new ArrayList<>();
        // 遍历所有医疗收费项目类别
        for (YbMedChrgItmType ybMedChrgItmType : YbMedChrgItmType.values()) {
            // 甲类
            GfTypeRatioDto gfTypeRatioDtoA =
                new GfTypeRatioDto().setYbClass(ybMedChrgItmType.getValue()).setYbClassName(ybMedChrgItmType.getInfo())
                    .setYbLv(YbChrgitmLv.CATEGORY_A.getCode()).setYbLvName(YbChrgitmLv.CATEGORY_A.getInfo());
            GfRatio gfRatioA = gfRatioMap.get(ybMedChrgItmType.getValue() + YbChrgitmLv.CATEGORY_A.getCode());
            if (gfRatioA != null) {
                // DB存在则取DB的值
                gfTypeRatioDtoA.setSelfRatio(gfRatioA.getSelfRatio());
            }
            gfTypeRatioDtoList.add(gfTypeRatioDtoA);
            // 乙类
            GfTypeRatioDto gfTypeRatioDtoB =
                new GfTypeRatioDto().setYbClass(ybMedChrgItmType.getValue()).setYbClassName(ybMedChrgItmType.getInfo())
                    .setYbLv(YbChrgitmLv.CATEGORY_B.getCode()).setYbLvName(YbChrgitmLv.CATEGORY_B.getInfo());
            GfRatio gfRatioB = gfRatioMap.get(ybMedChrgItmType.getValue() + YbChrgitmLv.CATEGORY_B.getCode());
            if (gfRatioB != null) {
                // DB存在则取DB的值
                gfTypeRatioDtoB.setSelfRatio(gfRatioB.getSelfRatio());
            }
            gfTypeRatioDtoList.add(gfTypeRatioDtoB);
        }
        // 返回列表
        return gfTypeRatioDtoList;
    }
}