package com.goafanti.admin.controller;

import com.goafanti.cognizance.service.*;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.enums.AttachmentType;
import com.goafanti.common.model.*;
import com.goafanti.common.utils.SHA256Util;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.copyright.service.CopyrightInfoService;
import com.goafanti.patent.service.PatentInfoService;
import com.goafanti.techproject.service.TechProjectService;
import com.goafanti.user.service.OrgHumanResourceService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Calendar;

@RestController
@RequestMapping(value = "/api/admin/preview")
public class AdminPreviewApiController extends CertifyApiController {
	@Resource
	private PatentInfoService				patentInfoService;
	@Resource
	private CopyrightInfoService			copyrightInfoService;
	@Resource
	private TechProjectService				techProjectService;
	@Resource
	private OrgTechCenterDetailService		orgTechCenterDetailService;
	@Resource
	private OrgActivityService				orgActivityService;
	@Resource
	private OrgActivityCostService			orgActivityCostService;
	@Resource
	private OrgTechProductService			orgTechProductService;
	@Resource
	private OrgIntellectualPropertyService	orgIntellectualPropertyService;
	@Resource
	private OrgTechAchievementService		orgTechAchievementService;
	@Resource
	private OrgRatepayService				orgRatepayService;
	@Resource
	private OrgFinanceService				orgFinanceService;
	@Resource
	private OrgHumanResourceService			OrgHumanResourceService;
	@Resource
	private OrgHonorDatumService			OrgHonorDatumService;
	@Resource
	private OrgStandardService				orgStandardService;

	@Value(value = "${aesSecretKey}")
	private String							secretKey	= null;

	/**
	 * 专利相关材料(专利代理委托书、专利稿件、授权通知书、专利证书、上年度纳税报表)预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/patent", method = RequestMethod.GET)
	public Result previewPatent(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "专利ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.LAST_YEAR_RATEPAY) {
			OrgRatepay ratepay = orgRatepayService.selectByPrimaryKey(id);
			if (null == ratepay) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "纳税报表ID"));
				return res;
			}
		} else {
			PatentInfo pi = patentInfoService.selectByPrimaryKey(id);
			if (null == pi) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "专利ID"));
				return res;
			}
		}
		if (attachmentType == AttachmentType.PATENT_PRORY_STATEMENT || attachmentType == AttachmentType.PATENT_WRITING
				|| attachmentType == AttachmentType.AUTHORIZATION_NOTICE
				|| attachmentType == AttachmentType.LAST_YEAR_RATEPAY
				|| attachmentType == AttachmentType.PATENT_CERTIFICATE) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 软著相关材料(软著申请书、软著证书)预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/copyright", method = RequestMethod.GET)
	public Result previewCopyright(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著ID"));
			return res;
		}

		CopyrightInfo ci = copyrightInfoService.selectByPrimaryKey(id);
		if (null == ci) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "软著ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.COPYRIGHT_AUTH || attachmentType == AttachmentType.COPYRIGHT_APPLY) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 科技项目材料预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/techProject", method = RequestMethod.GET)
	public Result previewTechProject(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技项目申报ID"));
			return res;
		}

		TechProject tp = techProjectService.selectByPrimaryKey(id);
		if (null == tp) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技项目申报ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.TECH_PROJECT) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token2=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 企业研发机构-产学研协议附件预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/techCenterDetail", method = RequestMethod.GET)
	public Result previewTechCenterDetail(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "产学研记录ID"));
			return res;
		}

		OrgTechCenterDetail otcd = orgTechCenterDetailService.selectByPrimaryKey(id);
		if (null == otcd) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "产学研记录ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.PROTOCOL) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 研发活动立项证明材料预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/activity", method = RequestMethod.GET)
	public Result previewActivity(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "研发活动ID"));
			return res;
		}

		OrgActivity oa = orgActivityService.selectOrgActivityByPrimaryKey(id);
		if (null == oa) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "研发活动ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.PROOF) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 研发活动费用台帐预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/activityCost", method = RequestMethod.GET)
	public Result previewActivityCost(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "研发活动费用ID"));
			return res;
		}

		OrgActivityCost oac = orgActivityCostService.selectByPrimaryKey(id);
		if (null == oac) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "研发活动费用ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.ACTIVITY_COST_ACCOUNT) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 高新技术产品台帐预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/techProduct", method = RequestMethod.GET)
	public Result previewTechProduct(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "高新技术产品ID"));
			return res;
		}

		OrgTechProduct otp = orgTechProductService.selectByPrimaryKey(id);
		if (null == otp) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "高新技术产品ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.TECH_PRODUCT) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 知识产权证书及稿件预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/intellectualProperty", method = RequestMethod.GET)
	public Result previewIntellectualProperty(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "知识产权ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.PROPERTY_RIGHT) {
			OrgIntellectualProperty oip = orgIntellectualPropertyService.selectByPrimaryKey(id);
			if (null == oip) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "知识产权ID"));
				return res;
			}
		} else if (attachmentType == AttachmentType.PATENT_WRITING) {
			PatentInfo pi = patentInfoService.selectByPrimaryKey(id);
			if (null == pi) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "专利稿件ID"));
				return res;
			}
		} else if (attachmentType == AttachmentType.COPYRIGHT_APPLY) {
			CopyrightInfo ci = copyrightInfoService.selectByPrimaryKey(id);
			if (null == ci) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "知识产权申请书ID"));
				return res;
			}
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
			return res;
		}

		String time = String.valueOf(Calendar.getInstance().getTime().getTime());
		String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
		res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		return res;
	}

	/**
	 * 科技成果附件预览授权
	 * 
	 * @param id
	 * @param sing
	 * @return
	 */
	@RequestMapping(value = "/achievement", method = RequestMethod.GET)
	public Result previewAchievement(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技成果ID"));
			return res;
		}
		OrgTechAchievement ota = orgTechAchievementService.selectByPrimaryKey(id);
		if (null == ota) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "科技成果ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.ACHIEVEMENT) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}

