package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderCourseSettingsDao;
import com.ym.mec.biz.dal.dao.MusicGroupPaymentCalenderDao;
import com.ym.mec.biz.dal.dto.DelRegisterDto;
import com.ym.mec.biz.dal.dto.MusicGroupSubjectGoodsAndInfoDto;
import com.ym.mec.biz.dal.dto.StudentAddDto;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalender;
import com.ym.mec.biz.dal.entity.MusicGroupPaymentCalenderCourseSettings;
import com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.biz.dal.enums.ClassGroupTypeEnum;
import com.ym.mec.biz.dal.page.StudentPreRegistrationQueryInfo;
import com.ym.mec.biz.dal.page.StudentRegistrationQueryInfo;
import com.ym.mec.biz.service.MusicGroupSubjectPlanService;
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;

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

    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupSubjectPlanService musicGroupSubjectPlanService;
    @Autowired
    private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;
    @Autowired
    private MusicGroupPaymentCalenderCourseSettingsDao musicGroupPaymentCalenderCourseSettingsDao;
    @Autowired
    private StudentPreRegistrationService studentPreRegistrationService;

    @ApiOperation(value = "乐团预报名列表分页查询")
    @GetMapping("/queryPreApplyList")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/queryPreApplyList')")
    public Object queryPreApplyList(StudentPreRegistrationQueryInfo queryInfo) {
        return succeed(studentPreRegistrationService.queryListForPage(queryInfo));
    }

    @ApiOperation(value = "乐团添加学员")
    @PostMapping("/insertStudent")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/insertStudent')")
    public Object add(@RequestBody StudentAddDto studentAddDto) throws Exception {
        return succeed(studentRegistrationService.insertStudent(studentAddDto));
    }

    @ApiOperation(value = "修改学生报名信息")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/update')")
    public Object update(StudentRegistration studentRegistration) {
        studentRegistration.setUpdateTime(new Date());
        studentRegistrationService.update(studentRegistration);
        return succeed();
    }

    @ApiOperation(value = "调剂学生报名专业")
    @PostMapping("/batchUpdateSubject")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/batchUpdateSubject')")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "学生编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "subId", value = "声部编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "Integer")})
    public Object batchUpdateSubject(Integer userId, Integer subId, String musicGroupId) throws Exception {
        if (null == userId || subId == null || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(studentRegistrationService.batchUpdateSubject(userId, subId, musicGroupId));
    }

    @ApiOperation(value = "乐团【报名中、缴费中】 学生详情列表分页查询")
    @GetMapping("/queryStudentApplyDetail")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/queryStudentApplyDetail')")
    public Object queryStudentApplyDetail(StudentRegistrationQueryInfo queryInfo) {
        return succeed(studentRegistrationService.queryStudentDetailPage(queryInfo));
    }

    @ApiOperation(value = "学生报名缴费金额详情")
    @GetMapping("/queryFeeDetail")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/queryFeeDetail')")
    @ApiImplicitParams({@ApiImplicitParam(name = "studentId", value = "学生编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public Object queryFeeDetail(String studentId, String musicGroupId) {
        if (StringUtils.isEmpty(studentId) || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(studentRegistrationService.queryFeeDetail(Integer.parseInt(studentId), musicGroupId));
    }

    @ApiOperation(value = "获取未分班的学生")
    @GetMapping("/getNoClassStu")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getNoClassStu')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "actualSubjectId", value = "科目(声部)id,多个逗号分开", required = true, dataType = "String")})
    public Object getNoClassStuBySubjectId(String musicGroupId, String actualSubjectId) {
        return succeed(studentRegistrationService.getNoClassStuBySubjectId(musicGroupId, actualSubjectId));
    }

    @ApiOperation(value = "获取班级学生")
    @GetMapping("/getClassStu")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getClassStu')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "classGroupId", value = "班级id", required = true, dataType = "int")})
    public Object getClassStu(String musicGroupId, int classGroupId) {
        return succeed(studentRegistrationService.findClassGroupStu(musicGroupId, classGroupId));
    }

    @ApiOperation(value = "获取乐团(声部)的学生")
    @GetMapping("/getMusicGroupStu")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getMusicGroupStu')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "actualSubjectId", value = "科目(声部)id,", required = false, dataType = "String")})
    public HttpResponseResult getMusicGroupStu(String musicGroupId, String actualSubjectId) {
        return succeed(studentRegistrationService.findMusicGroupStudent(musicGroupId, actualSubjectId));
    }

    @ApiOperation(value = "获取乐团未分班的学生")
    @GetMapping("/getMusicGroupNoClassStu")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getMusicGroupNoClassStu')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "actualSubjectId", value = "科目(声部)id,", required = false, dataType = "int")})
    public HttpResponseResult getMusicGroupNoClassStu(String musicGroupId, Integer actualSubjectId) {
        return succeed(studentRegistrationService.findMusicGroupNoClassGroupStudent(musicGroupId, actualSubjectId));
    }

    @ApiOperation(value = "开启缴费(单独学生)")
    @PostMapping("/openPayment")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/openPayment')")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "报名id（不是userId）,逗号分隔", required = true, dataType = "String")})
    public HttpResponseResult openPayment(String ids) throws IOException {
        return succeed(studentRegistrationService.openPayment(ids));
    }

    @ApiOperation(value = "获取乐团(声部)的(没有某种班级类型)学生")
    @GetMapping("/getMusicGroupStuNoClassType")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getMusicGroupStuNoClassType')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "type", value = "类型(MIX，HIGH)", required = true, dataType = "ClassGroupTypeEnum"),
            @ApiImplicitParam(name = "actualSubjectId", value = "科目(声部)id,", required = false, dataType = "String")})
    public HttpResponseResult getMusicGroupStuNoClassType(String musicGroupId, Long classGroupId, ClassGroupTypeEnum type, String actualSubjectId) {
        return succeed(studentRegistrationService.findMusicGroupStuNoClassType(musicGroupId, classGroupId, type, actualSubjectId));
    }

    @ApiOperation(value = "获取学生报名缴费详情")
    @GetMapping("/getApplyDetail")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getApplyDetail')")
    @ApiImplicitParams({@ApiImplicitParam(name = "studentId", value = "学生编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult<List<StudentPaymentOrderDetail>> getApplyDetail(Integer studentId, String musicGroupId) {
        if (studentId == null || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(studentRegistrationService.getStudentApplyDetail(studentId, musicGroupId));
    }

    @ApiOperation(value = "获取乐团声部费用信息及乐器和辅件")
    @GetMapping("/getSubjectGoodsAndInfo")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getSubjectGoodsAndInfo')")
    @ApiImplicitParams({@ApiImplicitParam(name = "subjectId", value = "声部信息", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult<MusicGroupSubjectGoodsAndInfoDto> getSubjectGoodsAndInfo(Integer subjectId, String musicGroupId, Long calenderId) {
        if (subjectId == null || StringUtils.isEmpty(musicGroupId) || calenderId == null) {
            return failed("参数校验异常");
        }
        MusicGroupSubjectGoodsAndInfoDto subjectGoodsAndInfo = musicGroupSubjectPlanService.getSubjectGoodsAndInfo(musicGroupId, subjectId);

        MusicGroupPaymentCalender musicGroupRegCalender = musicGroupPaymentCalenderDao.get(calenderId);
        if (musicGroupRegCalender != null && musicGroupRegCalender.getPayUserType().equals(MusicGroupPaymentCalender.PayUserType.STUDENT)) {
            List<MusicGroupPaymentCalenderCourseSettings> courseSettings = musicGroupPaymentCalenderCourseSettingsDao.getWithPaymentCalender(musicGroupRegCalender.getId());
            musicGroupRegCalender.setMusicGroupPaymentCalenderCourseSettingsList(courseSettings);
            subjectGoodsAndInfo.setMusicGroupPaymentCalender(musicGroupRegCalender);
        }
        return succeed(subjectGoodsAndInfo);
    }

    @ApiOperation(value = "获取班级课程的价值")
    @GetMapping("/getClassGroupCourseExpectPrice")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getClassGroupCourseExpectPrice')")
    public Object getClassGroupCourseExpect(List<Integer> classGroupIds) {
        return succeed(studentRegistrationService.getClassGroupCourseExpectPrice(classGroupIds));
    }

    @ApiOperation(value = "删除报名的学生")
    @PostMapping("/del")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/del')")
    public HttpResponseResult<Boolean> del(Long id) {
        StudentRegistration studentRegistration = studentRegistrationService.get(id);
        if (studentRegistration == null) {
            return failed("删除的报名信息不存在");
        }
        return succeed(studentRegistrationService.delReg(id));
    }


    @ApiOperation(value = "批量删除报名的学生")
    @PostMapping("/batchDel")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/batchDel')")
    public HttpResponseResult<Boolean> batchDel(@RequestBody DelRegisterDto delRegisterDto) {
        return succeed(studentRegistrationService.batchDelRegs(delRegisterDto.getMusicGroupId(), delRegisterDto.getUserIds()));
    }


    @ApiOperation(value = "获取乐团学生的退费信息")
    @GetMapping("/getMusicGroupStuReBack")
    @PreAuthorize("@pcs.hasPermissions('studentRegistration/getMusicGroupStuReBack')")
    @ApiImplicitParams({@ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult getMusicGroupStuReBack(String musicGroupId) {
        return succeed(studentRegistrationService.getMusicGroupStuReBack(musicGroupId));
    }

}
