package com.fs.his.service.impl;

import java.util.List;

import com.fs.common.BeanCopyUtils;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsMedicatedFood;
import com.fs.his.param.FsFamousPrescribeListUParam;
import com.fs.his.vo.FsFamousPrescribeExecelVO;
import com.fs.his.vo.FsFamousPrescribeListUVO;
import com.fs.his.vo.FsFamousPrescribeListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsFamousPrescribeMapper;
import com.fs.his.domain.FsFamousPrescribe;
import com.fs.his.service.IFsFamousPrescribeService;

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

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

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

    /**
     * 新增经典名方
     *
     * @param fsFamousPrescribe 经典名方
     * @return 结果
     */
    @Override
    @CacheEvict(value = "getFamousPrescribeList",allEntries = true)
    public int insertFsFamousPrescribe(FsFamousPrescribe fsFamousPrescribe)
    {
        fsFamousPrescribe.setCreateTime(DateUtils.getNowDate());
        return fsFamousPrescribeMapper.insertFsFamousPrescribe(fsFamousPrescribe);
    }

    /**
     * 修改经典名方
     *
     * @param fsFamousPrescribe 经典名方
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "getFamousPrescribeList", allEntries = true),
            @CacheEvict(value = "getFamousPrescribeById", key = "#fsFamousPrescribe.id")
    })
    public int updateFsFamousPrescribe(FsFamousPrescribe fsFamousPrescribe)
    {
        return fsFamousPrescribeMapper.updateFsFamousPrescribe(fsFamousPrescribe);
    }

    /**
     * 批量删除经典名方
     *
     * @param ids 需要删除的经典名方主键
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "getFamousPrescribeList", allEntries = true),
            @CacheEvict(value = "getFamousPrescribeById",
                    key = "#ids")
    })
    public int deleteFsFamousPrescribeByIds(Long[] ids)
    {
        return fsFamousPrescribeMapper.deleteFsFamousPrescribeByIds(ids);
    }

    /**
     * 删除经典名方信息
     *
     * @param id 经典名方主键
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "getFamousPrescribeList", allEntries = true),
            @CacheEvict(value = "getFamousPrescribeById",
                    key = "#id")
    })
    public int deleteFsFamousPrescribeById(Long id)
    {
        return fsFamousPrescribeMapper.deleteFsFamousPrescribeById(id);
    }

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

    @Override
    @CacheEvict(value = "getFamousPrescribeList",allEntries = true)
    public String importData(List<FsFamousPrescribeExecelVO> 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 (FsFamousPrescribeExecelVO vo : list)
        {
            try
            {

                FsFamousPrescribe copy = BeanCopyUtils.copy(vo, FsFamousPrescribe.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 ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

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