package com.goafanti.admin.controller;

import javax.annotation.Resource;

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.AchievementService;
import com.goafanti.admin.service.AdminService;
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.DemandAuditStatus;
import com.goafanti.common.model.Achievement;
import com.goafanti.common.model.Demand;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.demand.service.DemandService;

@RestController
@RequestMapping(value = "/api/admin/audit")
public class AdminAuditApiController extends CertifyApiController {
	@Resource
	private AdminService		adminService;
	@Resource
	private DemandService		demandService;
	@Resource
	private AchievementService	achievementService;
	
	/**
	 * 科技成果技术经纪人流转
	 */
	@RequestMapping(value = "/modifyAchievementTechBroker", method = RequestMethod.POST)
	public Result modifyAchievementTechBroker(String id, String techBrokerId){
		Result res =new Result();
		if (StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "科技成果ID"));
			return res;
		}
		
		if(StringUtils.isBlank(techBrokerId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "技术经纪人ID"));
			return res;
		}
		
		Achievement a = achievementService.selectByPrimaryKey(id);
		if (null == a){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技成果ID"));
			return res;
		}
		
		if (!AchievementAuditStatus.AUDITED.getCode().equals(a.getAuditStatus())){
			res.getError().add(buildError("", "当前状态无法更改技术经纪人！"));
			return res;
		}
		
		
		a.setTechBrokerId(techBrokerId);
		res.setData(achievementService.updateByPrimaryKeySelective(a));
		return res;
	}
	
	/**
	 * 科技需求技术经纪人流转
	 */
	@RequestMapping(value = "/modifyDemandTechBroker", method = RequestMethod.POST)
	public Result modifyDemandTechBroker(String id, String techBrokerId){
		Result res =new Result();
		if (StringUtils.isBlank(id)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "科技需求ID"));
			return res;
		}
		
		if(StringUtils.isBlank(techBrokerId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "", "技术经纪人ID"));
			return res;
		}
		
		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d){
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技需求ID"));
			return res;
		}
		
		if (!DemandAuditStatus.AUDITED.getCode().equals(d.getAuditStatus())){
			res.getError().add(buildError("", "当前状态无法更改技术经纪人！"));
			return res;
		}
		d.setTechBrokerId(techBrokerId);
		res.setData(demandService.updateByPrimaryKeySelective(d));
		return res;
	}

	/**
	 * 获取技术经纪人下拉
	 */
	@RequestMapping(value = "/techBroders", method = RequestMethod.GET)
	public Result getTechBroders() {
		Result res = new Result();
		res.setData(adminService.selectTechBroder());
		return res;
	}

	/**
	 * 审核科技需求
	 */
	@RequestMapping(value = "/demand", method = RequestMethod.POST)
	public Result demand(String id, String techBroderId, Integer auditStatus) {
		Result res = new Result();
		res = disposeDmandAchievement(res, "demand", id, techBroderId, auditStatus);
		if (!res.getError().isEmpty()) {
			return res;
		}
		Demand d = demandService.selectByPrimaryKey(id);
		if (null == d) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "需求ID"));
			return res;
		}

		/*if (!DemandAuditStatus.INAUDIT.getCode().equals(d.getAuditStatus())) {
			res.getError().add(buildError("", "当前需求状态无法审核！"));
			return res;
		}
*/
		res.setData(demandService.updateAuditDemand(d, techBroderId, auditStatus));
		return res;
	}

	/**
	 * 审核科技成果
	 */
	@RequestMapping(value = "/achievement", method = RequestMethod.POST)
	public Result achievement(String id, String techBroderId, Integer auditStatus) {
		Result res = new Result();
		res = disposeDmandAchievement(res, "achievement", id, techBroderId, auditStatus);
		if (!res.getError().isEmpty()) {
			return res;
		}
		Achievement a = achievementService.selectByPrimaryKey(id);
		if (null == a) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "成果ID"));
			return res;
		}

		if (!AchievementAuditStatus.SUBMIT.getCode().equals(a.getAuditStatus())) {
			res.getError().add(buildError("", "当前成果状态无法审核！"));
			return res;
		}

		res.setData(achievementService.updateAuditAchievement(a, techBroderId, auditStatus));
		return res;
	}

	private Result disposeDmandAchievement(Result res, String sign, String id, String techBroderId,
			Integer auditStatus) {
		if (StringUtils.isBlank(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到需求ID", "需求ID"));
			return res;
		}

		if (null == auditStatus) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到审核状态", "审核状态"));
			return res;
		}

		if (sign.equals("demand")) {
			if (!DemandAuditStatus.AUDITED.getCode().equals(auditStatus)
					&& !DemandAuditStatus.UNAUDITED.getCode().equals(auditStatus)) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "审核状态"));
				return res;
			}
			if (DemandAuditStatus.AUDITED.getCode().equals(auditStatus)) {
				if (StringUtils.isBlank(techBroderId)) {
					res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到技术经纪人", "技术经纪人"));
					return res;
				}
			}
		} else {
			if (!AchievementAuditStatus.AUDITED.getCode().equals(auditStatus)
					&& !AchievementAuditStatus.UNAUDITED.getCode().equals(auditStatus)) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "审核状态"));
				return res;
			}
			if (AchievementAuditStatus.AUDITED.getCode().equals(auditStatus)) {
				if (StringUtils.isBlank(techBroderId)) {
					res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到技术经纪人", "技术经纪人"));
					return res;
				}
			}
		}
		return res;
	}
}
