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

import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dao.SubjectDao;
import com.ym.mec.biz.dal.dao.SubjectGoodsMapperDao;
import com.ym.mec.biz.dal.dto.SubjectApplyDetailDto;
import com.ym.mec.biz.dal.entity.Subject;
import com.ym.mec.biz.dal.entity.SubjectGoodsMapper;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.SubjectQueryInfo;
import com.ym.mec.biz.service.SubjectService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

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

import java.util.Date;
import java.util.List;

@Service
public class SubjectServiceImpl extends BaseServiceImpl<Integer, Subject>  implements SubjectService {

	@Autowired
	private SubjectDao subjectDao;
	@Autowired
	private StudentRegistrationDao studentRegistrationDao;
	@Autowired
	private SubjectGoodsMapperDao subjectGoodsMapperDao;

	@Override
	public BaseDAO<Integer, Subject> getDAO() {
		return subjectDao;
	}

	@Override
	public List<Subject> findSubByMusicGroupId(Integer musicGroupId) {
		return subjectDao.findSubByMusicGroupId(musicGroupId);
	}

	@Override
	public PageInfo<Subject> queryPageTree(SubjectQueryInfo queryInfo) {
		PageInfo<Subject> pageInfo = queryPage(queryInfo);
		for (Subject subject:pageInfo.getRows()) {
			subject = getTree(subject,queryInfo.getDelFlag());
		}
		return pageInfo;
	}

	@Override
	public List<Subject> findDefaultSubByGroupId(Integer musicGroupId) {
		return subjectDao.findDefaultSubByGroupId(musicGroupId);
	}

	@Override
	public List<SubjectApplyDetailDto> findSubApplyDetail(Integer musicGroupId) {
		List<SubjectApplyDetailDto> subApplyDetail = subjectDao.findSubApplyDetail(musicGroupId);
		subApplyDetail.forEach(detail ->{
			detail.setPayNum(studentRegistrationDao.countPayNum(musicGroupId,detail.getSubjectId()));
		});
		return subApplyDetail;
	}

	@Override
	public void upSetSubject(Subject subject) {
		Integer parentId = upset(subject, null);
		List<Subject> subjects = subject.getSubjects();
		if(subjects != null && subjects.size() > 0){
			subjects.forEach(e->{
				upset(e, parentId);
			});
		}
	}

	@Override
	public void markGoods(List<SubjectGoodsMapper> subjectGoodsMappers) {
		subjectGoodsMappers.forEach(e->{
            upsetGoods(e);
        });
	}

	private void upsetGoods(SubjectGoodsMapper subjectGoodsMapper){
        if(subjectGoodsMapper.getId() != null){
            if(subjectGoodsMapper.getDelFlag()){
                subjectGoodsMapperDao.delete(subjectGoodsMapper.getId());
                return;
            }
            subjectGoodsMapper.setUpdateTime(new Date());
            subjectGoodsMapperDao.update(subjectGoodsMapper);
        }else {
            subjectGoodsMapperDao.insert(subjectGoodsMapper);
        }
	}


	private Integer upset(Subject subject,Integer parentId){
		if(parentId != null){
			subject.setParentSubjectId(parentId);
		}
		if(subject.getId() != null){
			subject.setUpdateTime(new Date());
			subjectDao.update(subject);
		}else {
			subjectDao.insert(subject);
		}
		return subject.getId();
	}

	private Subject getTree(Subject sub,YesOrNoEnum yesOrNoEnum){
		//得到根节点对象
		//获取子节点list
		List<Subject> subjects = subjectDao.findByParentId(sub.getCode(),yesOrNoEnum);
		//如果存在子节点
		if(subjects != null && subjects.size() > 0) {
			//将子节点list放入父节点对象
			sub.setSubjects(subjects);
			//遍历子节点....
			for (Subject subject : subjects) {
				getTree(subject,yesOrNoEnum);
			}
		}
		return sub;
	}
}