package com.fs.his.service.impl;

import java.util.List;

import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsMedicatedFood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsIcdMapper;
import com.fs.his.domain.FsIcd;
import com.fs.his.service.IFsIcdService;

/**
 * icd编码库Service业务层处理
 *
 * @author fs
 * @date 2024-04-22
 */
@Service
public class FsIcdServiceImpl implements IFsIcdService
{
    @Autowired
    private FsIcdMapper fsIcdMapper;

    /**
     * 查询icd编码库
     *
     * @param id icd编码库主键
     * @return icd编码库
     */
    @Override
    public FsIcd selectFsIcdById(Long id)
    {
        return fsIcdMapper.selectFsIcdById(id);
    }

    /**
     * 查询icd编码库列表
     *
     * @param fsIcd icd编码库
     * @return icd编码库
     */
    @Override
    public List<FsIcd> selectFsIcdList(FsIcd fsIcd)
    {
        return fsIcdMapper.selectFsIcdList(fsIcd);
    }

    /**
     * 新增icd编码库
     *
     * @param fsIcd icd编码库
     * @return 结果
     */
    @Override
    public int insertFsIcd(FsIcd fsIcd)
    {
        return fsIcdMapper.insertFsIcd(fsIcd);
    }

    /**
     * 修改icd编码库
     *
     * @param fsIcd icd编码库
     * @return 结果
     */
    @Override
    public int updateFsIcd(FsIcd fsIcd)
    {
        return fsIcdMapper.updateFsIcd(fsIcd);
    }

    /**
     * 批量删除icd编码库
     *
     * @param ids 需要删除的icd编码库主键
     * @return 结果
     */
    @Override
    public int deleteFsIcdByIds(Long[] ids)
    {
        return fsIcdMapper.deleteFsIcdByIds(ids);
    }

    /**
     * 删除icd编码库信息
     *
     * @param id icd编码库主键
     * @return 结果
     */
    @Override
    public int deleteFsIcdById(Long id)
    {
        return fsIcdMapper.deleteFsIcdById(id);
    }

    @Override
    public String importData(List<FsIcd> 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 (FsIcd vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsIcdMapper.insertFsIcd(vo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、名称 " + vo.getIcdName() + " 导入成功");

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + vo.getIcdName()  + " 导入失败：";
                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();
    }

    @Override
    public List<FsIcd> selectFsIcdByName(String name) {
        return fsIcdMapper.selectFsIcdByName(name);
    }
}
