package com.jw.app.common.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import net.sf.json.JSONObject;

import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.AssertUtil;
import com.gaowj.business.util.Encipher;
import com.gaowj.business.util.Java_tool;
import com.gaowj.business.util.RequestUtils;
import com.jw.app.business.UnitBusiness;
import com.jw.app.business.UserBusiness;
import com.jw.app.business.utils.QueryParamUtil;
import com.jw.app.business.utils.Status;

public class remotePushDataAction extends BasicAction {

	/**
	 * 浙大网新主动推送过来
	 * 高伟杰
	 * 2015-09-02
	 */
	private static final long serialVersionUID = -7876218926124645225L;

	private JSONObject jsonObject = new JSONObject();
	private Java_tool jt = new Java_tool();

	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String insertSysUser() throws BusinessException {
		UserBusiness business = BusinessManager.getBusiness(UserBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request,
				RequestUtils.UPDATEDATAFIELDPREFIX);
		try {
			List<Map<String, Object>> mylistData = AssertUtil
					.fromJsonString((String) entity.get("JSON"));
			mylistData = QueryParamUtil.StringToDate(mylistData);
			System.out.println(mylistData);
			System.out.println("人员同步开始......");
			for (Map<String, Object> map : mylistData) {

				if (map.get("OPER_TYPE") != null&& map.get("ID_NUMBER") != null) {
					String is_oper = (String) map.get("OPER_TYPE");
					String is_id_number = (String) map.get("ID_NUMBER");
					Map<String, Object> query = new HashMap<String, Object>();
					query.put("ID_NUMBER", is_id_number);
					List<Map<String,Object>> old_user = business.list_user(query);
					
					if (StringUtils.equalsIgnoreCase(is_oper, "INSERT")) {
						
						if (old_user==null || old_user.size()==0) {

							Map<String, Object> entity_user = new HashMap<String, Object>();
							Map<String, Object> entity_account = new HashMap<String, Object>();
							Map<String, Object> entity_ext = new HashMap<String, Object>();
							String uuid = (String) map.get("USER_ID");
							entity_user.put("USER_ID", uuid);
							entity_user.put("USER_PWD",Encipher.EncodePasswd("ninghai#123"));
							entity_user.put("USER_NAME",(String) map.get("USER_NAME"));
							entity_user.put("USER_SEX", "0");

							entity_account.put("USER_ID", uuid);
							entity_account.put("UNIT_ID",(String) map.get("UNIT_ID"));
							entity_account.put("ID_NUMBER",(String) map.get("ID_NUMBER"));
							entity_account.put("IS_ACTIVE", "1");
							entity_account.put("ID_TYPE", "1");
							entity_account.put("IS_MAIN_IDENTITY", "1");
							entity_account.put("BEGIN_TIME", new Date());
							entity_account.put("END_TIME",jt.String3date("2060-12-30 13:14:01"));
							entity_account.put("CREATE_TIME", new Date());
							entity_account.put("UPDATE_TIME", new Date());
							entity_account.put("DELETE_FLAG", "0");

							entity_ext.put("USER_ID", uuid);
							entity_ext.put("EMAIL", "sys@test.com");
							entity_ext.put("TELEPHONE", "1234567890");
							entity_ext.put("MOBILE", "1234567890");
							business.insert_user(entity_user);
							business.insert_user_account(entity_account);
							business.insert_user_ext(entity_ext);
						}else{
							jsonObject.putAll(Status.getStatusErrorMessage("新增数据已存在"));
							return "json";
						}

					} else if (StringUtils.equalsIgnoreCase(is_oper, "UPDATE")) {
						if (old_user!=null && old_user.size()>0 
								&& StringUtils.equals(old_user.get(0).get("USER_ID").toString(), map.get("USER_ID").toString())
								&& StringUtils.equals(old_user.get(0).get("ID_NUMBER").toString(), map.get("ID_NUMBER").toString())) {
							Map<String, Object> entity_user = new HashMap<String, Object>();
							Map<String, Object> entity_account = new HashMap<String, Object>();

							String uuid = (String) map.get("USER_ID");
							entity_user.put("USER_ID", uuid);
							entity_user.put("USER_NAME",
									(String) map.get("USER_NAME"));

							entity_account.put("USER_ID", uuid);
							entity_account.put("UNIT_ID",
									(String) map.get("UNIT_ID"));
							entity_account.put("UPDATE_TIME", new Date());

							business.update_user(entity_user);
							business.update_user_account(entity_account);
						}

					}
					jsonObject.putAll(Status.getStatus(Status.INSERT_OK));
				} else {
					jsonObject
							.putAll(Status.getStatusErrorMessage("JSON数据不全!"));
				}

			}

			System.out.println("人员同步结束......");

		} catch (Exception e) {
			jsonObject.putAll(Status.getStatusErrorMessage("数据保存失败!"));
			e.printStackTrace();
		}
		return "json";
	}

