package com.slodon.b2b2c.model.goods;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.goods.GoodsRankCategoryReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsRankCategoryWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsRankCategoryExample;
import com.slodon.b2b2c.goods.pojo.GoodsRankCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 榜单分类model
 */
@Component
@Slf4j
public class GoodsRankCategoryModel {

    @Resource
    private GoodsRankCategoryReadMapper goodsRankCategoryReadMapper;
    @Resource
    private GoodsRankCategoryWriteMapper goodsRankCategoryWriteMapper;

    /**
     * 新增榜单分类
     *
     * @param goodsRankCategory
     * @return
     */
    public Integer saveGoodsRankCategory(GoodsRankCategory goodsRankCategory) {
        int count = goodsRankCategoryWriteMapper.insert(goodsRankCategory);
        if (count == 0) {
            throw new MallException("添加榜单分类失败，请重试");
        }
        return count;
    }

    /**
     * 根据categoryId删除榜单分类
     *
     * @param categoryId categoryId
     * @return
     */
    public Integer deleteGoodsRankCategory(Integer categoryId) {
        if (StringUtils.isEmpty(categoryId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsRankCategoryWriteMapper.deleteByPrimaryKey(categoryId);
        if (count == 0) {
            log.error("根据categoryId：" + categoryId + "删除榜单分类失败");
            throw new MallException("删除榜单分类失败,请重试");
        }
        return count;
    }

    /**
     * 根据categoryId更新榜单分类
     *
     * @param goodsRankCategory
     * @return
     */
    public Integer updateGoodsRankCategory(GoodsRankCategory goodsRankCategory) {
        if (StringUtils.isEmpty(goodsRankCategory.getCategoryId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsRankCategoryWriteMapper.updateByPrimaryKeySelective(goodsRankCategory);
        if (count == 0) {
            log.error("根据categoryId：" + goodsRankCategory.getCategoryId() + "更新榜单分类失败");
            throw new MallException("更新榜单分类失败,请重试");
        }
        return count;
    }

    /**
     * 根据categoryId获取榜单分类详情
     *
     * @param categoryId categoryId
     * @return
     */
    public GoodsRankCategory getGoodsRankCategoryByCategoryId(Integer categoryId) {
        return goodsRankCategoryReadMapper.getByPrimaryKey(categoryId);
    }

    /**
     * 根据条件获取榜单分类列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsRankCategory> getGoodsRankCategoryList(GoodsRankCategoryExample example, PagerInfo pager) {
        List<GoodsRankCategory> goodsRankCategoryList;
        if (pager != null) {
            pager.setRowsCount(goodsRankCategoryReadMapper.countByExample(example));
            goodsRankCategoryList = goodsRankCategoryReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsRankCategoryList = goodsRankCategoryReadMapper.listByExample(example);
        }
        return goodsRankCategoryList;
    }
}