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.StoreWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.StoreWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.StoreWeekExample;
import com.slodon.b2b2c.statistics.pojo.StoreWeek;
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 StoreWeekModel {
    @Resource
    private StoreWeekReadMapper storeWeekReadMapper;

    @Resource
    private StoreWeekWriteMapper storeWeekWriteMapper;

    /**
     * 新增店铺周统计表
     *
     * @param storeWeek
     * @return
     */
    public Integer saveStoreWeek(StoreWeek storeWeek) {
        int count = storeWeekWriteMapper.insert(storeWeek);
        if (count == 0) {
            throw new MallException("添加店铺周统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取店铺周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public StoreWeek getStoreWeekByStatsId(Long statsId) {
        return storeWeekReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取店铺周统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<StoreWeek> getStoreWeekList(StoreWeekExample example, PagerInfo pager) {
        List<StoreWeek> storeWeekList;
        if (pager != null) {
            pager.setRowsCount(storeWeekReadMapper.countByExample(example));
            storeWeekList = storeWeekReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeWeekList = storeWeekReadMapper.listByExample(example);
        }
        return storeWeekList;
    }
}