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.BrandWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.BrandWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.BrandWeekExample;
import com.slodon.b2b2c.statistics.pojo.BrandWeek;
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 BrandWeekModel {
    @Resource
    private BrandWeekReadMapper brandWeekReadMapper;

    @Resource
    private BrandWeekWriteMapper brandWeekWriteMapper;

    /**
     * 新增品牌周统计表
     *
     * @param brandWeek
     * @return
     */
    public Integer saveBrandWeek(BrandWeek brandWeek) {
        int count = brandWeekWriteMapper.insert(brandWeek);
        if (count == 0) {
            throw new MallException("添加品牌周统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取品牌周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public BrandWeek getBrandWeekByStatsId(Long statsId) {
        return brandWeekReadMapper.getByPrimaryKey(statsId);
    }

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