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.CategoryHourReadMapper;
import com.slodon.b2b2c.dao.write.statistics.CategoryHourWriteMapper;
import com.slodon.b2b2c.statistics.example.CategoryHourExample;
import com.slodon.b2b2c.statistics.pojo.CategoryHour;
import com.slodon.b2b2c.vo.statistics.SalesTrendVO;
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 CategoryHourModel {

    @Resource
    private CategoryHourReadMapper categoryHourReadMapper;
    @Resource
    private CategoryHourWriteMapper categoryHourWriteMapper;

    /**
     * 新增商品分类时统计表
     *
     * @param categoryHour
     * @return
     */
    public Integer saveCategoryHour(CategoryHour categoryHour) {
        int count = categoryHourWriteMapper.insert(categoryHour);
        if (count == 0) {
            throw new MallException("添加商品分类时统计表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据statsId更新商品分类时统计表
     *
     * @param categoryHour
     * @return
     */
    public Integer updateCategoryHour(CategoryHour categoryHour) {
        if (StringUtils.isEmpty(categoryHour.getStatsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = categoryHourWriteMapper.updateByPrimaryKeySelective(categoryHour);
        if (count == 0) {
            log.error("根据statsId：" + categoryHour.getStatsId() + "更新商品分类时统计表失败");
            throw new MallException("更新商品分类时统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId获取商品分类时统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public CategoryHour getCategoryHourByStatsId(Long statsId) {
        return categoryHourReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取商品分类时统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<CategoryHour> getCategoryHourList(CategoryHourExample example, PagerInfo pager) {
        List<CategoryHour> categoryHourList;
        if (pager != null) {
            pager.setRowsCount(categoryHourReadMapper.countByExample(example));
            categoryHourList = categoryHourReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            categoryHourList = categoryHourReadMapper.listByExample(example);
        }
        return categoryHourList;
    }

    /**
     * 根据条件获取商品分类统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<SalesTrendVO> getCategoryTrendList(CategoryHourExample example) {
        return categoryHourReadMapper.getCategoryTrendList(example);
    }
}