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

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.Mapper;
import com.ym.mec.biz.dal.dto.MusicGroupRegAndMoneyDto;
import com.ym.mec.biz.dal.entity.CooperationOrgan;
import com.ym.mec.biz.dal.enums.SysUserRoleEnum;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.SysEmployeePositionService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.tenant.TenantContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("eduRegister")
@Api(tags = "乐团报名回款统计（教务端）")
@RestController
public class EduRegisterController extends BaseController {

    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentPreRegistrationDao studentPreRegistrationDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private StudentPaymentOrderDetailDao studentPaymentOrderDetailDao;
    @Autowired
    private StudentPaymentOrderDao studentPaymentOrderDao;
    @Autowired
    private CooperationOrganDao cooperationOrganDao;
    @Autowired
    private SysEmployeePositionService employeePositionService;
    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "获取分部报名、回款统计")
    @GetMapping("/getRegAndMoney")
    public HttpResponseResult<List<MusicGroupRegAndMoneyDto>> getRegAndMoney(String organId, String musicGroupName) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        organId = organizationService.getEmployeeOrgan(sysUser.getId(), organId, sysUser.getIsSuperAdmin());
        //是乐团主管，不是分部经理
        List<Integer> cooperationOrganIds = null;
        Boolean onlyForRole = employeePositionService.onlyForRole(SysUserRoleEnum.EDUCATION, sysUser.getId(), sysUser.getIsSuperAdmin());
        if (onlyForRole) {
            List<CooperationOrgan> cooperationOrgans = cooperationOrganDao.getCooperationOrganByEduTeacherId(sysUser.getId());
            if (cooperationOrgans.size() > 0) {
                cooperationOrganIds = cooperationOrgans.stream().map(CooperationOrgan::getId).collect(Collectors.toList());
            }else {
                return succeed();
            }
        }

        List<MusicGroupRegAndMoneyDto> musicGroups = musicGroupDao.getMusicGroupByStatus(organId, cooperationOrganIds, musicGroupName, TenantContextHolder.getTenantId());
        if (musicGroups.size() <= 0) {
            return succeed();
        }
        List<String> musicGroupIds = musicGroups.stream().map(MusicGroupRegAndMoneyDto::getMusicGroupId).collect(Collectors.toList());

        //预报名人数
        List<MusicGroupRegAndMoneyDto> preRegisters = studentPreRegistrationDao.getPreRegisterNum(musicGroupIds);
        List<MusicGroupRegAndMoneyDto> registers = studentRegistrationDao.getRegisters(musicGroupIds);
        List<Mapper> instrumentNumList = studentPaymentOrderDetailDao.getInstrumentNumInMusicApply(musicGroupIds);
        List<Mapper> moneyList = studentPaymentOrderDao.getMoneyInMusicApply(musicGroupIds);

		for (MusicGroupRegAndMoneyDto musicGroup : musicGroups) {

			for (Mapper mapper : instrumentNumList) {
				if (!musicGroup.getMusicGroupId().equals(mapper.getKey()))
					continue;
				musicGroup.setBuyInstrumentNum(Integer.parseInt(mapper.getValue().toString()));
			}
			for (Mapper mapper : moneyList) {
				if (!musicGroup.getMusicGroupId().equals(mapper.getKey()))
					continue;
				musicGroup.setMoney((BigDecimal) mapper.getValue());
			}

			for (MusicGroupRegAndMoneyDto preRegister : preRegisters) {
				if (!musicGroup.getMusicGroupId().equals(preRegister.getMusicGroupId()))
					continue;
				musicGroup.setPreRegNum(preRegister.getPreRegNum());
			}
			for (MusicGroupRegAndMoneyDto register : registers) {
				if (!musicGroup.getMusicGroupId().equals(register.getMusicGroupId()))
					continue;
				musicGroup.setRegNum(register.getRegNum());
				musicGroup.setPayNum(register.getPayNum());
				musicGroup.setCheckNum(register.getCheckNum());
				musicGroup.setBuyCloudTeacherNum(register.getBuyCloudTeacherNum());
				musicGroup.setStudentNumOfNormal(register.getStudentNumOfNormal());
				musicGroup.setNoCloudTeacherStudentNumOfNormal(register.getNoCloudTeacherStudentNumOfNormal());
				musicGroup.setCloudTeacherStudentNumOfApply(register.getCloudTeacherStudentNumOfApply());
			}
		}
        return succeed(musicGroups);
    }
}
