package com.goafanti.admin.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

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 org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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.DeleteStatus;
import com.goafanti.common.enums.OrgBasicInfoFields;
import com.goafanti.common.model.NatureOwnership;
import com.goafanti.common.model.OrgHumanResource;
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();
		OrgHumanResource ohr = new OrgHumanResource();

		BeanUtils.copyProperties(info, oi);
		BeanUtils.copyProperties(info, ohr);

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

		if (null != registrationTime) {
			oi.setRegistrationTime(registrationTime);
		}
		ohr.setId(info.getHid());

		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;
	}

	
	

	

	/**
	 * 自然人股权列表
	 * 
	 * @param uid
	 * @return
	 */
	@RequestMapping(value = "/listNature", method = RequestMethod.GET)
	public Result listNature(String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}

		res.setData(natureOwnershipService.selectByUid(uid));
		return res;
	}

	/**
	 * 删除自然人股权
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/deleteNature", method = RequestMethod.POST)
	public Result deleteNature(@RequestParam(name = "ids[]", required = false) String[] ids) {
		Result res = new Result();
		if (ids == null || ids.length < 1) {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", ""));
		} else {
			res.setData(natureOwnershipService.batchDeleteByPrimaryKey(Arrays.asList(ids)));
		}
		return res;
	}

	/**
	 * 自然人股权保存修改
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/nature", method = RequestMethod.POST)
	public Result nature(String data) {
		Result res = new Result();
		JSONArray ja = (JSONArray) JSON.parse(data);
		if (ja != null && !ja.isEmpty()) {
			List<NatureOwnership> no = new ArrayList<NatureOwnership>();
			for (int idx = 0; idx < ja.size(); idx++) {
				no.add(ja.getJSONObject(idx).toJavaObject(NatureOwnership.class));
			}
			for (NatureOwnership n : no) {
				if (StringUtils.isBlank(n.getUid())){
					res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
					return res;
				}
				if (StringUtils.isBlank(n.getId())) {
					n.setId(UUID.randomUUID().toString());
				}

				if (null == n.getDeletedSign()) {
					n.setDeletedSign(DeleteStatus.UNDELETE.getCode());
				}
			}
			res.setData(natureOwnershipService.batchInsert(no));
		} else {
			res.getError().add(buildError("", "参数格式不正确"));
		}
		return res;
	}

}
