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.StoreHourReadMapper;
import com.slodon.b2b2c.dao.write.statistics.StoreHourWriteMapper;
import com.slodon.b2b2c.statistics.example.StoreHourExample;
import com.slodon.b2b2c.statistics.pojo.StoreHour;
import com.slodon.b2b2c.vo.statistics.MovableGoodsVO;
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 StoreHourModel {

    @Resource
    private StoreHourReadMapper storeHourReadMapper;
    @Resource
    private StoreHourWriteMapper storeHourWriteMapper;

    /**
     * 新增店铺时统计表
     *
     * @param storeHour
     * @return
     */
    public Integer saveStoreHour(StoreHour storeHour) {
        int count = storeHourWriteMapper.insert(storeHour);
        if (count == 0) {
            throw new MallException("添加店铺时统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取店铺时统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public StoreHour getStoreHourByStatsId(Long statsId) {
        return storeHourReadMapper.getByPrimaryKey(statsId);
    }

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

    /**
     * 根据条件获取店铺统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<MovableGoodsVO> getStoreTrendList(StoreHourExample example) {
        return storeHourReadMapper.getStoreTrendList(example);
    }
}