package com.ssj.aijia.schoolmaster.controller.v1;

import com.ssj.service.conch.schoolmaster.request.*;
import com.ssj.service.conch.schoolmaster.service.SchoolService;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.security.annotation.ModifySecurity;
import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.Constant;
import com.ssj.framework.core.util.ResponseConstant;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.service.conch.schoolmaster.dto.*;
import com.ssj.service.conch.schoolmaster.v1.request.*;
import com.ssj.service.conch.schoolmaster.v1.service.SchoolV1Service;
import com.ssj.service.weixin.library.service.IBookManagerService;
import com.ssj.service.conch.schoolmaster.v1.dto.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author  ZhangCaibao 
 * @version 2018年4月12日 下午9:36:15 
 * 类说明 
 */
@Api(value = "一对一校长端相关的接口V1", tags = "一对一校长端相关的接口V1")
@RestController
@RequestMapping("/api/school/school/V1")
public class SchoolV1Controller extends BaseController {
	@Autowired
	private TokenManager tokenManager;

	@Reference(version = "1.0" ,timeout = 60000)
	private SchoolService schoolService;
	@Reference(version = "1.0" ,timeout = 60000)
	private SchoolV1Service schoolV1Service;
	
	@Reference(version = "1.0" ,timeout = 60000)
    private IBookManagerService managerService;

