package com.publiccms.views.directive.cms;

// Generated 2020-3-26 11:46:48 by com.publiccms.common.generator.SourceGenerator

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.common.handler.RenderHandler;
import com.publiccms.common.tools.CommonUtils;
import com.publiccms.entities.cms.CmsUserCollection;
import com.publiccms.entities.cms.CmsUserCollectionId;
import com.publiccms.logic.service.cms.CmsUserCollectionService;

import freemarker.template.TemplateException;

/**
 *
 * userCollection 用户收藏查询指令
 * <p>
 * 参数列表
 * <ul>
 * <li><code>userId</code>:用户id
 * <li><code>contentId</code>:内容id,结果返回<code>object</code>
 * {@link com.publiccms.entities.cms.CmsUserCollection}
 * <li><code>contentIds</code>
 * 多个项目id,逗号或空格间隔,当contentId为空时生效,结果返回<code>map</code>(contentId,<code>object</code>)
 * </ul>
 * <p>
 * 使用示例
 * <p>
 * &lt;@cms.userCollection userId=1
 * contentId=1&gt;${object.scores}&lt;/@cms.userCollection&gt;
 * <p>
 * &lt;@cms.userCollection userId=1 contentIds='1,2,3'&gt;&lt;#list map as
 * k,v&gt;${k}:${v.contentId}&lt;#sep&gt;,&lt;/#list&gt;&lt;/@cms.userCollection&gt;
 *
 * <pre>
&lt;script&gt;
fetch('${site.dynamicPath}api/directive/cms/userCollection?id=1').then(res => res.json()).then(data=>{
  console.log(data.scores);
});
&lt;/script&gt;
 * </pre>
 */
@Component
public class CmsUserCollectionDirective extends AbstractTemplateDirective {

    @Override
    public void execute(RenderHandler handler) throws IOException, TemplateException {
        Long userId = handler.getLong("userId");
        Long contentId = handler.getLong("contentId");
        if (null != userId) {
            if (null != contentId) {
                CmsUserCollection entity = service.getEntity(new CmsUserCollectionId(userId, contentId));
                if (null != entity) {
                    handler.put("object", entity).render();
                }
            } else {
                Long[] contentIds = handler.getLongArray("contentIds");
                if (CommonUtils.notEmpty(contentIds)) {
                    CmsUserCollectionId[] entityIds = Stream.of(contentIds).map(e -> new CmsUserCollectionId(userId, e))
                            .toArray(CmsUserCollectionId[]::new);
                    List<CmsUserCollection> entityList = service.getEntitys(entityIds);
                    Map<String, CmsUserCollection> map = CommonUtils.listToMapSorted(entityList,
                            k -> String.valueOf(k.getId().getContentId()), contentIds, e -> e.getId().getContentId());
                    handler.put("map", map).render();
                }
            }
        } else if (null != contentId) {
            Long[] userIds = handler.getLongArray("userIds");
            if (CommonUtils.notEmpty(userIds)) {
                CmsUserCollectionId[] entityIds = Stream.of(userIds).map(e -> new CmsUserCollectionId(e, contentId))
                        .toArray(CmsUserCollectionId[]::new);
                List<CmsUserCollection> entityList = service.getEntitys(entityIds);
                Map<String, CmsUserCollection> map = CommonUtils.listToMapSorted(entityList,
                        k -> String.valueOf(k.getId().getUserId()), userIds, e -> e.getId().getUserId());
                handler.put("map", map).render();
            }
        }
    }

    @Resource
    private CmsUserCollectionService service;

}
