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.PlatformWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.PlatformWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.PlatformWeekExample;
import com.slodon.b2b2c.statistics.pojo.PlatformWeek;
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 PlatformWeekModel {
    @Resource
    private PlatformWeekReadMapper platformWeekReadMapper;

    @Resource
    private PlatformWeekWriteMapper platformWeekWriteMapper;

    /**
     * 新增平台周统计表
     *
     * @param platformWeek
     * @return
     */
    public Integer savePlatformWeek(PlatformWeek platformWeek) {
        int count = platformWeekWriteMapper.insert(platformWeek);
        if (count == 0) {
            throw new MallException("添加平台周统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取平台周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public PlatformWeek getPlatformWeekByStatsId(Long statsId) {
        return platformWeekReadMapper.getByPrimaryKey(statsId);
    }

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