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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.ClassGroupDao;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.service.ImGroupMemberService;
import com.ym.mec.biz.service.ImGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.dao.CooperationOrganDao;
import com.ym.mec.biz.service.CooperationOrganService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CooperationOrganServiceImpl extends BaseServiceImpl<Integer, CooperationOrgan>  implements CooperationOrganService {
	
	@Autowired
	private CooperationOrganDao cooperationOrganDao;
	@Autowired
	private MusicGroupDao musicGroupDao;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private ImGroupMemberService imGroupMemberService;
	@Autowired
	private ImGroupService imGroupService;
	@Autowired
	private EmployeeDao employeeDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, CooperationOrgan> getDAO() {
		return cooperationOrganDao;
	}

	@Override
	public List<CooperationOrgan> queryByOrganId(String organId) {
		return cooperationOrganDao.queryByOrganId(organId);
	}

    @Override
    public List<MusicGroupPaymentCalender> getCooperationOrganCalender4School(Integer id) {
		return cooperationOrganDao.getCooperationOrganCalender4School(id);
    }

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCooperation(CooperationOrgan cooperationOrgan) {
		CooperationOrgan cooperationOrgan1 = cooperationOrganDao.get(cooperationOrgan.getId());
		//修改了乐团主管后，修改关联乐团的乐团主管
		if(!cooperationOrgan.getEducationUserId().equals(cooperationOrgan1.getEducationUserId())){
			SysUser sysUser = sysUserFeignService.queryUserById(cooperationOrgan.getEducationUserId());
			//获取进行中、暂停、筹备中的乐团列表
			List<MusicGroup> musicGroups = musicGroupDao.findByCooperationIdAndStatus(cooperationOrgan.getId());
			if(musicGroups.size() > 0){
				for (MusicGroup musicGroup : musicGroups) {
					//获取当前乐团所有班级
					List<ClassGroup> classGroups = classGroupDao.findClassGroups(musicGroup.getId());
					if (classGroups == null || classGroups.size() == 0) {
						continue;
					}
					Integer educationalTeacherId = musicGroup.getEducationalTeacherId();
					classGroups.forEach(e -> {
						if(educationalTeacherId != null){
							imGroupMemberService.quit(e.getId().longValue(), educationalTeacherId);
						}
						imGroupMemberService.join(e.getId().longValue(),"乐团主管",false,null,sysUser);
					});
				}
			}
			//更新预报名群信息
			List<MusicGroup> musicGroupList = musicGroupDao.findByCooperationId(cooperationOrgan.getId());
			if(musicGroupList.size() > 0){
				for (MusicGroup musicGroup : musicGroupList) {
					//不是导入的乐团
					if(musicGroup.getId().length() > 10){
						//是否有预报名的群聊
						ImGroup imGroup = imGroupService.get(Long.parseLong(musicGroup.getId()));
						if(imGroup != null){
							imGroupMemberService.quit(Long.parseLong(musicGroup.getId()),musicGroup.getEducationalTeacherId());
							imGroupMemberService.join(Long.parseLong(musicGroup.getId()),"乐团主管",false,imGroup,sysUser);
						}
					}
				}
			}
			//修改乐团主管
			musicGroupDao.batchUpdateEdu(cooperationOrgan.getId(),cooperationOrgan.getEducationUserId());
		}
		cooperationOrganDao.update(cooperationOrgan);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateCooperationEdu(List<Integer> organIds) {
		List<CooperationOrgan> organList = cooperationOrganDao.getCooperationOrganByIds(organIds);
		for (CooperationOrgan cooperationOrgan : organList) {
			if(cooperationOrgan.getEducationUserId() != null){
				Employee employee = employeeDao.get(cooperationOrgan.getEducationUserId());
				if(employee != null){
					//获取进行中、暂停、筹备中的乐团列表
					List<MusicGroup> musicGroups = musicGroupDao.findByCooperationIdAndStatus(cooperationOrgan.getId());
					if(musicGroups.size() > 0){
						for (MusicGroup musicGroup : musicGroups) {
							//获取当前乐团所有班级
							List<ClassGroup> classGroups = classGroupDao.findClassGroups(musicGroup.getId());
							if (classGroups == null || classGroups.size() == 0) {
								continue;
							}
							Integer educationalTeacherId = musicGroup.getEducationalTeacherId();
							List<Integer> quitUserIdList = new ArrayList<Integer>();
							if(educationalTeacherId != null){
								quitUserIdList.add(educationalTeacherId);
							}
							Map<Integer, String> userRoleMap = new HashMap<Integer, String>(1);
							userRoleMap.put(cooperationOrgan.getEducationUserId(), "乐团主管");
							classGroups.forEach(e -> {
								if(educationalTeacherId != null){
									imGroupMemberService.quit(e.getId().longValue(), quitUserIdList);
								}
								imGroupMemberService.join(e.getId().longValue(), userRoleMap);
							});
						}
					}
					//更新预报名群信息
					List<MusicGroup> musicGroupList = musicGroupDao.findByCooperationId(cooperationOrgan.getId());
					if(musicGroupList.size() > 0){
						for (MusicGroup musicGroup : musicGroupList) {
							//不是导入的乐团
							if(musicGroup.getId().length() > 10){
								//是否有预报名的群聊
								ImGroup imGroup = imGroupService.get(Long.parseLong(musicGroup.getId()));
								if(imGroup != null){
									imGroupMemberService.quit(Long.parseLong(musicGroup.getId()),musicGroup.getEducationalTeacherId());
									imGroupMemberService.join(Long.parseLong(musicGroup.getId()),cooperationOrgan.getEducationUserId(),"乐团主管",false);
								}
							}
						}
					}
					//修改乐团主管
					musicGroupDao.batchUpdateEdu(cooperationOrgan.getId(),cooperationOrgan.getEducationUserId());
					System.out.println(cooperationOrgan.getName() + "  OK");
				}
			}
		}
	}
}