package service

import (
	"DataShare/dao"
	"DataShare/global"
	"DataShare/model"
	"DataShare/service/api_proxy"
	"DataShare/util/gabs"
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/sirupsen/logrus"
	"reflect"
	"regexp"
	"strings"
)

func Business2_GetAccessInfo(accessId string) (string,*model.Access_Service_Struct,*model.Access_Data_Struct,error){

	key := fmt.Sprintf("access_%s",accessId)
	body, err := global.RedisClient.Get(key).Result()
	if err != nil {
		return "",nil,nil, err
	}

	var tempMap map[string]interface{}
	err = json.Unmarshal([]byte(body), &tempMap)
	if err != nil {
		//panic(err)
		return "",nil,nil,err
	}
	//for k, values := range tempMap {
	//	fmt.Printf("%s: %s\n", k, values) // ... and this line
	//}
	if tempMap["resType"]=="service"{
		//接口服务
		var accessValue model.Access_Service_Struct
		err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue)
		if err != nil{
			global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error()))
			return "",nil,nil, err
		}

		return "Service",&accessValue,nil,nil
	}else{
		//数据资源
		var accessValue model.Access_Data_Struct
		err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue)
		if err != nil{
			global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error()))
			return "",nil,nil, err
		}

		return "Data",nil,&accessValue,nil
	}

	//var accessValue model.AccessInfo
	//err = json.NewDecoder(strings.NewReader(body)).Decode(&accessValue)
	//if err != nil{
	//	global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("json转结构体出错, err:%s",err.Error()))
	//	return nil, err
	//}
	//
	//return &accessValue,nil

	//colArry := make([]model.ColNumDet,0)
	//
	//item := model.ColNumDet{
	//	Name:                "name",
	//	Type:                "varchar",
	//	IsDesensitization: 0,
	//	DesensitizationRule: model.DesensitizationRule{
	//	},
	//}
	//colArry = append(colArry,item)
	//item = model.ColNumDet{
	//	Name:                "identifyId",
	//	Type:                "varchar",
	//	IsDesensitization: 1,
	//	//DesensitizationRule: model.DesensitizationRule{
	//	//	Left: 3,
	//	//	Right: 4,
	//	//	Fill: "*******",
	//	//},
	//	DesensitizationRule: model.DesensitizationRule{
	//		RegStr: "^(.{3}).+(.{4})$",
	//		FillStr: "$1****$2",
	//	},
	//}
	//colArry = append(colArry,item)
	//
	//item = model.ColNumDet{
	//	Name:                "rysj",
	//	Type:                "varchar",
	//	IsDesensitization: 1,
	//	DesensitizationRule: model.DesensitizationRule{
	//		RegStr: "^(.{3}).+(.{4})$",
	//		FillStr: "$1****$2",
	//	},
	//}
	//colArry = append(colArry,item)
	//
	//info := model.AccessInfo{
	//	Enable: 1,
	//	DbId:   1,
	//	TbName: "inpatient_info",
	//	Colnum: colArry,
	//}
	//
	//return &info,nil
}

func getSqlQueryParam(req *model.RequestBody)(string) {

	orParam := make([]string,0)

	for i:=0; i<len(req.Search.OR);i++{
		item := req.Search.OR[i]
		keySplit := strings.Split(item.KeyStr,".")
		if len(keySplit) < 2 {
			continue
		}
		if keySplit[1] == "like" {
			or := fmt.Sprintf("%s like %s",keySplit[0],item.KeyVal)
			orParam = append(orParam,or)
		}else if keySplit[1] == "eq"{
			or := fmt.Sprintf("%s=%s",keySplit[0],item.KeyVal)
			orParam = append(orParam,or)
		}else if keySplit[1] == "lt"{
			or := fmt.Sprintf("%s<%s",keySplit[0],item.KeyVal)
			orParam = append(orParam,or)
		}else if keySplit[1] == "gt"{
			or := fmt.Sprintf("%s>%s",keySplit[0],item.KeyVal)
			orParam = append(orParam,or)
		}
	}

	andParam := make([]string,0)
	for i:=0;i<len(req.Search.AND);i++{
		item := req.Search.AND[i]
		keySplit := strings.Split(item.KeyStr,".")
		if len(keySplit) < 2 {
			continue
		}
		if keySplit[1] == "like" {
			or := fmt.Sprintf("%s like %s",keySplit[0],item.KeyVal)
			andParam = append(andParam,or)
		}else if keySplit[1] == "eq"{
			or := fmt.Sprintf("%s=%s",keySplit[0],item.KeyVal)
			andParam = append(andParam,or)
		}else if keySplit[1] == "lt"{
			or := fmt.Sprintf("%s<%s",keySplit[0],item.KeyVal)
			andParam = append(andParam,or)
		}else if keySplit[1] == "gt"{
			or := fmt.Sprintf("%s>%s",keySplit[0],item.KeyVal)
			andParam = append(andParam,or)
		}
	}

	queryParam := ""
	if len(orParam)>0{
		queryParam += "("
		for i:=0;i<len(orParam);i++{
			if i < len(orParam)-1{
				queryParam += orParam[i] + " or "
			}else{
				queryParam += orParam[i]
			}
		}
		queryParam += ")"
	}
	if len(andParam) > 0 {
		queryParam += " and "
		for i:=0;i<len(andParam);i++{
			if i < len(andParam)-1{
				queryParam += orParam[i] + " and "
			}else{
				queryParam += orParam[i]
			}
		}
	}

	return queryParam
}

