package com.slodon.b2b2c.model.video;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.video.LiveStoreBindMemberReadMapper;
import com.slodon.b2b2c.dao.write.video.LiveStoreBindMemberWriteMapper;
import com.slodon.b2b2c.video.example.LiveStoreBindMemberExample;
import com.slodon.b2b2c.video.pojo.LiveStoreBindMember;
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 LiveStoreBindMemberModel {

    @Resource
    private LiveStoreBindMemberReadMapper liveStoreBindMemberReadMapper;
    @Resource
    private LiveStoreBindMemberWriteMapper liveStoreBindMemberWriteMapper;

    /**
     * 新增店铺直播绑定会员表
     *
     * @param liveStoreBindMember
     * @return
     */
    public Integer saveLiveStoreBindMember(LiveStoreBindMember liveStoreBindMember) {
        int count = liveStoreBindMemberWriteMapper.insert(liveStoreBindMember);
        if (count == 0) {
            throw new MallException("添加店铺直播绑定会员表失败，请重试");
        }
        return count;
    }

    /**
     * 根据bindId删除店铺直播绑定会员表
     *
     * @param bindId bindId
     * @return
     */
    public Integer deleteLiveStoreBindMember(Integer bindId) {
        if (StringUtils.isEmpty(bindId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = liveStoreBindMemberWriteMapper.deleteByPrimaryKey(bindId);
        if (count == 0) {
            log.error("根据bindId：" + bindId + "删除店铺直播绑定会员表失败");
            throw new MallException("删除店铺直播绑定会员表失败,请重试");
        }
        return count;
    }

    /**
     * 根据查询条件信息删除店铺直播绑定会员
     *
     * @param example 查询条件信息
     * @return
     */
    public void deleteLiveStoreBindMemberByExample(LiveStoreBindMemberExample example) {
        liveStoreBindMemberWriteMapper.deleteByExample(example);
    }

    /**
     * 根据bindId更新店铺直播绑定会员表
     *
     * @param liveStoreBindMember
     * @return
     */
    public Integer updateLiveStoreBindMember(LiveStoreBindMember liveStoreBindMember) {
        if (StringUtils.isEmpty(liveStoreBindMember.getBindId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = liveStoreBindMemberWriteMapper.updateByPrimaryKeySelective(liveStoreBindMember);
        if (count == 0) {
            log.error("根据bindId：" + liveStoreBindMember.getBindId() + "更新店铺直播绑定会员表失败");
            throw new MallException("更新店铺直播绑定会员表失败,请重试");
        }
        return count;
    }

    /**
     * 根据bindId获取店铺直播绑定会员表详情
     *
     * @param bindId bindId
     * @return
     */
    public LiveStoreBindMember getLiveStoreBindMemberByBindId(Integer bindId) {
        return liveStoreBindMemberReadMapper.getByPrimaryKey(bindId);
    }

    /**
     * 根据条件获取店铺直播绑定会员表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<LiveStoreBindMember> getLiveStoreBindMemberList(LiveStoreBindMemberExample example, PagerInfo pager) {
        List<LiveStoreBindMember> liveStoreBindMemberList;
        if (pager != null) {
            pager.setRowsCount(liveStoreBindMemberReadMapper.countByExample(example));
            liveStoreBindMemberList = liveStoreBindMemberReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            liveStoreBindMemberList = liveStoreBindMemberReadMapper.listByExample(example);
        }
        return liveStoreBindMemberList;
    }
}