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.PlatformHourReadMapper;
import com.slodon.b2b2c.dao.write.statistics.PlatformHourWriteMapper;
import com.slodon.b2b2c.statistics.example.PlatformHourExample;
import com.slodon.b2b2c.statistics.pojo.PlatformHour;
import com.slodon.b2b2c.vo.statistics.MovableGoodsVO;
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 PlatformHourModel {

    @Resource
    private PlatformHourReadMapper platformHourReadMapper;
    @Resource
    private PlatformHourWriteMapper platformHourWriteMapper;

    /**
     * 新增平台时统计表
     *
     * @param platformHour
     * @return
     */
    public Integer savePlatformHour(PlatformHour platformHour) {
        int count = platformHourWriteMapper.insert(platformHour);
        if (count == 0) {
            throw new MallException("添加平台时统计表失败，请重试");
        }
        return count;
    }

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

    /**
     * 根据statsId更新平台时统计表
     *
     * @param platformHour
     * @return
     */
    public Integer updatePlatformHour(PlatformHour platformHour) {
        if (StringUtils.isEmpty(platformHour.getStatsId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = platformHourWriteMapper.updateByPrimaryKeySelective(platformHour);
        if (count == 0) {
            log.error("根据statsId：" + platformHour.getStatsId() + "更新平台时统计表失败");
            throw new MallException("更新平台时统计表失败,请重试");
        }
        return count;
    }

    /**
     * 根据statsId获取平台时统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public PlatformHour getPlatformHourByStatsId(Long statsId) {
        return platformHourReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取平台时统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<PlatformHour> getPlatformHourList(PlatformHourExample example, PagerInfo pager) {
        List<PlatformHour> platformHourList;
        if (pager != null) {
            pager.setRowsCount(platformHourReadMapper.countByExample(example));
            platformHourList = platformHourReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            platformHourList = platformHourReadMapper.listByExample(example);
        }
        return platformHourList;
    }

    /**
     * 根据条件获取平台趋势统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<MovableGoodsVO> getPlatformTrendList(PlatformHourExample example) {
        return platformHourReadMapper.getPlatformTrendList(example);
    }
}