package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.dto.CloseMusicGroupDto;
import com.ym.mec.biz.dal.dto.MusicGroupRegsDto;
import com.ym.mec.biz.dal.entity.StudentRegistration;
import com.ym.mec.common.entity.HttpResponseResult;

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

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.SubFeeSettingDto;
import com.ym.mec.biz.dal.dto.UpdateExpectedNumDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.page.MusicGroupQueryInfo;
import com.ym.mec.biz.service.MusicGroupService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.exception.BizException;

@RequestMapping("musicGroup")
@Api(tags = "乐团服务")
@RestController
public class MusicGroupController extends BaseController {

	@Autowired
	private MusicGroupService musicGroupService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private EmployeeDao employeeDao;

	@ApiOperation(value = "修改乐团基本信息")
	@PostMapping("/update")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/update')")
	public Object update(@RequestBody SubFeeSettingDto subFeeSettingDto) throws Exception {
		musicGroupService.updateBaseInfo(subFeeSettingDto);
		return succeed();
	}

	@ApiOperation(value = "获取乐团调整--基本信息数据")
	@PostMapping("/findMusicGroupBasicInfo")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/findMusicGroupBasicInfo')")
	public Object findMusicGroupBasicInfo(String musicGroupId) {
		if (StringUtils.isEmpty(musicGroupId)) {
			return failed("参数校验失败");
		}
		return succeed(musicGroupService.findMusicGroupBasicInfo(musicGroupId));
	}

	@ApiOperation(value = "获取乐团调整--声部信息数据")
	@GetMapping("/findMusicGroupSubjectInfo")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/findMusicGroupSubjectInfo')")
	public Object findMusicGroupSubjectInfo(String musicGroupId) {
		if (StringUtils.isEmpty(musicGroupId)) {
			return failed("参数校验失败");
		}
		return succeed(musicGroupService.findMusicGroupSubjectInfo(musicGroupId));
	}

	@ApiOperation(value = "修改乐团声部信息")
	@PostMapping("/updateSubjectInfo")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/updateSubjectInfo')")
	public Object updateSubjectInfo(@RequestBody SubFeeSettingDto subFeeSettingDto) throws Exception {
		musicGroupService.updateSubjectInfo(subFeeSettingDto);
		return succeed();
	}

	@ApiOperation(value = "修改乐团计划招生人数")
	@PostMapping("/updateExpectedStudentNum")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/updateExpectedStudentNum')")
	public Object updateExpectedStudentNum(@RequestBody List<UpdateExpectedNumDto> updateExpectedNumDtos){
		if(updateExpectedNumDtos == null || updateExpectedNumDtos.size() <= 0){
			return failed("参数校验错误");
		}
		musicGroupService.updateExpectedStudentNum(updateExpectedNumDtos);
		return succeed();
	}

	@ApiOperation(value = "根据乐团编号查询乐团详情")
	@GetMapping("/get")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/get')")
	public Object get(String musicGroupId) {
		return succeed(musicGroupService.getMusicGroupDetail(musicGroupId));
	}

