package com.goafanti.portal.controller;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.achievement.service.AchievementInterestService;
import com.goafanti.achievement.service.AchievementService;
import com.goafanti.business.service.JtBusinessService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.dao.AchievementInterestMapper;
import com.goafanti.common.dao.ProjectInterestMapper;
import com.goafanti.common.enums.AchievementAuditStatus;
import com.goafanti.common.enums.AchievementReleaseStatus;
import com.goafanti.common.enums.DeleteStatus;
import com.goafanti.common.enums.DemandAuditStatus;
import com.goafanti.common.enums.DemandReleaseStatus;
import com.goafanti.common.model.Achievement;
import com.goafanti.common.model.AchievementInterest;
import com.goafanti.common.model.Demand;
import com.goafanti.common.model.DemandInterest;
import com.goafanti.common.model.JtBusinessProject;
import com.goafanti.common.model.ProjectInterest;
import com.goafanti.common.model.UserInterest;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demand.service.DemandInterestService;
import com.goafanti.demand.service.DemandService;
import com.goafanti.user.bo.UserIdentityBo;
import com.goafanti.user.service.UserIdentityService;
import com.goafanti.user.service.UserInterestService;
import com.goafanti.user.service.UserService;

@RestController
@RequestMapping(value = "/api/user/portal")
public class PortalInterestApiController extends CertifyApiController{
	@Resource
	private AchievementService			achievementService;
	@Resource
	private DemandService				demandService;
	@Resource
	private AchievementInterestService	achievementInterestService;
	@Resource
	private DemandInterestService		demandInterestService;
	@Resource
	private JtBusinessService			jtBusinessService;
	@Resource
	private ProjectInterestMapper				projectInterestMapper;
	@Resource
	private UserInterestService					userInterestService;
	@Resource UserIdentityService				userIdentityService ;
	/**
	 * 科技需求"感兴趣列表"
	 */
	@RequestMapping(value = "/demandInterestList", method = RequestMethod.GET)
	public Result demandInterestList(String pageNo, String pageSize){
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		res.setData(demandInterestService.listDemandInterest(pNo, pSize));
		return res;
	}

	/**
	 * 科技成果"感兴趣列表"
	 */
	@RequestMapping(value = "/achievementInterestList", method = RequestMethod.GET)
	private Result achievementInterestList(String pageNo, String pageSize) {
		Result res = new Result();
		Integer pNo = 1;
		Integer pSize = 10;
		if (StringUtils.isNumeric(pageSize)) {
			pSize = Integer.parseInt(pageSize);
		}
		if (StringUtils.isNumeric(pageNo)) {
			pNo = Integer.parseInt(pageNo);
		}
		res.setData(achievementInterestService.listAchievementInterest(pNo, pSize));
		return res;
	}

