package com.openhis.web.nenu.appservice.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.core.common.enums.DeleteFlag;
import com.openhis.common.enums.GfItemType;
import com.openhis.common.enums.GfRatioType;
import com.openhis.common.enums.ybenums.YbChrgitmLv;
import com.openhis.common.utils.HisQueryUtils;
import com.openhis.nenu.domain.GfRatio;
import com.openhis.nenu.dto.GfTypeRatioDto;
import com.openhis.nenu.service.IGfRatioService;
import com.openhis.web.nenu.appservice.IGfRatioManageAppService;
import com.openhis.web.nenu.dto.GfIndividualRatioDto;
import com.openhis.web.nenu.mapper.GfRatioManageAppMapper;

/**
 * 公费医疗-自付比例管理 AppServiceImpl
 *
 * @author Thanking
 * @date 2025-10-21
 */
@Service
public class GfRatioManageAppServiceImpl implements IGfRatioManageAppService {

    @Autowired
    private IGfRatioService gfRatioService;
    @Autowired
    private GfRatioManageAppMapper gfRatioManageAppMapper;

    /**
     * 查询大项比例列表
     *
     * @return 大项比例列表
     */
    @Override
    public R<List<GfTypeRatioDto>> getTypeRatioList() {
        return R.ok(gfRatioService.getTypeRatioList());
    }

    /**
     * 保存大项比例列表
     *
     * @param typeRatioList 大项比例列表
     * @return 结果
     */
    @Override
    public R<?> saveTypeRatioList(List<GfTypeRatioDto> typeRatioList) {
        // 查询DB中的所有大项比例
        List<GfRatio> gfRatioList = gfRatioService
            .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<GfRatio> addGfRatioList = new ArrayList<>();
        List<GfRatio> editGfRatioList = new ArrayList<>();
        for (GfTypeRatioDto dto : typeRatioList) {
            if (dto.getSelfRatio() == null) {
                continue;
            }
            GfRatio editGfRatio = gfRatioMap.get(dto.getYbClass() + dto.getYbLv());
            if (editGfRatio == null) {
                // DB不存在时新增
                GfRatio addGfRatio = new GfRatio().setRatioType(GfRatioType.TYPE_RATIO.getValue())
                    .setYbClass(dto.getYbClass()).setYbLv(dto.getYbLv()).setSelfRatio(dto.getSelfRatio());
                addGfRatioList.add(addGfRatio);
            } else {
                // DB存在时修改
                editGfRatio.setSelfRatio(dto.getSelfRatio());
                editGfRatioList.add(editGfRatio);
            }
        }
        // 批量DB处理
        if (!addGfRatioList.isEmpty()) {
            gfRatioService.saveBatch(addGfRatioList);
        }
        if (!editGfRatioList.isEmpty()) {
            gfRatioService.updateBatchById(editGfRatioList);
        }
        return R.ok();
    }

    /**
     * 查询单项比例分页
     *
     * @param searchParam 查询参数
     * @param searchKey 模糊查询关键字
     * @param pageNo 页码
     * @param pageSize 行数
     * @return 单项比例分页
     */
    @Override
    public R<IPage<GfIndividualRatioDto>> getIndividualRatioPage(GfIndividualRatioDto searchParam, String searchKey,
        Integer pageNo, Integer pageSize) {
        // 构造查询条件
        QueryWrapper<GfIndividualRatioDto> queryWrapper = HisQueryUtils.buildQueryWrapper(searchParam, searchKey,
            new HashSet<>(Set.of("bus_no", "name", "py_str", "wb_str", "yb_no")), null);
        // 查询单项比例分页
        IPage<GfIndividualRatioDto> page =
            gfRatioManageAppMapper.selectGfIndividualRatioPage(new Page<>(pageNo, pageSize), queryWrapper);
        // 设置公费项目分类名称
        for (GfIndividualRatioDto dto : page.getRecords()) {
            dto.setItemTypeName(GfItemType.getByValue(dto.getItemType()).getInfo());
            YbChrgitmLv ybChrgitmLv = YbChrgitmLv.getByValue(dto.getChrgitmLv());
            if (ybChrgitmLv != null) {
                dto.setChrgitmLvName(ybChrgitmLv.getInfo());
            }
        }
        return R.ok(page);
    }
}