//func Business2_QueryTable(req *model.RequestBody,accessInfo *model.AccessInfo)(*model.CommonList,error){
//	//第一步：去出要查询的字段
//	dbInfo := getDataBaseInfoById(accessInfo.DbId)
//	if dbInfo==nil{
//		return nil,errors.New("数据库不存在!")
//	}
//
//	if dbInfo.Status == false {
//		return nil,errors.New(fmt.Sprintf("database:%s not connect",dbInfo.DbName))
//	}
//
//	if dbInfo.SqlType == "mysql" {
//		sql := "select"
//		if req.PageSize > 0 {
//			sql += " SQL_CALC_FOUND_ROWS"
//		}
//		args := make([]interface{},0)
//		first := 1
//		for i:=0;i<len(accessInfo.Colnum);i++{
//			if first == 0{
//				sql += ","
//			}else{
//				first = 0
//			}
//
//			if accessInfo.Colnum[i].IsDesensitization == 1 {
//				if accessInfo.Colnum[i].DesensitizationRule.Left > 0 && accessInfo.Colnum[i].DesensitizationRule.Right > 0{
//					sql += "concat(left(%s, %d),'%s',right(%s, %d)) AS %s"
//					args = append(args,accessInfo.Colnum[i].Name)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Left)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill)
//					args = append(args,accessInfo.Colnum[i].Name)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Right)
//					args = append(args,accessInfo.Colnum[i].Name)
//				}else if accessInfo.Colnum[i].DesensitizationRule.Left > 0 {
//					sql += "concat(left(%s, %d),'%s') AS %s"
//					args = append(args,accessInfo.Colnum[i].Name)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Left)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill)
//					args = append(args,accessInfo.Colnum[i].Name)
//				}else if accessInfo.Colnum[i].DesensitizationRule.Right > 0{
//					sql += "concat('%s',right(%s, %d)) AS %s"
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Fill)
//					args = append(args,accessInfo.Colnum[i].Name)
//					args = append(args,accessInfo.Colnum[i].DesensitizationRule.Right)
//					args = append(args,accessInfo.Colnum[i].Name)
//				}else{
//					sql +=" %s"
//					args = append(args,accessInfo.Colnum[i].Name)
//				}
//			}else {
//				sql +=" %s"
//				args = append(args,accessInfo.Colnum[i].Name)
//			}
//		}
//		sql += " from %s"
//		args = append(args,accessInfo.TbName)
//
//		queryParam := getSqlQueryParam(req,accessInfo)
//		if len(queryParam)> 0 {
//			sql += " where "+queryParam
//		}
//		if req.PageSize > 0 {
//			sql += " limit %d,%d"
//			args = append(args,(req.PageNo-1)*req.PageSize)
//			args = append(args,req.PageSize)
//		}
//		sqlStr := fmt.Sprintf(sql,args...)
//		fmt.Println("------------")
//		fmt.Println(sqlStr)
//		fmt.Println("------------")
//		items,_,_,total,_ := dao.GetData(dbInfo.DbHandle,sqlStr)
//
//		result := &model.CommonList{
//			Total: total,
//			Items: items,
//		}
//		//todo：
//		//脱敏处理
//
//		return result,nil
//	}
//
//	return nil,nil
//}
func Business2_QueryTable(req *model.DbQuery,accessInfo *model.Access_Data_Struct)(*model.CommonList,error){

	//第一步：去出要查询的字段
	dbInfo := getDataBaseInfoById(accessInfo.ResData.DbId)
	if dbInfo==nil{
		return nil,errors.New("数据库不存在!")
	}

	if dbInfo.Status == false {
		return nil,errors.New(fmt.Sprintf("database:%s not connect",dbInfo.DbName))
	}
	//fmt.Println("--------------------------1-------------------------")
	if dbInfo.SqlType == "mysql" {
		sql := "select"
		if req.PageSize > 0 {
			sql += " SQL_CALC_FOUND_ROWS"
		}
		args := make([]interface{},0)
		first := 1
		if req.QueryColn == nil {
			sql += " *"
		}else{
			for i:=0;i<len(req.QueryColn);i++{
				if first == 0{
					sql += ","
				}else{
					first = 0
				}
				sql +=" %s"
				args = append(args,req.QueryColn[i])
			}
			if len(req.QueryColn) == 0 {
				sql += " *"
			}
		}
		//fmt.Println("--------------------------2-------------------------")

		sql += " from %s"
		args = append(args,accessInfo.ResData.TbName)
		where := 0
		//用户查询参数
		for zz:=0;zz<len(req.Query);zz++{
			if len(req.Query[zz].ColName) > 0 {
				if where == 0 {
					sql += " where"
					where = 1
				}else{
					sql += " and"
				}

				if req.Query[zz].Cond == 0 {
					sql += " %s='%s'"
				}else if req.Query[zz].Cond == 1 {
					sql += " %s>'%s'"
				}else{
					sql += " %s<'%s'"
				}
				args = append(args,req.Query[zz].ColName)
				args = append(args,req.Query[zz].ColVal)
			}
		}
		//拼接默认参数
		cols := accessInfo.ResData.Colnums
		for zz:=0;zz<len(cols);zz++{
			//if cols[zz].IsSearchable != nil && *cols[zz].IsSearchable == 1 {
				if cols[zz].FilterOperator != nil && len(*cols[zz].FilterOperator) > 0 && cols[zz].FilterValue != nil && len(*cols[zz].FilterValue) > 0 {
					if where == 0 {
						sql += " where"
						where = 1
					}else{
						sql += " and"
					}
					//0-等于 1-大于 2-小于
					if *cols[zz].FilterOperator == "0" {
						sql += " %s='%s'"
					}else if *cols[zz].FilterOperator == "1" {
						sql += " %s>'%s'"
					}else if *cols[zz].FilterOperator == "2" {
						sql += " %s<'%s'"
					}
					args = append(args,cols[zz].Name)
					args = append(args,*cols[zz].FilterValue)
				}
			//}
		}
		//排序
		if len(req.Sort.ColName) > 0 {
			if req.Sort.IsDesc == 1 {
				sql += " order by %s desc"
			}else {
				sql += " order by %s"
			}
			args = append(args,req.Sort.ColName)
		}
		//分页
		if req.PageSize > 0 {
			sql += " limit %d,%d"
			args = append(args,(req.PageNo-1)*req.PageSize)
			args = append(args,req.PageSize)
		}
		sqlStr := fmt.Sprintf(sql,args...)

		//fmt.Println("---------------------------------------------------")
		//fmt.Println(sqlStr)
		//fmt.Println("---------------------------------------------------")
		//查询
		items,_,_,total,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums)
		result := &model.CommonList{
			Total: total,
			Items: items,
		}
		//todo：
		//脱敏处理
		return result,nil
	}else if dbInfo.SqlType == "postgres" || dbInfo.SqlType == "kingbase" {

		sql := "select"
		sqlCount := "select count(*)"
		args_count := make([]interface{},0)
		args := make([]interface{},0)
		first := 1
		if req.QueryColn == nil {
			sql += " *"
		}else{
			for i:=0;i<len(req.QueryColn);i++{
				if first == 0{
					sql += ","
				}else{
					first = 0
				}
				sql +=" %s"
				args = append(args,req.QueryColn[i])
			}
			if len(req.QueryColn) == 0 {
				sql += " *"
			}
		}

		sql += " from %s"
		args = append(args,accessInfo.ResData.TbName)
		args_count = append(args_count,accessInfo.ResData.TbName)
		where := 0
		//用户查询参数
		for zz:=0;zz<len(req.Query);zz++{
			if len(req.Query[zz].ColName) > 0 {
				if where == 0 {
					sql += " where"
					sqlCount += " where"
					where = 1
				}else{
					sql += " and"
					sqlCount += " and"
				}

				if req.Query[zz].Cond == 0 {
					sql += " %s='%s'"
					sqlCount += " %s='%s'"
				}else if req.Query[zz].Cond == 1 {
					sql += " %s>'%s'"
					sqlCount += " %s>'%s'"
				}else{
					sql += " %s<'%s'"
					sqlCount += " %s<'%s'"
				}
				args = append(args,req.Query[zz].ColName)
				args = append(args,req.Query[zz].ColVal)

				args_count = append(args_count,req.Query[zz].ColName)
				args_count = append(args_count,req.Query[zz].ColVal)
			}
		}
		//拼接默认参数
		cols := accessInfo.ResData.Colnums
		for zz:=0;zz<len(cols);zz++{
			//if cols[zz].IsSearchable != nil && *cols[zz].IsSearchable == 1 {
				if cols[zz].FilterOperator != nil && len(*cols[zz].FilterOperator) > 0 && cols[zz].FilterValue != nil && len(*cols[zz].FilterValue) > 0 {
					if where == 0 {
						sql += " where"
						sqlCount += " where"
						where = 1
					}else{
						sql += " and"
						sqlCount += " and"
					}
					//0-等于 1-大于 2-小于
					if *cols[zz].FilterOperator == "0" {
						sql += " %s='%s'"
					}else if *cols[zz].FilterOperator == "1" {
						sql += " %s>'%s'"
					}else if *cols[zz].FilterOperator == "2" {
						sql += " %s<'%s'"
					}
					args = append(args,cols[zz].Name)
					args = append(args,*cols[zz].FilterValue)
					args_count = append(args_count,cols[zz].Name)
					args_count = append(args_count,*cols[zz].FilterValue)
				}
			//}
		}
		//统计总数
		sqlCntStr := fmt.Sprintf(sqlCount,args_count...)
		total,_:= dao.GetCount(dbInfo.DbHandle,sqlCntStr)
		//排序
		if len(req.Sort.ColName) > 0 {
			if req.Sort.IsDesc == 1 {
				sql += " order by %s desc"
			}else {
				sql += " order by %s"
			}
			args = append(args,req.Sort.ColName)
		}
		//分页
		if req.PageSize > 0 {
			sql += " limit %d,%d"
			args = append(args,(req.PageNo-1)*req.PageSize)
			args = append(args,req.PageSize)
		}
		sqlStr := fmt.Sprintf(sql,args...)
		//查询
		items,_,_,_,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums)
		result := &model.CommonList{
			Total: total,
			Items: items,
		}
		return result,nil
	}

	return nil,nil
}

