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.BrandDayReadMapper;
import com.slodon.b2b2c.dao.write.statistics.BrandDayWriteMapper;
import com.slodon.b2b2c.statistics.example.BrandDayExample;
import com.slodon.b2b2c.statistics.pojo.BrandDay;
import com.slodon.b2b2c.statistics.pojo.base.BrandBase;
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 BrandDayModel {

    @Resource
    private BrandDayReadMapper brandDayReadMapper;
    @Resource
    private BrandDayWriteMapper brandDayWriteMapper;

    /**
     * 新增品牌日统计表
     *
     * @param brandDay
     * @return
     */
    public Integer saveBrandDay(BrandDay brandDay) {
        int count = brandDayWriteMapper.insert(brandDay);
        if (count == 0) {
            throw new MallException("添加品牌日统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取品牌日统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public BrandDay getBrandDayByStatsId(Long statsId) {
        return brandDayReadMapper.getByPrimaryKey(statsId);
    }

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

    /**
     * 根据条件获取品牌统计
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<BrandBase> getBrandList(BrandDayExample example, PagerInfo pager) {
        if (pager != null) {
            return brandDayReadMapper.getBrandList(example, pager.getStart(), pager.getPageSize());
        } else {
            return brandDayReadMapper.getBrandList(example, null, null);
        }
    }

}