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.MemberStoreMonthReadMapper;
import com.slodon.b2b2c.dao.write.statistics.MemberStoreMonthWriteMapper;
import com.slodon.b2b2c.statistics.example.MemberStoreMonthExample;
import com.slodon.b2b2c.statistics.pojo.MemberStoreMonth;
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 MemberStoreMonthModel {
    @Resource
    private MemberStoreMonthReadMapper memberStoreMonthReadMapper;

    @Resource
    private MemberStoreMonthWriteMapper memberStoreMonthWriteMapper;

    /**
     * 新增店铺会员月统计表
     *
     * @param memberStoreMonth
     * @return
     */
    public Integer saveMemberStoreMonth(MemberStoreMonth memberStoreMonth) {
        int count = memberStoreMonthWriteMapper.insert(memberStoreMonth);
        if (count == 0) {
            throw new MallException("添加店铺会员月统计表失败，请重试");
        }
        return count;
    }

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

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

    /**
     * 根据statsId获取店铺会员月统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public MemberStoreMonth getMemberStoreMonthByStatsId(Long statsId) {
        return memberStoreMonthReadMapper.getByPrimaryKey(statsId);
    }

    /**
     * 根据条件获取店铺会员月统计表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<MemberStoreMonth> getMemberStoreMonthList(MemberStoreMonthExample example, PagerInfo pager) {
        List<MemberStoreMonth> memberStoreMonthList;
        if (pager != null) {
            pager.setRowsCount(memberStoreMonthReadMapper.countByExample(example));
            memberStoreMonthList = memberStoreMonthReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            memberStoreMonthList = memberStoreMonthReadMapper.listByExample(example);
        }
        return memberStoreMonthList;
    }
}