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.TerminalHourReadMapper;
import com.slodon.b2b2c.dao.write.statistics.TerminalHourWriteMapper;
import com.slodon.b2b2c.statistics.example.TerminalHourExample;
import com.slodon.b2b2c.statistics.pojo.TerminalHour;
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 TerminalHourModel {

    @Resource
    private TerminalHourReadMapper terminalHourReadMapper;
    @Resource
    private TerminalHourWriteMapper terminalHourWriteMapper;

    /**
     * 新增终端时统计表
     *
     * @param terminalHour
     * @return
     */
    public Integer saveTerminalHour(TerminalHour terminalHour) {
        int count = terminalHourWriteMapper.insert(terminalHour);
        if (count == 0) {
            throw new MallException("添加终端时统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取终端时统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public TerminalHour getTerminalHourByStatsId(Long statsId) {
        return terminalHourReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取终端时统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<TerminalHour> getTerminalHourList(TerminalHourExample example, PagerInfo pager) {
        List<TerminalHour> terminalHourList;
        if (pager != null) {
            pager.setRowsCount(terminalHourReadMapper.countByExample(example));
            terminalHourList = terminalHourReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            terminalHourList = terminalHourReadMapper.listByExample(example);
        }
        return terminalHourList;
    }

    /**
     * 根据条件获取省份统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<SalesTrendVO> getTerminalTrendList(TerminalHourExample example) {
        return terminalHourReadMapper.getTerminalTrendList(example);
    }
}