package com.goafanti.admin.controller;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
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.enums.OrgBasicInfoFields;
import com.goafanti.common.model.OrganizationIdentity;
import com.goafanti.user.bo.InputOrgBasicInfo;
import com.goafanti.user.service.NatureOwnershipService;
import com.goafanti.user.service.OrganizationIdentityService;

@Controller
@RequestMapping(value = "/api/admin/basic")
public class AdminOrgBaiscInfoApiController extends CertifyApiController {
	@Resource
	private NatureOwnershipService		natureOwnershipService;
	@Resource
	private OrganizationIdentityService	organizationIdentityService;

	/**
	 * 企业基本信息保存
	 * 
	 * @param info
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value = "/disposeInfo", method = RequestMethod.POST)
	public Result disposeInfo(@Valid InputOrgBasicInfo info, BindingResult bindingResult,
			String listedDateFormattedDate, String registrationTimeFormattedDate) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					OrgBasicInfoFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(info.getUid())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}

		Date listedDate = null;
		Date registrationTime = null;
		if (!StringUtils.isBlank(listedDateFormattedDate)) {
			try {
				listedDate = DateUtils.parseDate(listedDateFormattedDate, AFTConstants.YYYYMMDD);
			} catch (ParseException e) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "上市时间"));
				return res;
			}
		}

		if (!StringUtils.isBlank(registrationTimeFormattedDate)) {
			try {
				registrationTime = DateUtils.parseDate(registrationTimeFormattedDate, AFTConstants.YYYYMMDD);
			} catch (ParseException e) {
				res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "注册时间"));
				return res;
			}
		}

		OrganizationIdentity oi = new OrganizationIdentity();

		BeanUtils.copyProperties(info, oi);

		if (null != listedDate) {
			oi.setListedDate(listedDate);
		}

		if (null != registrationTime) {
			oi.setRegistrationTime(registrationTime);
		}

		//organizationIdentityService.saveBasicInfo(oi, ohr);

		return res;
	}

	/**
	 * 企业基本信息入口
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "/info", method = RequestMethod.GET)
	public Result info(String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}
		Calendar now = Calendar.getInstance();
		res.setData(organizationIdentityService.selectBasicInfo(uid, now.get(Calendar.YEAR) - 1));
		return res;
	}
}
