package com.zfire.jiasm.syncdata.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zfire.jiasm.syncdata.service.UploadService;
import com.zfire.jiasm.syncdata.utils.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UploadServiceImpl implements UploadService {
	private static String sql ="SELECT * FROM out_jiasm_to_gree_syn_rule where src_tab_name=\'%s\' order by orderno";


	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Map<String, Object>> getTaskData() {
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM out_jiasm_to_gree_syn_rule order by orderno ");
		return result;
	}
	@Override
	public List<Map<String, Object>> getTaskData(String table){
		String sqls= String.format(sql,table);
		List<Map<String, Object>> result = jdbcTemplate.queryForList(sqls);
		return result;
	}

	@Override
	public List<Map<String, Object>> getSourceData(String srcTabName) {
		List<Map<String, Object>> result = jdbcTemplate
				.queryForList("SELECT * FROM " + srcTabName +
						" where syn_status=0 order by syn_req_id ");
		return result;
	}

	@Override
	public void updateSuccess(String srcTabName, long reqId) {
		jdbcTemplate.update("UPDATE " + srcTabName + " set syn_status=1,syn_time=? where syn_req_id=?",
				new Object[] { new Date(), reqId });
	}

	@Override
	public void updateError(String srcTabName, long reqId, String errMessage) {
		if (errMessage != null && errMessage.trim().length() > 255) {
			errMessage = errMessage.substring(0, 255);
		}
		jdbcTemplate.update("UPDATE " + srcTabName + " set syn_err_msg=?,syn_time=? where syn_req_id=?",
				new Object[] { errMessage, new Date(), reqId });

	}

	@Override
	public void updateNotSync(String srcTabName,long reqId,String msg){
		int synStatus = 99;
		jdbcTemplate.update("UPDATE " + srcTabName +
						" set syn_err_msg=?,syn_time=?,syn_status=? where syn_req_id=?",
				new Object[] { msg, new Date(),synStatus,reqId });
	}

	@Override
	public void updateNotSyncT(String srcTabName,String reqId,String msg){
		int synStatus = 99;
		jdbcTemplate.update("UPDATE " + srcTabName +
						" set syn_err_msg=?,syn_time=?,syn_status=? where syn_req_id=?",
				new Object[] { msg, new Date(),synStatus,reqId });
	}




	@Override
	public void addAssignLog(JSONArray jsonArray, Map<String, Object> sourceData) throws Exception {
		Map<String, Object> result = jdbcTemplate.queryForMap(
				"select * from itf_gree_to_fa_download_task_rule where id=5", new Object[] {});
		String destTabColList = ((String) result.get("dest_tab_col_list"));
		String[] arySrcTabCol = destTabColList.split("\\Q,\\E");
		Object[] arySrcTabColValue = new Object[arySrcTabCol.length + 4];
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);

			arySrcTabColValue[0] = sourceData.get("syn_req_id");
			arySrcTabColValue[1] = sourceData.get("syn_req_time");
			arySrcTabColValue[2] = sourceData.get("syn_time");
			arySrcTabColValue[3] = sourceData.get("worker_order_no");

			for (int j = 4; j < arySrcTabColValue.length; j++) {
				arySrcTabColValue[j] = findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j - 4].trim()), aData);
			}
			insertAData("itf_tbl_assign_upload_after",
					"syn_req_id,syn_req_time,syn_time,worker_order_no," + destTabColList.trim(), arySrcTabColValue);

		}
	}

	@Override
	public void addAzAssignLog(JSONArray jsonArray, Map<String, Object> sourceData) throws Exception {
		
		Map<String, Object> result = jdbcTemplate.queryForMap(
				"select * from itf_gree_to_fa_download_task_rule where id=8", new Object[] {});
		String destTabColList = ((String) result.get("dest_tab_col_list"));
		String[] arySrcTabCol = destTabColList.split("\\Q,\\E");
		Object[] arySrcTabColValue = new Object[arySrcTabCol.length + 4];
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject aData = jsonArray.getJSONObject(i);

			arySrcTabColValue[0] = sourceData.get("syn_req_id");
			arySrcTabColValue[1] = sourceData.get("syn_req_time");
			arySrcTabColValue[2] = sourceData.get("syn_time");
			arySrcTabColValue[3] = sourceData.get("worker_order_no");

			for (int j = 4; j < arySrcTabColValue.length; j++) {
				arySrcTabColValue[j] = findValue(DataUtil.fromDbName2ServiceName(arySrcTabCol[j - 4].trim()), aData);
			}
			insertAData("itf_tbl_az_assign_lc_ls_upload_after",
					"syn_req_id,syn_req_time,syn_time,worker_order_no," + destTabColList.trim(), arySrcTabColValue);

		}

	}

	private String findValue(String srcTabCol, JSONObject aData) {

		Set<String> set = aData.keySet();
		for (String key : set) {
			if (key.equalsIgnoreCase(srcTabCol)) {
				return aData.getString(key);
			}
		}

		return null;

	}

	private void insertAData(String destTabName, String destTabColList, Object[] arySrcTabColValue) throws Exception {

		StringBuffer mark = new StringBuffer();
		for (int i = 0; i < arySrcTabColValue.length; i++) {
			if (mark.length() > 0) {
				mark.append(",");
			}
			mark.append("?");
		}

		String sql = "insert into " + destTabName + " (" + destTabColList + ") values (" + mark + ") ";

		jdbcTemplate.update(sql, arySrcTabColValue);

	}

}
