package com.wechat.service;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.wechat.common.Constants;
import com.wechat.common.utils.DateUtils;
import com.wechat.dao.MdeJobAdvertisedDao;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.dbEntity.MdeJobAdvertised;
import com.wechat.model.dbEntity.MdeUser;
import com.wechat.model.dto.AvailableAndUsedCountDto;
import com.wechat.model.responseDto.ResultEntity;

@Service
public class RecruitmentInformationReleaseService {

	@Autowired
	MdeUserService mdeUserService;

	@Autowired
	MdePositionAnotherNameService mdePositionAnotherNameService;

	@Autowired
	MdeJobAdvertisedService mdeJobAdvertisedService;

	@Autowired
	MdeJobAdvertisedDao jobAdvertisedDao;

	@Autowired
	CommonService commonService;
	
	//非付费企业可发职位次数
	@Value("#{configProperties['unvip_release_no']}")
	private int unvipReleaseNo;
	
	//限制非VIP企业启动时间
	@Value("#{configProperties['unvip_discount_date']}")
	private String unvipDiscountDate;

	
	/**
	 * 验证企业发布次数
	 * 
	 * @param mdeJobAdvertised
	 * @return true 可发布  false 不可发布
	 * @throws Exception 
	 */
	public Boolean checkReleaseLimit(String publisher) throws Exception {
		MdeUser user = commonService.getUserInfo(publisher);
		//发布次数
		boolean result = true;
		//优惠结束时间
		Date discountTime = DateUtils.stringToDate(unvipDiscountDate, DateUtils.PATTEN_YMD);
		
		//非会员 并且 注册时间超过了优惠期限的
		if ("0".equals(user.getVipFlag()) && DateUtils.dateCompare(discountTime,new Date()))
		{
			//非会员的情况下验证发布次数
			if(jobAdvertisedDao.alreadyPublishNotVipCount(publisher, "0") >= unvipReleaseNo)
			{
				//超限
				result = false;
			}
		}
		return result;
	}

	/**
	 * 招聘信息发布
	 * 
	 * @param mdeJobAdvertised
	 * @return
	 */
	public ResultEntity<String> recruitmentInformationRelease(MdeJobAdvertised mdeJobAdvertised,HttpServletRequest req) {
		// 获取发布人信息
		String publisher = mdeJobAdvertised.getPublisher();
		
		// 职位类别
		Integer jobCode = mdeJobAdvertised.getJobId();
		mdeJobAdvertised.setJobName(StringEscapeUtils.unescapeHtml(mdeJobAdvertised.getJobName().trim()));
		// 职位名称
		String jobName = mdeJobAdvertised.getJobName();

		// 发布
		mdeJobAdvertised.setJobName(jobName.trim());
		
		// 装载首推次数
		String recommendFlag = mdeJobAdvertised.getRecommendFlag();
		if(Constants.RECOMMEND_YES.equals(recommendFlag))
		{
			//当是首推情况下验证首推个数
			AvailableAndUsedCountDto availableAndUsedNum = jobAdvertisedDao.getAvailableAndUsedCount(publisher);
			if((availableAndUsedNum.getUsed()+1) > availableAndUsedNum.getAvailable())
			{
				return new ResultEntity<>(InfoMsg.ERROR_RECOMMEND_BEYOND);
			}
		}
		mdeJobAdvertised.setRecommendFlag(recommendFlag);
		
		int change = 0;
		if(jobCode == null) {
			//新增
			change = mdeJobAdvertisedService.insert(mdeJobAdvertised);
		}else {
			//修改
			mdeJobAdvertised.setJobStatus("0");
			change = mdeJobAdvertisedService.update(mdeJobAdvertised);
		} 
		if(change < 1)
		{
			return new ResultEntity<>(InfoMsg.ERROR_DATA_UPDATE);
		}

		return new ResultEntity<>(InfoMsg.SUCCESS_REQUEST);
	}
}
