/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.party.comment.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.party.comment.entity.Comment;
import org.springblade.party.comment.service.CommentService;
import org.springblade.party.comment.vo.CommentVO;
import org.springblade.party.member.entity.Member;
import org.springblade.party.member.service.IMemberService;
import org.springframework.context.annotation.Bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2021-04-07
 */
public class CommentWrapper extends BaseEntityWrapper<Comment, CommentVO>  {

	private CommentService commentService;
	private IMemberService memberService;

	public CommentWrapper(CommentService meetingCommentService, IMemberService memberService){
		this.commentService = meetingCommentService;
		this.memberService = memberService;
	}
	public static CommentWrapper build(CommentService meetingCommentService, IMemberService memberService) {
		return new CommentWrapper(meetingCommentService,memberService);
 	}

	@Override
	public CommentVO entityVO(Comment comment) {
		CommentVO commentVO = Objects.requireNonNull(BeanUtil.copy(comment, CommentVO.class));
		Member sendMember = memberService.getById(comment.getMemberId());
		if(sendMember != null){
			commentVO.setSenderName(sendMember.getName());
		}
		if(comment.getRootId() == 0){
			List<Comment> childrenComments = commentService.getBaseMapper().selectList(new QueryWrapper<>(new Comment()).lambda().eq(Comment::getRootId,comment.getId()).eq(Comment::getIsShow,1));
			commentVO.setTotalComment(childrenComments.size());
			/*if (childrenComments.size() ==0){
				commentVO.setHasChildren(false);
			}else {
				commentVO.setHasChildren(true);
			}*/
			List<CommentVO> commentVOS = new ArrayList<>();
			childrenComments.stream().forEach(child -> {
				CommentVO commentVO1 = Objects.requireNonNull(BeanUtil.copy(child,CommentVO.class));

				//发送者
				Member sender = memberService.getById(commentVO1.getMemberId());
				if(sender != null){
					commentVO1.setSenderName(sender.getName());
				}

				//接收者
				Comment parent = commentService.getById(commentVO1.getPid());
				if(parent != null){
					Member accepter = memberService.getById(parent.getMemberId());
					if(accepter != null){
						commentVO1.setAccepterName(accepter.getName());
					}
				}

				commentVOS.add(commentVO1);

			});
			commentVO.setChildren(commentVOS);
		}



		return commentVO;
	}

}
