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 com.fs.his.param.FsQuestionsListUParam;
import com.fs.his.vo.FsQuestionsListUVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsQuestionsMapper;
import com.fs.his.domain.FsQuestions;
import com.fs.his.service.IFsQuestionsService;

/**
 * 问答专区Service业务层处理
 *
 * @author fs
 * @date 2023-10-23
 */
@Service
public class FsQuestionsServiceImpl implements IFsQuestionsService
{
    @Autowired
    private FsQuestionsMapper fsQuestionsMapper;

    /**
     * 查询问答专区
     *
     * @param id 问答专区主键
     * @return 问答专区
     */
    @Override
    public FsQuestions selectFsQuestionsById(Long id)
    {
        return fsQuestionsMapper.selectFsQuestionsById(id);
    }

    /**
     * 查询问答专区列表
     *
     * @param fsQuestions 问答专区
     * @return 问答专区
     */
    @Override
    public List<FsQuestions> selectFsQuestionsList(FsQuestions fsQuestions)
    {
        return fsQuestionsMapper.selectFsQuestionsList(fsQuestions);
    }

    /**
     * 新增问答专区
     *
     * @param fsQuestions 问答专区
     * @return 结果
     */
    @Override
    public int insertFsQuestions(FsQuestions fsQuestions)
    {
        fsQuestions.setCreateTime(DateUtils.getNowDate());
        return fsQuestionsMapper.insertFsQuestions(fsQuestions);
    }

    /**
     * 修改问答专区
     *
     * @param fsQuestions 问答专区
     * @return 结果
     */
    @Override
    public int updateFsQuestions(FsQuestions fsQuestions)
    {
        return fsQuestionsMapper.updateFsQuestions(fsQuestions);
    }

    /**
     * 批量删除问答专区
     *
     * @param ids 需要删除的问答专区主键
     * @return 结果
     */
    @Override
    public int deleteFsQuestionsByIds(Long[] ids)
    {
        return fsQuestionsMapper.deleteFsQuestionsByIds(ids);
    }

    /**
     * 删除问答专区信息
     *
     * @param id 问答专区主键
     * @return 结果
     */
    @Override
    public int deleteFsQuestionsById(Long id)
    {
        return fsQuestionsMapper.deleteFsQuestionsById(id);
    }

    @Override
    public String importData(List<FsQuestions> 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 (FsQuestions vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsQuestionsMapper.insertFsQuestions(vo);

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

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + vo.getTitle()  + " 导入失败：";
                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<FsQuestionsListUVO> selectFsQuestionsListUVO(FsQuestionsListUParam param) {
        return fsQuestionsMapper.selectFsQuestionsListUVO(param);
    }
}
