123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- package controller
- import (
- "demo/internal/app"
- "demo/internal/setting"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/siddontang/go/log"
- "io/ioutil"
- "net/http"
- "strconv"
- "strings"
- "time"
- )
- func GetDataByName(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- sqlnamestr := r.Get("name").String()
- offset := r.Get("offset").Int()
- pagecount := r.Get("pagecount").Int()
- returntype := r.Get("returntype").String()
- //tx := restful.Engine.NewSession()
- //err := tx.Begin()
- //defer func() {
- // switch {
- // case err != nil:
- // logging.Error("tx.Begin 事务启动失败__error:", err)
- // if tx != nil {
- // tx.Rollback()
- // }
- // default:
- // if tx != nil {
- // err = tx.Commit()
- // }
- // }
- // if tx != nil {
- // tx.Close()
- // }
- //}()
- sql, p, err := srv.GetSqlByNameDBT(sqlnamestr)
- if err != nil {
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- if sql != "" {
- s_params := make([]interface{}, 0)
- if !r.Get("parammaps").IsEmpty() {
- parammaps := r.Get("parammaps").Map()
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- //if parammaps["jwt_username"] != nil {
- s_params = append(s_params, r.Context().Value("jwt_username"))
- //} else {
- // s_params = append(s_params, "")
- //}
- } else {
- if parammaps[strings.Trim(value, " ")] == nil {
- s_params = append(s_params, "")
- } else {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- }
- } else if !r.Get("params").IsEmpty() {
- params := r.Get("params").Array()
- //logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- for _, v_params := range params {
- s_params = append(s_params, v_params)
- }
- }
- queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
- if err != nil {
- log.Error("GetDataByName err: ", err)
- appG.Response(http.StatusOK, app.ERROR, err)
- } else {
- appG.Response(http.StatusOK, app.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, app.SUCCESS, nil)
- }
- }
- func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
- //if offset > 0 {
- // offset = offset - 1
- //}
- //if pagecount == 0 {
- // pagecount = setting.AppSetting.PageSize
- //}else if pagecount>setting.AppSetting.PageSize{
- // pagecount = setting.AppSetting.PageSize
- //}
- if returntype == "list" {
- queryData, err := srv.QueryByListT(sqlstr, offset, pagecount, params)
- return queryData, err
- } else if returntype == "listlist" {
- queryData, err := srv.QueryByListListT(sqlstr, offset, pagecount, params)
- return queryData, err
- } else {
- fmt.Println(params)
- queryData, err := srv.QueryByMapT(sqlstr, offset, pagecount, params)
- return queryData, err
- }
- }
- func ExecDataByConfig(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- dataByte, _ := ioutil.ReadAll(r.Request.Body)
- tempval_ := make(map[string]interface{})
- tempCommon := make(map[string]interface{})
- tempval := make([]map[string]interface{}, 0)
- err := json.Unmarshal(dataByte, &tempval_)
- //logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
- // fmt.Println("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
- if err != nil {
- } else {
- if v, ok := tempval_["common"].(map[string]interface{}); ok {
- tempCommon = v
- }
- if v, ok := tempval_["data"].([]interface{}); ok {
- for _, Tvalue := range v {
- if v1, ok := Tvalue.(map[string]interface{}); ok {
- tempval = append(tempval, v1)
- }
- }
- }
- if err == nil {
- if r.Get("jwt_username").String() != "" {
- tempCommon["jwt_username"] = r.Get("jwt_username").String()
- }
- for _, paramvalue := range tempval {
- if _, ok := paramvalue["resultname"]; !ok {
- paramvalue["resultname"] = paramvalue["name"]
- }
- _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil)
- if err != nil {
- log.Error("ExecDataParam error:", err)
- break
- }
- }
- if err == nil {
- if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
- appG.Response(http.StatusOK, app.SUCCESS, tempval)
- } else {
- appG.Response(http.StatusOK, app.SUCCESS, "成功执行!")
- }
- } else {
- appG.Response(http.StatusOK, app.ERROR, err)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- }
- }
- }
- if err != nil {
- appG.Response(http.StatusOK, app.ERROR, err)
- //appG.Response(http.StatusOK, e.ERROR, err.Error())
- }
- }
- func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}) ([]map[string]interface{}, error) {
- sqlname := fasion["name"].(string)
- sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
- if v, ok := fasion["type"]; ok {
- sqltype = v.(string)
- }
- sql := ""
- p := ""
- var err error
- s_params := make([]interface{}, 0)
- if sqltype != "" {
- sql, p, err = srv.GetSqlByNameDBT(sqlname)
- if err != nil {
- return nil, err
- }
- //sqlApi, _ := models.GetApisqlByName(sqlname)
- if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- ss := ""
- if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- //ids, err := setting.SnowIds.NextId()
- //if err != nil {
- ids := time.Now().UnixNano()
- log.Info("create SnowIds err", err)
- //}
- s_params = append(s_params, ids)
- } else {
- if ss == "" && CommonParamMap != nil {
- if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
- ss = valuestring
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
- if CommonParamMap[valuestring[tempstre+1:]] != nil {
- ss = CommonParamMap[valuestring[tempstre+1:]].(string)
- }
- } else {
- ss = ""
- }
- }
- }
- if ss == "" && ParentParamMap != nil {
- ss = getParamValue_(v, value, ParentParamMap)
- }
- if ss == "" && sameParamMap != nil {
- ss = getParamValue(v, value, sameParamMap)
- }
- if ss == "" && ParamMap != nil {
- ss = getParamValue(v, value, ParamMap)
- }
- s_params = append(s_params, ss)
- }
- }
- }
- }
- }
- if sql == "" {
- sqltype = ""
- }
- if sqltype == "s1" {
- tempValParam, err := srv.QueryByMap(sql, 0, 1, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- }
- if err != nil {
- return nil, err
- }
- err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
- if err != nil {
- return nil, err
- }
- return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
- } else if sqltype == "sm" {
- tempValParam, err := srv.QueryByMap(sql, 0, 0, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- } else {
- }
- if err != nil {
- return nil, err
- }
- err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
- if err != nil {
- return nil, err
- }
- return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
- } else if sqltype == "v" {
- tempValParam, err := srv.QueryByMap(sql, 0, 1, s_params)
- if v, ok := tempValParam.(map[string]interface{}); ok {
- fasion["resultmaps"] = v["list"].([]map[string]interface{})
- } else {
- }
- if err != nil {
- return nil, err
- }
- if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
- return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
- }
- } else if sqltype == "e" {
- tempVal_arrary := make([]map[string]interface{}, 0)
- tempVal_map := make(map[string]interface{})
- tempValParam, err := srv.ExecQueryT(sql, s_params)
- if err == nil {
- fasion["resultmaps"] = tempValParam.(map[string]interface{})
- tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
- tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
- tempVal_arrary = append(tempVal_arrary, tempVal_map)
- return tempVal_arrary, err
- }
- return nil, err
- } else {
- if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
- err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
- if err != nil {
- return nil, err
- }
- return nil, err
- }
- }
- return nil, nil
- }
- func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
- if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if tempstrb > -1 && tempstre > -1 {
- if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
- if s, ok := v.(string); ok {
- return s
- } else if s, ok := v.(float64); ok {
- return strconv.FormatFloat(s, 'f', -1, 64)
- // return strconv.FormatInt(int64(s), 10)
- }
- }
- }
- }
- return ""
- }
- func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
- if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
- tempstrb := strings.IndexAny(valuestring, "@")
- tempstre := strings.IndexAny(valuestring, ".")
- if tempstrb > -1 && tempstre > -1 {
- for _, ParamMapList := range ParamMap {
- if v, ok := ParamMapList["resultname"]; ok {
- if v == valuestring[tempstrb+1:tempstre] {
- if v1, ok := ParamMapList["resultmaps"]; ok {
- if newA, ok := v1.([]map[string]interface{}); ok {
- return interfacetostring(newA[0][valuestring[tempstre+1:]])
- } else if newA, ok := v1.(map[string]interface{}); ok {
- return interfacetostring(newA[valuestring[tempstre+1:]])
- }
- }
- }
- }
- }
- } else {
- return valuestring
- }
- } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
- return strconv.FormatInt(int64(valuestring), 10)
- }
- return ""
- }
- func interfacetostring(val interface{}) string {
- switch val.(type) {
- case string:
- ss := val.(string)
- return ss
- break
- case int:
- ss := val.(int)
- return strconv.Itoa(ss)
- break
- case int64:
- ss := val.(int64)
- return strconv.FormatInt(ss, 10)
- break
- case float64:
- ss := val.(float64)
- return strconv.FormatFloat(ss, 'E', -1, 32)
- break
- }
- return ""
- }
- func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}) error {
- var err error
- subarray, ok := fasion["children"]
- if ok {
- for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
- tempVal_arrary := make([]map[string]interface{}, 0)
- for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
- tempVal_ := make(map[string]interface{})
- if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
- tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
- } else {
- tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
- }
- tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary) //递归调用执行
- if err != nil {
- return err
- }
- tempVal_arrary = append(tempVal_arrary, tempVal_)
- }
- v.(map[string]interface{})["children"] = tempVal_arrary
- }
- }
- return nil
- }
- func PostDataByName(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- //dataByte, _ := ioutil.ReadAll(c.Request.Body)
- //fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := r.Get("name").String()
- s_params := make([]interface{}, 0)
- sql, p, err := srv.GetSqlByNameDBT(sqlnamestr)
- if err != nil {
- log.Error(err)
- appG.Response(app.StatusInternalServerError, app.ERROR, err)
- return
- }
- if sql != "" {
- if !r.Get("parammaps").IsEmpty() {
- parammaps := r.Get("parammaps").Map()
- //logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if r.Get("jwt_username").String() != "" {
- s_params = append(s_params, r.Get("jwt_username").String())
- } else {
- s_params = append(s_params, "")
- }
- } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
- ids, err := setting.SnowIds.NextId()
- if err != nil {
- ids = time.Now().UnixNano()
- //logging.Info("create SnowIds err", err)
- }
- s_params = append(s_params, ids)
- } else {
- if parammaps[strings.Trim(value, " ")] == nil {
- s_params = append(s_params, "")
- } else {
- s_params = append(s_params, parammaps[strings.Trim(value, " ")])
- }
- }
- }
- }
- } else if !r.Get("params").IsEmpty() {
- params := r.Get("params").Array()
- for _, v_params := range params {
- s_params = append(s_params, v_params)
- }
- }
- queryData, err := execDataBySqlT(sql, s_params)
- if err != nil {
- log.Error("PostDataByName err: ", err)
- } else {
- appG.Response(http.StatusOK, app.SUCCESS, queryData)
- }
- } else {
- appG.Response(http.StatusOK, app.SUCCESS, nil)
- }
- }
- func execDataBySqlT(sqlstr string, params []interface{}) (interface{}, error) {
- queryData, err := srv.ExecQueryT(sqlstr, params)
- return queryData, err
- }
- func GetRecuDataByName(r *ghttp.Request) {
- appG := app.Ghttp{C: r}
- //dataByte, _ := ioutil.ReadAll(c.Request.Body)
- //fsion := gofasion.NewFasion(string(dataByte))
- sqlnamestr := r.Get("name").String()
- parentidname := r.Get("idname").String()
- sql, p := srv.GetSqlByNameDB(sqlnamestr)
- params := make(map[string]interface{})
- s_params := make([]interface{}, 0)
- if !r.Get("params").IsEmpty() {
- for _, v_params := range r.Get("params").Array() {
- s_params = append(s_params, v_params)
- }
- params["params"] = s_params
- } else {
- jparams := r.Get("parammaps").Map()
- paramslist := strings.Split(p, ",")
- if len(paramslist) > 0 && p != "" {
- for _, value := range paramslist {
- if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
- if r.Get("jwt_username").String() != "" {
- params[value] = r.Get("jwt_username").String()
- } else {
- params[value] = ""
- }
- } else {
- params[value] = jparams[strings.Trim(value, " ")]
- }
- }
- }
- }
- queryData, err := GetRecuData_BySql(sql, p, parentidname, params, !r.Get("params").IsEmpty())
- //logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
- if err != nil {
- appG.Response(http.StatusOK, app.ERROR, err.Error())
- }
- appG.Response(http.StatusOK, app.SUCCESS, queryData)
- }
- func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
- paramslist := strings.Split(Params, ",")
- s_params := make([]interface{}, 0)
- if isList {
- for _, v_params := range thisParams["params"].([]interface{}) {
- s_params = append(s_params, v_params)
- }
- } else {
- paramslist := strings.Split(Params, ",")
- if len(paramslist) > 0 && Params != "" {
- for _, value := range paramslist {
- s_params = append(s_params, thisParams[strings.Trim(value, " ")])
- }
- }
- }
- queryData, err := srv.QueryByMapMenu(sql, 0, 0, s_params)
- for _, queryData_ := range queryData {
- if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
- if isList {
- //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
- if queryData_[idname] != nil {
- thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
- }
- } else {
- if queryData_[idname] != nil {
- thisParams[idname] = queryData_[idname].(interface{})
- }
- if len(paramslist) > 0 && Params != "" {
- s_params = make([]interface{}, 0)
- for _, value := range paramslist {
- s_params = append(s_params, thisParams[strings.Trim(value, " ")])
- }
- }
- }
- queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
- } else {
- }
- }
- return queryData, err
- }
|