    @ModifySecurity
	@RequestMapping(value = "/swipeBinding", method = RequestMethod.POST)
	@ApiOperation(value = "校长端扫卡绑定", httpMethod = "POST", notes = "校长端扫卡绑定")
	public ResponseEntity swipeBinding(@RequestParam(name = "accessToken") String accessToken,
                                       @RequestBody SwipeBindingRequest req) {
		String masterId = tokenManager.getUserId(accessToken);
		ResponseEntity responseEntity = null;
		if (StringUtils.isEmpty(masterId) || StringUtils.isEmpty(req.getTeacherCodeUrl())){
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_000, "不存在校长id，或者老师二维码连接为空， master_id="
					+masterId + ", codeUrl=" + req.getTeacherCodeUrl() + ", accessToken=" + accessToken);
		} else {
			responseEntity = schoolService.swipeBinding(masterId, req);
		}
		return responseEntity;
	}

	/******************************************************我的信息*********************************************************/
	@RequestMapping(value = "/myInfo", method = RequestMethod.POST)
	@ApiOperation(value = "个人信息获取", httpMethod = "POST", notes = "个人信息获取")
	public ResponseEntity myInfo(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolService.myInfo(masterId);
		} catch (Exception e) {
            responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/systemMessageList", method = RequestMethod.POST)
	@ApiOperation(value = "获取系统消息列表", httpMethod = "POST", notes = "获取系统消息列表")
	public ResponseEntity systemMessageList(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String data = "{\"title\":\"绑定成功\",\"content\":\"今天天气真好！\",\"sendTime\":\"18-02-26\"}";
			responseEntity.success(JSON.parse(data), "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	/******************************************************课务管理*********************************************************/


	@RequestMapping(value = "/courseManagementInfo", method = RequestMethod.POST)
	@ApiOperation(value = "获取课务管理数据列表", httpMethod = "POST", notes = "获取课务管理数据列表")
	public ResponseEntity courseManagementInfo(@RequestParam(name = "accessToken") String accessToken,
                                               @RequestBody CourseManagementInfoRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			CourseManagementInfoDto data = schoolService.courseManagementInfo(masterId);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/batchSendUrge", method = RequestMethod.POST)
	@ApiOperation(value = "一键完成催促", httpMethod = "POST", notes = "一键完成催促")
	public ResponseEntity batchSendUrge(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			schoolService.pushAllAffairs(masterId);
			responseEntity.success(ResponseConstant.CODE_999, "催促成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/sendUrge", method = RequestMethod.POST)
	@ApiOperation(value = "单独催促老师", httpMethod = "POST", notes = "单独催促老师")
	public ResponseEntity sendUrge(@RequestParam(name = "accessToken") String accessToken,
                                   @RequestBody SendUrgeRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			if (StringUtils.isEmpty(request.getBizId()) || StringUtils.isEmpty(request.getBizType())){
				responseEntity.success(ResponseConstant.CODE_999, "传入参数存在空值,bizid=" + request.getBizId() + ", biztype=" + request.getBizType());
			} else {
				boolean result = schoolService.pushTeacher(request.getBizId(), request.getBizType(), masterId);
				if (result){
					responseEntity.success(ResponseConstant.CODE_999, "催促成功");
				} else {
					responseEntity.failure(ResponseConstant.CODE_999, "催促失败");
				}
			}

		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/changeTeacher", method = RequestMethod.POST)
	@ApiOperation(value = "更换老师", httpMethod = "POST", notes = "更换老师")
	@ModifySecurity
	public ResponseEntity changeTeacher(@RequestParam(name = "accessToken") String accessToken,
                                        @RequestBody ChangeTeacherRequest request) {
		logger.info("请求参数req={}", JSONObject.toJSON(request));
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolService.changeTeacher(request.getBizId(), request.getTeacherId());
		} catch (Exception e) {
            responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/confirmAdjustSchoolHours", method = RequestMethod.POST)
	@ApiOperation(value = "同意（确定）调课申请", httpMethod = "POST", notes = "同意（确定）调课申请")
	@ModifySecurity
	public ResponseEntity confirmAdjustSchoolHours(@RequestParam(name = "accessToken") String accessToken,
                                                   @RequestBody ConfirmAdjustSchoolHoursRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String result = schoolService.confirmAdjustSchoolHours(request.getBizId());
			if (StringUtils.isNotBlank(result)) {
				responseEntity.failure(ResponseConstant.CODE_000, result);
				return responseEntity;
			}
			responseEntity.success();
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/getStudentCourseData", method = RequestMethod.POST)
	@ApiOperation(value = "获取当前课程情况", httpMethod = "POST", notes = "获取当前课程情况")
	public ResponseEntity getStudentCourseData(@RequestParam(name = "accessToken") String accessToken,
                                               @RequestBody ConfirmAdjustSchoolHoursRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			List<CourseDateDataDto> list = schoolService.findStudentCourseData(request.getBizId(), request.getTime());
			Map<String, Object> data = new HashMap<>();
			data.put("list", list);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
			logger.error("异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/adjustSchoolHours", method = RequestMethod.POST)
	@ApiOperation(value = "校长调整上课时间", httpMethod = "POST", notes = "校长调整上课时间")
	@ModifySecurity
	public ResponseEntity adjustSchoolHours(@RequestParam(name = "accessToken") String accessToken,
                                            @RequestBody AdjustSchoolHoursRequest request) {
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolService.adjustSchoolHours(request);
		} catch (Exception e) {
			logger.error("异常", e);
            responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/teacherCoursePlanDetail", method = RequestMethod.POST)
	@ApiOperation(value = "获取当前老师排课状况", httpMethod = "POST", notes = "获取当前老师排课状况")
	public ResponseEntity teacherCoursePlanDetail(@RequestParam(name = "accessToken") String accessToken,
                                                  @RequestBody TeacherCoursePlanDetailRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			List<Map<String, String>> result = schoolService.teacherCoursePlanDetail(request);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", result);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
			logger.error("异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/getTeacherCourseTimeDetail", method = RequestMethod.POST)
	@ApiOperation(value = "获取当前老师排课状况V2", httpMethod = "POST", notes = "获取当前老师排课状况V2")
	public ResponseEntity getTeacherCourseTimeDetail(@RequestParam(name = "accessToken") String accessToken,
                                                     @RequestBody TeacherCoursePlanDetailRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			List<Map<String, String>> result = schoolService.getTeacherCourseTimeDetail(request);
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", result);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
			logger.error("异常信息", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/reviewRefundRequest", method = RequestMethod.POST)
	@ApiOperation(value = "退款申请处理", httpMethod = "POST", notes = "退款申请处理")
	@ModifySecurity
	public ResponseEntity reviewRefundRequest(@RequestParam(name = "accessToken") String accessToken,
                                              @RequestBody ReviewRefundRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			schoolService.reviewRefund(request.getBizId(), request.getResult(),request.getRefuseReason());
			responseEntity.success();
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/tutorshipFeedbackHistoryList", method = RequestMethod.POST)
	@ApiOperation(value = "查看往期反馈记录列表", httpMethod = "POST", notes = "查看往期反馈记录列表")
	public ResponseEntity tutorshipFeedbackHistoryList(@RequestParam(name = "accessToken") String accessToken,
                                                       @RequestBody LessonFeedbackRequest request) {
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolService.lessonFeedbackHistory(request.getPlanId());
		} catch (Exception e) {
            responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

//	@RequestMapping(value = "/confirmAdjustTeacher", method = RequestMethod.POST)
//	@ApiOperation(value = "同意（确定）换老师申请", httpMethod = "POST", notes = "同意（确定）换老师申请")
//	@ModifySecurity
//	public ResponseEntity confirmAdjustTeacher(@RequestParam(name = "accessToken") String accessToken,
//											   @RequestBody ChangeTeacherRequest request) {
//		ResponseEntity responseEntity = null;
//		try {
//			responseEntity = schoolService.confirmAdjustTeacher(request);
//		} catch (Exception e) {
//			responseEntity = new ResponseEntity();
//			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
//		}
//		return responseEntity;
//	}


	/******************************************************人员管理*********************************************************/

	@RequestMapping(value = "/myStudentList", method = RequestMethod.POST)
	@ApiOperation(value = "我的学生列表", httpMethod = "POST", notes = "我的学生列表")
	public ResponseEntity myStudentList(@RequestParam(name = "accessToken") String accessToken,
                                        @RequestBody PageRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			Pageable pageable = initPage(request.getPageNo(), request.getPageSize());
			responseEntity.success(schoolService.myStudentList(masterId, request.getMaxCreateTime(), pageable), "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/myTeacherList", method = RequestMethod.POST)
	@ApiOperation(value = "我的老师列表", httpMethod = "POST", notes = "我的老师列表")
	public ResponseEntity myTeacherList(@RequestParam(name = "accessToken") String accessToken,
                                        @RequestBody PageRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			Pageable pageable = initPage(request.getPageNo(), request.getPageSize());
			responseEntity.success(schoolService.myTeacherList(masterId, request.getMaxCreateTime(), pageable), "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/searchMyStudentList", method = RequestMethod.POST)
	@ApiOperation(value = "搜索学员", httpMethod = "POST", notes = "搜索学员")
	public ResponseEntity searchMyStudentList(@RequestParam(name = "accessToken") String accessToken,
                                              @RequestBody SearchKeyRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			List<StudentDto> list = schoolService.searchMyStudentList(masterId, request.getSearchKey());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", list);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/searchMyTeacherList", method = RequestMethod.POST)
	@ApiOperation(value = "搜索老师", httpMethod = "POST", notes = "搜索老师")
	public ResponseEntity searchMyTeacherList(@RequestParam(name = "accessToken") String accessToken,
                                              @RequestBody SearchKeyRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			List<TeacherDto> list = schoolService.searchMyTeacherList(masterId, request.getSearchKey());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", list);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
            logger.error("异常", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/teacherDetail", method = RequestMethod.POST)
	@ApiOperation(value = "老师详情", httpMethod = "POST", notes = "老师详情")
	public ResponseEntity teacherDetail(@RequestParam(name = "accessToken") String accessToken,
                                        @RequestBody SearchTeacherRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
		    if (StringUtils.isEmpty(request.getTeacherId())){
		        responseEntity.success(ResponseConstant.CODE_999, "传参teacherId为空");
            } else {
                responseEntity.success(schoolService.teacherDetail(request.getTeacherId(), request.getTeacherType()), "成功");
            }
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/teacherInfo", method = RequestMethod.POST)
	@ApiOperation(value = "老师个人简历", httpMethod = "POST", notes = "老师详情")
	public ResponseEntity teacherInfo(@RequestParam(name = "accessToken") String accessToken,
                                      @RequestBody SearchTeacherInfoReq req) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			if (StringUtils.isEmpty(req.getTeacherId())){
				responseEntity.success(ResponseConstant.CODE_999, "传参teacherId为空");
			} else {
				responseEntity.success(schoolService.teacherInfo(req.getTeacherId()), "成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/saveTeacherInfo", method = RequestMethod.POST)
	@ApiOperation(value = "完善老师信息", httpMethod = "POST", notes = "完善老师信息")
	public ResponseEntity saveTeacherInfo(@RequestParam(name = "accessToken") String accessToken,
                                          @RequestBody TeacherInfoRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			schoolService.saveTeacherInfo(request);
			responseEntity.success();
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/saveStudentInfo", method = RequestMethod.POST)
	@ApiOperation(value = "完善学员信息", httpMethod = "POST", notes = "完善学员信息")
	public ResponseEntity saveStudentInfo(@RequestParam(name = "accessToken") String accessToken,
                                          @RequestBody StudentInfoRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			schoolService.saveStudentInfo(request);
			responseEntity.success();
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/teacherListForStudent", method = RequestMethod.POST)
	@ApiOperation(value = "查看学生授课老师列表", httpMethod = "POST", notes = "查看学生授课老师列表")
	public ResponseEntity teacherListForStudent(@RequestParam(name = "accessToken") String accessToken,
                                                @RequestBody ForStudentRequest request) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolService.teacherListForStudent(masterId,request);
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/courseListForStudent", method = RequestMethod.POST)
	@ApiOperation(value = "查看学生课程表", httpMethod = "POST", notes = "查看学生课程表")
	public ResponseEntity courseListForStudent(@RequestParam(name = "accessToken") String accessToken,
                                               @RequestBody StudentRequest request) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolService.courseListForStudent(masterId, request.getStudentId());
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/studentInfo", method = RequestMethod.POST)
	@ApiOperation(value = "查看学生资料", httpMethod = "POST", notes = "查看学生资料")
	public ResponseEntity studentInfo(@RequestParam(name = "accessToken") String accessToken,
                                      @RequestBody StudentRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			//根据vipid查询小孩
            if (StringUtils.isEmpty(request.getStudentId())){
                responseEntity.success(ResponseConstant.CODE_999, "传入的学生id为空");
            } else {
                StudentInfoDto dto = schoolService.findStudentInfo(request.getStudentId());
                responseEntity.success(dto, "成功");
            }
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/orderListForStudent", method = RequestMethod.POST)
	@ApiOperation(value = "查看学生订单", httpMethod = "POST", notes = "查看学生订单")
	public ResponseEntity orderListForStudent(@RequestParam(name = "accessToken") String accessToken,
                                              @RequestBody StudentRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);
			List<OrderDto> list = schoolService.orderListForStudent(masterId, request.getStudentId());
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("list", list);
			responseEntity.success(data, "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/teacherListForSystemMatch", method = RequestMethod.POST)
	@ApiOperation(value = "获取系统自动匹配的推荐老师列表", httpMethod = "POST", notes = "获取系统自动匹配的推荐老师列表")
	public ResponseEntity teacherListForSystemMatch(@RequestParam(name = "accessToken") String accessToken,
                                                    @RequestBody ForSystemMatchRequest request) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolService.teacherListForSystemMatch(masterId, request);
		} catch (Exception e) {
			logger.error("异常日志", e);
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}


	@RequestMapping(value = "/estimateOrderMoney", method = RequestMethod.POST)
	@ApiOperation(value = "预估订单价格", httpMethod = "POST", notes = "预估订单价格")
	public ResponseEntity estimateOrderMoney(@RequestParam(name = "accessToken") String accessToken,
                                             @RequestBody EstimateOrderRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			//如果校长是私塾家VIP体验合作馆则预估价格为0
			String masterId = tokenManager.getUserId(accessToken);
			TbLibManager master = managerService.findByUserId(masterId, Constant.SCHOOL_MASTER_TYPE);
			if("844abb1c-0599-435d-b125-3a6e65411bf7".equals(master.getLibId())||
					"f44b335b-126f-4f78-855a-501656a6ad91".equals(master.getLibId())||
					master.getLibId().equals("cdf451d8-4764-4522-a591-64531cef0394")) {
				Map<String, Object> data = new HashMap<String, Object>();
				data.put("orderMoney", "0");
				data.put("coursePrice", "0");
				responseEntity.success(data, "成功");
				return responseEntity;
			}
			responseEntity = schoolService.computeOrderAmount(request.getStudentId(),request.getCourseCount(),request.getTeacherType());
		} catch (Exception e) {
			logger.error("异常日志", e);
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/commitOrder", method = RequestMethod.POST)
	@ApiOperation(value = "提交订单", httpMethod = "POST", notes = "提交订单")
	@ModifySecurity
	public ResponseEntity commitOrder(@RequestParam(name = "accessToken") String accessToken,
                                      @RequestBody CommitOrderRequest request) {
		ResponseEntity responseEntity = null;
		String userId = tokenManager.getUserId(accessToken);
		try {
			responseEntity = schoolService.commitOrder(request, userId);
		} catch (Exception e) {
			//抛异常将1分钟不能重复下单key置为失效
			tokenManager.delString(userId+request.getStudentId()+"VALIDATE_TIME");
			responseEntity = new ResponseEntity();
			logger.error("异常日志", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/refundSchedule", method = RequestMethod.POST)
	@ApiOperation(value = "退款进度查询", httpMethod = "POST", notes = "退款进度查询")
	public ResponseEntity refundSchedule(@RequestParam(name = "accessToken") String accessToken,
                                         @RequestBody RefundRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			RefundScheduleDto data = schoolService.refundSchedule(request.getOrderId());
			responseEntity.success(data, "成功");
		} catch (Exception e) {
            e.printStackTrace();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/giveLessonsServiceDetailForStudent", method = RequestMethod.POST)
	@ApiOperation(value = "查看X学生一对一产品(授课)服务详情", httpMethod = "POST", notes = "查看X学生一对一产品(授课)服务详情")
	public ResponseEntity giveLessonsServiceDetailForStudent(@RequestParam(name = "accessToken") String accessToken,
                                                             @RequestBody StudentRequest request) {
		ResponseEntity responseEntity = null;
		try {
			String userId = tokenManager.getUserId(accessToken);
			responseEntity = schoolService.giveLessonsServiceDetailForStudent(userId, request.getStudentId(),request.getSubjectName());
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/homeworkLibraryServiceDetailForStudent", method = RequestMethod.POST)
	@ApiOperation(value = "查看X学生作业吧产品务详情", httpMethod = "POST", notes = "查看X学生作业吧产品务详情")
	public ResponseEntity homeworkLibraryServiceDetailForStudent(@RequestParam(name = "accessToken") String accessToken,
                                                                 @RequestBody StudentRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			if (StringUtils.isNotEmpty(request.getStudentId())){
				responseEntity.success(schoolService.studentZybDetail(request.getStudentId()), "成功");
			} else {
				responseEntity.failure("学生id不能为空");
			}
		} catch (Exception e) {
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	
	/******************************************************2018-9-30 新增接口*********************************************************/
	@RequestMapping(value = "/refuseAdjustSchoolHours", method = RequestMethod.POST)
	@ApiOperation(value = "拒绝调课申请", httpMethod = "POST", notes = "拒绝调课申请")
	public ResponseEntity refuseAdjustSchoolHours(@RequestParam(name = "accessToken") String accessToken, @RequestBody RefuseAdjustRequest request) {
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolService.refuseAdjustSchoolHours(request.getBizId(),request.getRefuseReason());
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/refuseAdjustTeacher", method = RequestMethod.POST)
	@ApiOperation(value = "拒绝换老师", httpMethod = "POST", notes = "拒绝换老师")
	public ResponseEntity refuseAdjustTeacher(@RequestParam(name = "accessToken") String accessToken, @RequestBody RefuseAdjustRequest request) {
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolService.refuseAdjustTeacher(request.getBizId(),request.getRefuseReason());
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	
	//**************************************校长端V1接口***********************************/
	//*******************************课务管理模块***************************/
	@RequestMapping(value = "/homeworkManager", method = RequestMethod.POST)
	@ApiOperation(value = "作业模块", httpMethod = "POST", notes = "作业模块")
	public ResponseEntity homeworkManager(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolV1Service.homeworkManager(masterId);
		} catch (Exception e) {
			logger.error("异常日志：", e);
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/problemManager", method = RequestMethod.POST)
	@ApiOperation(value = "难题模块", httpMethod = "POST", notes = "难题模块")
	public ResponseEntity problemManager(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolV1Service.problemManager(masterId);
		} catch (Exception e) {
			logger.error("异常日志：", e);
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/lessonServiceManager", method = RequestMethod.POST)
	@ApiOperation(value = "一对一模块", httpMethod = "POST", notes = "一对一模块")
	public ResponseEntity lessonServiceManager(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = null;
		try {
			String masterId = tokenManager.getUserId(accessToken);
			responseEntity = schoolV1Service.lessonServiceManager(masterId);
		} catch (Exception e) {
			logger.error("异常日志：", e);
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	/**
	 * 查询对应校长名下的小课小组列表
	 */
	@RequestMapping(value = "/lessonGroupManager", method = RequestMethod.POST)
	@ApiOperation(value = "小课列表", httpMethod = "POST", notes = "小课列表")
	public ResponseEntity lessonGroupManager(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			if (StringUtils.isBlank(accessToken)) {
				logger.info("校长端-课务管理-小课列表，查询失败，accessToken=" + accessToken);
				responseEntity.failure("查询失败");
				return responseEntity;
			}

			String masterId = tokenManager.getUserId(accessToken);
			List<LessonGroupDTO> lessonGroupList = schoolV1Service.findLessonGroupList(masterId);
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("list", lessonGroupList);
			responseEntity.success(resultMap, "查询成功");
		} catch (Exception e) {
			logger.error("校长端-课务管理-小课列表，查询异常：", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}

		return responseEntity;
	}

	/**
	 * 根据小课的小组id查询组内的学生列表
	 */
	@RequestMapping(value = "/studentGroupList", method = RequestMethod.POST)
	@ApiOperation(value = "学生列表", httpMethod = "POST", notes = "学生列表")
	public ResponseEntity studentGroupList(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonGroupIdRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			if (StringUtils.isBlank(accessToken)) {
				logger.info("校长端-课务管理-学生列表，查询失败，accessToken=" + accessToken);
				responseEntity.failure("查询失败");
				return responseEntity;
			}

			String lessonGroupId = request.getLessonGroupId();
			if (StringUtils.isBlank(lessonGroupId)) {
				logger.info("校长端-课务管理-学生列表，小组id，lessonGroupId=" + lessonGroupId);
				responseEntity.failure("查询失败");
				return responseEntity;
			}

			List<StudentGroupDTO> studentGroupList = schoolV1Service.findStudentGroupList(lessonGroupId, 1);
			Map<String, Object> resultMap = new HashMap<>();
			resultMap.put("list", studentGroupList);
			responseEntity.success(resultMap, "查询成功");
		} catch (Exception e) {
			logger.error("校长端-课务管理-学生列表，查询异常：", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}

		return responseEntity;
	}
	
	@RequestMapping(value = "/lessonFeedbackList", method = RequestMethod.POST)
	@ApiOperation(value = "学生小课往期反馈记录列表", httpMethod = "POST", notes = "学生小课往期反馈记录列表")
	public ResponseEntity lessonFeedbackList(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonServiceIdRequest request) {
		ResponseEntity responseEntity = null;
		try {
			responseEntity = schoolV1Service.lessonFeedbackList(request.getLessonServiceId());
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

    /**
     * 根据小课小组的id查询详情
     */
	@RequestMapping(value = "/lessonGroupDetail", method = RequestMethod.POST)
	@ApiOperation(value = "小课详情", httpMethod = "POST", notes = "小课详情")
	public ResponseEntity lessonGroupDetail(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonGroupIdRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			if (StringUtils.isBlank(accessToken)) {
				logger.info("校长端-课务管理-课程详情，查询失败，accessToken=" + accessToken);
				responseEntity.failure("查询失败");
				return responseEntity;
			}

			String lessonGroupId = request.getLessonGroupId();
			if (StringUtils.isBlank(lessonGroupId)) {
				logger.info("校长端-课务管理-课程详情，小组id，lessonGroupId=" + lessonGroupId);
				responseEntity.failure("查询失败");
				return responseEntity;
			}

			LessonGroupDetailDTO lessonGroupDetail = schoolV1Service.findLessonGroupDetail(lessonGroupId);
			responseEntity.success(lessonGroupDetail, "查询成功");
		} catch (Exception e) {
			logger.error("校长端-课务管理-课程详情，查询异常：", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

    /**
     * 根据小课小组的id查询小组内的所有学生
     */
    @RequestMapping(value = "/studentGroupOrderList", method = RequestMethod.POST)
    @ApiOperation(value = "小课详情-上课学生", httpMethod = "POST", notes = "小课详情-上课学生")
    public ResponseEntity studentGroupOrderList(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonGroupIdRequest request) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            if (StringUtils.isBlank(accessToken)) {
                logger.info("校长端-课务管理-课程详情-上课学生，查询失败，accessToken=" + accessToken);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            String lessonGroupId = request.getLessonGroupId();
            if (StringUtils.isBlank(lessonGroupId)) {
                logger.info("校长端-课务管理-课程详情-上课学生，小组id，lessonGroupId=" + lessonGroupId);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            List<StudentGroupDTO> studentGroupOrderList = schoolV1Service.findStudentGroupList(lessonGroupId, null);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", studentGroupOrderList);
            responseEntity.success(resultMap, "查询成功");
        } catch (Exception e) {
            logger.error("校长端-课务管理-课程详情-上课学生，查询异常：", e);
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }

    /**
     * 根据小课小组的id，查询可以添加到小组内的学生列表
     */
    @RequestMapping(value = "/addStudentGroupList", method = RequestMethod.POST)
    @ApiOperation(value = "小课详情-上课学生-添加学生", httpMethod = "POST", notes = "小课详情-上课学生")
    public ResponseEntity addStudentGroupList(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonGroupIdRequest request) {
        ResponseEntity responseEntity = new ResponseEntity();
        try {
            if (StringUtils.isBlank(accessToken)) {
                logger.info("校长端-课务管理-课程详情-上课学生-添加学生，查询失败，accessToken=" + accessToken);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            String lessonGroupId = request.getLessonGroupId();
            if (StringUtils.isBlank(lessonGroupId)) {
                logger.info("校长端-课务管理-课程详情-上课学生-添加学生，小组id，lessonGroupId=" + lessonGroupId);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            String masterId = tokenManager.getUserId(accessToken);
            Pageable pageable = initPage(1, 1844674407);//pageSize是一个比较大的数字，用来实现查询所有符合条件的数据
            List<StudentGroupDTO> addStudentGroupList = schoolV1Service.findAddStudentGroupList(masterId, lessonGroupId, pageable);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", addStudentGroupList);
            responseEntity.success(resultMap, "查询成功");
        } catch (Exception e) {
            logger.error("校长端-课务管理-课程详情-上课学生-添加学生，查询异常：", e);
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
        }
        return responseEntity;
    }
    
    @RequestMapping(value = "/addStudentCommitOrder", method = RequestMethod.POST)
    @ModifySecurity
    @ApiOperation(value = "小课详情-添加学生下单", httpMethod = "POST", notes = "小课详情-添加学生下单")
    public ResponseEntity addStudentCommitOrder(@RequestParam(name = "accessToken") String accessToken, @RequestBody AddStudentOrderRequest request) {
    	ResponseEntity responseEntity = null;
		try {
			String userId = tokenManager.getUserId(accessToken);
			responseEntity = schoolV1Service.addStudentCommitOrder(request,userId);
		} catch (Exception e) {
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
    }
    
	//*******************************小课下单模块***************************/

    /**
     * 查询校长名下的所有学生
     */
	@RequestMapping(value = "/findStudentGroupListByOrder", method = RequestMethod.POST)
	@ApiOperation(value = "小课下单-学生列表", httpMethod = "POST", notes = "小课下单-学生列表")
	public ResponseEntity findStudentGroupListByOrder(@RequestParam(name = "accessToken") String accessToken, @RequestBody OrderStudentListRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
            if (StringUtils.isBlank(accessToken)) {
                logger.info("小课下单-学生列表，查询失败，accessToken=" + accessToken);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

			List<String> courseTimeList = request.getCourseTimeList();
			if (CollectionUtils.isEmpty(courseTimeList)) {
				logger.warn("小课下单-学生列表，查询失败:courseTimeList为空");
				responseEntity.failure("请先选择授课时间");
				return responseEntity;
			}

			String masterId = tokenManager.getUserId(accessToken);
            Pageable pageable = initPage(1, 1844674407);//pageSize是一个比较大的数字，用来实现查询所有符合条件的数据
            List<StudentGroupDTO> studentGroupList2Order = schoolV1Service.findStudentGroupList2Order(masterId, courseTimeList, pageable);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", studentGroupList2Order);
            responseEntity.success(resultMap, "查询成功");
		} catch (Exception e) {
            logger.error("小课下单-学生列表，查询异常：", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/groupEstimateOrderMoney", method = RequestMethod.POST)
	@ApiOperation(value = "小课-预估订单价格", httpMethod = "POST", notes = "小课-预估订单价格")
	public ResponseEntity groupEstimateOrderMoney(@RequestParam(name = "accessToken") String accessToken,
                                                  @RequestBody GroupEstimateOrderRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			responseEntity = schoolV1Service.computeGroupOrderAmount(request.getGrade(),request.getCourseCount());
		} catch (Exception e) {
			logger.error("异常日志", e);
            responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/lessonGroupCommitOrder", method = RequestMethod.POST)
	@ModifySecurity
	@ApiOperation(value = "小课提交订单", httpMethod = "POST", notes = "小课提交订单")
	public ResponseEntity lessonGroupCommitOrder(@RequestParam(name = "accessToken") String accessToken, @RequestBody GroupCommitOrderRequest request) {
		ResponseEntity responseEntity = null;
		try {
			String userId = tokenManager.getUserId(accessToken);
			responseEntity = schoolV1Service.lessonGroupCommitOrder(request,userId);
		} catch (Exception e) {
			logger.error("提交小课订单异常：", e);
			responseEntity = new ResponseEntity();
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

    /**
     * 下单完成后，查询小课的信息
     */
	@RequestMapping(value = "/completeOrder", method = RequestMethod.POST)
	@ApiOperation(value = "小课下单-下单完成", httpMethod = "POST", notes = "小课下单-下单完成")
	public ResponseEntity completeOrder(@RequestParam(name = "accessToken") String accessToken, @RequestBody LessonGroupIdRequest request) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
            if (StringUtils.isBlank(accessToken)) {
                logger.info("小课下单-下单完成，查询失败，accessToken=" + accessToken);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            String lessonGroupId = request.getLessonGroupId();
            if (StringUtils.isBlank(lessonGroupId)) {
                logger.info("小课下单-下单完成，小组id，lessonGroupId=" + lessonGroupId);
                responseEntity.failure("查询失败");
                return responseEntity;
            }

            LessonGroupDetailDTO lessonGroupDetail = schoolV1Service.findLessonGroupDetail(lessonGroupId);
            responseEntity.success(lessonGroupDetail, "查询成功");
		} catch (Exception e) {
            logger.error("小课下单-下单完成，查询异常：", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
	
	@RequestMapping(value = "/teacherListForGroup", method = RequestMethod.POST)
	@ApiOperation(value = "小课老师列表", httpMethod = "POST", notes = "小课老师列表")
	public ResponseEntity teacherListForGroup(@RequestParam(name = "accessToken") String accessToken,
		@RequestBody Teachers4GroupRequest request) {
			ResponseEntity responseEntity = new ResponseEntity();
			try {
				String masterId = tokenManager.getUserId(accessToken);
				List<TeacherDto> list = schoolService.teacherList4Group(masterId, request);
				Map<String, Object> data = new HashMap<>();
				data.put("list", list);
				responseEntity.success(data, "查询成功");
			} catch (Exception e) {
				logger.error("异常日志", e);
				responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
			}
			return responseEntity;
	}

	@RequestMapping(value = "/findChangeBindStudentList", method = RequestMethod.POST)
	@ApiOperation(value = "人员换绑-学生列表", httpMethod = "POST", notes = "人员换绑-学生列表")
	public ResponseEntity findStudentListChangeBind(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);

			Map<String, Object> resultMap = new HashMap<>();
			List<AppStudentChangeBindDTO> studentChangeBindDTOList = schoolV1Service.findStudentListChangeBind(masterId);
			resultMap.put("list", studentChangeBindDTOList);
			responseEntity.success(resultMap, "成功");
		} catch (Exception e) {
			logger.error("人员换绑-学生列表:", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}

	@RequestMapping(value = "/findChangeBindMasterList", method = RequestMethod.POST)
	@ApiOperation(value = "人员换绑-校长列表", httpMethod = "POST", notes = "人员换绑-校长列表")
	public ResponseEntity findChangeBindMasterList(@RequestParam(name = "accessToken") String accessToken) {
		ResponseEntity responseEntity = new ResponseEntity();
		try {
			String masterId = tokenManager.getUserId(accessToken);

			Map<String, Object> resultMap = new HashMap<>();
			List<AppMasterChangeBindDTO> masterChangeBindDTOList = schoolV1Service.findMasterListChangeBind(masterId);
			resultMap.put("list", masterChangeBindDTOList);
			responseEntity.success(resultMap, "成功");
		} catch (Exception e) {
			logger.error("人员换绑-校长列表:", e);
			responseEntity.failure(ResponseConstant.CODE_500, "系统繁忙，请稍后重试");
		}
		return responseEntity;
	}
}
