package com.fs.hisStore.service.impl;

import com.fs.common.BeanCopyUtils;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsFamousPrescribeScrm;
import com.fs.hisStore.mapper.FsFamousPrescribeScrmMapper;
import com.fs.hisStore.param.FsFamousPrescribeListUParam;
import com.fs.hisStore.service.IFsFamousPrescribeScrmService;
import com.fs.hisStore.vo.FsFamousPrescribeExecelVO;
import com.fs.hisStore.vo.FsFamousPrescribeListUVO;
import com.fs.hisStore.vo.FsFamousPrescribeListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 经典名方Service业务层处理
 *
 * @author fs
 * @date 2023-10-23
 */
@Service
public class FsFamousPrescribeScrmServiceImpl implements IFsFamousPrescribeScrmService
{
    @Autowired
    private FsFamousPrescribeScrmMapper fsFamousPrescribeMapper;

    /**
     * 查询经典名方
     *
     * @param id 经典名方主键
     * @return 经典名方
     */
    @Override
    public FsFamousPrescribeScrm selectFsFamousPrescribeById(Long id)
    {
        return fsFamousPrescribeMapper.selectFsFamousPrescribeById(id);
    }

    /**
     * 查询经典名方列表
     *
     * @param fsFamousPrescribe 经典名方
     * @return 经典名方
     */
    @Override
    public List<FsFamousPrescribeScrm> selectFsFamousPrescribeList(FsFamousPrescribeScrm fsFamousPrescribe)
    {
        return fsFamousPrescribeMapper.selectFsFamousPrescribeList(fsFamousPrescribe);
    }

    /**
     * 新增经典名方
     *
     * @param fsFamousPrescribe 经典名方
     * @return 结果
     */
    @Override
    public int insertFsFamousPrescribe(FsFamousPrescribeScrm fsFamousPrescribe)
    {
        fsFamousPrescribe.setCreateTime(DateUtils.getNowDate());
        return fsFamousPrescribeMapper.insertFsFamousPrescribe(fsFamousPrescribe);
    }

    /**
     * 修改经典名方
     *
     * @param fsFamousPrescribe 经典名方
     * @return 结果
     */
    @Override
    public int updateFsFamousPrescribe(FsFamousPrescribeScrm fsFamousPrescribe)
    {
        return fsFamousPrescribeMapper.updateFsFamousPrescribe(fsFamousPrescribe);
    }

    /**
     * 批量删除经典名方
     *
     * @param ids 需要删除的经典名方主键
     * @return 结果
     */
    @Override
    public int deleteFsFamousPrescribeByIds(Long[] ids)
    {
        return fsFamousPrescribeMapper.deleteFsFamousPrescribeByIds(ids);
    }

    /**
     * 删除经典名方信息
     *
     * @param id 经典名方主键
     * @return 结果
     */
    @Override
    public int deleteFsFamousPrescribeById(Long id)
    {
        return fsFamousPrescribeMapper.deleteFsFamousPrescribeById(id);
    }

    @Override
    public List<FsFamousPrescribeListVO> selectFsFamousPrescribeListVO(FsFamousPrescribeScrm fsFamousPrescribe) {
        return fsFamousPrescribeMapper.selectFsFamousPrescribeListVO(fsFamousPrescribe);
    }

    @Override
    public String importData(List<FsFamousPrescribeExecelVO> list) {
        if (com.fs.common.utils.StringUtils.isNull(list) || list.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (FsFamousPrescribeExecelVO vo : list)
        {
            try
            {
                System.out.println(vo);
                FsFamousPrescribeScrm copy = BeanCopyUtils.copy(vo, FsFamousPrescribeScrm.class);
                copy.setCreateTime(DateUtils.getNowDate());
                int i = fsFamousPrescribeMapper.insertFsFamousPrescribe(copy);

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

            }
            catch (Exception e)
            {

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

    @Override
    public List<FsFamousPrescribeListUVO> selectFsFamousPrescribeListUVO(FsFamousPrescribeListUParam param) {
        return fsFamousPrescribeMapper.selectFsFamousPrescribeListUVO(param);
    }
}
