package com.api.aiyh_pcn.fadada.dao;

import aiyh.utils.Util;
import aiyh.utils.sqlUtil.sqlResult.impl.PrepSqlResultImpl;
import aiyh.utils.sqlUtil.whereUtil.Where;
import aiyh.utils.zwl.common.ToolUtil;
import com.api.aiyh_pcn.fadada.entity.FaDaDaConfigDTO;
import weaver.aiyh_pcn.fadada.entity.FileInfo;
import weaver.conn.RecordSet;
import weaver.workflow.workflow.WorkflowVersion;

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

/**
 * @author EBU7-dev1-ayh
 * @create 2021/9/30 0030 11:18
 * 数据
 */


public class FaDDServiceMapping {
	private final RecordSet rs = new RecordSet();
	private final ToolUtil toolUtil = new ToolUtil();

//	private final String MAIN_TABLE = "formtable_main_17";
//	private final String DETAIL_TABLE = "formtable_main_17_dt1";

	public FaDaDaConfigDTO queryConfig(String workflowId, int type) {
		String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
		String query = "select main.id,main.workflow_type,main.api_type, main.params_config, " +
				"wf.fieldname field_control, " +
				"(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem where FIND_IN_SET(id,main.check_personal)) check_personal, " +
				"(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem where FIND_IN_SET(id,main.check_enterprise)) check_enterprise,  " +
				"main.check_source_type,wdt.tablename check_source " +
				"from uf_contract_config  main " +
				"left join workflow_field_table_view  wf on wf.id = main.field_control " +
				"left join workflow_detail_table_view wdt on wdt.id = main.check_source and wdt.workflow_id = main.workflow_type " +
				"where main.workflow_type in ( " + versionStringByWfid + ") and api_type = ?";
		rs.executeQuery(query, type);
		return Util.recordeSet2Entity(rs, FaDaDaConfigDTO.class, true);
	}

	public List<FileInfo> queryImgFileIdByDocIds(String fileIds) {
		String query = "select imagefileid,imagefilename from docimagefile where docid in ( " + fileIds + " )";
		rs.executeQuery(query);
		return Util.recordeSet2Array(rs, FileInfo.class);
	}

	public List<Map<String, Object>> queryDetailInfo(String requestId,String workflowId,int type) {

		String mainTable = this.getMainTable(workflowId);
		toolUtil.writeDebuggerLog("进入查询数据库方法");
		String query = "select id from " + mainTable + " where requestid = ?";
		rs.executeQuery(query, requestId);
		rs.next();
		String mainId = rs.getString("id");
		String detailTable = this.getDetailTable(workflowId, type);
		query = "select * from " + detailTable + " where mainid = ? and ( contract_status in (1,6) or contract_status is null) ";
		this.toolUtil.writeDebuggerLog(query);
		RecordSet rs = new RecordSet();
		rs.executeQuery(query, mainId);
		List<Map<String, Object>> maps = null;
		try {
			maps = Util.recordSet2MapList(rs);
			toolUtil.writeDebuggerLog("查询到状态:" + maps);
		} catch (Exception e) {
			e.printStackTrace();
			toolUtil.writeErrorLog("工具类出现异常:" + e);
		}
		return maps == null ? new ArrayList<>() : maps;
	}

	public List<Map<String, Object>> queryDetailDownInfo(String requestId,String workflowId,int type) {

		String mainTable = this.getMainTable(workflowId);
		toolUtil.writeDebuggerLog("进入查询数据库方法");
		String query = "select id from " + mainTable + " where requestid = ?";
		rs.executeQuery(query, requestId);
		rs.next();
		String mainId = rs.getString("id");
		String detailTable = this.getDetailTable(workflowId, type);
		query = "select * from " + detailTable + " where mainid = ?";
		this.toolUtil.writeDebuggerLog(query);
		RecordSet rs = new RecordSet();
		rs.executeQuery(query, mainId);
		List<Map<String, Object>> maps = null;
		try {
			maps = Util.recordSet2MapList(rs);
			toolUtil.writeDebuggerLog("查询到状态:" + maps);
		} catch (Exception e) {
			e.printStackTrace();
			toolUtil.writeErrorLog("工具类出现异常:" + e);
		}
		return maps == null ? new ArrayList<>() : maps;
	}


