package com.fs.hisStore.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsMedicatedFoodScrm;
import com.fs.hisStore.mapper.FsMedicatedFoodScrmMapper;
import com.fs.hisStore.param.FsMedicateFoodListUParam;
import com.fs.hisStore.service.IFsMedicatedFoodScrmService;
import com.fs.hisStore.vo.FsMedicateFoodListUVO;
import com.fs.hisStore.vo.FsMedicatedFoodLListVO;
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 FsMedicatedFoodScrmServiceImpl implements IFsMedicatedFoodScrmService
{
    @Autowired
    private FsMedicatedFoodScrmMapper fsMedicatedFoodMapper;


    /**
     * 查询药膳食疗
     *
     * @param id 药膳食疗主键
     * @return 药膳食疗
     */
    @Override
    public FsMedicatedFoodScrm selectFsMedicatedFoodById(Long id)
    {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodById(id);
    }

    /**
     * 查询药膳食疗列表
     *
     * @param fsMedicatedFood 药膳食疗
     * @return 药膳食疗
     */
    @Override
    public List<FsMedicatedFoodScrm> selectFsMedicatedFoodList(FsMedicatedFoodScrm fsMedicatedFood)
    {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodList(fsMedicatedFood);
    }

    /**
     * 新增药膳食疗
     *
     * @param fsMedicatedFood 药膳食疗
     * @return 结果
     */
    @Override
    public int insertFsMedicatedFood(FsMedicatedFoodScrm fsMedicatedFood)
    {
        fsMedicatedFood.setCreateTime(DateUtils.getNowDate());
        return fsMedicatedFoodMapper.insertFsMedicatedFood(fsMedicatedFood);
    }

    /**
     * 修改药膳食疗
     *
     * @param fsMedicatedFood 药膳食疗
     * @return 结果
     */
    @Override
    public int updateFsMedicatedFood(FsMedicatedFoodScrm fsMedicatedFood)
    {
        return fsMedicatedFoodMapper.updateFsMedicatedFood(fsMedicatedFood);
    }

    /**
     * 批量删除药膳食疗
     *
     * @param ids 需要删除的药膳食疗主键
     * @return 结果
     */
    @Override
    public int deleteFsMedicatedFoodByIds(Long[] ids)
    {
        return fsMedicatedFoodMapper.deleteFsMedicatedFoodByIds(ids);
    }

    /**
     * 删除药膳食疗信息
     *
     * @param id 药膳食疗主键
     * @return 结果
     */
    @Override
    public int deleteFsMedicatedFoodById(Long id)
    {
        return fsMedicatedFoodMapper.deleteFsMedicatedFoodById(id);
    }

    @Override
    public List<FsMedicatedFoodLListVO> selectFsMedicatedFoodListVO(FsMedicatedFoodScrm fsMedicatedFood) {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodListVO(fsMedicatedFood);
    }
    @SuppressWarnings("all")
    @Override
    public String importDataMedicatedFoodExcel(List<FsMedicatedFoodScrm> list, boolean updateSupport) {
        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 (FsMedicatedFoodScrm vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsMedicatedFoodMapper.insertFsMedicatedFood(vo);

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

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " + vo.getFoodName()  + " 导入失败：";
                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<FsMedicateFoodListUVO> selectFsMedicatedFoodListUVO(FsMedicateFoodListUParam param) {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodListUVO(param);
    }
}
