package com.ym.mec.biz.service.impl;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkReplyDao;
import com.ym.mec.biz.dal.dto.StudentCourseHomeworkCommentDto;
import com.ym.mec.biz.dal.entity.StudentCourseHomeworkReply;
import com.ym.mec.biz.dal.page.StudentCourseHomeworkReplyQueryInfo;
import com.ym.mec.biz.service.StudentCourseHomeworkReplyService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class StudentCourseHomeworkReplyServiceImpl extends BaseServiceImpl<Long, StudentCourseHomeworkReply> implements StudentCourseHomeworkReplyService {
	
	@Autowired
	private StudentCourseHomeworkReplyDao studentCourseHomeworkReplyDao;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Long, StudentCourseHomeworkReply> getDAO() {
		return studentCourseHomeworkReplyDao;
	}

	@Override
	public long insert(StudentCourseHomeworkReply bean) {
		SysUser user = sysUserFeignService.queryUserInfo();
		bean.setUserId(user.getId());
		return super.insert(bean);
	}

	@Override
	public PageInfo queryPage(QueryInfo queryInfo) {
		StudentCourseHomeworkReplyQueryInfo studentCourseHomeworkReplyQueryInfo= (StudentCourseHomeworkReplyQueryInfo) queryInfo;
		studentCourseHomeworkReplyQueryInfo.setParentID(null);
		PageInfo studentCourseHomeworkReplyPageInfo = super.queryPage(studentCourseHomeworkReplyQueryInfo);
		for(Object temp:studentCourseHomeworkReplyPageInfo.getRows()){
			studentCourseHomeworkReplyQueryInfo.setParentID(((StudentCourseHomeworkCommentDto)temp).getReplyId());
			getTree((StudentCourseHomeworkCommentDto) temp,studentCourseHomeworkReplyQueryInfo);
		}
		return studentCourseHomeworkReplyPageInfo;
	}

	@Override
	public int delete(Long id) {
		StudentCourseHomeworkReply studentCourseHomeworkReply = super.get(id);
		List<Long> ids=new ArrayList<>();
		ids.add(id);
		List<StudentCourseHomeworkReply> allReplyByStudentCourseHomeworkID = studentCourseHomeworkReplyDao.findAllReplyByStudentCourseHomeworkID(studentCourseHomeworkReply.getStudentCourseHomeworkId());
		findAllChildID(id,allReplyByStudentCourseHomeworkID,ids);
		return studentCourseHomeworkReplyDao.batchDeleteReplys(ids);
	}

	private void findAllChildID(Long parentID,List<StudentCourseHomeworkReply> datas,List<Long> results){
		for(StudentCourseHomeworkReply data:datas){
			if(parentID.equals(data.getParentId())){
				results.add(data.getId());
				findAllChildID(data.getId(),datas,results);
			}
		}
	}

	private StudentCourseHomeworkCommentDto getTree(StudentCourseHomeworkCommentDto studentCourseHomeworkCommentDto, StudentCourseHomeworkReplyQueryInfo queryInfo){
		PageInfo pageInfo = super.queryPage(queryInfo);
		if(pageInfo != null && pageInfo.getRows().size() > 0) {
			studentCourseHomeworkCommentDto.setReplys(pageInfo.getRows());
			for (Object  object: pageInfo.getRows()) {
				queryInfo.setParentID(((StudentCourseHomeworkCommentDto)object).getReplyId());
				getTree((StudentCourseHomeworkCommentDto) object,queryInfo);
			}
		}
		return studentCourseHomeworkCommentDto;
	}

}