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.TerminalWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.TerminalWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.TerminalWeekExample;
import com.slodon.b2b2c.statistics.pojo.TerminalWeek;
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 TerminalWeekModel {
    @Resource
    private TerminalWeekReadMapper terminalWeekReadMapper;

    @Resource
    private TerminalWeekWriteMapper terminalWeekWriteMapper;

    /**
     * 新增终端周统计表
     *
     * @param terminalWeek
     * @return
     */
    public Integer saveTerminalWeek(TerminalWeek terminalWeek) {
        int count = terminalWeekWriteMapper.insert(terminalWeek);
        if (count == 0) {
            throw new MallException("添加终端周统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取终端周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public TerminalWeek getTerminalWeekByStatsId(Long statsId) {
        return terminalWeekReadMapper.getByPrimaryKey(statsId);
    }

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