	public List<Map<String, Object>> querySignedInfo(String requestId, String workflowId) {
		String mainTable = this.getMainTable(workflowId);
		String query = "select id from " + mainTable + " where requestid = ? and signed_oneself = 1";
		rs.executeQuery(query, requestId);
		return Util.recordSet2MapList(rs);
	}

	public void updateContractStatus(Map<String, Object> map,String workflowId,int type) {
		String detailTable = this.getDetailTable(workflowId,type);
		Map<String, Object> updateData = new HashMap<>();
		updateData.put("contract_status", map.get("contract_status"));
		Where where = Util.createPrepWhereImpl().whereAnd("id").whereEqual(map.get("id"));
		PrepSqlResultImpl sqlResult = Util.createSqlBuilder().updateSql(detailTable, updateData, where);
		rs.executeUpdate(sqlResult.getSqlStr(), sqlResult.getArgs());
	}

	public String getAllVersion(String versionStringByWfid) {
		String query = "select distinct workflow_type from uf_contract_config where workflow_type in (" + versionStringByWfid +  ")";
		rs.executeQuery(query);
		rs.next();
		return rs.getString(1);
	}

	public String getMainTable(String workflowId){
		String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
		String query = "select tablename from workflow_bill " +
				" where id in (select formid from workflow_base " +
				" where id in (" + versionStringByWfid + ") )";
		rs.executeQuery(query);
		rs.next();
		String mainTable = rs.getString(1);
		toolUtil.writeDebuggerLog("mainTable:" + mainTable);
		return mainTable;
	}

	public String getDetailTable(String workflowId, int type){
		FaDaDaConfigDTO faDaDaConfigDTO = this.queryConfig(workflowId, type);
		String detailTable = faDaDaConfigDTO.getCheckSource();
		toolUtil.writeDebuggerLog("detailTable:" + detailTable);
		return detailTable;
	}

	public String getNodes(String versionStringByWfid, String markOnly) {
		String query = "select workflow_nodes from uf_node_config where workflow_type in (" + versionStringByWfid + ") and mark_only = ?";
		rs.executeQuery(query,markOnly);
		rs.next();
		return rs.getString(1);
	}

/*	public FaDDConfigMainDTO getConfigParam(String workflowId) {
		String versionStringByWfid = WorkflowVersion.getVersionStringByWfid(workflowId);
		System.out.println(versionStringByWfid);
		String query = "select main.id,main.workflow_type, wf.fieldname field_control, " +
				"(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem where FIND_IN_SET(id,main.check_personal)) check_personal, " +
				"(select GROUP_CONCAT('',selectvalue,'') selectvalue from workflow_selectitem where FIND_IN_SET(id,main.check_enterprise)) check_enterprise, " +
				"main.check_source_type,wdt.tablename check_source " +
				"from uf_attestation_conf  main " +
				"left join workflow_field_table_view  wf on wf.id = main.field_control " +
				"left join workflow_detail_table_view wdt on wdt.id = main.check_source and wdt.workflow_id = main.workflow_type " +
				"where main.workflow_type in ( " + versionStringByWfid + ")";
		rs.executeQuery(query);
		FaDDConfigMainDTO faDDConfigMainDTO = Util.recordeSet2Entity(rs, FaDDConfigMainDTO.class, true);
		query = "select dt.id,dt.line_num,dt.param_name,dt.param_type,dt.object_child,dt.parent_line,dt.change_rule, " +
				"dt.param_value,wf.fieldname workflow_field,wf.tablename tablename,dt.array_sql,dt.api_type " +
				"from uf_attestation_conf_dt1 dt " +
				"left join workflow_field_table_view wf on wf.id = dt.workflow_field  " +
				"where dt.mainid = ?";
		rs.executeQuery(query, faDDConfigMainDTO.getId());
		List<FaDDConfigDetailDTO> faDDConfigDetailDTOS = Util.recordeSet2Array(rs, FaDDConfigDetailDTO.class, true);
		faDDConfigMainDTO.setList(faDDConfigDetailDTOS);
		return faDDConfigMainDTO;
	}*/
}