	public String insertSysUnit() throws BusinessException {
		UnitBusiness business = BusinessManager.getBusiness(UnitBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request,
				RequestUtils.UPDATEDATAFIELDPREFIX);
		try {
			List<Map<String, Object>> mylistData = AssertUtil
					.fromJsonString((String) entity.get("JSON"));
			mylistData = QueryParamUtil.StringToDate(mylistData);
			System.out.println(mylistData);
			System.out.println("单位同步开始......");
			for (Map<String, Object> map : mylistData) {
				if (map.get("OPER_TYPE") != null&& map.get("ID_NUMBER") != null) {
					String is_oper = (String) map.get("OPER_TYPE");
					String is_id_number = (String) map.get("ID_NUMBER");
					Map<String, Object> query = new HashMap<String, Object>();
					query.put("UNIT_ID", is_id_number);
					List<Map<String,Object>> old_unit = business.list_unit(query);
					if (StringUtils.equalsIgnoreCase(is_oper, "INSERT")) {
						if (old_unit==null || old_unit.size()==0) {
							Map<String, Object> entity_unit = new HashMap<String, Object>();
							entity_unit.put("UUID", map.get("UNIT_ID"));
							entity_unit.put("UNIT_ID", map.get("ID_NUMBER"));
							entity_unit.put("NAME", map.get("UNIT_NAME"));
							entity_unit.put("PARENT_ID", map.get("PARENT_ID"));
							entity_unit.put("TYPE", "999");
							entity_unit.put("IS_ENTITY", "1");
							entity_unit.put("DELETE_FLAG", "0");
							entity_unit.put("ORDER_ID", "0");
							entity_unit.put("CREATE_TIME", new Date());
							entity_unit.put("CREATOR_ID", "system");
							entity_unit.put("CREATOR_NAME", "system");
							entity_unit.put("UPDATE_TIME", new Date());
							entity_unit.put("AREA_TYPE", "3");
							business.insert_unit(entity_unit);
						}else{
							jsonObject.putAll(Status.getStatusErrorMessage("新增数据已存在"));
							return "json";
						}
						
					} else if (StringUtils.equalsIgnoreCase(is_oper, "UPDATE")) {
						if (old_unit!=null && old_unit.size()>0 
								&& StringUtils.equals(old_unit.get(0).get("UUID").toString(), map.get("UNIT_ID").toString())
								&& StringUtils.equals(old_unit.get(0).get("UNIT_ID").toString(), map.get("ID_NUMBER").toString())) {
							
							Map<String, Object> entity_unit = new HashMap<String, Object>();
							entity_unit.put("UUID", map.get("UNIT_ID"));
							//entity_unit.put("UNIT_ID", map.get("ID_NUMBER"));
							entity_unit.put("NAME", map.get("UNIT_NAME"));
							entity_unit.put("PARENT_ID", map.get("PARENT_ID"));
							business.update_unit(entity_unit);
							
						}
						
					}
					jsonObject.putAll(Status.getStatus(Status.INSERT_OK));
				} else {
					jsonObject.putAll(Status.getStatusErrorMessage("JSON数据不全!"));
				}

			}

			System.out.println("单位同步结束......");

		} catch (Exception e) {
			jsonObject.putAll(Status.getStatusErrorMessage("数据保存失败!"));
			e.printStackTrace();
		}
		return "json";
	}
}
