package ink.wgink.module.article.service.content.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.article.pojo.dtos.content.ContentDTO;
import ink.wgink.module.article.service.content.IContentDataAuthService;
import ink.wgink.module.article.service.content.IContentService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ContentDataAuthService
 * @Description: 文章内容数据权限
 * @Author: wanggeng
 * @Date: 2021/6/11 10:17 上午
 * @Version: 1.0
 */
@Service
public class ContentDataAuthService extends DefaultBaseService implements IContentDataAuthService {

    @Autowired
    private IContentService contentService;

    @Override
    public List<ContentDTO> list(Map<String, Object> params) {
        setDataAuthorityInfo(params);
        return contentService.list(params);
    }

    @Override
    public List<ContentDTO> list(String categoryId) {
        if (StringUtils.isBlank(categoryId)) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(2);
        params.put("categoryId", categoryId);
        return list(params);
    }

    @Override
    public List<ContentDTO> listByUserId(String userId) {
        if (StringUtils.isBlank(userId)) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(2);
        params.put("creator", userId);
        return list(params);
    }

    @Override
    public List<ContentDTO> listByUserIds(List<String> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(2);
        params.put("creators", userIds);
        return list(params);
    }

    @Override
    public List<ContentDTO> listByUserId(String categoryId, String userId) {
        if (StringUtils.isBlank(userId)) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(4);
        params.put("categoryId", categoryId);
        params.put("creator", userId);
        return list(params);
    }

    @Override
    public List<ContentDTO> listByUserIds(String categoryId, List<String> userIds) {
        if (StringUtils.isBlank(categoryId)) {
            return new ArrayList<>();
        }
        if (userIds == null || userIds.isEmpty()) {
            return new ArrayList<>();
        }
        Map<String, Object> params = getHashMap(4);
        params.put("categoryId", categoryId);
        params.put("creators", userIds);
        return list(params);
    }

    @Override
    public SuccessResultList<List<ContentDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ContentDTO> articleContentDTOs = list(page.getParams());
        PageInfo<ContentDTO> pageInfo = new PageInfo<>(articleContentDTOs);
        return new SuccessResultList<>(articleContentDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public SuccessResultList<List<ContentDTO>> listPage(String categoryId, ListPage page) {
        if (StringUtils.isBlank(categoryId)) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        page.getParams().put("categoryId", categoryId);
        return listPage(page);
    }

    @Override
    public SuccessResultList<List<ContentDTO>> listPageByUserId(String categoryId, String userId, ListPage page) {
        if (StringUtils.isBlank(categoryId)) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        if (StringUtils.isBlank(userId)) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        page.getParams().put("categoryId", categoryId);
        page.getParams().put("creator", userId);
        return listPage(page);
    }

    @Override
    public SuccessResultList<List<ContentDTO>> listPageByUserIds(String categoryId, List<String> userIds, ListPage page) {
        if (StringUtils.isBlank(categoryId)) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        if (userIds == null || userIds.isEmpty()) {
            return new SuccessResultList<>(new ArrayList<>(), 1, 0L);
        }
        page.getParams().put("categoryId", categoryId);
        page.getParams().put("creators", userIds);
        return listPage(page);
    }
}
