package com.wechat.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wechat.common.Constants;
import com.wechat.common.utils.StringsUtils;
import com.wechat.global.base.BaseController;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeIntvContacts;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dto.InterviewDetailDto;
import com.wechat.model.dto.IntvContactsDto;
import com.wechat.model.dto.WxBaseUserInfoDto;
import com.wechat.model.requestDto.InterviewConfirmReq;
import com.wechat.model.requestDto.InterviewCreateReq;
import com.wechat.model.requestDto.InterviewHisReq;
import com.wechat.model.requestDto.InterviewRefuseReq;
import com.wechat.model.requestDto.InterviewUpdateReq;
import com.wechat.model.responseDto.DPResp;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.model.responseDto.WxMiniCode2SessionResp;
import com.wechat.service.CommonService;
import com.wechat.service.InterviewService;
import com.wechat.service.MdeUserService;
import com.wechat.service.WeiXinProgService;

/**
* @author jadyn
* @version 创建时间：2020年6月28日 下午4:32:51
* 面试相关接口
*/

@Controller
@RequestMapping(value = "interview")
public class InterviewController extends BaseController{

	@Resource
	private InterviewService intvService;
	
	@Resource
	private CommonService commonService;

	@Resource
	private WeiXinProgService wxProgService;
	
	@Resource
	private MdeUserService userService;
	
