package com.fs.his.service.impl;

import java.util.Collections;
import java.util.List;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsChineseMedicine;
import com.fs.his.vo.FsInquiryDiseaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsInquiryDiseaseMapper;
import com.fs.his.domain.FsInquiryDisease;
import com.fs.his.service.IFsInquiryDiseaseService;

/**
 * 问诊疾病Service业务层处理
 *
 * @author fs
 * @date 2024-08-02
 */
@Service
public class FsInquiryDiseaseServiceImpl implements IFsInquiryDiseaseService
{
    @Autowired
    private FsInquiryDiseaseMapper fsInquiryDiseaseMapper;

    /**
     * 查询问诊疾病
     *
     * @param diseaseId 问诊疾病主键
     * @return 问诊疾病
     */
    @Override
    public FsInquiryDisease selectFsInquiryDiseaseByDiseaseId(Long diseaseId)
    {
        return fsInquiryDiseaseMapper.selectFsInquiryDiseaseByDiseaseId(diseaseId);
    }

    /**
     * 查询问诊疾病列表
     *
     * @param fsInquiryDisease 问诊疾病
     * @return 问诊疾病
     */
    @Override
    public List<FsInquiryDisease> selectFsInquiryDiseaseList(FsInquiryDisease fsInquiryDisease)
    {
        return fsInquiryDiseaseMapper.selectFsInquiryDiseaseList(fsInquiryDisease);
    }

    /**
     * 新增问诊疾病
     *
     * @param fsInquiryDisease 问诊疾病
     * @return 结果
     */
    @Override
    public int insertFsInquiryDisease(FsInquiryDisease fsInquiryDisease)
    {
        fsInquiryDisease.setCreateTime(DateUtils.getNowDate());
        return fsInquiryDiseaseMapper.insertFsInquiryDisease(fsInquiryDisease);
    }

    /**
     * 修改问诊疾病
     *
     * @param fsInquiryDisease 问诊疾病
     * @return 结果
     */
    @Override
    public int updateFsInquiryDisease(FsInquiryDisease fsInquiryDisease)
    {
        fsInquiryDisease.setUpdateTime(DateUtils.getNowDate());
        return fsInquiryDiseaseMapper.updateFsInquiryDisease(fsInquiryDisease);
    }

    /**
     * 批量删除问诊疾病
     *
     * @param diseaseIds 需要删除的问诊疾病主键
     * @return 结果
     */
    @Override
    public int deleteFsInquiryDiseaseByDiseaseIds(Long[] diseaseIds)
    {
        return fsInquiryDiseaseMapper.deleteFsInquiryDiseaseByDiseaseIds(diseaseIds);
    }

    /**
     * 删除问诊疾病信息
     *
     * @param diseaseId 问诊疾病主键
     * @return 结果
     */
    @Override
    public int deleteFsInquiryDiseaseByDiseaseId(Long diseaseId)
    {
        return fsInquiryDiseaseMapper.deleteFsInquiryDiseaseByDiseaseId(diseaseId);
    }

    @Override
    public List<FsInquiryDiseaseVO> selectFsInquiryDiseaseListVO(FsInquiryDisease fsInquiryDisease) {
        return fsInquiryDiseaseMapper.selectFsInquiryDiseaseListVO(fsInquiryDisease);
    }

    @Override
    public String importData(List<FsInquiryDisease> list) {
        if (com.fs.common.utils.StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (FsInquiryDisease vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsInquiryDiseaseMapper.insertFsInquiryDisease(vo);

                successNum++;
                successMsg.append("<br/>" + successNum + "、名称 " + vo.getDiseaseName() + " 导入成功");

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " +vo.getDiseaseName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
