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

import com.ym.mec.biz.dal.dao.MusicGroupDao;
import com.ym.mec.biz.dal.dto.StudentPreRegistrationDto;
import com.ym.mec.biz.dal.entity.MusicGroup;
import com.ym.mec.biz.dal.entity.StudentInstrument;
import com.ym.mec.biz.dal.entity.StudentPreRegistration;
import com.ym.mec.biz.dal.page.StudentInstrumentQueryInfo;
import com.ym.mec.biz.dal.page.StudentPreRegistrationQueryInfo;
import com.ym.mec.biz.service.StudentInstrumentService;
import com.ym.mec.biz.service.StudentPreRegistrationService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RequestMapping("eduStudentRegistration")
@Api(tags = "学生报名信息服务")
@RestController
public class EduStudentRegistrationController extends BaseController {

    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private StudentPreRegistrationService studentPreRegistrationService;
    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private StudentInstrumentService studentInstrumentService;


    @ApiOperation(value = "获取乐团学员列表（包含乐保信息）")
    @GetMapping("/queryMusicStudentInstrument")
    public Object queryMusicStudentInstrument(StudentPreRegistrationQueryInfo queryInfo) {
        return succeed(studentRegistrationService.queryMusicStudentInstrument(queryInfo));
    }

    @ApiOperation(value = "获取乐器与乐保列表")
    @GetMapping("/getList")
    public HttpResponseResult<PageInfo<StudentInstrument>> getList(StudentInstrumentQueryInfo queryInfo) {
        return succeed(studentInstrumentService.queryPage(queryInfo));
    }

    @ApiOperation(value = "乐团预报名列表分页查询")
    @GetMapping("/queryPreApplyList")
    public Object queryPreApplyList(StudentPreRegistrationQueryInfo queryInfo) {
        MusicGroup musicGroup = musicGroupDao.get(queryInfo.getMusicGroupId());

        ModelMap model = new ModelMap();
        //FIX:2238 直接从乐团组获取tenantId
        queryInfo.setTenantId(musicGroup.getTenantId());

    	PageInfo<StudentPreRegistrationDto> dataList = studentPreRegistrationService.queryListForPage(queryInfo);
    	model.put("dataList", dataList);
    	
    	Map<String, Object> params = new HashMap<String, Object>();
		params.put("musicGroupId", queryInfo.getMusicGroupId());
		params.put("tenantId", queryInfo.getTenantId());
    	List<StudentPreRegistration> studentPreRegistrationList = studentPreRegistrationService.findAll(params);
    	
    	int instrument = 0;
    	long cloudTeacher = 0;
		for (StudentPreRegistration spr : studentPreRegistrationList) {
			if (spr.getCloudTeacherMethod() != null && spr.getCloudTeacherMethod().equals("OWNED")) {
				++cloudTeacher;
			}
			if (spr.getKitPurchaseMethod() != null && spr.getKitPurchaseMethod().equals("OWNED")) {
				++instrument;
			}
		}
    	
    	model.put("instrument", instrument);
    	model.put("cloudTeacher", cloudTeacher);
    	model.put("total", studentPreRegistrationList.size());
    	
    	model.put("musicGroup", musicGroup);
    	
        return succeed(model);
    }
}
