package com.goafanti.admin.controller;

import java.math.BigDecimal;
import java.util.Calendar;

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

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
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.RestController;

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.AttachmentType;
import com.goafanti.common.enums.IdentityAuditStatus;
import com.goafanti.common.enums.IdentityProcess;
import com.goafanti.common.enums.OrganizationIdentityFields;
import com.goafanti.common.enums.UserIdentityFields;
import com.goafanti.common.enums.UserLevel;
import com.goafanti.common.model.OrganizationIdentity;
import com.goafanti.common.model.UserIdentity;
import com.goafanti.user.bo.InputOrganizationIdentity;
import com.goafanti.user.bo.InputUserIdentity;
import com.goafanti.user.service.OrganizationIdentityService;
import com.goafanti.user.service.UserIdentityService;

@RestController
@RequestMapping(value = "/api/admin/userCertify")
public class AdminUserCertifyApiController extends CertifyApiController {
	@Resource
	private UserIdentityService			userIdentityService;
	@Resource
	private OrganizationIdentityService	organizationIdentityService;
	@Resource
	private AdminService				adminService;

	/**
	 * 审核员--获取客户经理下拉
	 */
	@RequestMapping(value = "/accountManager", method = RequestMethod.GET)
	public Result getAccountManager() {
		Result res = new Result();
		res.setData(adminService.selectAccoutManager());
		return res;
	}

	/**
	 * 审核员--获取业务员(技术员)下拉
	 */
	@RequestMapping(value = "/technician", method = RequestMethod.GET)
	public Result getTechnician() {
		Result res = new Result();
		res.setData(adminService.selectTechnician());
		return res;
	}

	/**
	 * 上传认证资料（营业执照图片、组织机构代码证图片、上年度纳税申报表）
	 */
	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	public Result uploadCertify(HttpServletRequest req, String sign, String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}

