package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataBpmFieldAction extends DefaultAction{
	//当前数据源绑定的表名
	private static final String THIS_DS_TABLE_NAME="tbl_data_bpm_field";
	@Resource
	private BaseService baseService;
	@Resource
	private UserService userService;
	@Resource
	private ApiService apiService;
	private static DataBpmFieldAction action;
	@PostConstruct
	public void init(){
		action=this;
		action.baseService=this.baseService;
		action.userService=this.userService;
		action.apiService =this.apiService;
	}

	public Map<String,Object> getBpmFieldByFormCode(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
		JackJSONObject json= JackJSONObject.fromObject(jsonStr);
		//注意这两个code是动态传过来的，骆驼命名和下划线命名不一样，骆驼命名是能找到对应的实现类，下划线就是纯参数。
		String formCode= String.valueOf(json.get("form_code"));
		String dsCode= String.valueOf(json.get("ds_code"));
		String sql="select * from tbl_data_bpm_field where form_code='"+formCode+"' and status=0";
		List<Map<String, Object>> fieldList= this.getJdbcTemplate().queryForList(sql);
		String fieldStr="";
		List<Map<String, Object>> dictList=new ArrayList<>();
		for (int i = 0; i < fieldList.size(); i++) {
			fieldStr+=fieldList.get(i).get("field_name")+",";
		}
		if(fieldStr!=""){
			fieldStr=fieldStr.substring(0,fieldStr.length()-1);
		}
		Map<String,Object> dsMap=action.apiService.findDataSourceByCode(dsCode);
		String showFieldSql="SHOW FULL COLUMNS FROM "+dsMap.get("ds_table_name");
		List<Map<String,Object>> list=this.getJdbcTemplate().queryForList(showFieldSql);
		for (int i = 0; i < list.size(); i++) {
			Map<String,Object> dictMap=new HashMap<>();
			String field=String.valueOf(list.get(i).get("field"));
			dictMap.put("key",field);
			dictMap.put("value",field+"==="+list.get(i).get("comment"));
			dictList.add(dictMap);
		}
		Map<String,Object> retMap=new HashMap<>();
		Map<String,Object> map=new HashMap<>();
		map.put("fieldData",fieldStr);
		map.put("dictData",dictList);
		retMap.put("message","查询成功");
		retMap.put("data",map);
		return map;
	}

	public JackJSONArray bpmFieldSaveBefore(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
		for (int i = 0; i < array.size(); i++) {
			Map<String,Object> map=new HashMap<>();
			String dsCode=String.valueOf(array.getJackJSONObject(i).get("ds_code"));
			String formCode=String.valueOf(array.getJackJSONObject(i).get("form_code"));
			this.getJdbcTemplate().execute("delete from tbl_data_bpm_field where form_code='"+formCode+"'");
			map.put("ds_code",dsCode);
			map.put("form_code",formCode);
			map.put("status","0");
			String fieldName=String.valueOf(array.getJackJSONObject(i).get("field_name"));
			String[] fieldArray = fieldName.split(",");
			for(int j=0;j<fieldArray.length;j++) {
				map.put("field_name",fieldArray[j]);
				action.baseService.save("tbl_data_bpm_field",map);
				map.remove("id");
			}
		}
		retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("ok")));
		Map<String,Object> map=new HashMap<>();
		map.put("saveData",array);
		retJson.put("data",map);
		return new JackJSONArray();
	}
}
