package com.api.aiyh_pcn.copy_attachment.dao;

import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.copy_attachment.model.ConfigEmpty;
import weaver.conn.RecordSet;
import weaver.general.Util;

/**
 * @author EBU7-dev1-ayh
 * @create 2021/8/26 0026 18:21
 * config
 */


public class ConfigTableData {

	public static ConfigEmpty getConfig(String workflowId) {
		ToolUtil toolUtil = new ToolUtil();
		RecordSet rs = new RecordSet();
//		查询配置表,获取配置数据
		String querySql = "select ufta.id,ufta.workflow_type,wftva.fieldname as template_field,  " +
				"wftvb.fieldname as attachment_field,wftv.fieldname as show_field, wftvb.id as attachment_field_id,  " +
				"ufta.is_template_show,ufta.model_table_name, ufta.model_field,wsl.selectvalue as show_value,  " +
				"wftv.tablename as show_table_name,wftva.tablename as template_table_name,wftvb.tablename as attachment_table_name,  " +
				"wsla.selectvalue as hidden_value from uf_temp_attachment as ufta  " +
				"left join workflow_selectitem as wsl on wsl.id = ufta.show_value " +
				"left join workflow_selectitem as wsla on wsla.id = ufta.hidden_value " +
				"left join workflow_field_table_view as wftv on wftv.id = ufta.show_field " +
				"left join workflow_field_table_view as wftva on wftva.id = ufta.template_field " +
				"left join workflow_field_table_view as wftvb on wftvb.id = ufta.attachment_field " +
				"where workflow_type in " +
				"(select id from workflow_base where activeVersionID in " +
				"(select activeVersionID  from workflow_base where id = ?) or id = ?) and attachment_type = ?";
		rs.executeQuery(querySql, workflowId, workflowId,0);
		if(rs.next()){
//			id
			int id = Util.getIntValue(rs.getString("id"));
//			显示值
			int showValue = Util.getIntValue(rs.getString("show_value"));
//			隐藏值
			int hiddenValue = Util.getIntValue(rs.getString("hidden_value"));
//			流程id
			String workflowType = rs.getString("workflow_type");
//			模板字段
			String templateField = rs.getString("template_field");
//			附件字段
			String attachmentField = rs.getString("attachment_field");
//			是否用字段控制模板
			String isTemplateShow = rs.getString("is_template_show");
//			模板建模表表名
			String modelTableName = rs.getString("model_table_name");
//			控制模板显示与否的字段
			String showField = rs.getString("show_field");
//			建模表福建模板字段
			String modelField = rs.getString("model_field");
//			控制显示所在表
			String showTableName = rs.getString("show_table_name");
//			模板字段所在表
			String templateTableName = rs.getString("template_table_name");
//			附件字段所在表
			String attachmentTableName = rs.getString("attachment_table_name");
//			附件字段id
			String attachmentFieldId = rs.getString("attachment_field_id");
			toolUtil.writeDebuggerLog("已经查询到数据!");
			return ConfigEmpty.create()
					.id(id)
					.showValue(showValue)
					.hiddenValue(hiddenValue)
					.workflowType(workflowType)
					.templateField(templateField)
					.attachmentField(attachmentField)
					.isTemplateShow(isTemplateShow)
					.modelTableName(modelTableName)
					.showField(showField)
					.modelField(modelField)
					.showTableName(showTableName)
					.templateTableName(templateTableName)
					.attachmentTableName(attachmentTableName)
					.attachmentFieldId(attachmentFieldId)
					.builder();
		}
		return null;
	}


	public static String[] getTemplateData(String tableName, String fieldName, String configId){
		RecordSet rs = new RecordSet();
		String queryBuilder = "select " + fieldName +
				" from " +
				tableName +
				" where id = ?";
		ToolUtil toolUtil = new ToolUtil();
		toolUtil.writeDebuggerLog("接收到参数tableName{" + tableName + "},fieldName{" + fieldName + "}configId{" + configId);
		toolUtil.writeDebuggerLog("sql:" + queryBuilder);
		rs.executeQuery(queryBuilder,configId);
		if(rs.next()){
			String docIds = Util.null2String(rs.getString(1));
			toolUtil.writeDebuggerLog("查询到数据:" + docIds);
			return docIds.split(",");
		}
		return new String[0];
	}

}