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.ProvinceMonthReadMapper;
import com.slodon.b2b2c.dao.write.statistics.ProvinceMonthWriteMapper;
import com.slodon.b2b2c.statistics.example.ProvinceMonthExample;
import com.slodon.b2b2c.statistics.pojo.ProvinceMonth;
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 ProvinceMonthModel {

    @Resource
    private ProvinceMonthReadMapper provinceMonthReadMapper;
    @Resource
    private ProvinceMonthWriteMapper provinceMonthWriteMapper;

    /**
     * 新增省份月统计表
     *
     * @param provinceMonth
     * @return
     */
    public Integer saveProvinceMonth(ProvinceMonth provinceMonth) {
        int count = provinceMonthWriteMapper.insert(provinceMonth);
        if (count == 0) {
            throw new MallException("添加省份月统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取省份月统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public ProvinceMonth getProvinceMonthByStatsId(Long statsId) {
        return provinceMonthReadMapper.getByPrimaryKey(statsId);
    }

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

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