package com.retdata.yaoyibi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.retdata.yaoyibi.domain.WmDaDrugEntDrugtable;
import com.retdata.yaoyibi.domain.WmDaDrugEntDrugtableSummary;
import com.retdata.yaoyibi.mapper.WmDaDrugEntDrugtableMapper;
import com.retdata.yaoyibi.service.WmDaDrugEntDrugtableService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Transactional(readOnly = true)
@DataSource(DataSourceType.CSO)
public class WmDaDrugEntDrugtableServiceImpl extends ServiceImpl<WmDaDrugEntDrugtableMapper, WmDaDrugEntDrugtable>
        implements WmDaDrugEntDrugtableService {
    @Override
    public void parseDrugtable(String drugtable, Map<Integer, WmDaDrugEntDrugtableSummary> drugEntDrugtableMap) {
        if (StringUtils.hasText(drugtable)) {
            String[] drugtableItems = drugtable.split(",");
            for (String item : drugtableItems) {
                if (StringUtils.hasText(item)) {
                    drugEntDrugtableMap.put(Integer.valueOf(item), null);
                }
            }
        }
    }

    @Override
    public List<WmDaDrugEntDrugtableSummary> batchFetchDrugs(Collection<Integer> drugIds) {
        List<Integer> batchIds = new ArrayList<>(drugIds);
        List<WmDaDrugEntDrugtableSummary> result = new ArrayList<>(drugIds.size());
        int batchSize = 1000;
        for (int i = 0; i < batchIds.size(); i += batchSize) {
            int end = Math.min(i + batchSize, batchIds.size());
            List<WmDaDrugEntDrugtableSummary> batchResult =
                    getBaseMapper().selectDrugtableSummary(batchIds.subList(i, end));
            result.addAll(batchResult);
        }
        return result;
    }

    @Override
    public String[] getDrugtableInfo(String drugtable, Map<Integer, WmDaDrugEntDrugtableSummary> drugEntDrugtableMap) {
        String[] result = new String[2];
        if (StringUtils.hasText(drugtable)) {
            String[] drugtableItems = drugtable.split(",");
            Set<String> drugNames = new LinkedHashSet<>();
            Set<String> drugEntNames = new LinkedHashSet<>();
            for (String item : drugtableItems) {
                if (StringUtils.hasText(item)) {
                    WmDaDrugEntDrugtableSummary drugSummary = drugEntDrugtableMap.get(Integer.valueOf(item));
                    if (drugSummary != null) {
                        drugNames.add(drugSummary.getDrugname());
                        if (StringUtils.hasText(drugSummary.getDrugEntName())) {
                            drugEntNames.add(drugSummary.getDrugEntName());
                        }
                    }
                }
            }
            result[0] = StringUtils.collectionToCommaDelimitedString(drugNames);
            result[1] = StringUtils.collectionToCommaDelimitedString(drugEntNames);
        } else {
            result[0] = "";
            result[1] = "";
        }
        return result;
    }
}
