package com.alucn.weblab.service;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alucn.casemanager.server.common.constant.Constant;
import com.alucn.casemanager.server.common.util.DateUtil;
import com.alucn.casemanager.server.common.util.JdbcUtil;
import com.alucn.casemanager.server.common.util.ParamUtil;
import com.alucn.weblab.dao.impl.ErrorCaseDaoImpl;
/**
 * @author haiqiw
 * 2017年6月23日 下午2:02:28
 * desc:ErrorCaseInfoService
 */
@Service("errorCaseInfoService")
public class ErrorCaseInfoService {
	
	@Autowired(required=true)
	private ErrorCaseDaoImpl errorCaseDaoImpl;
	private Map<String, String> errroCases;
	private Map<String, String>failedReason;
	 
	public Map<String, String> getErrorCaseInfo(String userName, String auth) throws Exception{
		errroCases = new HashMap<String, String>();
		String dbFile = ParamUtil.getUnableDynamicRefreshedConfigVal("CaseInfoDB");
		JdbcUtil jdbc = new JdbcUtil(Constant.DATASOURCE, dbFile);
		String getFeatureOfUser = "SELECT DISTINCT feature_number FROM DailyCase WHERE 1=1";
		if(!auth.equals(Constant.AUTH)){
			getFeatureOfUser = getFeatureOfUser+" and author='"+userName+"'";
		}
		ArrayList<HashMap<String, Object>> result = errorCaseDaoImpl.query(jdbc, getFeatureOfUser);
		for(int i=0; i<result.size();i++){
			Map<String, Object> obj = result.get(i);
			for(String key: obj.keySet()){
				String featureNum = (String)obj.get(key);
				String getCaseName = "SELECT case_name FROM DailyCase WHERE feature_number='" + featureNum+"' AND case_status='F'";
				ArrayList<HashMap<String, Object>> resultCaseName = errorCaseDaoImpl.query(jdbc, getCaseName);
				if(resultCaseName.size()>0){
					errroCases.put(featureNum, String.valueOf(resultCaseName.size()));
				}
			}
		}
		return errroCases;
	}
	
	public ArrayList<HashMap<String, Object>> getErrorCaseInfo(String featureName, String author, String auth) throws Exception{
		String dbFile = ParamUtil.getUnableDynamicRefreshedConfigVal("CaseInfoDB");
		JdbcUtil jdbc = new JdbcUtil(Constant.DATASOURCE, dbFile);
		String getErrorCase = "SELECT casename, err_reason FROM errorcaseinfo WHERE 1=1 and feature='"+featureName+"'";
		if(!auth.equals(Constant.AUTH)){
			getErrorCase = getErrorCase+" and owner='"+author+"'";
		}
		ArrayList<HashMap<String, Object>> result = errorCaseDaoImpl.query(jdbc, getErrorCase);
		return result;
	}
	
	public ArrayList<HashMap<String, Object>> getErrorCaseReason() throws Exception{
		failedReason = new HashMap<String, String>();
		String dbFile = ParamUtil.getUnableDynamicRefreshedConfigVal("CaseInfoDB");
		JdbcUtil jdbc = new JdbcUtil(Constant.DATASOURCE, dbFile);
		String getErrorType = "SELECT error_tpye, err_mark FROM caseerrortype;";
		ArrayList<HashMap<String, Object>> result = errorCaseDaoImpl.query(jdbc, getErrorType);
		for(int i=0; i<result.size();i++){
			Map<String, Object> obj = result.get(i);
			for(String key: obj.keySet()){
				failedReason.put((String)obj.get(key), null);
			}
		}
		return result;
	}
	
	public void setMarkCase(String userName, String featureName, String errorcases, String failedreasons) throws Exception{
		String dbFile = ParamUtil.getUnableDynamicRefreshedConfigVal("CaseInfoDB");
		JdbcUtil jdbc = new JdbcUtil(Constant.DATASOURCE, dbFile);
		for(String acase : errorcases.split(",")){
//			String markCaseSql = "UPDATE  errorcaseinfo SET err_reason='"+failedreasons+"', mark_date='"+curDate+"' WHERE casename='"+ acase +"' AND owner='"+ userName + "' AND feature='"+featureName.trim()+"'";
			String markCaseSql = "UPDATE  errorcaseinfo SET err_reason='"+failedreasons+"', mark_date='"+DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss")+"' WHERE casename='"+ acase +"' AND feature='"+featureName.trim()+"'";
			errorCaseDaoImpl.insert(jdbc, markCaseSql);
		}
	}
}

@@ -1145,30 +1145,27 @@
 ture
-_number FROM DailyCase
+ FROM errorcaseinfo
  WHE
@@ -1253,21 +1253,20 @@
 r+%22 and 
-autho
+owne
 r='%22+use
@@ -1572,17 +1572,16 @@
 ECT case
-_
 name FRO
@@ -1586,17 +1586,21 @@
 ROM 
-DailyCase
+errorcaseinfo
  WHE
@@ -1613,19 +1613,11 @@
 ture
-_number
 ='%22
- 
 + fe
@@ -1631,28 +1631,8 @@
 m+%22'
- AND case_status='F'
 %22;%0A%09

fix error case bug