package com.wechat.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
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.ResponseBody;

import com.wechat.common.utils.MessageUtils;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeJobAdvertised;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.service.CommonService;
import com.wechat.service.RecruitmentInformationReleaseService;

@Controller
@RequestMapping("recruit")
public class RecruitmentReleaseController {

	@Autowired
	RecruitmentInformationReleaseService releaseService;

	@Autowired
	CommonService commonService;

	//非付费企业可发职位次数
	@Value("#{configProperties['unvip_release_no']}")
	private String unvipReleaseNo;
	
	/**
	 * 是否可发布招聘信息
	 * 
	 * @param mdeJobAdvertised
	 * @return true 可发布  false 不可发布
	 * @throws Exception 
	 */
	@RequestMapping(value = "checkReleaseLimit", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<Boolean> checkReleaseLimit(HttpServletRequest req) throws Exception {
		ResultEntity<Boolean> rest = new ResultEntity<Boolean>();
		// 获取发布人信息
		String publisher = commonService.getUserCode(req);
		boolean bol = releaseService.checkReleaseLimit(publisher);
		//验证企业发布次数
		if(!bol)
		{
			//超限
			InfoMsg msg = InfoMsg.ERROR_RELEASE_BEYOND;
			msg.setMsg(MessageUtils.getText(msg.getMsg(), unvipReleaseNo));
			rest.setMsg(msg);
		}
		rest.setResult(bol);
		return rest;
	}

	/**
	 * 招聘信息提交
	 * 	并进行入参数据校验
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "recruitmentInformationRelease", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> recruitmentInformationRelease(
			@Validated(value = MdeJobAdvertised.SaveGroup.class) @RequestBody MdeJobAdvertised jobAdvertised, HttpServletRequest req) throws Exception {
		// 获取发布人信息
		String publisher = commonService.getUserCode(req);
		jobAdvertised.setPublisher(publisher);
		
		// 职位类别
		Integer jobCode = jobAdvertised.getJobId();

		// 新增或者重启结束的职位 (职位发布状态[0-招聘中；1-结束]) 验证发布次数
		if(jobCode== null && "1".equals(jobAdvertised.getJobStatus()))
		{
			//验证企业发布次数
			if(!releaseService.checkReleaseLimit(publisher))
			{
				//超限
				InfoMsg msg = InfoMsg.ERROR_RELEASE_BEYOND;
				msg.setMsg(MessageUtils.getText(msg.getMsg(), unvipReleaseNo));
				return new ResultEntity<>(msg);
			}
		}

		return releaseService.recruitmentInformationRelease(jobAdvertised, req);
	}
}
