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.GoodsBindReserveNameReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsBindReserveNameWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsBindReserveNameExample;
import com.slodon.b2b2c.goods.pojo.GoodsBindReserveName;
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 GoodsBindReserveNameModel {

    @Resource
    private GoodsBindReserveNameReadMapper goodsBindReserveNameReadMapper;
    @Resource
    private GoodsBindReserveNameWriteMapper goodsBindReserveNameWriteMapper;

    /**
     * 新增虚拟商品用户预留信息名称表
     *
     * @param goodsBindReserveName
     * @return
     */
    public Integer saveGoodsBindReserveName(GoodsBindReserveName goodsBindReserveName) {
        int count = goodsBindReserveNameWriteMapper.insert(goodsBindReserveName);
        if (count == 0) {
            throw new MallException("添加虚拟商品用户预留信息名称表失败，请重试");
        }
        return count;
    }

    /**
     * 根据reserveNameId删除虚拟商品用户预留信息名称表
     *
     * @param reserveNameId reserveNameId
     * @return
     */
    public Integer deleteGoodsBindReserveName(Integer reserveNameId) {
        if (StringUtils.isEmpty(reserveNameId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsBindReserveNameWriteMapper.deleteByPrimaryKey(reserveNameId);
        if (count == 0) {
            log.error("根据reserveNameId：" + reserveNameId + "删除虚拟商品用户预留信息名称表失败");
            throw new MallException("删除虚拟商品用户预留信息名称表失败,请重试");
        }
        return count;
    }

    /**
     * 根据查询条件删除虚拟商品用户预留信息名称
     *
     * @param example 查询条件信息
     * @return
     */
    public Integer deleteGoodsBindReserveNameByExample(GoodsBindReserveNameExample example) {
        return goodsBindReserveNameWriteMapper.deleteByExample(example);
    }

    /**
     * 根据reserveNameId更新虚拟商品用户预留信息名称表
     *
     * @param goodsBindReserveName
     * @return
     */
    public Integer updateGoodsBindReserveName(GoodsBindReserveName goodsBindReserveName) {
        if (StringUtils.isEmpty(goodsBindReserveName.getReserveNameId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsBindReserveNameWriteMapper.updateByPrimaryKeySelective(goodsBindReserveName);
        if (count == 0) {
            log.error("根据reserveNameId：" + goodsBindReserveName.getReserveNameId() + "更新虚拟商品用户预留信息名称表失败");
            throw new MallException("更新虚拟商品用户预留信息名称表失败,请重试");
        }
        return count;
    }

    /**
     * 根据reserveNameId获取虚拟商品用户预留信息名称表详情
     *
     * @param reserveNameId reserveNameId
     * @return
     */
    public GoodsBindReserveName getGoodsBindReserveNameByReserveNameId(Integer reserveNameId) {
        return goodsBindReserveNameReadMapper.getByPrimaryKey(reserveNameId);
    }

    /**
     * 根据条件获取虚拟商品用户预留信息名称表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsBindReserveName> getGoodsBindReserveNameList(GoodsBindReserveNameExample example, PagerInfo pager) {
        List<GoodsBindReserveName> goodsBindReserveNameList;
        if (pager != null) {
            pager.setRowsCount(goodsBindReserveNameReadMapper.countByExample(example));
            goodsBindReserveNameList = goodsBindReserveNameReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsBindReserveNameList = goodsBindReserveNameReadMapper.listByExample(example);
        }
        return goodsBindReserveNameList;
    }
}