package com.ym.mec.teacher.controller;

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

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
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.entity.Teacher;
import com.ym.mec.biz.dal.entity.TeacherContracts;
import com.ym.mec.biz.service.TeacherContractsService;
import com.ym.mec.biz.service.TeacherService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;

@RequestMapping("teacherContract")
@Api(tags = "教师协议服务")
@RestController
public class TeacherContractController extends BaseController {

	@Autowired
	private TeacherContractsService teacherContractsService;

	@Autowired
	private SysUserFeignService sysUserFeignService;
	
	@Autowired
	private TeacherService teacherService;

	@ApiOperation(value = "是否需要签订协议")
	@GetMapping("/isRequireSign")
	public HttpResponseResult<Boolean> isRequireSign() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请重新登录");
		}

		if (sysUser.getTenantId() == 1) {
			//判断分部
			Teacher teacher = teacherService.get(sysUser.getId());
			
			if(teacher == null || teacher.getTeacherOrganId() == null || (teacher.getTeacherOrganId() == 55 ||teacher.getTeacherOrganId() == 59)){
				return succeed(false);
			}
			
			TeacherContracts teacherContracts = teacherContractsService.queryByUserId(sysUser.getId());
			if (teacherContracts == null) {
				return succeed(true);
			}
		}
		return succeed(false);
	}

	@ApiOperation(value = "签订协议")
	@PostMapping("/sign")
	public HttpResponseResult<Boolean> sign() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed(HttpStatus.FORBIDDEN, "请重新登录");
		}

		return succeed(teacherContractsService.sign(sysUser));
	}

	@ApiOperation(value = "异步通知")
	@PostMapping("/callback")
	public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String jsonStr = IOUtils.toString(request.getInputStream(), "UTF-8");
		teacherContractsService.callback(jsonStr);
		returnMsg("success", response);
	}
	
	private void returnMsg(String msg, HttpServletResponse response) throws IOException {
		response.addHeader("Content-Type", "text/html;charset=UTF-8");
		PrintWriter print = response.getWriter();
		print.print(msg);
		print.flush();
	}
}
