package com.fs.hisStore.service.impl;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.hisStore.domain.FsIntegralGoodsScrm;
import com.fs.hisStore.mapper.FsIntegralGoodsScrmMapper;
import com.fs.hisStore.param.FsIntegralGoodsListUParam;
import com.fs.hisStore.service.IFsIntegralGoodsScrmService;
import com.fs.hisStore.vo.FsIntegralGoodsListUVO;
import com.fs.hisStore.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 FsIntegralGoodsScrmServiceImpl implements IFsIntegralGoodsScrmService
{
    @Autowired
    private FsIntegralGoodsScrmMapper fsIntegralGoodsMapper;

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

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

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

    /**
     * 修改积分商品
     *
     * @param fsIntegralGoods 积分商品
     * @return 结果
     */
    @Override
    public int updateFsIntegralGoods(FsIntegralGoodsScrm 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(FsIntegralGoodsScrm fsIntegralGoods) {
        return fsIntegralGoodsMapper.selectFsIntegralGoodsListVO(fsIntegralGoods);
    }

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

    @Override
    public String importIntegralGoodsService(List<FsIntegralGoodsScrm> 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 (FsIntegralGoodsScrm 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();
    }
}
