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.DrawRecordReadMapper;
import com.slodon.b2b2c.dao.write.promotion.DrawRecordWriteMapper;
import com.slodon.b2b2c.promotion.example.DrawRecordExample;
import com.slodon.b2b2c.promotion.pojo.DrawRecord;
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 DrawRecordModel {

    @Resource
    private DrawRecordReadMapper drawRecordReadMapper;
    @Resource
    private DrawRecordWriteMapper drawRecordWriteMapper;

    /**
     * 新增抽奖记录表
     *
     * @param drawRecord
     * @return
     */
    public Integer saveDrawRecord(DrawRecord drawRecord) {
        int count = drawRecordWriteMapper.insert(drawRecord);
        if (count == 0) {
            throw new MallException("添加抽奖记录表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据recordId更新抽奖记录表
     *
     * @param drawRecord
     * @return
     */
    public Integer updateDrawRecord(DrawRecord drawRecord) {
        if (StringUtils.isEmpty(drawRecord.getRecordId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = drawRecordWriteMapper.updateByPrimaryKeySelective(drawRecord);
        if (count == 0) {
            log.error("根据recordId：" + drawRecord.getRecordId() + "更新抽奖记录表失败");
            throw new MallException("更新抽奖记录表失败,请重试");
        }
        return count;
    }

    /**
     * 根据recordId获取抽奖记录表详情
     *
     * @param recordId recordId
     * @return
     */
    public DrawRecord getDrawRecordByRecordId(Integer recordId) {
        return drawRecordReadMapper.getByPrimaryKey(recordId);
    }

    /**
     * 根据条件获取抽奖记录表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<DrawRecord> getDrawRecordList(DrawRecordExample example, PagerInfo pager) {
        List<DrawRecord> drawRecordList;
        if (pager != null) {
            pager.setRowsCount(drawRecordReadMapper.countByExample(example));
            drawRecordList = drawRecordReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            drawRecordList = drawRecordReadMapper.listByExample(example);
        }
        return drawRecordList;
    }

    /**
     * 根据条件获取抽奖记录数量
     *
     * @param example 查询条件信息
     * @return
     */
    public Integer getDrawRecordCount(DrawRecordExample example) {
        return drawRecordReadMapper.countByExample(example);
    }

}