//func Business2_QueryTable(req *model.RequestBody,accessInfo *model.Access_Data_Struct)(*model.CommonList,error){
//	//第一步：去出要查询的字段
//	dbInfo := getDataBaseInfoById(accessInfo.ResData.DbId)
//	if dbInfo==nil{
//		return nil,errors.New("数据库不存在!")
//	}
//
//	if dbInfo.Status == false {
//		return nil,errors.New(fmt.Sprintf("database:%s not connect",dbInfo.DbName))
//	}
//
//	if dbInfo.SqlType == "mysql" {
//		sql := "select"
//		if req.PageSize > 0 {
//			sql += " SQL_CALC_FOUND_ROWS"
//		}
//		args := make([]interface{},0)
//		first := 1
//		for i:=0;i<len(accessInfo.ResData.Colnums);i++{
//			if first == 0{
//				sql += ","
//			}else{
//				first = 0
//			}
//
//			sql +=" %s"
//			args = append(args,accessInfo.ResData.Colnums[i].Name)
//		}
//		sql += " from %s"
//		args = append(args,accessInfo.ResData.TbName)
//
//		queryParam := getSqlQueryParam(req)
//		if len(queryParam)> 0 {
//			sql += " where "+queryParam
//		}
//		if req.PageSize > 0 {
//			sql += " limit %d,%d"
//			args = append(args,(req.PageNo-1)*req.PageSize)
//			args = append(args,req.PageSize)
//		}
//		sqlStr := fmt.Sprintf(sql,args...)
//		fmt.Println("------------")
//		fmt.Println(sqlStr)
//		fmt.Println("------------")
//		items,_,_,total,_ := dao.GetData2(dbInfo.DbHandle,sqlStr,accessInfo.ResData.Colnums)
//
//		result := &model.CommonList{
//			Total: total,
//			Items: items,
//		}
//		//todo：
//		//脱敏处理
//
//		return result,nil
//	}
//
//	return nil,nil
//}

