package com.fs.his.service.impl;

import java.util.List;


import com.fs.common.exception.ServiceException;
import com.fs.his.param.FsIllnessLibraryListDParam;
import com.fs.his.vo.FsIllnessLibraryListDVO;
import com.fs.his.vo.FsIllnessLibraryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsIllnessLibraryMapper;
import com.fs.his.domain.FsIllnessLibrary;
import com.fs.his.service.IFsIllnessLibraryService;

/**
 * 中医疾病Service业务层处理
 *
 * @author fs
 * @date 2023-09-04
 */
@Service
public class FsIllnessLibraryServiceImpl implements IFsIllnessLibraryService
{
    @Autowired
    private FsIllnessLibraryMapper fsIllnessLibraryMapper;

    /**
     * 查询中医疾病
     *
     * @param illnessId 中医疾病主键
     * @return 中医疾病
     */
    @Override
    public FsIllnessLibrary selectFsIllnessLibraryByIllnessId(Long illnessId)
    {
        return fsIllnessLibraryMapper.selectFsIllnessLibraryByIllnessId(illnessId);
    }

    /**
     * 查询中医疾病列表
     *
     * @param fsIllnessLibrary 中医疾病
     * @return 中医疾病
     */
    @Override
    public List<FsIllnessLibrary> selectFsIllnessLibraryList(FsIllnessLibrary fsIllnessLibrary)
    {
        return fsIllnessLibraryMapper.selectFsIllnessLibraryList(fsIllnessLibrary);
    }

    /**
     * 新增中医疾病
     *
     * @param fsIllnessLibrary 中医疾病
     * @return 结果
     */
    @Override
    public int insertFsIllnessLibrary(FsIllnessLibrary fsIllnessLibrary)
    {
        return fsIllnessLibraryMapper.insertFsIllnessLibrary(fsIllnessLibrary);
    }

    /**
     * 修改中医疾病
     *
     * @param fsIllnessLibrary 中医疾病
     * @return 结果
     */
    @Override
    public int updateFsIllnessLibrary(FsIllnessLibrary fsIllnessLibrary)
    {
        return fsIllnessLibraryMapper.updateFsIllnessLibrary(fsIllnessLibrary);
    }

    /**
     * 批量删除中医疾病
     *
     * @param illnessIds 需要删除的中医疾病主键
     * @return 结果
     */
    @Override
    public int deleteFsIllnessLibraryByIllnessIds(Long[] illnessIds)
    {
        return fsIllnessLibraryMapper.deleteFsIllnessLibraryByIllnessIds(illnessIds);
    }

    /**
     * 删除中医疾病信息
     *
     * @param illnessId 中医疾病主键
     * @return 结果
     */
    @Override
    public int deleteFsIllnessLibraryByIllnessId(Long illnessId)
    {
        return fsIllnessLibraryMapper.deleteFsIllnessLibraryByIllnessId(illnessId);
    }

    @Override
    public List<FsIllnessLibraryVO> selectFsIllnessLibraryVOByIllnessName(String name) {
        return fsIllnessLibraryMapper.selectFsIllnessLibraryVOByIllnessName(name);
    }

    @Override
    public List<FsIllnessLibraryListDVO> selectFsIllnessLibraryListDVO(FsIllnessLibraryListDParam param) {
        return fsIllnessLibraryMapper.selectFsIllnessLibraryListDVO(param);
    }

    @SuppressWarnings("all")
    public String importFsIllnessLibrary(List<FsIllnessLibrary> list) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (FsIllnessLibrary vo : list)
        {
            try
            {
                vo.setFormulaName(vo.getFormulaName().substring(2, vo.getFormulaName().length() - 2));
                vo.setFormulaDescs(vo.getFormulaDescs().substring(2, vo.getFormulaDescs().length() - 2));
                this.insertFsIllnessLibrary(vo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、商品 " + vo.getIllnessName() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、商品 " + vo.getIllnessName()  + " 导入失败：";
                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();
    }
}
