package com.wechat.controller;

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

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.CommonHelper;
import com.wechat.common.utils.StringsUtils;
import com.wechat.global.message.InfoMsg;
import com.wechat.model.requestDto.AdCtrReq;
import com.wechat.model.responseDto.JobAdvertisedNologinResp;
import com.wechat.model.responseDto.PhotoAlbumResp;
import com.wechat.model.responseDto.ResultEntity;
import com.wechat.model.responseDto.UserHomePageResp;
import com.wechat.service.CommonService;
import com.wechat.service.MdeAdImagesService;
import com.wechat.service.PersonCeneterService;
import com.wechat.service.PositionService;



/**
 * 免登陆接口
 * @author jadyn.wu
 * @date: 2020-05-20 14:18
 */
@Controller
@RequestMapping("noLogin")
public class NoLoginController {
	
	@Autowired
	private PositionService positionService;
	
	@Resource
	private CommonHelper commonHelper;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private PersonCeneterService personCenterService;
	
	@Autowired
	private MdeAdImagesService adImagesService;
	
	
	@Value("#{configProperties['upload.base.path']}")
	private String basePackagePath;
	@Value("#{configProperties['upload.path.file']}")
	private String uploadFilePath;
	
	// log 处理
	protected static final Log log = LogFactory.getLog(NoLoginController.class);
	
	/**
	 * 职位页面初始化
	 * @param userCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResultEntity<Map<String,Object>> initMainPage(HttpServletRequest request) throws Exception {
		String userCode = commonService.getUserCode(request);
		String lang = commonService.getLanguage(request);
		Map<String,Object> map = positionService.init(userCode,lang);
		
		return new ResultEntity<Map<String,Object>>(InfoMsg.SUCCESS_REQUEST,map);
	}
	
	/**
	 * 个人主页单独使用
	 * 获取用的详细信息
	 * @param req
	 * @param jobId
	 * @return
	 */
	@RequestMapping(value = "getHomePageNologin", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<UserHomePageResp<JobAdvertisedNologinResp>> getHomePageNologin(HttpServletRequest request,@RequestParam String shareCode) {
		ResultEntity<UserHomePageResp<JobAdvertisedNologinResp> > rest = new ResultEntity<UserHomePageResp<JobAdvertisedNologinResp> >();
		UserHomePageResp<JobAdvertisedNologinResp> resp = new UserHomePageResp<JobAdvertisedNologinResp> ();
		String lang = commonService.getLanguage(request);
		//获取解密后数据
		String[] codeAndTime = commonHelper.getUserCodeAes(shareCode);
		if(codeAndTime.length > 1)
		{	//查询目标用户code
			String userCode = codeAndTime[0];
			//验证用时间戳
			String creatTime = codeAndTime[1];
			//查询页面信息
			resp = personCenterService.getHomePageNologin(userCode,lang,"",creatTime);
		} else {
			log.error("有攻击嫌疑！！ip：【"+ commonHelper.getIRealIPAddr(request)+"】");
			rest.setMsg(InfoMsg.ERROR_USER_MISS);
		}
		rest.setResult(resp);
		return rest;
	}
	
	
	/**
	 * 获取企业相册
	 * 
	 * @param userCode
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getPhotoAlbum", method = RequestMethod.GET)
	@ResponseBody
	public ResultEntity<List<PhotoAlbumResp>> getPhotoAlbum(@RequestParam String shareCode) throws Exception {
		ResultEntity<List<PhotoAlbumResp>> rest = new ResultEntity<List<PhotoAlbumResp>>();
		//获取解密后数据
		String[] codeAndTime = commonHelper.getUserCodeAes(shareCode);
		if(codeAndTime.length>1)
		{
			rest.setResult(personCenterService.getPhotoAlbum(codeAndTime[0]));
		}else {
			rest.setMsg(InfoMsg.ERROR_USER_MISS);
		}

		return rest;
	}
	
	
	/**
	 * 职位详情页广告点击记录（非登陆）
	 * @param jobId 发布职位id
	 * @return
	 */
	@RequestMapping(value="setPDetailAdCtr", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> setPDetailAdCtr(@RequestBody AdCtrReq adCtrReq,HttpServletRequest request){
		ResultEntity<String> rest = new ResultEntity<String>();
		
		if(commonService.limitApiFrequencyAdCtrs(request)) {
			String userCode = commonService.getUserCode(request);
			String lang = commonService.getLanguage(request);
			String shareJob = adCtrReq.getShareJob();
			int jobId = 0;
			if(!StringsUtils.isEmpty(shareJob))
			{
				jobId = commonHelper.getJobId(shareJob);
			}
			Map<String, Object> mapResult = positionService.getPositionDetail(jobId,userCode,lang,null);
			//职位id不正确
			if(mapResult == null)
			{
				log.error("有攻击嫌疑！！ip：【"+ commonHelper.getIRealIPAddr(request)+"】");
			} else {
				//记录广告点击率
				if(adImagesService.recodeAdCtr(adCtrReq) > 0) {
					rest.setMsg(InfoMsg.ERROR_SAVE_FIELD);
				};
			}
		}
		return rest;
	}

	
	
	/**
	 * 登陆页广告点击记录（非登陆）
	 * @return
	 */
	@RequestMapping(value="setAdsCtr", method = RequestMethod.POST)
	@ResponseBody
	public ResultEntity<String> setAdsCtr(@RequestBody List<AdCtrReq> adCtrReqList,HttpServletRequest request){
		ResultEntity<String> rest = new ResultEntity<String>();
		int result = 0;
		//接口调用频率控制
		if(commonService.limitApiFrequencyAdCtrs(request)) {
			for(AdCtrReq adCtrReq : adCtrReqList) {
				//验证广告点击率
				if(adImagesService.checkAdsCtr(adCtrReq)) {
					//记录广告点击率
					result = result+ adImagesService.recodeAdCtr(adCtrReq);
				}
			}
		}

		if(result > 0) {
			rest.setMsg(InfoMsg.ERROR_SAVE_FIELD);
		};

		return rest;
	}
	
	
	
}
