package com.gx.example.itemapply.web.action;

import com.gx.example.itemapply.business.factory.Factory;
import com.gx.example.itemapply.business.valueobject.ApplyChildModel;
import com.gx.example.itemapply.business.valueobject.ApplyMainModel;
import com.gx.webpage.component.model.*;
import com.gx.webpage.engine.WebPageHelper;

import javax.servlet.http.*;
import java.util.*;

/**
 * 页面取得展示值的响应类
 * @author Administrator
 *
 */
public class ApplyValueAction implements WebPageValueApi {
	/**
	 *
	 * @param wpm 页面定义模型
	 * @param paramMap 取值的参数Map,格式是：key-数据库字段，value-参数值，由需要显示的取值页面来传入
	 * @param userId 操作的用户编号
	 * @param request 客户端请求的HttpServletRequest
	 * @return 填充好值的WebPageDbModel实例
	 * @throws Exception
	 */
	public WebPageDbModel getWebPageDbModel(WebPageModel wpm, Map paramMap,
			String userId, HttpServletRequest request) throws Exception {
		WebPageDbModel wpdm = new WebPageDbModel();
		//首先获取查询的参数值，一个是查询主表的，一个是查询子表的
		String uuid = "";
		uuid = (String) paramMap.get("uuid");
		if (uuid == null || uuid.trim().length() == 0) {
			uuid = (String) paramMap.get("tbl_shenQing_fk".toUpperCase());
		}

		//1：从逻辑层获取相应的数据	    
		ApplyMainModel amm = Factory.getInstance().getApplyEbi().getAMMByUuid(
				uuid);
		//2：把数据封装成WebPage3通用的WebPageDbModel

		//主表的值
		String mainUuid[] = new String[1];
		String title[] = new String[1];
		String description[] = new String[1];
		String shenQingRen[] = new String[1];
		String shiJian[] = new String[1];

		mainUuid[0] = amm.getMainUuid();
		title[0] = amm.getTitle();
		description[0] = amm.getDescription();
		shenQingRen[0] = amm.getShenQingRen();
		shiJian[0] = "" + amm.getShiJian_Hidden();

		//子表的值
		int len = amm.getColChild().size();
		String childUuid[] = new String[len];
		String tbl_shenQing_fk[] = new String[len];
		String files[] = new String[len];
		String description_c[] = new String[len];

		Iterator it = amm.getColChild().iterator();
		int index = 0;
		while (it.hasNext()) {
			ApplyChildModel acm = (ApplyChildModel) it.next();
			childUuid[index] = acm.getChildUuid();
			tbl_shenQing_fk[index] = uuid;
			files[index] = acm.getFiles_Hidden();
			description_c[index] = acm.getDescription_c();

			index++;
		}
		//开始赋值,注意这里的key值必须跟界面上组件的id一样，要对应起来
		wpdm.getMapValues().put("mainUuid".toUpperCase(), mainUuid);
		wpdm.getMapValues().put("title".toUpperCase(), title);
		wpdm.getMapValues().put("description".toUpperCase(), description);
		wpdm.getMapValues().put("shenQingRen".toUpperCase(), shenQingRen);
		wpdm.getMapValues().put("shiJian".toUpperCase(), shiJian);
		wpdm.getMapValues().put("childUuid".toUpperCase(), childUuid);
		wpdm.getMapValues().put("tbl_shenQing_fk".toUpperCase(),
				tbl_shenQing_fk);
		wpdm.getMapValues().put("files".toUpperCase(), files);
		wpdm.getMapValues().put("description_c".toUpperCase(), description_c);

		wpdm.setPageUuid(wpm.getPageUuid());
		wpdm.setMaxRow(len);

		return wpdm;
	}

}
