package com.slodon.b2b2c.model.statistics;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.statistics.BrandMonthReadMapper;
import com.slodon.b2b2c.dao.write.statistics.BrandMonthWriteMapper;
import com.slodon.b2b2c.statistics.example.BrandMonthExample;
import com.slodon.b2b2c.statistics.pojo.BrandMonth;
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 BrandMonthModel {
    @Resource
    private BrandMonthReadMapper brandMonthReadMapper;

    @Resource
    private BrandMonthWriteMapper brandMonthWriteMapper;

    /**
     * 新增品牌月统计表
     *
     * @param brandMonth
     * @return
     */
    public Integer saveBrandMonth(BrandMonth brandMonth) {
        int count = brandMonthWriteMapper.insert(brandMonth);
        if (count == 0) {
            throw new MallException("添加品牌月统计表失败，请重试");
        }
        return count;
    }

    /**
     * 根据statsId删除品牌月统计表
     *
     * @param statsId statsId
     * @return
     */
    public Integer deleteBrandMonth(Long statsId) {
        if (StringUtils.isEmpty(statsId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = brandMonthWriteMapper.deleteByPrimaryKey(statsId);
        if (count == 0) {
            log.error("根据statsId：" + statsId + "删除品牌月统计表失败");
            throw new MallException("删除品牌月统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId更新品牌月统计表
     *
     * @param brandMonth
     * @return
     */
    public Integer updateBrandMonth(BrandMonth brandMonth) {
        if (StringUtils.isEmpty(brandMonth.getStatsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = brandMonthWriteMapper.updateByPrimaryKeySelective(brandMonth);
        if (count == 0) {
            log.error("根据statsId：" + brandMonth.getStatsId() + "更新品牌月统计表失败");
            throw new MallException("更新品牌月统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId获取品牌月统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public BrandMonth getBrandMonthByStatsId(Long statsId) {
        return brandMonthReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取品牌月统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<BrandMonth> getBrandMonthList(BrandMonthExample example, PagerInfo pager) {
        List<BrandMonth> brandMonthList;
        if (pager != null) {
            pager.setRowsCount(brandMonthReadMapper.countByExample(example));
            brandMonthList = brandMonthReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            brandMonthList = brandMonthReadMapper.listByExample(example);
        }
        return brandMonthList;
    }
}