	@ApiOperation(value = "分页查询乐团列表")
	@GetMapping("/queryPage")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/queryPage')")
	public Object queryPage(MusicGroupQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		if(!sysUser.getIsSuperAdmin()){
			Employee employee = employeeDao.get(sysUser.getId());
			if (StringUtils.isEmpty(queryInfo.getOrganId())) {
				queryInfo.setOrganId(employee.getOrganIdList());
			}else if(StringUtils.isEmpty(employee.getOrganIdList())){
				return failed("用户所在分部异常");
			}else {
				List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
				if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
					return failed("非法请求");
				}
			}
		}
		return succeed(musicGroupService.queryMusicGroupPage(queryInfo));
	}

	@ApiOperation(value = "新增乐团  建团申请数据提交")
	@PostMapping("/createGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/createGroup')")
	public Object createGroup(@RequestBody SubFeeSettingDto subFeeSettingDto) throws Exception {
		return succeed(musicGroupService.createGroup(subFeeSettingDto));
	}

	@ApiOperation(value = "乐团筹备结束后，3.0乐团推送小班课缴费信息")
	@PostMapping("/pushMessage")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/pushMessage')")
	public Object pushMessage(String musicGroupId,String classGroupIds) throws Exception {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed("获取用户信息失败");
		}
		if(StringUtils.isEmpty(musicGroupId)){
			throw new BizException("参数校验错误");
		}
		return succeed(	musicGroupService.pushMessage(musicGroupId,classGroupIds));
	}

	@ApiOperation(value = "根据乐团ids获取乐团列表")
	@PostMapping("/queryListByIds")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/queryListByIds')")
	public Object queryListByIds(@RequestBody Set<String> musicGroupIds) {
		if (musicGroupIds.isEmpty()) {
			return failed("参数校验失败");
		}
		return succeed(musicGroupService.queryListByIds(musicGroupIds));
	}

	@ApiOperation(value = "开启缴费（报名中 -->  缴费中）")
	@PostMapping("/openPay")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/openPay')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "expireDate", value = "截止缴费日期", required = true, dataType = "String") })
	public Object openPay(String musicGroupId, String expireDate) throws Exception {
		return succeed(musicGroupService.openPay(musicGroupId, expireDate));
	}

	@ApiOperation(value = "确认开团（缴费中(报名中) -->  筹备中）")
	@PostMapping("/found")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/found')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object found(String musicGroupId) throws Exception {
		musicGroupService.musicGroupFound(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "提交审核（草稿 -> 审核中）")
	@PostMapping("/audit")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/audit')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object audit(String musicGroupId) throws Exception {
		musicGroupService.musicGroupAudit(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "审核失败（审核中 -> 审核失败）")
	@PostMapping("/auditFailed")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/auditFailed')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "memo", value = "memo", required = true, dataType = "String") })
	public Object auditFailed(String musicGroupId,String memo) throws Exception {
		musicGroupService.musicGroupAuditFailed(musicGroupId,memo);
		return succeed();
	}

	@ApiOperation(value = "审核通过（审核中 -> 预报名）")
	@PostMapping("/auditSuccess")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/auditSuccess')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
	public Object auditSuccess(String musicGroupId) throws Exception {
		musicGroupService.musicGroupAuditSuccess(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "开启乐团（筹备中 -->  进行中）")
	@PostMapping("/action")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/action')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object action(String musicGroupId) throws Exception {
		musicGroupService.musicGroupAction(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "取消乐团")
	@PostMapping("/cancelMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/cancelMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object cancelMusicGroup(String musicGroupId) {
		musicGroupService.cancelMusicGroup(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "删除乐团")
	@PostMapping("/deleteMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/deleteMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object deleteMusicGroup(String musicGroupId) {
		musicGroupService.deleteMusicGroup(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "暂停乐团")
	@PostMapping("/pauseMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/pauseMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object pauseMusicGroup(String musicGroupId) {
		musicGroupService.pauseMusicGroup(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "恢复乐团")
	@PostMapping("/resumeMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/resumeMusicGroup')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String") })
	public Object resumeMusicGroup(String musicGroupId) {
		musicGroupService.resumeMusicGroup(musicGroupId);
		return succeed();
	}

	@ApiOperation(value = "延长缴费时间")
	@PostMapping("/extensionPayment")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/extensionPayment')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "expireDate", value = "延长缴费的截止日期", required = true, dataType = "Date") })
	public Object extensionPayment(String musicGroupId, Date expireDate) {
		musicGroupService.extensionPaymentExpireDate(musicGroupId, expireDate);
		return succeed();
	}

	@ApiOperation(value = "延长报名时间")
	@PostMapping("/extensionApplyExpireDate")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/extensionApplyExpireDate')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "expireDate", value = "延长报名的截止日期", required = true, dataType = "Date") })
	public Object extensionApplyExpireDate(String musicGroupId, Date expireDate) {
		musicGroupService.extensionApplyExpireDate(musicGroupId, expireDate);
		return succeed();
	}

	@ApiOperation(value = "延长预报名时间")
	@PostMapping("/extensionPreApplyExpireDate")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/extensionApplyExpireDate')")
	@ApiImplicitParams({ @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String"),
			@ApiImplicitParam(name = "expireDate", value = "延长预报名的截止日期", required = true, dataType = "Date") })
	public Object extensionPreApplyExpireDate(String musicGroupId, Date expireDate) {
		musicGroupService.extensionPreApplyExpireDate(musicGroupId, expireDate);
		return succeed();
	}

	@ApiOperation("获取分部乐团列表名称")
	@GetMapping(value = "/queryOrganMusicInfos")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/queryOrganMusicInfos')")
	public Object queryOrganMusicInfos(Integer organId) {
		return succeed(musicGroupService.queryOrganMusicInfos(organId));
	}


	@ApiOperation(value = "乐团添加报名学员")
	@PostMapping("/addMusicGroupRegs")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/addMusicGroupRegs')")
	public HttpResponseResult<List<StudentRegistration>> addMusicGroupRegs(@RequestBody MusicGroupRegsDto musicGroupRegsDto) throws Exception {
		if(musicGroupRegsDto.getRegisterIds().size() <=0){
			return failed("请选择要添加的学员");
		}
		return succeed(musicGroupService.addMusicGroupRegs(musicGroupRegsDto.getMusicGroupId(),musicGroupRegsDto.getRegisterIds()));
	}

	@ApiOperation(value = "关闭乐团")
	@PostMapping("/closeMusicGroup")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/closeMusicGroup')")
	public HttpResponseResult<Boolean> closeMusicGroup(@RequestBody CloseMusicGroupDto closeMusicGroupDto) {
		return succeed(musicGroupService.closeMusicGroup(closeMusicGroupDto));
	}

	@ApiOperation(value = "完成预报名")
	@PostMapping("/finishPreApply")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/finishPreApply')")
	public HttpResponseResult<Boolean> finishPreApply(String musicGroupId,boolean isCheckStudentNum) {
		return musicGroupService.finishPreApply(musicGroupId, isCheckStudentNum) ? succeed() : failed();
	}

	@ApiOperation(value = "发送家长会通知")
	@PostMapping("/sendParentMeetingNotice")
	@PreAuthorize("@pcs.hasPermissions('musicGroup/sendParentMeetingNotice')")
	public HttpResponseResult<Boolean> sendParentMeetingNotice(String musicGroupId, Date meetingDate, String address) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed(HttpStatus.FORBIDDEN, "用户信息获取失败");
		}
		return musicGroupService.sendParentMeetingNotice(sysUser.getId(), musicGroupId, meetingDate, address) ? succeed() : failed();
	}

}
