package com.fs.his.service.impl;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.fs.common.exception.CustomException;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.DateUtils;
import com.fs.his.domain.FsStoreOrder;
import com.fs.his.param.FsMedicateFoodListUParam;
import com.fs.his.vo.*;
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.FsMedicatedFoodMapper;
import com.fs.his.domain.FsMedicatedFood;
import com.fs.his.service.IFsMedicatedFoodService;

/**
 * 药膳食疗Service业务层处理
 *
 * @author fs
 * @date 2023-10-23
 */
@Service
public class FsMedicatedFoodServiceImpl implements IFsMedicatedFoodService
{
    @Autowired
    private FsMedicatedFoodMapper fsMedicatedFoodMapper;


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

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

    /**
     * 新增药膳食疗
     *
     * @param fsMedicatedFood 药膳食疗
     * @return 结果
     */
    @Override
    @CacheEvict(value = "getMedicatedFoodList",allEntries = true)
    public int insertFsMedicatedFood(FsMedicatedFood fsMedicatedFood)
    {
        fsMedicatedFood.setCreateTime(DateUtils.getNowDate());
        return fsMedicatedFoodMapper.insertFsMedicatedFood(fsMedicatedFood);
    }

    /**
     * 修改药膳食疗
     *
     * @param fsMedicatedFood 药膳食疗
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "getMedicatedFoodList", allEntries = true),
            @CacheEvict(value = "getMedicatedFoodById", key = "#fsMedicatedFood.id")
    })
    public int updateFsMedicatedFood(FsMedicatedFood fsMedicatedFood)
    {
        return fsMedicatedFoodMapper.updateFsMedicatedFood(fsMedicatedFood);
    }

    /**
     * 批量删除药膳食疗
     *
     * @param ids 需要删除的药膳食疗主键
     * @return 结果
     */
    @Caching(evict = {
            @CacheEvict(value = "getMedicatedFoodList", allEntries = true),
            @CacheEvict(value = "getMedicatedFoodById",
                    key = "#ids")
    })
    @Override
    public int deleteFsMedicatedFoodByIds(Long[] ids)
    {
        return fsMedicatedFoodMapper.deleteFsMedicatedFoodByIds(ids);
    }

    /**
     * 删除药膳食疗信息
     *
     * @param id 药膳食疗主键
     * @return 结果
     */
    @Override
    @Caching(evict = {
            @CacheEvict(value = "getMedicatedFoodList", allEntries = true),
            @CacheEvict(value = "getMedicatedFoodById",
                    key = "id")
    })
    public int deleteFsMedicatedFoodById(Long id)
    {
        return fsMedicatedFoodMapper.deleteFsMedicatedFoodById(id);
    }

    @Override
    public List<FsMedicatedFoodLListVO> selectFsMedicatedFoodListVO(FsMedicatedFood fsMedicatedFood) {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodListVO(fsMedicatedFood);
    }
    @SuppressWarnings("all")
    @Override
    @CacheEvict(value = "getChineseMedicineList",allEntries = true)
    public String importDataMedicatedFoodExcel(List<FsMedicatedFood> list, boolean updateSupport) {
        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 (FsMedicatedFood 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 ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<FsMedicateFoodListUVO> selectFsMedicatedFoodListUVO(FsMedicateFoodListUParam param) {
        return fsMedicatedFoodMapper.selectFsMedicatedFoodListUVO(param);
    }
}
