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.*;
import com.ym.mec.biz.dal.dto.DemoGroupApplyDto;
import com.ym.mec.biz.dal.dto.VipGroupApplyBaseInfoDto;
import com.ym.mec.biz.dal.entity.ClassGroup;
import com.ym.mec.biz.dal.entity.ClassGroupStudentMapper;
import com.ym.mec.biz.dal.entity.DemoGroupClassGroupMapper;
import com.ym.mec.biz.dal.enums.ClassGroupStudentStatusEnum;
import com.ym.mec.biz.dal.enums.ClassGroupTypeEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.StudentDemoGroupQueryInfo;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ym.mec.biz.dal.entity.DemoGroup;
import com.ym.mec.biz.service.DemoGroupService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class DemoGroupServiceImpl extends BaseServiceImpl<Long, DemoGroup>  implements DemoGroupService {
	
	@Autowired
	private DemoGroupDao demoGroupDao;
	@Autowired
	private ClassGroupDao classGroupDao;
	@Autowired
	private DemoGroupClassGroupMapperDao demoGroupClassGroupMapperDao;
	@Autowired
	private DemoGroupCoursesPlanDao demoGroupCoursesPlanDao;
	@Autowired
	private CourseScheduleDao courseScheduleDao;
	@Autowired
	private ClassGroupStudentMapperDao classGroupStudentMapperDao;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Long, DemoGroup> getDAO() {
		return demoGroupDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void demoGroupApply(DemoGroupApplyDto demoGroupApplyDto) {
		SysUser user = sysUserFeignService.queryUserInfo();
		demoGroupApplyDto.setUserId(user.getId());
		demoGroupDao.insert(demoGroupApplyDto);
		ClassGroup classGroup=new ClassGroup();
		classGroup.setName(demoGroupApplyDto.getName());
		classGroup.setType(ClassGroupTypeEnum.DEMO);
		classGroup.setDelFlag(YesOrNoEnum.NO);
		classGroupDao.insert(classGroup);
		DemoGroupClassGroupMapper demoGroupClassGroupMapper=new DemoGroupClassGroupMapper();
		demoGroupClassGroupMapper.setDemoGroupId(demoGroupApplyDto.getId());
		demoGroupClassGroupMapper.setClassGroupId(classGroup.getId());
		demoGroupClassGroupMapperDao.insert(demoGroupClassGroupMapper);
		demoGroupCoursesPlanDao.batchInsertByDates(demoGroupApplyDto.getCourseDates(),demoGroupApplyDto.getId());
	}

	@Override
	public PageInfo queryStudentDemoGroups(StudentDemoGroupQueryInfo queryInfo) {
		PageInfo pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);

		List dataList = null;
		int count = demoGroupDao.countDemoGroupList(params);
		if (count > 0) {
			pageInfo.setTotal(count);
			params.put("offset", pageInfo.getOffset());
			dataList = demoGroupDao.findDemoGroupList(params);
		}
		if (count == 0) {
			dataList = new ArrayList<>();
		}
		pageInfo.setRows(dataList);
		return pageInfo;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void demoGroupSubscribe(Long demoGroupId) {
		SysUser user = sysUserFeignService.queryUserInfo();
		ClassGroupStudentMapper classGroupStudentMapper=new ClassGroupStudentMapper();
		classGroupStudentMapper.setClassGroupId(demoGroupDao.findClassGroupIDByDemoGroupId(demoGroupId));
		classGroupStudentMapper.setUserId(user.getId());
		classGroupStudentMapper.setStatus(ClassGroupStudentStatusEnum.NORMAL);
		classGroupStudentMapperDao.insert(classGroupStudentMapper);
		createCourseScheduleAndUpdateOrganId(demoGroupId);
	}

	/**
	 * @Author: Joburgess
	 * @Date: 2019/9/25
	 * 创建上课计划及跟新所属分部列表
	 */
	public void createCourseScheduleAndUpdateOrganId(Long demoGroupId){
		DemoGroup demoGroup=demoGroupDao.get(demoGroupId);
		if(demoGroupDao.countDemoGroupStudents(demoGroupId)>0){
			courseScheduleDao.batchAddCourseSchedules(demoGroupDao.createCourseScheduleByDemoGroup(demoGroup));
		}
		List<Integer> organIds = demoGroupDao.organIdListByDemoGroupId(demoGroup.getId());
		organIds.add(demoGroupDao.getOrganIdByUserId(demoGroup.getUserId().longValue()));
		HashSet<Integer> hashSet = new HashSet<>(organIds);
		String organIdsString = StringUtils.join(hashSet, ",");
		demoGroup.setOrganIdList(organIdsString);
		demoGroupDao.update(demoGroup);
	}
}