		return res;
	}

	/**
	 * 所得税纳税申报表预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/ratepay", method = RequestMethod.GET)
	public Result previewRatepay(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "纳税申报表ID"));
			return res;
		}
		OrgRatepay ratepay = orgRatepayService.selectByPrimaryKey(id);
		if (null == ratepay) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "纳税申报表ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.RATEPAY) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 财务报表预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/finance", method = RequestMethod.GET)
	public Result previewFinance(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "财务报表ID"));
			return res;
		}

		OrgFinance of = orgFinanceService.selectByPrimaryKey(id);
		if (null == of) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "财务报表ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.FINANCE) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 人力资源情况预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/humanResource", method = RequestMethod.GET)
	public Result previewHumanResource(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "人力资源ID"));
			return res;
		}

		OrgHumanResource ohr = OrgHumanResourceService.selectByPrimaryKey(id);
		if (null == ohr) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "人力资源ID"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.ROSTER || attachmentType == AttachmentType.SOCIAL_SECURITY) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 企业荣誉及其他证明材料预览授权
	 * 
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/honor", method = RequestMethod.GET)
	public Result previewHonor(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "企业荣誉及其他证明材料ID"));
			return res;
		}
		OrgHonorDatum ohd = OrgHonorDatumService.selectByPrimaryKey(id);
		if (null == ohd) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "企业荣誉及其他证明材料ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.HONOR) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 标准制定情况附件预览授权
	 * @param id
	 * @param sign
	 * @return
	 */
	@RequestMapping(value = "/standard", method = RequestMethod.GET)
	public Result previewStandard(String id, String sign) {
		Result res = new Result();
		if (StringUtils.isEmpty(id)) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "标准制定情况ID"));
			return res;
		}

		OrgStandard os = orgStandardService.selectByPrimaryKey(id);
		if (null == os) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "标准制定情况ID"));
			return res;
		}
		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.STANDARD) {
			String time = String.valueOf(Calendar.getInstance().getTime().getTime());
			String auth = SHA256Util.toHash(sign + "|" + id + "|" + secretKey, time);
			res.setData("sign=" + sign + "&token=" + id + "&auth=" + auth + "&temp=" + time);
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

}