func getAccessParamByName(key string,access *model.Access_Service_Struct)(*model.Access_Service_Params){

	for i:=0;i<len(access.ResData.Params);i++{
		if key == access.ResData.Params[i].ParamName {
			return &access.ResData.Params[i]
		}
	}
	return nil
}

//总队标准接口服务解析校验
func Business2_Cq119NormalApiVerify(dataByte []byte,access *model.Access_Service_Struct)(*model.DataShareNormalResponse,error) {

	fmt.Println("---resp:", string(dataByte))
	var cq119NormalResp model.CQ119Response
	err := json.NewDecoder(strings.NewReader(string(dataByte))).Decode(&cq119NormalResp)
	if err != nil {
		global.SystemLogger.Log(logrus.ErrorLevel, fmt.Sprintf("json转结构体出错, err:%s", err.Error()))
		return nil, err
	}

	normalRespPacket := &model.DataShareNormalResponse{
		Code:    cq119NormalResp.Code,
		Page:    cq119NormalResp.Page,
		Size:    cq119NormalResp.Size,
		Total:   cq119NormalResp.Total,
		Columns: cq119NormalResp.Columns,
	}

	for i := 0; i < len(cq119NormalResp.Data); i++ {
		fmt.Println("===================index:",i)
		item := cq119NormalResp.Data[i]
		v := reflect.ValueOf(item)

		if v.Kind() == reflect.Map {
			n := make(map[string]interface{})
			for _, key := range v.MapKeys() {
				strct := v.MapIndex(key)
				fmt.Println(key.Interface(),":", strct.Interface())
				newValue := strct.Interface()
				param := getAccessParamByName(key.Interface().(string),access)
				if param != nil {
					if param.DesensitizationRule != nil {
						//脱敏
						if len(param.DesensitizationRule.RegStr)>0 && len(param.DesensitizationRule.FillStr)>0{
							reg := regexp.MustCompile(param.DesensitizationRule.RegStr)
							newValue = reg.ReplaceAllString(strct.Interface().(string),param.DesensitizationRule.FillStr)
						}
					}
					n[key.Interface().(string)]=newValue
				}
			}
			if len(n)>0{
				normalRespPacket.Data = append(normalRespPacket.Data, n)
			}
		}
	}

	//v, err := json.Marshal(normalRespPacket)

	return normalRespPacket,nil
}

