package com.goafanti.patent.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.patent.bo.PatentNewBo;
import com.goafanti.patent.service.PatentNewService;


@Controller
@RequestMapping(value = "/api/admin/patentNew")
public class AdminPatentNewApiController extends CertifyApiController {

	@Resource
	private PatentNewService patentNewService;
 
	
	/**
	 * 新增专利信息
	 */
	@RequestMapping(value = "/addPatentNew", method = RequestMethod.POST)
	public Result addPatentNew(PatentNewBo p){
		Result res = new Result();
		if (StringUtils.isBlank(p.getPatentNo())|| StringUtils.isBlank(p.getName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "专利", "专利"));
			return res;
		}
		if (StringUtils.isBlank(p.getEmail())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "邮箱", "邮箱"));
			return res;
		}
		if ( StringUtils.isBlank(p.getApplyDates())|| StringUtils.isBlank(p.getAuthorizationDates())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "时间", "时间"));
			return res;
		}else {
			 SimpleDateFormat sdf = new SimpleDateFormat( AFTConstants.YYYYMMDD);
		        
			 try {
				p.setApplyDate(sdf.parse(p.getApplyDates()));
				p.setAuthorizationDate(sdf.parse(p.getAuthorizationDates()));
			} catch (ParseException e) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "时间", "时间"));
				return res;
			}
		}
		return res.data(patentNewService.addPatenNew(p));
	}
	/**
	 * 修改专利信息
	 */
	@RequestMapping(value = "/updatePatentNew", method = RequestMethod.POST)
	public Result updatePatentNew(PatentNewBo p){
		Result res = new Result();
		if (StringUtils.isBlank(p.getPatentNo())|| StringUtils.isBlank(p.getName())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "专利", "专利"));
			return res;
		}
		if ( StringUtils.isBlank(p.getEmail())) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "邮箱", "邮箱"));
			return res;
		}
		SimpleDateFormat sdf = new SimpleDateFormat( AFTConstants.YYYYMMDD);
		try {
			p.setApplyDate(sdf.parse(p.getApplyDates()));
			p.setAuthorizationDate(sdf.parse(p.getAuthorizationDates()));
		} catch (ParseException e) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "时间", "时间"));
			return res;
		}
		return res.data(patentNewService.updatePatenNew(p));
	}
	/**
	 * 专利列表
	 */
	@RequestMapping(value = "/selectPatentNew", method = RequestMethod.GET)
	public Result selectPatentNew(PatentNewBo p,Integer pageSize,Integer pageNo){
		Result res = new Result();
		return res.data(patentNewService.selectPatentNew(p,pageSize,pageNo));
	}
	/** 证件上传 **/
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result uploadRefundOrderFile(HttpServletRequest req,String sign){
		Result res = new Result();
		//order_refund_file
		res.setData(handleFile(res, "/patent_certificate/", false, req, sign));
		return res;
	}
	
}