	/**
	 * 科技需求取消关注
	 * 
	 */
	@RequestMapping(value = "/demandCancelInterest", method = RequestMethod.POST)
	public Result demandCancelInterest(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return res;
		}
		DemandInterest demandInterest=demandInterestService.selectDemandInterestByUidAndDemandId(TokenManager.getUserId(), id);
		if(demandInterest==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "需求"));
			return res;
		}
		res.setData(demandInterestService.saveCancelDemandInterest(id));
		return res;
	}

	/**
	 * 科技需求关注
	 */
	@RequestMapping(value = "/demandInterest", method = RequestMethod.POST)
	public Result demandInterest(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}

		DemandInterest di = demandInterestService.selectDemandInterestByUidAndDemandId(TokenManager.getUserId(), id);
		if (null != di) {
			res.getError().add(buildError("", "当前科技需求已关注!"));
			return res;
		}
		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d 
				|| !d.getAuditStatus().equals(DemandAuditStatus.AUDITED.getCode())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}
		res.setData(demandInterestService.saveDemandInterest(id));
		return res;
	}

	/**
	 * 科技成果取消关注
	 * 
	 */
	@RequestMapping(value = "/achievementCancelInterest", method = RequestMethod.POST)
	public Result achievementCancelInterest(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
			return res;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return res;
		}
		AchievementInterest achievementInterest=achievementInterestService.selectAchievementInterestByUidAndAchievementId(TokenManager.getUserId(), id);
		if(achievementInterest==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "找不到成果ID", "找不到成果"));
			return res;
		}
		res.setData(achievementInterestService.saveCancelAchievementInterest(achievementInterest.getId()));
		return res;
	}

	/**
	 * 科技成果关注
	 */
	@RequestMapping(value = "/achievementInterest", method = RequestMethod.POST)
	public Result achievementInterest(String id) {
		Result res = new Result();

		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "科技成果ID"));
			return res;
		}

		AchievementInterest ai = achievementInterestService
				.selectAchievementInterestByUidAndAchievementId(TokenManager.getUserId(), id);
		if (null != ai) {
			res.getError().add(buildError("", "当前科技成果已关注!"));
			return res;
		}

		Achievement a = achievementService.selectByPrimaryKey(id);
		if (null == a 
				|| !a.getAuditStatus().equals(AchievementAuditStatus.AUDITED.getCode())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "成果ID"));
			return res;
		}
		res.setData(achievementInterestService.saveAchievementInterest(id));
		return res;
	}

	
	/**
	 * 科技成果详情
	 */
	@RequestMapping(value = "/achievementDetail", method = RequestMethod.GET)
	public Result achievementDetail(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到成果ID", "成果ID"));
			return res;
		}
		res.setData(achievementService.selectAchievementDetail(TokenManager.getUserId(), id));
		return res;
	}

	/**
	 * 科技需求详情
	 */
	@RequestMapping(value = "/demandDetail", method = RequestMethod.GET)
	public Result demandDetail(String id) {
		Result res = new Result();
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return res;
		}
		res.setData(demandService.selectDemandDetail(TokenManager.getUserId(), id));
		return res;
	}
	
	/*
	 * 服务项目取消关注
	 * */
	@RequestMapping(value="/projectCancelInterest",method=RequestMethod.POST)
	public Result projectCancelInterest(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return result;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		result.setData(projectInterestMapper.deleteInterest(TokenManager.getUserId(), id));
		return result;
	}
	
	/*
	 * 服务项目关注
	 * */
	@RequestMapping(value="/projectInterest",method=RequestMethod.POST)
	public Result projectInterest(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "项目ID"));
			return result;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		projectInterestMapper.deleteInterest(TokenManager.getUserId(), id);
		ProjectInterest projectInterest=new ProjectInterest();
		projectInterest.setCreateTime(new Date());
		projectInterest.setId(UUID.randomUUID().toString());
		projectInterest.setProjectId(id);
		projectInterest.setUid(TokenManager.getUserId());
		result.setData(projectInterestMapper.insert(projectInterest));
		return result;
	}
	
	/*
	 * 服务-关注列表
	 * */
	@RequestMapping(value="/listProjectIInterestedIn",method=RequestMethod.GET)
	public Result listProjectIInterestedIn(Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		result.setData(jtBusinessService.listProjectIInterestedIn(pageNo,pageSize));
		return result;
	}
	
	/*
	 * 成果关注列表
	 * */
	@RequestMapping(value="/listAchievementIInterestedIn",method=RequestMethod.GET)
	public Result listAchievementIInterestedIn(Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}

		result.setData(achievementService.listInterestedAchievement(pageNo, pageSize));
		return result;
		
	}
	
	/*
	 * 需求关注列表
	 * */
	@RequestMapping(value="/listDemandIInterestedIn",method=RequestMethod.GET)
	public Result listDemandIInterestedIn(Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		result.setData(demandService.listInterestedDemand(pageNo, pageSize));
		return result;
		
	}
	
	/*
	 * 关注专家
	 * */
	@RequestMapping(value="/expertInterest",method=RequestMethod.POST)
	public Result expertInterest(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "专家ID"));
			return result;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
	UserInterest userInterest=userInterestService.findByFromUidAndToUid(TokenManager.getUserId(), id);
	if(userInterest==null )
		userInterestService.insert(id);
		return result;
	}
	
	/*
	 * 取消关注专家
	 * */
	@RequestMapping(value="/expertCancelInterest",method=RequestMethod.POST)
	public Result expertCancelInterest(String id) {
		Result result=new Result();
		if(StringUtils.isBlank(id)) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "专家ID"));
			return result;
		}
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		UserInterest userInterest=userInterestService.findByFromUidAndToUid(TokenManager.getUserId(), id);
		if(userInterest!=null && userInterest.getId()!=null)
		userInterestService.deleteByPrimaryKey(userInterest.getId());
		return result;
	}
	
	/*
	 * 专家-关注列表
	 * */
	@RequestMapping(value="/listInterestedInExpert",method=RequestMethod.GET)
	public Result listExpertIInterestedIn(Integer pageNo,Integer pageSize) {
		Result result=new Result();
		if(StringUtils.isBlank(TokenManager.getUserId())) {
			result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "当前用户未登陆"));
			return result;
		}
		result.setData(userIdentityService.listInterestedExpert(pageNo,pageSize));
		return result;
	}

}