//数据缓存
func Business2_CacheParser(dataByte []byte,req *model.DataCacheOfRedis) (*model.CQ119Response,[]map[string]interface{},error){

	var cq119NormalResp model.CQ119Response
	err := json.NewDecoder(strings.NewReader(string(dataByte))).Decode(&cq119NormalResp)
	if err != nil {
		global.SystemLogger.Log(logrus.ErrorLevel, fmt.Sprintf("json转结构体出错, err:%s", err.Error()))
		return nil,nil,err
	}
	serviceConfig := req.ServiceConfig
	column_map := serviceConfig.ColumnMap
	rows_array := make([]map[string]interface{},0)

	for i := 0; i < len(cq119NormalResp.Data);i++{
		fmt.Println("===================index:",i)
		item := cq119NormalResp.Data[i]
		v := reflect.ValueOf(item)

		if v.Kind() == reflect.Map {
			n := make(map[string]interface{})
			for _, key := range v.MapKeys() {
				strct := v.MapIndex(key)
				fmt.Println(key.Interface(),":", strct.Interface())
				for m:=0;m<len(column_map);m++{
					if key.Interface().(string) == column_map[m].FieldName {
						n[column_map[m].ColumnName] = strct.Interface()
						break
					}
				}
			}
			if len(n)>0{
				rows_array = append(rows_array, n)
			}
		}
	}
	//fmt.Println("------------print rows_array------------------")
	//for i:=0;i<len(rows_array);i++{
	//	item := rows_array[i]
	//	for k,v := range item {
	//		fmt.Println("key:",k,"value:",v)
	//	}
	//}
	//fmt.Println("--------------end print-----------------------")

	return &cq119NormalResp,rows_array,nil
}
func Business2_DataCache_DbConnect(dbInfo *model.CacheDbConfig) (*sql.DB,error){
	find := false
	for i:=0;i<len(global.Databases);i++{
		if dbInfo.DbId == global.Databases[i].Id{
			find = true
			return global.Databases[i].DbHandle,nil
		}
	}
	if find == false {
		//连接数据库
		db, err := dao.DbConnect(dbInfo.DbType,dbInfo.ConnectionString)
		if err != nil {
			return nil,err
		}

		info := global.DbInfo{
			DbHandle: db,
			ConnInfo: dbInfo.ConnectionString,
			Status:   true,
			SqlType: dbInfo.DbType,
			DbName: "",
			Id: dbInfo.DbId,
		}
		global.Databases = append(global.Databases, info)
		return db,nil
	}

	return nil,errors.New("连接数据库失败")
}
//数据保存
func Business2_DataSave(rows_array []map[string]interface{},dbInfo *model.CacheDbConfig)(error){

	//写数据表

	column_data := model.ColnDataInsert{
		Id:           dbInfo.DbId,
		TbName:       dbInfo.TbName,
	}
	for i:=0;i<len(rows_array);i++{
		item := rows_array[i]
		for k, _ := range item {
			//fmt.Printf("--------%s: %s\n", k, values) // ... and this line
			column_data.ColNameArray = append(column_data.ColNameArray,k)
		}
		break
	}
	for i:=0;i<len(rows_array);i++{
		var row model.ColnItem
		item := rows_array[i]
		for j:=0;j<len(column_data.ColNameArray);j++ {
			name := column_data.ColNameArray[j]
			row.List = append(row.List,item[name].(string))
		}
		//for k, values := range item {
		//	fmt.Printf("%s: %s\n", k, values) // ... and this line
		//	row.List = append(row.List,values.(string))
		//}
		column_data.Rows = append(column_data.Rows,row)
	}

	return DatabaseInsert(&column_data)
}
func Business2_DataCacheTest(){

	data_cache_list := dao.LoadDataCacheList()
	for i:=0;i< len(data_cache_list);i++{
		_,err := Business2_DataCache_DbConnect(&data_cache_list[i].DbConfig)
		if err != nil {
			continue
		}
		serviceConfig := data_cache_list[i].ServiceConfig
		if serviceConfig.AuthWay == "cq119"{
			_,body,err := api_proxy.CQ119_GetInstance().CQ119_ApiCall(serviceConfig.ServiceUrl,[]byte(serviceConfig.RequestParam))
			if err != nil {
				global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("serviceUrl:%s ,cache data failed.",serviceConfig.ServiceUrl))
				continue
			}
			_,rows_array,err := Business2_CacheParser(body,&data_cache_list[i])
			//save
			Business2_DataSave(rows_array,&data_cache_list[i].DbConfig)
		}
	}
}

