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.ProvinceHourReadMapper;
import com.slodon.b2b2c.dao.write.statistics.ProvinceHourWriteMapper;
import com.slodon.b2b2c.statistics.example.ProvinceHourExample;
import com.slodon.b2b2c.statistics.pojo.ProvinceHour;
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 ProvinceHourModel {

    @Resource
    private ProvinceHourReadMapper provinceHourReadMapper;
    @Resource
    private ProvinceHourWriteMapper provinceHourWriteMapper;

    /**
     * 新增省份时统计表
     *
     * @param provinceHour
     * @return
     */
    public Integer saveProvinceHour(ProvinceHour provinceHour) {
        int count = provinceHourWriteMapper.insert(provinceHour);
        if (count == 0) {
            throw new MallException("添加省份时统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取省份时统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public ProvinceHour getProvinceHourByStatsId(Long statsId) {
        return provinceHourReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取省份时统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<ProvinceHour> getProvinceHourList(ProvinceHourExample example, PagerInfo pager) {
        List<ProvinceHour> provinceHourList;
        if (pager != null) {
            pager.setRowsCount(provinceHourReadMapper.countByExample(example));
            provinceHourList = provinceHourReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            provinceHourList = provinceHourReadMapper.listByExample(example);
        }
        return provinceHourList;
    }

    /**
     * 根据条件获取省份统计
     *
     * @param example 查询条件信息
     * @return
     */
    public List<SalesTrendVO> getProvinceTrendList(ProvinceHourExample example) {
        return provinceHourReadMapper.getProvinceTrendList(example);
    }
}