package com.gaowj.formtable.action;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.gaowj.business.FormfieldBusiness;
import com.gaowj.business.FormtableBusiness;
import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.QueryParamUtil;
import com.gaowj.business.util.RequestUtils;
import com.gaowj.business.util.Status;
import com.gaowj.standard.utils.CacheKit;
import com.gaowj.util.Property;

public class actAction extends BasicAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1559759499504671708L;

	private JSONObject jsonObject = new JSONObject();

	public JSONObject getJsonObject() {
		return jsonObject;
	}

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

	private List<Property> LIST;

	public List<Property> getLIST() {
		return LIST;
	}

	public void setLIST(List<Property> lIST) {
		LIST = lIST;
	}

	/**
	 * 插入
	 * 
	 * @param entity
	 * @throws BusinessException
	 */
	public String insertFormTable() throws BusinessException {

		FormtableBusiness business = BusinessManager.getBusiness(FormtableBusiness.class);
		FormfieldBusiness fieldBusiness = BusinessManager.getBusiness(FormfieldBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		jsonObject = new JSONObject();
		try {
			entity = QueryParamUtil.StringToDate(entity);
			// 去除不要更新的字段
			entity.remove("OPETYPE");
			// 新增
			Map<String, Object> formTable = business.insert_form_table(entity);
			// 做子表的插入
			Map<String, Object> param = new HashMap<String, Object>();
			for (Property property : LIST) {
				if (property == null) {
					continue;
				}
				if (property.getCONTENT() != null && "" != property.getCONTENT()) {
					param.put("CONTENT", property.getCONTENT());
				}
				if (property.getTYPE().equals("date") || property.getTYPE().equals("text")) {
					param.put("LENGTH", null);
				} else {
					param.put("LENGTH", property.getLENGTH());
				}
				param.put("FIELD_WIDTH", property.getFIELD_WIDTH());
				param.put("FIELD_NAME", property.getNAME());
				param.put("POINT", property.getPOINT());
				param.put("TABLE_ID", formTable.get("UUID"));
				param.put("TYPE", property.getTYPE());
				param.put("ORDER_NUM", property.getORDER_NUM());
				if (property.getIS_NULL().equals("")) {
					param.put("IS_NULL", 0);
				} else {
					param.put("IS_NULL", 1);
				}
				if (property.getIS_KEY().equals("")) {
					param.put("IS_KEY", 0);
				} else {
					param.put("IS_KEY", 1);
				}
				if (property.getIS_SHOW_LIST().equals("")) {
					param.put("IS_SHOW_LIST", 0);
				} else {
					param.put("IS_SHOW_LIST", 1);
				}
				if (property.getFIELD_DEFAULT() != null && "" != property.getFIELD_DEFAULT()) {
					param.put("FIELD_DEFAULT", property.getFIELD_DEFAULT());
				}
				fieldBusiness.insert_form_field(param);
			}

			if (entity.get("IS_DBSYNCH").equals("1")) {
				String TABLE_ID = formTable.get("UUID").toString();
				String TABLE_NAME = formTable.get("TABLE_NAME").toString();
				String CONTENT = formTable.get("CONTENT").toString();
				List<Map<String, Object>> result = fieldBusiness.list_form_field_byTableid(TABLE_ID);
				Map<String, Object> query = new HashMap<String, Object>();
				query.put("tableName", TABLE_NAME);
				query.put("content", CONTENT);
				query.put("list", result);
				business.create_form_table(query);
			}

			jsonObject.putAll(Status.getStatusSuccessMessage("保存成功"));
		} catch (Exception e) {
			jsonObject.putAll(Status.getStatusErrorMessage("保存失败"));
			e.printStackTrace();
		}
		return "json";
	}

	/**
	 * 更新
	 * 
	 * @param entity
	 * @throws BusinessException
	 */
	public String updateFormTable() throws BusinessException {

		FormtableBusiness business = BusinessManager.getBusiness(FormtableBusiness.class);
		FormfieldBusiness fieldBusiness = BusinessManager.getBusiness(FormfieldBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		try {

			// 去除不要更新的字段
			entity.remove("OPETYPE");
			// 修改
			entity = QueryParamUtil.StringToDate(entity);
			business.update_form_table(entity);
			// 做子表的插入
			List<Map<String, Object>> listField = new ArrayList<Map<String, Object>>();
			Map<String, Object> param = null;
			for (Property property : LIST) {
				param = new HashMap<String, Object>();
				if (property == null) {
					continue;
				}
				if (property.getCONTENT() != null && "" != property.getCONTENT()) {
					param.put("CONTENT", property.getCONTENT());
				}
				param.put("UUID", property.getUUID());
				param.put("FIELD_NAME", property.getNAME());
				param.put("FIELD_WIDTH", property.getFIELD_WIDTH());
				param.put("LENGTH", property.getLENGTH());
				param.put("POINT", property.getPOINT());
				param.put("TABLE_ID", entity.get("UUID"));
				param.put("TYPE", property.getTYPE());
				param.put("ORDER_NUM", property.getORDER_NUM());
				if (property.getIS_NULL().equals("")) {
					param.put("IS_NULL", 0);
				} else {
					param.put("IS_NULL", 1);
				}
				if (property.getIS_KEY().equals("")) {
					param.put("IS_KEY", 0);
				} else {
					param.put("IS_KEY", 1);
				}
				if (property.getIS_SHOW_LIST().equals("")) {
					param.put("IS_SHOW_LIST", 0);
				} else {
					param.put("IS_SHOW_LIST", 1);
				}
				if (property.getFIELD_DEFAULT() != null && "" != property.getFIELD_DEFAULT()) {
					param.put("FIELD_DEFAULT", property.getFIELD_DEFAULT());
				}
				listField.add(param);
			}
			fieldBusiness.update_form_field(listField, entity.get("UUID").toString());
			jsonObject.putAll(Status.getStatusSuccessMessage("修改成功"));
		} catch (Exception e) {
			jsonObject.putAll(Status.getStatusErrorMessage("修改失败"));
			e.printStackTrace();
		}
		return "json";
	}

	/**
	 * 在软删除基础上删除记录
	 * 
	 * @param entity
	 * @throws BusinessException
	 */
	public String deleteFormTable() throws BusinessException {

		FormtableBusiness business = BusinessManager.getBusiness(FormtableBusiness.class);
		FormfieldBusiness fieldBusiness = BusinessManager.getBusiness(FormfieldBusiness.class);
		Map<String, Object> entity = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		String deletekey = (String) entity.get("CODE");
		List<String> ListKey = QueryParamUtil.StringToList(deletekey);

		try {
			business.delete_form_table(ListKey);
			fieldBusiness.delete_form_field_byTableid(ListKey);
			jsonObject.putAll(Status.getStatusSuccessMessage("删除成功"));
		} catch (Exception e) {
			jsonObject.putAll(Status.getStatusErrorMessage("删除失败"));
			e.printStackTrace();
		}
		return "json";
	}

	public String synchFormTable() throws BusinessException {
		FormfieldBusiness fieldBusiness = BusinessManager.getBusiness(FormfieldBusiness.class);
		FormtableBusiness tableBusiness = BusinessManager.getBusiness(FormtableBusiness.class);

		jsonObject = new JSONObject();
		try {
			String table_id = RequestUtils.getString(request, "tableId");
			String table_name = RequestUtils.getString(request, "tableName");
			String content = RequestUtils.getString(request, "content");
			List<Map<String, Object>> listField = fieldBusiness.list_form_field_byTableid(table_id);
			Map<String, Object> create_param = new HashMap<String, Object>();
			create_param.put("tableName", table_name);
			create_param.put("content", content);
			create_param.put("list", listField);
			tableBusiness.create_form_table(create_param);
			Map<String, Object> entity = new HashMap<String, Object>();
			entity.put("UUID", table_id);
			entity.put("IS_DBSYNCH", 1);
			tableBusiness.update_form_table(entity);
			jsonObject.putAll(Status.getStatusSuccessMessage("同步成功"));
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if(e.getMessage().indexOf("A table must have at least 1 column")!=-1){
				jsonObject.putAll(Status.getStatusErrorMessage("同步失败,一个表至少含有一个字段!"));
			}else{
				jsonObject.putAll(Status.getStatusErrorMessage("同步失败!"));
			}
			
			e.printStackTrace();
		}

		return "json";
	}

	public String listTableFile() throws BusinessException {
		FormfieldBusiness fieldBusiness = BusinessManager.getBusiness(FormfieldBusiness.class);
		String tableId = RequestUtils.getString(request, "UUID");
		List<Map<String, Object>> list = fieldBusiness.list_form_field_byTableid(tableId);
		String result = JSON.toJSONString(list);
		jsonObject.put("listfield", result);
		return "json";

	}

}