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.GoodsBindRankReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsBindRankWriteMapper;
import com.slodon.b2b2c.goods.dto.GoodsRankListDTO;
import com.slodon.b2b2c.goods.example.GoodsBindRankExample;
import com.slodon.b2b2c.goods.pojo.GoodsBindRank;
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 GoodsBindRankModel {

    @Resource
    private GoodsBindRankReadMapper goodsBindRankReadMapper;
    @Resource
    private GoodsBindRankWriteMapper goodsBindRankWriteMapper;

    /**
     * 新增榜单与商品绑定关系表
     *
     * @param goodsBindRank
     * @return
     */
    public Integer saveGoodsBindRank(GoodsBindRank goodsBindRank) {
        int count = goodsBindRankWriteMapper.insert(goodsBindRank);
        if (count == 0) {
            throw new MallException("添加榜单与商品绑定关系表失败，请重试");
        }
        return count;
    }

    /**
     * 根据bindId删除榜单与商品绑定关系表
     *
     * @param bindId bindId
     * @return
     */
    public Integer deleteGoodsBindRank(Integer bindId) {
        if (StringUtils.isEmpty(bindId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsBindRankWriteMapper.deleteByPrimaryKey(bindId);
        if (count == 0) {
            log.error("根据bindId：" + bindId + "删除榜单与商品绑定关系表失败");
            throw new MallException("删除榜单与商品绑定关系表失败,请重试");
        }
        return count;
    }

    /**
     * 根据bindId更新榜单与商品绑定关系表
     *
     * @param goodsBindRank
     * @return
     */
    public Integer updateGoodsBindRank(GoodsBindRank goodsBindRank) {
        if (StringUtils.isEmpty(goodsBindRank.getBindId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsBindRankWriteMapper.updateByPrimaryKeySelective(goodsBindRank);
        if (count == 0) {
            log.error("根据bindId：" + goodsBindRank.getBindId() + "更新榜单与商品绑定关系表失败");
            throw new MallException("更新榜单与商品绑定关系表失败,请重试");
        }
        return count;
    }

    /**
     * 根据bindId获取榜单与商品绑定关系表详情
     *
     * @param bindId bindId
     * @return
     */
    public GoodsBindRank getGoodsBindRankByBindId(Integer bindId) {
        return goodsBindRankReadMapper.getByPrimaryKey(bindId);
    }

    /**
     * 根据条件获取榜单与商品绑定关系表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsBindRank> getGoodsBindRankList(GoodsBindRankExample example, PagerInfo pager) {
        List<GoodsBindRank> goodsBindRankList;
        if (pager != null) {
            pager.setRowsCount(goodsBindRankReadMapper.countByExample(example));
            goodsBindRankList = goodsBindRankReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsBindRankList = goodsBindRankReadMapper.listByExample(example);
        }
        return goodsBindRankList;
    }

    /**
     * 根据条件获取榜单商品排名列表
     *
     * @param example 查询条件信息
     * @return
     */
    public List<GoodsRankListDTO> getGoodsRankList(GoodsBindRankExample example) {
        return goodsBindRankReadMapper.getGoodsRankList(example);
    }
}