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.exceptions.ParamsException;
import ink.wgink.module.article.dao.content.IContentDao;
import ink.wgink.module.article.pojo.dtos.content.ContentDTO;
import ink.wgink.module.article.pojo.vos.content.ContentVO;
import ink.wgink.module.article.service.content.IContentService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.map.HashMapUtil;
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;

/**
 * @ClassName: ArticleContentServiceImpl
 * @Description: 文章内容
 * @Author: WenG
 * @Date: 2020-04-03 15:37
 * @Version: 1.0
 **/
@Service
public class ContentServiceImpl extends DefaultBaseService implements IContentService {

    @Autowired
    private IContentDao contentDao;

    @Override
    public void save(ContentVO contentVO) {
        saveReturnId(null, contentVO);
    }

    @Override
    public void save(String token, ContentVO contentVO) {
        saveReturnId(token, contentVO);
    }

    @Override
    public String saveReturnId(ContentVO contentVO) {
        return saveReturnId(null, contentVO);
    }

    @Override
    public String saveReturnId(String token, ContentVO contentVO) {
        String contentId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(contentVO);
        params.put("contentId", contentId);
        if (token != null) {
            setAppSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        contentDao.save(params);
        return contentId;
    }

    @Override
    public void remove(List<String> ids) {
        removeInfo(null, ids);
    }

    @Override
    public void removeByToken(String token, List<String> ids) {
        removeInfo(token, ids);
    }

    /**
     * 删除文章内容
     *
     * @param token
     * @param ids
     */
    private void removeInfo(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("contentIds", ids);
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        contentDao.remove(params);
    }

    @Override
    public void update(String contentId, ContentVO contentVO) {
        updateArticleContentInfo(null, contentId, contentVO);
    }

    @Override
    public void updateByToken(String token, String contentId, ContentVO contentVO) {
        updateArticleContentInfo(token, contentId, contentVO);
    }

    @Override
    public void updatePublishStatus(String contentId, Integer isPublish) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentId", contentId);
        params.put("isPublish", isPublish);
        setUpdateInfo(params);
        contentDao.update(params);
    }

    /**
     * 修改文章内容
     *
     * @param token
     * @param contentId
     * @param contentVO
     */
    private void updateArticleContentInfo(String token, String contentId, ContentVO contentVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentVO);
        params.put("contentId", contentId);
        if (token != null) {
            setAppUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        contentDao.update(params);
    }

    @Override
    public ContentDTO get(String contentId) {
        Map<String, Object> params = super.getHashMap(1);
        params.put("contentId", contentId);
        return contentDao.get(params);
    }

    @Override
    public List<ContentDTO> list(Map<String, Object> params) {
        return contentDao.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);
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = contentDao.count(params);
        return count == null ? 0 : count;
    }

}