		AttachmentType attachmentType = AttachmentType.getField(sign);
		if (attachmentType == AttachmentType.ORGCODE || attachmentType == AttachmentType.LICENCE) {
			res.setData(handleFiles(res, "/identity/", true, req, sign, uid));
		} else if (attachmentType == AttachmentType.RATEPAY) {
			sign = sign + "_" + (Calendar.getInstance().get(Calendar.YEAR) - 1);
			res.setData(handleFiles(res, "/cognizance/", true, req, sign, uid));
		} else {
			res.getError().add(buildError(ErrorConstants.PARAM_ERROR, "", "附件标示"));
		}
		return res;
	}

	/**
	 * 审核员--个人用户详情
	 */
	@RequestMapping(value = "/userDetail", method = RequestMethod.GET)
	public Result userDetail(String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}
		res.setData(userIdentityService.selectAuditorUserIdentityByUserId(uid));
		return res;
	}

	/**
	 * 审核员审核个人用户信息
	 */
	@RequestMapping(value = "/updateUserDetail", method = RequestMethod.POST)
	public Result disposeUser(@Valid InputUserIdentity userIdentity, BindingResult bindingResult,
			String paymentDateFormattedDate, String aid, String mid) {
		Result res = new Result();
		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					UserIdentityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(userIdentity.getId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户认证ID", "用户认证ID"));
			return res;
		}

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

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

		/*if (null == userIdentity.getLevel()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户等级", "用户等级"));
			return res;
		}*/

		if (!UserLevel.GENERAL.getCode().equals(userIdentity.getLevel())
				|| IdentityAuditStatus.PASSED.getCode().equals(userIdentity.getAuditStatus())) {
			if (StringUtils.isBlank(aid)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到业务员ID", "业务员ID"));
				return res;
			}

			if (StringUtils.isBlank(mid)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到客户经理ID", "客户经理ID"));
				return res;
			}
		}

		UserIdentity ui = new UserIdentity();
		BeanUtils.copyProperties(userIdentity, ui);

		if (IdentityAuditStatus.UNCOMMITTED.getCode().equals(ui.getAuditStatus())) {
			ui.setProcess(IdentityProcess.UNCOMMITTED.getCode());
		} else if (IdentityAuditStatus.COMMITTED.getCode().equals(ui.getAuditStatus())
				|| IdentityAuditStatus.UNPAID.getCode().equals(ui.getAuditStatus())) {
			ui.setProcess(IdentityProcess.COMMITTED.getCode());
		} else if (IdentityAuditStatus.PAID.getCode().equals(ui.getAuditStatus())) {
			ui.setProcess(IdentityProcess.FAIL.getCode());
		} else if (IdentityAuditStatus.NOTPASSED.getCode().equals(ui.getAuditStatus())
				|| IdentityAuditStatus.PASSED.getCode().equals(ui.getAuditStatus())) {
			ui.setProcess(IdentityProcess.SUCCESS.getCode());
		}
		res.setData(userIdentityService.updateUserDetailByAuditAdmin(ui, aid, mid, userIdentity.getLevel()));
		return res;
	}

	/**
	 * 审核员---团体用户详情
	 */
	@RequestMapping(value = "/orgDetail", method = RequestMethod.GET)
	public Result orgDetail(String uid) {
		Result res = new Result();
		if (StringUtils.isBlank(uid)) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户", "用户"));
			return res;
		}
		res.setData(organizationIdentityService.selectAuditorOrgIdentityDetailByUserId(uid));
		return res;
	}

	/**
	 * 审核员审核团体用户信息
	 */
	@RequestMapping(value = "/updateOrgDetail", method = RequestMethod.POST)
	public Result diposeOrg(@Valid InputOrganizationIdentity orgIdentity, BindingResult bindingResult,
			String paymentDateFormattedDate, String aid, String mid) {
		Result res = new Result();

		if (bindingResult.hasErrors()) {
			res.getError().add(buildErrorByMsg(bindingResult.getFieldError().getDefaultMessage(),
					OrganizationIdentityFields.getFieldDesc(bindingResult.getFieldError().getField())));
			return res;
		}

		if (StringUtils.isBlank(orgIdentity.getId())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户认证ID", "用户认证ID"));
			return res;
		}

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

		if (null == orgIdentity.getAuditStatus()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到审核状态", "审核状态"));
			return res;
		}
		
		/*if (null == orgIdentity.getLevel()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到用户等级", "用户等级"));
			return res;
		}*/

		if (!UserLevel.GENERAL.getCode().equals(orgIdentity.getLevel())
				|| IdentityAuditStatus.PASSED.getCode().equals(orgIdentity.getAuditStatus())) {
			if (StringUtils.isBlank(aid)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到业务员ID", "业务员ID"));
				return res;
			}

			if (StringUtils.isBlank(mid)) {
				res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "找不到客户经理ID", "客户经理ID"));
				return res;
			}
		}

		OrganizationIdentity oi = new OrganizationIdentity();
		BeanUtils.copyProperties(orgIdentity, oi);
		if (orgIdentity.getRegisteredCapital() != null) {
			oi.setRegisteredCapital(new BigDecimal(orgIdentity.getRegisteredCapital()));
		}
		if (IdentityAuditStatus.UNCOMMITTED.getCode().equals(oi.getAuditStatus())) {
			oi.setProcess(IdentityProcess.UNCOMMITTED.getCode());
		} else if (IdentityAuditStatus.COMMITTED.getCode().equals(oi.getAuditStatus())
				|| IdentityAuditStatus.UNPAID.getCode().equals(oi.getAuditStatus())) {
			oi.setProcess(IdentityProcess.COMMITTED.getCode());
		} else if (IdentityAuditStatus.PAID.getCode().equals(oi.getAuditStatus())) {
			oi.setProcess(IdentityProcess.FAIL.getCode());
		} else if (IdentityAuditStatus.NOTPASSED.getCode().equals(oi.getAuditStatus())
				|| IdentityAuditStatus.PASSED.getCode().equals(oi.getAuditStatus())){
			oi.setProcess(IdentityProcess.SUCCESS.getCode());
		}
		res.setData(organizationIdentityService.updateOrgDetailByAuditAdmin(oi, aid, mid, orgIdentity.getLevel()));
		return res;
	}
	

}
