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.GoodsPlatformBindReserveNameReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsPlatformBindReserveNameWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsPlatformBindReserveNameExample;
import com.slodon.b2b2c.goods.pojo.GoodsPlatformBindReserveName;
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 GoodsPlatformBindReserveNameModel {

    @Resource
    private GoodsPlatformBindReserveNameReadMapper goodsPlatformBindReserveNameReadMapper;
    @Resource
    private GoodsPlatformBindReserveNameWriteMapper goodsPlatformBindReserveNameWriteMapper;

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

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

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

    /**
     * 根据reserveNameId获取平台库虚拟商品用户预留信息名称表详情
     *
     * @param reserveNameId reserveNameId
     * @return
     */
    public GoodsPlatformBindReserveName getGoodsPlatformBindReserveNameByReserveNameId(Integer reserveNameId) {
        return goodsPlatformBindReserveNameReadMapper.getByPrimaryKey(reserveNameId);
    }

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