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.ProductPlatformReadMapper;
import com.slodon.b2b2c.dao.write.goods.ProductPlatformWriteMapper;
import com.slodon.b2b2c.goods.example.ProductPlatformExample;
import com.slodon.b2b2c.goods.pojo.ProductPlatform;
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 ProductPlatformModel {

    @Resource
    private ProductPlatformReadMapper productPlatformReadMapper;
    @Resource
    private ProductPlatformWriteMapper productPlatformWriteMapper;

    /**
     * 新增平台库货品表
     *
     * @param productPlatform
     * @return
     */
    public Integer saveProductPlatform(ProductPlatform productPlatform) {
        int count = productPlatformWriteMapper.insert(productPlatform);
        if (count == 0) {
            throw new MallException("添加平台库货品表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据platformProductId更新平台库货品表
     *
     * @param productPlatform
     * @return
     */
    public Integer updateProductPlatform(ProductPlatform productPlatform) {
        if (StringUtils.isEmpty(productPlatform.getPlatformProductId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = productPlatformWriteMapper.updateByPrimaryKeySelective(productPlatform);
        if (count == 0) {
            log.error("根据platformProductId：" + productPlatform.getPlatformProductId() + "更新平台库货品表失败");
            throw new MallException("更新平台库货品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据platformProductId获取平台库货品表详情
     *
     * @param platformProductId platformProductId
     * @return
     */
    public ProductPlatform getProductPlatformByPlatformProductId(Long platformProductId) {
        return productPlatformReadMapper.getByPrimaryKey(platformProductId);
    }

    /**
     * 根据条件获取平台库货品表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<ProductPlatform> getProductPlatformList(ProductPlatformExample example, PagerInfo pager) {
        List<ProductPlatform> productPlatformList;
        if (pager != null) {
            pager.setRowsCount(productPlatformReadMapper.countByExample(example));
            productPlatformList = productPlatformReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            productPlatformList = productPlatformReadMapper.listByExample(example);
        }
        return productPlatformList;
    }

    /**
     * 根据条件获取平台库货品数量
     *
     * @param example 查询条件信息
     * @return
     */
    public Integer getProductPlatformListCount(ProductPlatformExample example) {
        return productPlatformReadMapper.countByExample(example);
    }
}