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.AuthorFollowReadMapper;
import com.slodon.b2b2c.dao.write.video.AuthorFollowWriteMapper;
import com.slodon.b2b2c.video.example.AuthorFollowExample;
import com.slodon.b2b2c.video.pojo.AuthorFollow;
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 AuthorFollowModel {
    @Resource
    private AuthorFollowReadMapper authorFollowReadMapper;

    @Resource
    private AuthorFollowWriteMapper authorFollowWriteMapper;

    /**
     * 新增作者关注表
     *
     * @param authorFollow
     * @return
     */
    public Integer saveAuthorFollow(AuthorFollow authorFollow) {
        int count = authorFollowWriteMapper.insert(authorFollow);
        if (count == 0) {
            throw new MallException("添加作者关注表失败，请重试");
        }
        return count;
    }

    /**
     * 根据followId删除作者关注表
     *
     * @param followId followId
     * @return
     */
    public Integer deleteAuthorFollow(Integer followId) {
        if (StringUtils.isEmpty(followId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = authorFollowWriteMapper.deleteByPrimaryKey(followId);
        if (count == 0) {
            log.error("根据followId：" + followId + "删除作者关注表失败");
            throw new MallException("删除作者关注表失败,请重试");
        }
        return count;
    }

    /**
     * 根据followId更新作者关注表
     *
     * @param authorFollow
     * @return
     */
    public Integer updateAuthorFollow(AuthorFollow authorFollow) {
        if (StringUtils.isEmpty(authorFollow.getFollowId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = authorFollowWriteMapper.updateByPrimaryKeySelective(authorFollow);
        if (count == 0) {
            log.error("根据followId：" + authorFollow.getFollowId() + "更新作者关注表失败");
            throw new MallException("更新作者关注表失败,请重试");
        }
        return count;
    }

    /**
     * 根据followId获取作者关注表详情
     *
     * @param followId followId
     * @return
     */
    public AuthorFollow getAuthorFollowByFollowId(Integer followId) {
        return authorFollowReadMapper.getByPrimaryKey(followId);
    }

    /**
     * 根据条件获取作者关注表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<AuthorFollow> getAuthorFollowList(AuthorFollowExample example, PagerInfo pager) {
        List<AuthorFollow> authorFollowList;
        if (pager != null) {
            pager.setRowsCount(authorFollowReadMapper.countByExample(example));
            authorFollowList = authorFollowReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            authorFollowList = authorFollowReadMapper.listByExample(example);
        }
        return authorFollowList;
    }
}