package com.awspaas.user.apps.donenow_crm.event;

import java.util.List;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.org.cache.DepartmentCache;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.sdk.local.SDK;
import com.alipay.remoting.util.StringUtils;
import com.awspaas.user.apps.donenow_crm.constant.CrmConstant;


public class FormAfterSaveEvent
		extends ExecuteListener
{
	public String getDescription() {
		return "����������¼�";
	}

	public String getProvider() {
		return "�Ϻ�����";
	}

	public String getVersion() {
		return "1.0";
	}


	public void execute(ProcessExecutionContext process) throws Exception {
		try {
			UserContext uc = process.getUserContext();
			BO formData = process.getFormData();
			String boName = process.getParameterOfString("$BONAME");

			System.out.println("��λboName:" + boName);

			String companyId = uc.getCompanyModel().getId();
			System.out.println("��λcrmCompanyId:" + companyId);
			String parentDepartmentId=DBSql.getString("select ID from orgdepartment where COMPANYID='"+companyId+"' and  DEPARTMENTNO='kehu'");
			System.out.println("parentDepartmentId:" + parentDepartmentId);


			BO accountBo = process.getBO(CrmConstant.BO_EU_DNCRM_ACCOUNT);
			if (accountBo != null) {

				System.out.println("�ͻ�������:" + accountBo.getBindId());

				String deptId = DBSql.getString("SELECT ID FROM ORGDEPARTMENT WHERE EXT1=?", new Object[] { accountBo.getId() });

				System.out.println("������ID:" + parentDepartmentId);

				String parentId = accountBo.getString("PARENT_ID");

				if (StringUtils.isBlank(parentId)) {
					parentId = parentDepartmentId;
				}

				//�ͻ���������,��ϵͳ���ű����������߸�������
				if (StringUtils.isNotBlank(deptId)) {
					DBSql.update("UPDATE ORGDEPARTMENT SET PARENTDEPARTMENTID=? WHERE ID=?", new Object[] { parentId, deptId });
					SDK.getORGAPI().updateDepartment(deptId, accountBo.getString("NAME"));
				} else {

					deptId = SDK.getORGAPI().createDepartment(companyId, accountBo.getString("NAME"), "", "", parentId, accountBo.getId(), "");
				}
				DepartmentCache.getCache().reload();

				//˵���ǿͻ���������
				if (CrmConstant.BO_EU_DNCRM_ACCOUNT.equals(boName)) {
					//����ַ��ͬ������
//					List<BO> list = SDK.getBOAPI().query(CrmConstant.BO_EU_DNCRM_LOCATION).bindId(accountBo.getBindId()).addQuery("IS_DEFAULT=", Integer.valueOf(1)).list();
//					//�Ѿ�����Ĭ�ϵ�ַ
//					if(list.size()>0) {
//						for (BO bo : list) {
//							bo.set("COUNTRY_ID", accountBo.get("COUNTRY_ID"));
//							bo.set("DISTRICT_ID", accountBo.get("DISTRICT_ID"));
//							bo.set("ADDRESS", accountBo.get("ADDRESS"));
//							bo.set("POSTAL_CODE", accountBo.get("POSTAL_CODE"));
//							bo.set("CATE_ID", accountBo.get("CATE_ID"));
//							SDK.getBOAPI().update(CrmConstant.BO_EU_DNCRM_LOCATION, bo);
//
//						}
//					}else {
//						//����ַ������������
//						BO bo=new BO();
//						bo.setBindId(accountBo.getBindId());
//						bo.set("COUNTRY_ID", accountBo.get("COUNTRY_ID"));
//						bo.set("DISTRICT_ID", accountBo.get("DISTRICT_ID"));
//						bo.set("ADDRESS", accountBo.get("ADDRESS"));
//						bo.set("POSTAL_CODE", accountBo.get("POSTAL_CODE"));
//						bo.set("IS_DEFAULT", 1);
//						bo.set("ACCOUNT_ID", accountBo.getId());
//						bo.set("CATE_ID", CrmConstant.OFFICELOCATION);
//						bo.set("ID", UUIDGener.getUUID());
//						SDK.getBOAPI().create(CrmConstant.BO_EU_DNCRM_LOCATION, bo,SDK.getProcessAPI().getInstanceById(accountBo.getBindId()), uc);
//						//���¿ͻ�����LocationId�ֶ�
//						accountBo.set("LOCATION_ID", bo.getId());
//						SDK.getBOAPI().update(CrmConstant.BO_EU_DNCRM_ACCOUNT, bo);
//					}

					//����ϵ�˱�ͬ������
					List<BO> mainList = SDK.getBOAPI().query(CrmConstant.BO_EU_DNCRM_CONTACT).addQuery("ACCOUNT_ID=", accountBo.getId()).addQuery("IS_PRIMARY_CONTACT=", 1).list();
					System.out.println("mainList:"+mainList.size());
					if(mainList.size()==0) {
						ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance(CrmConstant.obj_fcf1c6204d084ea78e152af821ff4874, uc.getUID(), "");
						BO bo=new BO();
						bo.setId(UUIDGener.getUUID());
						bo.set("NAME", accountBo.get("CONTACT_NAME"));
						bo.set("SUFFIX_ID", accountBo.get("CONTACT_SUFFIX_ID"));
						bo.set("TITLE", accountBo.get("CONTACT_TITLE"));
						bo.set("EMAIL", accountBo.get("CONTACT_EMAIL"));
						bo.set("MOBILE_PHONE", accountBo.get("PHONE"));
						bo.set("PHONE", accountBo.get("ALTERNATE_PHONE1"));
						bo.set("ACCOUNT_ID", accountBo.getId());
						bo.set("IS_PRIMARY_CONTACT", 1);
						SDK.getBOAPI().create(CrmConstant.BO_EU_DNCRM_CONTACT, bo, createBOProcessInstance, uc);
						//ͬ����ϵ�����ݵ��û���
						new ContactFormAfterSaveEvent().saveContactData(uc, bo, CrmConstant.BO_EU_DNCRM_CONTACT, false);
					}else {
						BO bo = mainList.get(0);
						bo.set("NAME", accountBo.get("CONTACT_NAME"));
						bo.set("SUFFIX_ID", accountBo.get("CONTACT_SUFFIX_ID"));
						bo.set("TITLE", accountBo.get("CONTACT_TITLE"));
						bo.set("EMAIL", accountBo.get("CONTACT_EMAIL"));
						bo.set("MOBILE_PHONE", accountBo.get("PHONE"));
						bo.set("PHONE", accountBo.get("ALTERNATE_PHONE1"));
						bo.set("ACCOUNT_ID", accountBo.getId());
						SDK.getBOAPI().update(CrmConstant.BO_EU_DNCRM_CONTACT, bo);
						//ͬ����ϵ�����ݵ��û���
						new ContactFormAfterSaveEvent().saveContactData(uc, bo, CrmConstant.BO_EU_DNCRM_CONTACT, false);
					}
				}

				//˵���ǵ�ַ��������
//				else if (CrmConstant.BO_EU_DNCRM_LOCATION.equals(boName)) {
//					BO locationBo = process.getBO(CrmConstant.BO_EU_DNCRM_LOCATION);
//					if (locationBo != null && "1".equals(locationBo.getString("IS_DEFAULT"))) {
//						accountBo.set("COUNTRY_ID", locationBo.get("COUNTRY_ID"));
//						accountBo.set("DISTRICT_ID", locationBo.get("DISTRICT_ID"));
//						accountBo.set("ADDRESS", locationBo.get("ADDRESS"));
//						accountBo.set("POSTAL_CODE", locationBo.get("POSTAL_CODE"));
//						SDK.getBOAPI().update(CrmConstant.BO_EU_DNCRM_ACCOUNT, accountBo);
//					}
//				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("�ͻ������¼�������:"+e.getMessage());
		}
	}


}
