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.MemberWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.MemberWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.MemberWeekExample;
import com.slodon.b2b2c.statistics.pojo.MemberWeek;
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 MemberWeekModel {
    @Resource
    private MemberWeekReadMapper memberWeekReadMapper;

    @Resource
    private MemberWeekWriteMapper memberWeekWriteMapper;

    /**
     * 新增会员周统计表
     *
     * @param memberWeek
     * @return
     */
    public Integer saveMemberWeek(MemberWeek memberWeek) {
        int count = memberWeekWriteMapper.insert(memberWeek);
        if (count == 0) {
            throw new MallException("添加会员周统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取会员周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public MemberWeek getMemberWeekByStatsId(Long statsId) {
        return memberWeekReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取会员周统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<MemberWeek> getMemberWeekList(MemberWeekExample example, PagerInfo pager) {
        List<MemberWeek> memberWeekList;
        if (pager != null) {
            pager.setRowsCount(memberWeekReadMapper.countByExample(example));
            memberWeekList = memberWeekReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            memberWeekList = memberWeekReadMapper.listByExample(example);
        }
        return memberWeekList;
    }
}