	/**
	 * 提交面试邀请
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "createIntvInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> createIntvInfo(@RequestBody @Valid InterviewCreateReq intvReq, HttpServletRequest request){
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//创建面试邀请信息
    	int count = intvService.createIntv(request,intvReq,userCode);
    	if(count == 0) {
    		resp.setMsg(InfoMsg.ERROR_SAVE_FIELD);
    	} 
    	return resp;
    }
    
    /**
	 * 获取面试邀请信息
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "getIntvInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<InterviewDetailDto> getIntvInfo(@RequestParam String intvId, HttpServletRequest request) {
    	ResultEntity<InterviewDetailDto> resp = new ResultEntity<InterviewDetailDto>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	//获取当前登录用户的身份标识[0-个人；1-企业]
    	String identityFlag = commonService.getIdentityFlag(request);
    	
    	String publisher = null;
    	String interviewee = null;
    	
    	if(Constants.IDENTITY_P.equals(identityFlag))
    	{
    		interviewee = userCode;
    	} else {
    		publisher = userCode;
    	}
    	
    	//获取面试邀请信息
    	resp.setResult(intvService.getIntvDetail(publisher,interviewee,intvId));

    	return resp;
    }
	
	
	/**
	 * 修改联系人
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "updateContacts", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> updateContacts(@RequestBody MdeIntvContacts req, HttpServletRequest request) throws Exception {
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//修改联系人
    	int count = intvService.updateContacts(req,userCode);
    	if(count == 0) {
    		resp.setMsg(InfoMsg.ERROR_DATA_UPDATE);
    	}

    	return resp;
    }
    
	/**
	 * 获取联系人
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "getContacts", method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<List<IntvContactsDto>> getContacts(HttpServletRequest request) throws Exception {
    	ResultEntity<List<IntvContactsDto>> resp = new ResultEntity<List<IntvContactsDto>>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//获取联系人列表
    	resp.setResult(intvService.getContacts(userCode));
    	
    	return resp;
    }
    
	/**
	 * 删除联系人
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "delContact", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> delContact(@RequestParam Integer contId, HttpServletRequest request) throws Exception {
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//修改联系人
    	int count = intvService.delContact(contId,userCode);
    	if(count < 1) {
    		resp.setMsg(InfoMsg.ERROR_DATA_UPDATE);
    	}

    	return resp;
    }
	
	/**
	 * 修改面试预约时间
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "updateIntvTimes", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> updateIntvTimes(@RequestBody @Valid InterviewUpdateReq intvUpdReq, HttpServletRequest request) {
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);
    	
    	//获取当前登录用户的身份标识[0-个人；1-企业]
    	String identityFlag = commonService.getIdentityFlag(request);	
    	
    	//修改面试预约时间
    	intvService.updateIntvTimes(intvUpdReq, userCode, identityFlag);
    	
    	//发送信息提示
    	intvService.sendIntvChangeTimeMsg(intvUpdReq,identityFlag);

    	return resp;
    }
    
	/**
	 * 确认面试时间
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "confirmIntvTime", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> confirmIntvTime(@RequestBody @Valid InterviewConfirmReq confirmReq, HttpServletRequest request){
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);

    	//确认面试时间
    	intvService.confirmIntvTime(confirmReq, userCode);
    	
    	// 发送通知（企业和个人）-异步处理
		// 个人用户信息
		MdeUser personUser = userService.getFullInfo(confirmReq.getInterviewee());
		// 企业用户信息
		MdeUser enterpriseUser = userService.getFullInfo(confirmReq.getPublisher());
		
		intvService.sendIntvConfirmMsgAsync(confirmReq,personUser,enterpriseUser);

    	return resp;
    }
    

    /**
	 * 拒绝面试
     * 
     * @param 
     * @return
     */
    @RequestMapping(value = "refuseIntv", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity<String> refuseIntv(@RequestBody @Valid InterviewRefuseReq refuseReq, HttpServletRequest request){
    	ResultEntity<String> resp = new ResultEntity<String>();
    	
    	//获取当前登录用户
    	String userCode = commonService.getUserCode(request);

    	//拒绝面试
    	int count = intvService.refuseIntv(refuseReq, userCode);
    	if(count < 1) {
    		resp.setMsg(InfoMsg.ERROR_DATA_UPDATE);
    	} else {
    		try {
    			//发送信息
        		intvService.sendRefuseIntvMsg(refuseReq,commonService.getUserInfo(userCode));
			} catch (Exception e) {
				log.error("*************拒绝面试信息发送异常*************",e);
			}
    		
    	}

    	return resp;
    }
    
    
	/**
	 * 获取拒绝理由下拉列表
	 * 
	 */
	@RequestMapping(value = "getRefuseReasonList", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<DPResp>> getRefuseReasonList() {
		ResultEntity<List<DPResp>> rest = new ResultEntity<List<DPResp>>();
		List<DPResp> prepareFlagList = commonService.getDictByType("refuse_reasons",Constants.LANG_CN);
		rest.setResult(prepareFlagList);
		return rest;
	}
	
	
	/**
	 * 获取视频面试时长
	 * 
	 */
	@RequestMapping(value = "markIntvTimes", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<DPResp>> markIntvTimes() {
		ResultEntity<List<DPResp>> rest = new ResultEntity<List<DPResp>>();
		List<DPResp> prepareFlagList = commonService.getDictByType("refuse_reasons",Constants.LANG_CN);
		rest.setResult(prepareFlagList);
		return rest;
	}
	
	
	/**
	 * 记录使用记录-Start
     * 
     * @param 
     * @return
     */
	@RequestMapping(value = "setInterviewHis", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> setInterviewHis(@RequestBody @Valid InterviewHisReq req, HttpServletRequest request) throws Exception {
		ResultEntity<String> resp = new ResultEntity<String>();
		
	  	//获取当前用户信息
    	WxMiniCode2SessionResp wxResp = wxProgService.getJscodeToSession(req.getCode());
    	if(StringsUtils.isEmpty(wxResp.getOpenid()))
    	{
    		//调用小程序登陆出错
    		log.error("**************调用小程序登陆出错【jscode2session】【"+wxResp.getErrmsg()+"】*********************"); 
    		resp.setMsg(InfoMsg.ERROR_AUZ_FAILED);
    	} else {
    		//缓存中获取openid等信息
        	WxBaseUserInfoDto wxBUser = wxProgService.getSessionKey(wxResp.getOpenid());
        	if (wxBUser == null)
        	{
        		//非登录小程序用户
        		log.error("**************小程序非登录状态OpenId【"+wxResp.getOpenid()+"】*********************"); 
        		resp.setMsg(InfoMsg.ERROR_AUZ_FAILED);
        	} else {
    			// 记录使用记录
    			String usedId = intvService.setInterviewHis(req,wxBUser.getUserCode(),wxBUser.getIdentityFlag());
    			resp.setResult(usedId);
        	}
    	}

		return resp;
	}
	
	

	 
    
}
