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.GoodsMonthReadMapper;
import com.slodon.b2b2c.dao.write.statistics.GoodsMonthWriteMapper;
import com.slodon.b2b2c.statistics.example.GoodsMonthExample;
import com.slodon.b2b2c.statistics.pojo.GoodsMonth;
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 GoodsMonthModel {
    @Resource
    private GoodsMonthReadMapper goodsMonthReadMapper;

    @Resource
    private GoodsMonthWriteMapper goodsMonthWriteMapper;

    /**
     * 新增商品月统计表
     *
     * @param goodsMonth
     * @return
     */
    public Integer saveGoodsMonth(GoodsMonth goodsMonth) {
        int count = goodsMonthWriteMapper.insert(goodsMonth);
        if (count == 0) {
            throw new MallException("添加商品月统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取商品月统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public GoodsMonth getGoodsMonthByStatsId(Long statsId) {
        return goodsMonthReadMapper.getByPrimaryKey(statsId);
    }

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

    /**
     * 根据条件获取商品趋势统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<SalesTrendVO> getGoodsTrendList(GoodsMonthExample example) {
        return goodsMonthReadMapper.getGoodsTrendList(example);
    }
}