package com.eitc.patient.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eitc.common.utils.PatientBaseUtil;
import com.eitc.common.utils.bean.BeanUtils;
import com.eitc.patient.domain.DiagnosisManage;
import com.eitc.patient.excel.DiagnosisManageTemplateExcel;
import com.eitc.patient.mapper.DiagnosisManageMapper;
import com.eitc.patient.service.IDiagnosisManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * DiagnosisManageServiceImpl
 *
 * @author mtx
 * @date: 2024/05/17 15:39
 */
@Service
public class DiagnosisManageServiceImpl extends ServiceImpl<DiagnosisManageMapper, DiagnosisManage> implements IDiagnosisManageService {

    @Resource
    private IDiagnosisManageService diagnosisManageService;

    /**
     * 导入模板数据
     *
     * @param list 待导入的诊断管理模板Excel列表
     * @return 导入失败的诊断管理模板Excel列表，包含错误信息
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public List<DiagnosisManageTemplateExcel> importTemplate(List<DiagnosisManageTemplateExcel> list) {
        // 用于存储导入成功的诊断管理对象
        List<DiagnosisManage> successList = new ArrayList<>();
        // 用于存储导入失败的诊断管理模板Excel对象，保留错误信息
        List<DiagnosisManageTemplateExcel> errorList = new ArrayList<>();

        // 封装已存在的诊断管理
        List<DiagnosisManage> manageList = diagnosisManageService.list();
        List<String> diagnosisCodeList = new ArrayList<>();
        List<String> diagnosisNameList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(manageList)) {
            diagnosisCodeList.addAll(manageList.stream().map(DiagnosisManage::getDiagnosisCode).collect(Collectors.toList()));
            diagnosisNameList.addAll(manageList.stream().map(DiagnosisManage::getDiagnosisName).collect(Collectors.toList()));
        }

        // 校验数据是否正确
        for (DiagnosisManageTemplateExcel excel : list) {
            DiagnosisManage diagnosisManage = new DiagnosisManage();
            // 检查诊断编码是否为空
            if (StringUtils.isEmpty(excel.getDiagnosisCode())) {
                excel.setMsg("诊断编码不能为空");
                errorList.add(excel);
                continue;
            }
            // 检查诊断编码是否存在
            if (diagnosisCodeList.contains(excel.getDiagnosisCode())) {
                excel.setMsg("诊断编码已存在");
                errorList.add(excel);
                continue;
            }
            // 检查诊断名称是否为空
            if (StringUtils.isEmpty(excel.getDiagnosisName())) {
                excel.setMsg("诊断名称不能为空");
                errorList.add(excel);
                continue;
            }
            // 检查诊断名称是否存在
            if (diagnosisNameList.contains(excel.getDiagnosisName())) {
                excel.setMsg("诊断名称已存在");
                errorList.add(excel);
                continue;
            }
            // 将Excel对象的属性复制到诊断管理对象
            BeanUtils.copyProperties(excel, diagnosisManage);
            // 首拼
            diagnosisManage.setDiagnosisNamePinyinFirstLetter(PatientBaseUtil.getPinYinFirstLetter(diagnosisManage.getDiagnosisName()));
            diagnosisManage.setDiagnosisNamePinyin(PatientBaseUtil.getPinyin(diagnosisManage.getDiagnosisName()));
            // 将校验成功的诊断管理对象添加到成功列表
            successList.add(diagnosisManage);
        }
        // 如果有成功导入的数据，则批量保存
        if (CollectionUtils.isNotEmpty(successList)) {
            this.saveBatch(successList);
        }
        // 返回导入失败的列表，外部可用于显示错误信息
        return errorList;
    }

}
