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.TerminalDayReadMapper;
import com.slodon.b2b2c.dao.write.statistics.TerminalDayWriteMapper;
import com.slodon.b2b2c.statistics.example.TerminalDayExample;
import com.slodon.b2b2c.statistics.pojo.TerminalDay;
import com.slodon.b2b2c.statistics.pojo.base.TerminalBase;
import com.slodon.b2b2c.vo.statistics.ChangeTrendVO;
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 TerminalDayModel {

    @Resource
    private TerminalDayReadMapper terminalDayReadMapper;
    @Resource
    private TerminalDayWriteMapper terminalDayWriteMapper;

    /**
     * 新增终端日统计表
     *
     * @param terminalDay
     * @return
     */
    public Integer saveTerminalDay(TerminalDay terminalDay) {
        int count = terminalDayWriteMapper.insert(terminalDay);
        if (count == 0) {
            throw new MallException("添加终端日统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取终端日统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public TerminalDay getTerminalDayByStatsId(Long statsId) {
        return terminalDayReadMapper.getByPrimaryKey(statsId);
    }

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

    /**
     * 根据条件获取终端统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<TerminalBase> getTerminalList(TerminalDayExample example) {
        return terminalDayReadMapper.getTerminalList(example);
    }

    /**
     * 根据条件获取近30天变化趋势
     *
     * @param example 查询条件信息
     * @return
     */
    public List<ChangeTrendVO> getChangeTrendList(TerminalDayExample example) {
        return terminalDayReadMapper.getChangeTrendList(example);
    }

    /**
     * 根据条件获取各终端销售变化趋势
     *
     * @param example 查询条件信息
     * @return
     */
    public List<SalesTrendVO> getTerminalTrendList(TerminalDayExample example) {
        return terminalDayReadMapper.getTerminalTrendList(example);
    }

    /**
     * 根据条件获取各终端销售占比
     *
     * @param example 查询条件信息
     * @return
     */
    public List<TerminalBase> getTerminalPercentList(TerminalDayExample example) {
        return terminalDayReadMapper.getTerminalPercentList(example);
    }
}