package com.slodon.b2b2c.model.integral;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.integral.IntegralGoodsBindReserveNameReadMapper;
import com.slodon.b2b2c.dao.write.integral.IntegralGoodsBindReserveNameWriteMapper;
import com.slodon.b2b2c.integral.example.IntegralGoodsBindReserveNameExample;
import com.slodon.b2b2c.integral.pojo.IntegralGoodsBindReserveName;
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 IntegralGoodsBindReserveNameModel {

    @Resource
    private IntegralGoodsBindReserveNameReadMapper integralGoodsBindReserveNameReadMapper;
    @Resource
    private IntegralGoodsBindReserveNameWriteMapper integralGoodsBindReserveNameWriteMapper;

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

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

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

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

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