func Business2_GetApiFieldName(columnName string,dataCacheConfig *model.DataCacheOfRedis) (string,error){

	for i:=0;i<len(dataCacheConfig.ServiceConfig.ColumnMap);i++{
		if dataCacheConfig.ServiceConfig.ColumnMap[i].ColumnName == columnName{
			return dataCacheConfig.ServiceConfig.ColumnMap[i].FieldName,nil
		}
	}

	return "",errors.New("未找到搜索参数名称")
}

func Business2_DataCache(dataCacheConfig *model.DataCacheOfRedis) error{

	db,err := Business2_DataCache_DbConnect(&dataCacheConfig.DbConfig)
	if err != nil {
		return err
	}

	serviceConfig := dataCacheConfig.ServiceConfig
	if serviceConfig.AuthWay == "119"{
		//组装参数
		if dataCacheConfig.Mode == "full"{
			//全量：删除数据表，重新同步数据
			pageNo := 1
			pageSize := 10000
			total := 0
			for ;;{
				//参数
				paramMap := make(map[string]interface{},0)
				err := json.Unmarshal([]byte(serviceConfig.RequestParam),&paramMap)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error()))
					//return err
				}
				paramMap[dataCacheConfig.PageField] = pageNo
				paramMap[dataCacheConfig.PageSizeField] = pageSize

				newParam,err := json.Marshal(&paramMap)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error()))
					return err
				}

				_,body,err := api_proxy.CQ119_GetInstance().CQ119_ApiCall(serviceConfig.ServiceUrl,newParam)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("serviceUrl:%s ,cache data failed.",serviceConfig.ServiceUrl))
					return err
				}
				if serviceConfig.AuthWay == "gov" {
					_,body,err = api_proxy.GovDataFormatToResource(body)
				}

				resp,rows_array,err := Business2_CacheParser(body,dataCacheConfig)
				if len(rows_array)<1{
					global.SystemLogger.Log(logrus.InfoLevel,fmt.Sprintf("serviceUrl:%s ,cache data complete.",serviceConfig.ServiceUrl))
					break;
				}
				if total == 0 {
					total = resp.Total
				}
				//save
				Business2_DataSave(rows_array,&dataCacheConfig.DbConfig)
				pageNo += 1
				if pageNo*pageSize >= total{
					break
				}
				break;
			}
		}else if dataCacheConfig.Mode == "add"{
			total := 0
			pageNo := 1
			pageSize := 100
			//增量:按照增量标识，获取最大值，作为条件
			maxValue,err := dao.GetMaxValue(dataCacheConfig.AddField,dataCacheConfig.DbConfig.TbName,db)
			if err != nil{
				return err
			}
			fieldName,err := Business2_GetApiFieldName(dataCacheConfig.AddField,dataCacheConfig)
			if err != nil {
				return err
			}

			//参数
			for ;;{
				//var paramMap map[string]interface{}

				paramMap := make(map[string]interface{},0)
				err := json.Unmarshal([]byte(serviceConfig.RequestParam),&paramMap)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error()))
					//return err
				}
				paramMap[dataCacheConfig.PageField] = pageNo
				paramMap[dataCacheConfig.PageSizeField] = pageSize

				if maxValue != "<nil>" {
					if paramMap[dataCacheConfig.SearchField] != nil {
						searchNode,err := gabs.ParseJSON([]byte(paramMap[dataCacheConfig.SearchField].(string)))
						if err != nil {
							return err
						}
						searchNode.Set(maxValue,fmt.Sprintf("%s.gt",fieldName))
						paramMap[dataCacheConfig.SearchField] = searchNode.String()
					}else{
						paramMap[dataCacheConfig.SearchField] = fmt.Sprintf("{\"%s.gt\":\"%s\"}",fieldName,maxValue)
					}
				}else{
					//paramMap[dataCacheConfig.SearchField] = model.NullObj{}
				}

				newParam,err := json.Marshal(&paramMap)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("Business2_DataCache failed, errmsg:"+err.Error()))
					return err
				}
				fmt.Println("-------------------------------")
				fmt.Println(string(newParam))
				fmt.Println("---------------------------------")
				_,body,err := api_proxy.CQ119_GetInstance().CQ119_ApiCall(serviceConfig.ServiceUrl,newParam)
				if err != nil {
					global.SystemLogger.Log(logrus.ErrorLevel,fmt.Sprintf("serviceUrl:%s ,cache data failed.",serviceConfig.ServiceUrl))
					return err
				}
				if serviceConfig.AuthWay == "gov" {
					_,body,err = api_proxy.GovDataFormatToResource(body)
				}

				resp,rows_array,err := Business2_CacheParser(body,dataCacheConfig)
				if len(rows_array)<1{
					global.SystemLogger.Log(logrus.InfoLevel,fmt.Sprintf("serviceUrl:%s ,cache data complete.",serviceConfig.ServiceUrl))
					break;
				}

				if total == 0 {
					total = resp.Total
				}
				//save
				Business2_DataSave(rows_array,&dataCacheConfig.DbConfig)
				pageNo += 1

				if pageNo*pageSize >= total{
					break
				}
				break
			}
		}

	}

	return nil
}



