package com.goafanti.portal.controller;

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.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
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.core.shiro.token.TokenManager;
import com.goafanti.demand.service.DemandInterestService;
import com.goafanti.demand.service.DemandService;

@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;
	
	/**
	 * 科技需求"感兴趣列表"
	 */
	@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;
		}
		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.getDeletedSign().equals(DeleteStatus.DELETED.getCode())
				|| !d.getAuditStatus().equals(DemandAuditStatus.AUDITED.getCode())
				|| !d.getReleaseStatus().equals(DemandReleaseStatus.RELEASED.getCode())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}
		res.setData(demandInterestService.saveAchievementInterest(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;
		}
		res.setData(achievementInterestService.saveCancelAchievementInterest(id));
		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.getDeletedSign().equals(DeleteStatus.DELETED.getCode())
				|| !a.getAuditStatus().equals(AchievementAuditStatus.AUDITED.getCode())
				|| !a.getReleaseStatus().equals(AchievementReleaseStatus.RELEASED.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.selectAchievementSearchDetail(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;
		}
		res.setData(demandService.selectDemandSearchDetail(TokenManager.getUserId(), id));
		return res;
	}

}
