package com.ruoyi.mall.service.impl;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mall.domain.VO.SeckillGoodsVO;
import org.apache.commons.collections4.MapUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.MallSeckillGoodsMapper;
import com.ruoyi.mall.domain.MallSeckillGoods;
import com.ruoyi.mall.service.IMallSeckillGoodsService;

import javax.annotation.Resource;

/**
 * 秒杀商品Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-01
 */
@Service
public class MallSeckillGoodsServiceImpl implements IMallSeckillGoodsService
{
    @Resource
    private MallSeckillGoodsMapper mallSeckillGoodsMapper;

    /**
     * 商品详情，秒杀信息
     * @return
     */
    public SeckillGoodsVO seckillGoodsInfo(Map<String,Object> param){
        return mallSeckillGoodsMapper.seckillGoodsInfo(param);
    }

    /**
     * 秒杀可选商品列表
     *
     * @return 秒杀商品
     */
    @Override
    public List<Map<String,Object>> enableSeckillGoodsList(){
        return mallSeckillGoodsMapper.enableSeckillGoodsList();
    }

    /**
     * 秒杀商品列表
     *
     * @return 秒杀商品
     */
    @Override
    public ApiResult getSeckillGoodsList(Map<String,Object> param){
        Integer seckillTimeId = MapUtils.getInteger(param, "seckillTimeId");
        PageHelper.startPage(param);
        return ApiResult.ok().put("data",new PageInfo<>(mallSeckillGoodsMapper.getSeckillGoodsList(seckillTimeId)));
    }

    /**
     * 查询秒杀商品
     *
     * @param id 秒杀商品主键
     * @return 秒杀商品
     */
    @Override
    public MallSeckillGoods selectMallSeckillGoodsById(Integer id)
    {
        return mallSeckillGoodsMapper.selectMallSeckillGoodsById(id);
    }

    /**
     * 查询秒杀商品列表
     *
     * @param mallSeckillGoods 秒杀商品
     * @return 秒杀商品
     */
    @Override
    public List<MallSeckillGoods> selectMallSeckillGoodsList(MallSeckillGoods mallSeckillGoods)
    {
        return mallSeckillGoodsMapper.selectMallSeckillGoodsList(mallSeckillGoods);
    }

    /**
     * 新增秒杀商品
     *
     * @param mallSeckillGoods 秒杀商品
     * @return 结果
     */
    @Override
    public AjaxResult insertMallSeckillGoods(MallSeckillGoods mallSeckillGoods)
    {
        //是否已添加过此商品
        if (mallSeckillGoodsMapper.selectMallSeckillGoodsByGoodsId(mallSeckillGoods.getGoodsId()) > 0){
            return AjaxResult.error("此商品已添加");
        }
        mallSeckillGoods.setCreateAt(DateUtils.getTime());
        mallSeckillGoods.setDelFlag(0);
        return AjaxResult.toAjax(mallSeckillGoodsMapper.insertMallSeckillGoods(mallSeckillGoods));
    }

    /**
     * 修改秒杀商品
     *
     * @param mallSeckillGoods 秒杀商品
     * @return 结果
     */
    @Override
    public int updateMallSeckillGoods(MallSeckillGoods mallSeckillGoods)
    {
//        mallSeckillGoods.setUpdateBy(DateUtils.getTime());
        return mallSeckillGoodsMapper.updateMallSeckillGoods(mallSeckillGoods);
    }

    /**
     * 秒杀信息修改
     * @param goodsId 商品id
     * @param type 类型（0-减少库存，1-增加销量）
     * @return
     */
    @Override
    public int editSeckillGoodsInfo(@Param("goodsId") Long goodsId,@Param("type") Integer type,@Param("count") Integer count){
        return mallSeckillGoodsMapper.editSeckillGoodsInfo(goodsId,type,count);
    }

    /**
     * 批量删除秒杀商品
     *
     * @param ids 需要删除的秒杀商品主键
     * @return 结果
     */
    @Override
    public int deleteMallSeckillGoodsByIds(Integer[] ids)
    {
        return mallSeckillGoodsMapper.deleteMallSeckillGoodsByIds(ids);
    }

    /**
     * 删除秒杀商品信息
     *
     * @param id 秒杀商品主键
     * @return 结果
     */
    @Override
    public int deleteMallSeckillGoodsById(Integer id)
    {
        return mallSeckillGoodsMapper.deleteMallSeckillGoodsById(id);
    }
}
