package com.ym.mec.thirdparty.adapay;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.huifu.adapay.core.exception.BaseAdaPayException;
import com.huifu.adapay.model.AdapayCommon;
import com.huifu.adapay.model.CorpMember;
import com.ym.mec.thirdparty.adapay.entity.EnterpriseUser;
import com.ym.mec.thirdparty.exception.ThirdpartyException;

/**
 * 企业用户相关服务
 */
@Service
public class CorpMemberService {

	private final static Logger logger = LoggerFactory.getLogger(CorpMemberService.class);

	public boolean create(String merchantKey, String appId, EnterpriseUser req, String notifyUrl) {

		Map<String, Object> memberParams = new HashMap<String, Object>();
		memberParams.put("member_id", req.getMemberId());
		memberParams.put("app_id", appId);
		memberParams.put("order_no", merchantKey + "_" + System.currentTimeMillis());
		memberParams.put("social_credit_code_expires", req.getSocialCreditCodeExpires());
		memberParams.put("business_scope", req.getBusinessScope());
		memberParams.put("name", req.getName());
		memberParams.put("prov_code", req.getProvCode());
		memberParams.put("area_code", req.getAreaCode());
		memberParams.put("social_credit_code", req.getSocialCreditCode());
		memberParams.put("legal_person", req.getLegalPerson());
		memberParams.put("legal_cert_id", req.getLegalCertId());
		memberParams.put("legal_cert_id_expires", req.getLegalCertIdExpires());
		memberParams.put("legal_mp", req.getLegalMp());
		memberParams.put("address", req.getAddress());
		memberParams.put("bank_code", req.getBankCode());
		memberParams.put("bank_acct_type", req.getBankAcctType());
		memberParams.put("card_no", req.getCardNo());
		memberParams.put("card_name", req.getCardName());
		memberParams.put("notify_url", req.getNotifyUrl());
		
		if(StringUtils.isNotBlank(notifyUrl)){
			memberParams.put("notify_url", notifyUrl);
		}
		
		try {
			Map<String, Object> resp = null;
			if (StringUtils.isNotBlank(merchantKey)) {
				resp = CorpMember.create(memberParams, req.getAttachFile(), merchantKey);
			} else {
				resp = CorpMember.create(memberParams, req.getAttachFile());
			}
			logger.info("创建企业信息 Resp:{}",resp);
		} catch (BaseAdaPayException e) {
			throw new ThirdpartyException("创建企业用户失败:{}", e, e.getMessage());
		}

		return true;
	}
	
	public boolean update(String merchantKey, String appId, EnterpriseUser req, String notifyUrl){
		Map<String, Object> memberParams = new HashMap<String, Object>();
		memberParams.put("adapay_func_code", "corp_members.update");
		memberParams.put("member_id", req.getMemberId());
		memberParams.put("app_id", appId);
		memberParams.put("order_no", merchantKey + "_" + System.currentTimeMillis());
		memberParams.put("social_credit_code_expires", req.getSocialCreditCodeExpires());
		memberParams.put("business_scope", req.getBusinessScope());
		memberParams.put("name", req.getName());
		memberParams.put("prov_code", req.getProvCode());
		memberParams.put("area_code", req.getAreaCode());
		memberParams.put("social_credit_code", req.getSocialCreditCode());
		memberParams.put("legal_person", req.getLegalPerson());
		memberParams.put("legal_cert_id", req.getLegalCertId());
		memberParams.put("legal_cert_id_expires", req.getLegalCertIdExpires());
		memberParams.put("legal_mp", req.getLegalMp());
		memberParams.put("address", req.getAddress());
		memberParams.put("notify_url", req.getNotifyUrl());
		
		if(StringUtils.isNotBlank(notifyUrl)){
			memberParams.put("notify_url", notifyUrl);
		}
		try {
			Map<String, Object> resp = null;
			if (StringUtils.isNotBlank(merchantKey)) {
				resp = AdapayCommon.requestAdapayFile(memberParams, req.getAttachFile(), merchantKey);
			} else {
				resp = AdapayCommon.requestAdapayFile(memberParams, req.getAttachFile());
			}
			logger.info("修改企业信息 Resp:{}",resp);
		} catch (BaseAdaPayException e) {
			throw new ThirdpartyException("修改企业用户信息失败:{}", e, e.getMessage());
		}

		return true;
	}
	
	public EnterpriseUser query(String merchantKey, String appId, String memberId) {
		Map<String, Object> memberParams = new HashMap<String, Object>();
		memberParams.put("member_id", memberId);
		memberParams.put("app_id", appId);
		Map<String, Object> resp = null;
		try {
			if (StringUtils.isNotBlank(merchantKey)) {
				resp = CorpMember.query(memberParams, merchantKey);
			} else {
				resp = CorpMember.query(memberParams);
			}
			if(StringUtils.equals(resp.get("status").toString(), "succeeded")){
				EnterpriseUser  user = new EnterpriseUser();
				user.setAddress(resp.get("address").toString());
				user.setAreaCode(resp.get("area_code").toString());
				user.setBusinessScope(resp.get("business_scope").toString());
				user.setLegalCertId(resp.get("legal_cert_id").toString());
				user.setLegalCertIdExpires(resp.get("legal_cert_id_expires").toString());
				user.setLegalMp(resp.get("legal_mp").toString());
				user.setLegalPerson(resp.get("legal_person").toString());
				user.setMemberId(resp.get("member_id").toString());
				user.setName(resp.get("name").toString());
				user.setProvCode(resp.get("prov_code").toString());
				user.setSocialCreditCode(resp.get("social_credit_code").toString());
				user.setSocialCreditCodeExpires(resp.get("social_credit_code_expires").toString());
				
				return user;
			}
		} catch (BaseAdaPayException e) {
			throw new ThirdpartyException("查询企业用户信息失败:{}", e, e.getMessage());
		}

		return null;
	}
	
}
