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.GoodsPlatformPictureReadMapper;
import com.slodon.b2b2c.dao.write.goods.GoodsPlatformPictureWriteMapper;
import com.slodon.b2b2c.goods.example.GoodsPlatformPictureExample;
import com.slodon.b2b2c.goods.pojo.GoodsPlatformPicture;
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 GoodsPlatformPictureModel {

    @Resource
    private GoodsPlatformPictureReadMapper goodsPlatformPictureReadMapper;
    @Resource
    private GoodsPlatformPictureWriteMapper goodsPlatformPictureWriteMapper;

    /**
     * 新增平台库商品图片表
     *
     * @param goodsPlatformPicture
     * @return
     */
    public Integer saveGoodsPlatformPicture(GoodsPlatformPicture goodsPlatformPicture) {
        int count = goodsPlatformPictureWriteMapper.insert(goodsPlatformPicture);
        if (count == 0) {
            throw new MallException("添加平台库商品图片表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据pictureId更新平台库商品图片表
     *
     * @param goodsPlatformPicture
     * @return
     */
    public Integer updateGoodsPlatformPicture(GoodsPlatformPicture goodsPlatformPicture) {
        if (StringUtils.isEmpty(goodsPlatformPicture.getPictureId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = goodsPlatformPictureWriteMapper.updateByPrimaryKeySelective(goodsPlatformPicture);
        if (count == 0) {
            log.error("根据pictureId：" + goodsPlatformPicture.getPictureId() + "更新平台库商品图片表失败");
            throw new MallException("更新平台库商品图片表失败,请重试");
        }
        return count;
    }

    /**
     * 根据pictureId获取平台库商品图片表详情
     *
     * @param pictureId pictureId
     * @return
     */
    public GoodsPlatformPicture getGoodsPlatformPictureByPictureId(Integer pictureId) {
        return goodsPlatformPictureReadMapper.getByPrimaryKey(pictureId);
    }

    /**
     * 根据条件获取平台库商品图片表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<GoodsPlatformPicture> getGoodsPlatformPictureList(GoodsPlatformPictureExample example, PagerInfo pager) {
        List<GoodsPlatformPicture> goodsPlatformPictureList;
        if (pager != null) {
            pager.setRowsCount(goodsPlatformPictureReadMapper.countByExample(example));
            goodsPlatformPictureList = goodsPlatformPictureReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            goodsPlatformPictureList = goodsPlatformPictureReadMapper.listByExample(example);
        }
        return goodsPlatformPictureList;
    }
}