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.GoodsPlatformBindAttributeValueReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsPlatformBindAttributeValueWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsPlatformBindAttributeValueExample;
import com.slodon.b2b2c.goods.pojo.GoodsPlatformBindAttributeValue;
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 GoodsPlatformBindAttributeValueModel {

    @Resource
    private GoodsPlatformBindAttributeValueReadMapper goodsPlatformBindAttributeValueReadMapper;
    @Resource
    private GoodsPlatformBindAttributeValueWriteMapper goodsPlatformBindAttributeValueWriteMapper;

    /**
     * 新增平台库商品属性表
     *
     * @param goodsPlatformBindAttributeValue
     * @return
     */
    public Integer saveGoodsPlatformBindAttributeValue(GoodsPlatformBindAttributeValue goodsPlatformBindAttributeValue) {
        int count = goodsPlatformBindAttributeValueWriteMapper.insert(goodsPlatformBindAttributeValue);
        if (count == 0) {
            throw new MallException("添加平台库商品属性表失败，请重试");
        }
        return count;
    }

    /**
     * 根据bindId删除平台库商品属性表
     *
     * @param bindId bindId
     * @return
     */
    public Integer deleteGoodsPlatformBindAttributeValue(Integer bindId) {
        if (StringUtils.isEmpty(bindId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = goodsPlatformBindAttributeValueWriteMapper.deleteByPrimaryKey(bindId);
        if (count == 0) {
            log.error("根据bindId：" + bindId + "删除平台库商品属性表失败");
            throw new MallException("删除平台库商品属性表失败,请重试");
        }
        return count;
    }

    /**
     * 根据bindId更新平台库商品属性表
     *
     * @param goodsPlatformBindAttributeValue
     * @return
     */
    public Integer updateGoodsPlatformBindAttributeValue(GoodsPlatformBindAttributeValue goodsPlatformBindAttributeValue) {
        if (StringUtils.isEmpty(goodsPlatformBindAttributeValue.getBindId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsPlatformBindAttributeValueWriteMapper.updateByPrimaryKeySelective(goodsPlatformBindAttributeValue);
        if (count == 0) {
            log.error("根据bindId：" + goodsPlatformBindAttributeValue.getBindId() + "更新平台库商品属性表失败");
            throw new MallException("更新平台库商品属性表失败,请重试");
        }
        return count;
    }

    /**
     * 根据bindId获取平台库商品属性表详情
     *
     * @param bindId bindId
     * @return
     */
    public GoodsPlatformBindAttributeValue getGoodsPlatformBindAttributeValueByBindId(Integer bindId) {
        return goodsPlatformBindAttributeValueReadMapper.getByPrimaryKey(bindId);
    }

    /**
     * 根据条件获取平台库商品属性表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsPlatformBindAttributeValue> getGoodsPlatformBindAttributeValueList(GoodsPlatformBindAttributeValueExample example, PagerInfo pager) {
        List<GoodsPlatformBindAttributeValue> goodsPlatformBindAttributeValueList;
        if (pager != null) {
            pager.setRowsCount(goodsPlatformBindAttributeValueReadMapper.countByExample(example));
            goodsPlatformBindAttributeValueList = goodsPlatformBindAttributeValueReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsPlatformBindAttributeValueList = goodsPlatformBindAttributeValueReadMapper.listByExample(example);
        }
        return goodsPlatformBindAttributeValueList;
    }
}