package com.ym.mec.web.controller.education;

import com.ym.mec.biz.dal.dao.StudentRegistrationDao;
import com.ym.mec.biz.dal.dto.Mapper;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.MusicGroupTrainPlan;
import com.ym.mec.biz.dal.entity.MusicGroupTrainPlanSaveDto;
import com.ym.mec.biz.dal.entity.Organization;
import com.ym.mec.biz.dal.enums.GradeTypeEnum;
import com.ym.mec.biz.service.ClassGroupService;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.biz.service.MusicGroupTrainPlanService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.exception.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("eduMusicGroupTrainPlan")
@Api(tags = "乐团训练规划")
@RestController
public class EduMusicGroupTrainPlanController extends BaseController {

	@Autowired
	private MusicGroupTrainPlanService musicGroupTrainPlanService;

	@Autowired
	private MusicGroupService musicGroupService;

	@Autowired
	private OrganizationService organizationService;

	@Autowired
	private StudentRegistrationDao studentRegistrationDao;

	@Autowired
	private ClassGroupService classGroupService;

	@ApiOperation("新增乐团训练规划")
	@PostMapping(value = "add")
	public Object add(@RequestBody MusicGroupTrainPlanSaveDto musicGroupTrainPlanSaveDto) {
		return succeed(musicGroupTrainPlanService.add(musicGroupTrainPlanSaveDto));
	}

	@ApiOperation("删除乐团训练规划")
	@PostMapping(value = "del")
	public Object del(Integer id) {
		return succeed(musicGroupTrainPlanService.delete(id));
	}

	@ApiOperation("修改乐团训练规划")
	@PostMapping(value = "update")
	public Object update(String plan,Integer planId) {
		MusicGroupTrainPlan musicGroupTrainPlan = musicGroupTrainPlanService.get(planId);
		if(musicGroupTrainPlan == null){
			throw new BizException("课程规划不存在");
		}
		musicGroupTrainPlan.setPlan(plan);
		return succeed(musicGroupTrainPlanService.update(musicGroupTrainPlan));
	}

	@ApiOperation("获取乐团训练规划")
	@PostMapping(value = "queryPlanPage")
	public Object queryPlanPage(@RequestBody MusicGroupTrainPlanSaveDto musicGroupTrainPlanSaveDto) {
		return succeed(musicGroupTrainPlanService.queryPlanPage(musicGroupTrainPlanSaveDto));
	}

	@ApiOperation("获取乐团训练规划异常规划")
	@PostMapping(value = "queryExceptionPlan")
	public Object queryExceptionPlan(@RequestBody MusicGroupTrainPlanSaveDto musicGroupTrainPlanSaveDto) {
		Map<String,Object> map = musicGroupTrainPlanService.queryExceptionPlan(musicGroupTrainPlanSaveDto);

		String musicGroupId = musicGroupTrainPlanSaveDto.getMusicGroupId();
		MusicGroup musicGroup = musicGroupService.get(musicGroupId);
		if(musicGroup == null){
			return failed("乐团信息查询失败");
		}
		Map<Object, Object> gradeMap = studentRegistrationDao.countStudentWithGradeByMusicGroupId(musicGroupId).stream().collect(Collectors.toMap(Mapper:: getKey, Mapper :: getValue));

		Organization organization = organizationService.get(musicGroup.getOrganId());
		if(organization == null){
			return failed("分部信息查询失败");
		}
		if(organization.getGradeType() == GradeTypeEnum.FIVE_PLUS){
			map.put("graduateNum", gradeMap.get(5));
		}else {
			map.put("graduateNum", gradeMap.get(6));
		}

		return succeed(map);
	}

	@ApiOperation("获取乐团训练规划课时")
	@PostMapping(value = "queryPlanCourseNum")
	public Object queryPlanCourseNum(@RequestBody MusicGroupTrainPlanSaveDto musicGroupTrainPlanSaveDto) {
		return succeed(musicGroupTrainPlanService.queryPlanCourseNum(musicGroupTrainPlanSaveDto));
	}

	@ApiOperation("获取乐团训练规划课程类型")
	@PostMapping(value = "queryCourseType")
	public Object queryCourseType(@RequestBody MusicGroupTrainPlanSaveDto musicGroupTrainPlanSaveDto) {
		return succeed(musicGroupTrainPlanService.queryCourseType(musicGroupTrainPlanSaveDto));
	}

	@ApiOperation(value = "乐团所有班级列表")
	@GetMapping("/findAllClassGroupByMusicGroup")
	public HttpResponseResult findAllClassGroupByMusicGroup(String musicGroupId, String type) {
		return succeed(classGroupService.findAllClassGroupByMusicGroup(musicGroupId, type));
	}
}
