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.MemberStoreWeekReadMapper;
import com.slodon.b2b2c.dao.write.statistics.MemberStoreWeekWriteMapper;
import com.slodon.b2b2c.statistics.example.MemberStoreWeekExample;
import com.slodon.b2b2c.statistics.pojo.MemberStoreWeek;
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 MemberStoreWeekModel {
    @Resource
    private MemberStoreWeekReadMapper memberStoreWeekReadMapper;

    @Resource
    private MemberStoreWeekWriteMapper memberStoreWeekWriteMapper;

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

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

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

    /**
     * 根据statsId获取店铺会员周统计表详情
     *
     * @param statsId statsId
     * @return
     */
    public MemberStoreWeek getMemberStoreWeekByStatsId(Long statsId) {
        return memberStoreWeekReadMapper.getByPrimaryKey(statsId);
    }

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