package com.fs.store.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsIntegralGoods;
import com.fs.store.mapper.FsIntegralGoodsMapper;
import com.fs.store.param.FsIntegralGoodsListUParam;
import com.fs.store.service.IFsIntegralGoodsService;
import com.fs.store.vo.FsIntegralGoodsListUVO;
import com.fs.store.vo.FsIntegralGoodsListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 积分商品Service业务层处理
 *
 * @author fs
 * @date 2023-11-02
 */
@Service
public class FsIntegralGoodsServiceImpl implements IFsIntegralGoodsService
{
    @Autowired
    private FsIntegralGoodsMapper fsIntegralGoodsMapper;

    /**
     * 查询积分商品
     *
     * @param goodsId 积分商品主键
     * @return 积分商品
     */
    @Override
    public FsIntegralGoods selectFsIntegralGoodsByGoodsId(Long goodsId)
    {
        return fsIntegralGoodsMapper.selectFsIntegralGoodsByGoodsId(goodsId);
    }

    /**
     * 查询积分商品列表
     *
     * @param fsIntegralGoods 积分商品
     * @return 积分商品
     */
    @Override
    public List<FsIntegralGoods> selectFsIntegralGoodsList(FsIntegralGoods fsIntegralGoods)
    {
        return fsIntegralGoodsMapper.selectFsIntegralGoodsList(fsIntegralGoods);
    }

    /**
     * 新增积分商品
     *
     * @param fsIntegralGoods 积分商品
     * @return 结果
     */
    @Override
    public int insertFsIntegralGoods(FsIntegralGoods fsIntegralGoods)
    {
        fsIntegralGoods.setCreateTime(DateUtils.getNowDate());
        return fsIntegralGoodsMapper.insertFsIntegralGoods(fsIntegralGoods);
    }

    /**
     * 修改积分商品
     *
     * @param fsIntegralGoods 积分商品
     * @return 结果
     */
    @Override
    public int updateFsIntegralGoods(FsIntegralGoods fsIntegralGoods)
    {
        return fsIntegralGoodsMapper.updateFsIntegralGoods(fsIntegralGoods);
    }

    /**
     * 批量删除积分商品
     *
     * @param goodsIds 需要删除的积分商品主键
     * @return 结果
     */
    @Override
    public int deleteFsIntegralGoodsByGoodsIds(Long[] goodsIds)
    {
        return fsIntegralGoodsMapper.deleteFsIntegralGoodsByGoodsIds(goodsIds);
    }

    /**
     * 删除积分商品信息
     *
     * @param goodsId 积分商品主键
     * @return 结果
     */
    @Override
    public int deleteFsIntegralGoodsByGoodsId(Long goodsId)
    {
        return fsIntegralGoodsMapper.deleteFsIntegralGoodsByGoodsId(goodsId);
    }

    @Override
    public List<FsIntegralGoodsListVO> selectFsIntegralGoodsListVO(FsIntegralGoods fsIntegralGoods) {
        return fsIntegralGoodsMapper.selectFsIntegralGoodsListVO(fsIntegralGoods);
    }

    @Override
    public List<FsIntegralGoodsListUVO> selectFsIntegralGoodsListUVO(FsIntegralGoodsListUParam param) {
        return fsIntegralGoodsMapper.selectFsIntegralGoodsListUVO(param);
    }

    @Override
    public String importIntegralGoodsService(List<FsIntegralGoods> 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 (FsIntegralGoods vo : list)
        {
            try
            {
                vo.setCreateTime(DateUtils.getNowDate());
                int i = fsIntegralGoodsMapper.insertFsIntegralGoods(vo);

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

            }
            catch (Exception e)
            {

                failureNum++;
                String msg = "<br/>" + failureNum + "、名称 " +vo.getGoodsName() + " 导入失败：";
                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();
    }
}
