package com.slodon.b2b2c.model.promotion;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.promotion.DrawPrizeReadMapper;
import com.slodon.b2b2c.dao.write.promotion.DrawPrizeWriteMapper;
import com.slodon.b2b2c.promotion.example.DrawPrizeExample;
import com.slodon.b2b2c.promotion.pojo.DrawPrize;
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 DrawPrizeModel {

    @Resource
    private DrawPrizeReadMapper drawPrizeReadMapper;
    @Resource
    private DrawPrizeWriteMapper drawPrizeWriteMapper;

    /**
     * 新增抽奖奖品表
     *
     * @param drawPrize
     * @return
     */
    public Integer saveDrawPrize(DrawPrize drawPrize) {
        int count = drawPrizeWriteMapper.insert(drawPrize);
        if (count == 0) {
            throw new MallException("添加抽奖奖品表失败，请重试");
        }
        return count;
    }

    /**
     * 根据prizeId删除抽奖奖品表
     *
     * @param prizeId prizeId
     * @return
     */
    public Integer deleteDrawPrize(Integer prizeId) {
        if (StringUtils.isEmpty(prizeId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = drawPrizeWriteMapper.deleteByPrimaryKey(prizeId);
        if (count == 0) {
            log.error("根据prizeId：" + prizeId + "删除抽奖奖品表失败");
            throw new MallException("删除抽奖奖品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据prizeId更新抽奖奖品表
     *
     * @param drawPrize
     * @return
     */
    public Integer updateDrawPrize(DrawPrize drawPrize) {
        if (StringUtils.isEmpty(drawPrize.getPrizeId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = drawPrizeWriteMapper.updateByPrimaryKeySelective(drawPrize);
        if (count == 0) {
            log.error("根据prizeId：" + drawPrize.getPrizeId() + "更新抽奖奖品表失败");
            throw new MallException("更新抽奖奖品表失败,请重试");
        }
        return count;
    }

    /**
     * 根据prizeId获取抽奖奖品表详情
     *
     * @param prizeId prizeId
     * @return
     */
    public DrawPrize getDrawPrizeByPrizeId(Integer prizeId) {
        return drawPrizeReadMapper.getByPrimaryKey(prizeId);
    }

    /**
     * 根据条件获取抽奖奖品表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<DrawPrize> getDrawPrizeList(DrawPrizeExample example, PagerInfo pager) {
        List<DrawPrize> drawPrizeList;
        if (pager != null) {
            pager.setRowsCount(drawPrizeReadMapper.countByExample(example));
            drawPrizeList = drawPrizeReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            drawPrizeList = drawPrizeReadMapper.listByExample(example);
        }
        return drawPrizeList;
    }
}