db.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package controller
  2. import (
  3. "demo/internal/app"
  4. "demo/internal/setting"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/gogf/gf/v2/net/ghttp"
  9. "github.com/siddontang/go/log"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func GetDataByName(r *ghttp.Request) {
  17. appG := app.Ghttp{C: r}
  18. sqlnamestr := r.Get("name").String()
  19. offset := r.Get("offset").Int()
  20. pagecount := r.Get("pagecount").Int()
  21. returntype := r.Get("returntype").String()
  22. //tx := restful.Engine.NewSession()
  23. //err := tx.Begin()
  24. //defer func() {
  25. // switch {
  26. // case err != nil:
  27. // logging.Error("tx.Begin 事务启动失败__error:", err)
  28. // if tx != nil {
  29. // tx.Rollback()
  30. // }
  31. // default:
  32. // if tx != nil {
  33. // err = tx.Commit()
  34. // }
  35. // }
  36. // if tx != nil {
  37. // tx.Close()
  38. // }
  39. //}()
  40. sql, p, err := srv.GetSqlByNameDBT(sqlnamestr)
  41. if err != nil {
  42. log.Error(err)
  43. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  44. return
  45. }
  46. if sql != "" {
  47. s_params := make([]interface{}, 0)
  48. if !r.Get("parammaps").IsEmpty() {
  49. parammaps := r.Get("parammaps").Map()
  50. paramslist := strings.Split(p, ",")
  51. if len(paramslist) > 0 && p != "" {
  52. for _, value := range paramslist {
  53. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  54. //if parammaps["jwt_username"] != nil {
  55. s_params = append(s_params, r.Context().Value("jwt_username"))
  56. //} else {
  57. // s_params = append(s_params, "")
  58. //}
  59. } else {
  60. if parammaps[strings.Trim(value, " ")] == nil {
  61. s_params = append(s_params, "")
  62. } else {
  63. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  64. }
  65. }
  66. }
  67. }
  68. } else if !r.Get("params").IsEmpty() {
  69. params := r.Get("params").Array()
  70. //logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  71. for _, v_params := range params {
  72. s_params = append(s_params, v_params)
  73. }
  74. }
  75. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
  76. if err != nil {
  77. log.Error("GetDataByName err: ", err)
  78. appG.Response(http.StatusOK, app.ERROR, err)
  79. } else {
  80. appG.Response(http.StatusOK, app.SUCCESS, queryData)
  81. }
  82. } else {
  83. appG.Response(http.StatusOK, app.SUCCESS, nil)
  84. }
  85. }
  86. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  87. //if offset > 0 {
  88. // offset = offset - 1
  89. //}
  90. //if pagecount == 0 {
  91. // pagecount = setting.AppSetting.PageSize
  92. //}else if pagecount>setting.AppSetting.PageSize{
  93. // pagecount = setting.AppSetting.PageSize
  94. //}
  95. if returntype == "list" {
  96. queryData, err := srv.QueryByListT(sqlstr, offset, pagecount, params)
  97. return queryData, err
  98. } else if returntype == "listlist" {
  99. queryData, err := srv.QueryByListListT(sqlstr, offset, pagecount, params)
  100. return queryData, err
  101. } else {
  102. fmt.Println(params)
  103. queryData, err := srv.QueryByMapT(sqlstr, offset, pagecount, params)
  104. return queryData, err
  105. }
  106. }
  107. func ExecDataByConfig(r *ghttp.Request) {
  108. appG := app.Ghttp{C: r}
  109. dataByte, _ := ioutil.ReadAll(r.Request.Body)
  110. tempval_ := make(map[string]interface{})
  111. tempCommon := make(map[string]interface{})
  112. tempval := make([]map[string]interface{}, 0)
  113. err := json.Unmarshal(dataByte, &tempval_)
  114. //logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  115. // fmt.Println("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  116. if err != nil {
  117. } else {
  118. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  119. tempCommon = v
  120. }
  121. if v, ok := tempval_["data"].([]interface{}); ok {
  122. for _, Tvalue := range v {
  123. if v1, ok := Tvalue.(map[string]interface{}); ok {
  124. tempval = append(tempval, v1)
  125. }
  126. }
  127. }
  128. if err == nil {
  129. if r.Get("jwt_username").String() != "" {
  130. tempCommon["jwt_username"] = r.Get("jwt_username").String()
  131. }
  132. for _, paramvalue := range tempval {
  133. if _, ok := paramvalue["resultname"]; !ok {
  134. paramvalue["resultname"] = paramvalue["name"]
  135. }
  136. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil)
  137. if err != nil {
  138. log.Error("ExecDataParam error:", err)
  139. break
  140. }
  141. }
  142. if err == nil {
  143. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  144. appG.Response(http.StatusOK, app.SUCCESS, tempval)
  145. } else {
  146. appG.Response(http.StatusOK, app.SUCCESS, "成功执行!")
  147. }
  148. } else {
  149. appG.Response(http.StatusOK, app.ERROR, err)
  150. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  151. }
  152. }
  153. }
  154. if err != nil {
  155. appG.Response(http.StatusOK, app.ERROR, err)
  156. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  157. }
  158. }
  159. 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) {
  160. sqlname := fasion["name"].(string)
  161. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  162. if v, ok := fasion["type"]; ok {
  163. sqltype = v.(string)
  164. }
  165. sql := ""
  166. p := ""
  167. var err error
  168. s_params := make([]interface{}, 0)
  169. if sqltype != "" {
  170. sql, p, err = srv.GetSqlByNameDBT(sqlname)
  171. if err != nil {
  172. return nil, err
  173. }
  174. //sqlApi, _ := models.GetApisqlByName(sqlname)
  175. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  176. paramslist := strings.Split(p, ",")
  177. if len(paramslist) > 0 && p != "" {
  178. for _, value := range paramslist {
  179. ss := ""
  180. if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  181. //ids, err := setting.SnowIds.NextId()
  182. //if err != nil {
  183. ids := time.Now().UnixNano()
  184. log.Info("create SnowIds err", err)
  185. //}
  186. s_params = append(s_params, ids)
  187. } else {
  188. if ss == "" && CommonParamMap != nil {
  189. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  190. ss = valuestring
  191. tempstrb := strings.IndexAny(valuestring, "@")
  192. tempstre := strings.IndexAny(valuestring, ".")
  193. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  194. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  195. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  196. }
  197. } else {
  198. ss = ""
  199. }
  200. }
  201. }
  202. if ss == "" && ParentParamMap != nil {
  203. ss = getParamValue_(v, value, ParentParamMap)
  204. }
  205. if ss == "" && sameParamMap != nil {
  206. ss = getParamValue(v, value, sameParamMap)
  207. }
  208. if ss == "" && ParamMap != nil {
  209. ss = getParamValue(v, value, ParamMap)
  210. }
  211. s_params = append(s_params, ss)
  212. }
  213. }
  214. }
  215. }
  216. }
  217. if sql == "" {
  218. sqltype = ""
  219. }
  220. if sqltype == "s1" {
  221. tempValParam, err := srv.QueryByMap(sql, 0, 1, s_params)
  222. if v, ok := tempValParam.(map[string]interface{}); ok {
  223. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  224. }
  225. if err != nil {
  226. return nil, err
  227. }
  228. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
  229. if err != nil {
  230. return nil, err
  231. }
  232. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  233. } else if sqltype == "sm" {
  234. tempValParam, err := srv.QueryByMap(sql, 0, 0, s_params)
  235. if v, ok := tempValParam.(map[string]interface{}); ok {
  236. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  237. } else {
  238. }
  239. if err != nil {
  240. return nil, err
  241. }
  242. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
  243. if err != nil {
  244. return nil, err
  245. }
  246. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  247. } else if sqltype == "v" {
  248. tempValParam, err := srv.QueryByMap(sql, 0, 1, s_params)
  249. if v, ok := tempValParam.(map[string]interface{}); ok {
  250. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  251. } else {
  252. }
  253. if err != nil {
  254. return nil, err
  255. }
  256. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  257. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  258. }
  259. } else if sqltype == "e" {
  260. tempVal_arrary := make([]map[string]interface{}, 0)
  261. tempVal_map := make(map[string]interface{})
  262. tempValParam, err := srv.ExecQueryT(sql, s_params)
  263. if err == nil {
  264. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  265. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  266. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  267. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  268. return tempVal_arrary, err
  269. }
  270. return nil, err
  271. } else {
  272. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  273. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return nil, err
  278. }
  279. }
  280. return nil, nil
  281. }
  282. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  283. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  284. tempstrb := strings.IndexAny(valuestring, "@")
  285. tempstre := strings.IndexAny(valuestring, ".")
  286. if tempstrb > -1 && tempstre > -1 {
  287. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  288. if s, ok := v.(string); ok {
  289. return s
  290. } else if s, ok := v.(float64); ok {
  291. return strconv.FormatFloat(s, 'f', -1, 64)
  292. // return strconv.FormatInt(int64(s), 10)
  293. }
  294. }
  295. }
  296. }
  297. return ""
  298. }
  299. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  300. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  301. tempstrb := strings.IndexAny(valuestring, "@")
  302. tempstre := strings.IndexAny(valuestring, ".")
  303. if tempstrb > -1 && tempstre > -1 {
  304. for _, ParamMapList := range ParamMap {
  305. if v, ok := ParamMapList["resultname"]; ok {
  306. if v == valuestring[tempstrb+1:tempstre] {
  307. if v1, ok := ParamMapList["resultmaps"]; ok {
  308. if newA, ok := v1.([]map[string]interface{}); ok {
  309. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  310. } else if newA, ok := v1.(map[string]interface{}); ok {
  311. return interfacetostring(newA[valuestring[tempstre+1:]])
  312. }
  313. }
  314. }
  315. }
  316. }
  317. } else {
  318. return valuestring
  319. }
  320. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  321. return strconv.FormatInt(int64(valuestring), 10)
  322. }
  323. return ""
  324. }
  325. func interfacetostring(val interface{}) string {
  326. switch val.(type) {
  327. case string:
  328. ss := val.(string)
  329. return ss
  330. break
  331. case int:
  332. ss := val.(int)
  333. return strconv.Itoa(ss)
  334. break
  335. case int64:
  336. ss := val.(int64)
  337. return strconv.FormatInt(ss, 10)
  338. break
  339. case float64:
  340. ss := val.(float64)
  341. return strconv.FormatFloat(ss, 'E', -1, 32)
  342. break
  343. }
  344. return ""
  345. }
  346. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}) error {
  347. var err error
  348. subarray, ok := fasion["children"]
  349. if ok {
  350. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  351. tempVal_arrary := make([]map[string]interface{}, 0)
  352. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  353. tempVal_ := make(map[string]interface{})
  354. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  355. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  356. } else {
  357. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  358. }
  359. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary) //递归调用执行
  360. if err != nil {
  361. return err
  362. }
  363. tempVal_arrary = append(tempVal_arrary, tempVal_)
  364. }
  365. v.(map[string]interface{})["children"] = tempVal_arrary
  366. }
  367. }
  368. return nil
  369. }
  370. func PostDataByName(r *ghttp.Request) {
  371. appG := app.Ghttp{C: r}
  372. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  373. //fsion := gofasion.NewFasion(string(dataByte))
  374. sqlnamestr := r.Get("name").String()
  375. s_params := make([]interface{}, 0)
  376. sql, p, err := srv.GetSqlByNameDBT(sqlnamestr)
  377. if err != nil {
  378. log.Error(err)
  379. appG.Response(app.StatusInternalServerError, app.ERROR, err)
  380. return
  381. }
  382. if sql != "" {
  383. if !r.Get("parammaps").IsEmpty() {
  384. parammaps := r.Get("parammaps").Map()
  385. //logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  386. paramslist := strings.Split(p, ",")
  387. if len(paramslist) > 0 && p != "" {
  388. for _, value := range paramslist {
  389. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  390. if r.Get("jwt_username").String() != "" {
  391. s_params = append(s_params, r.Get("jwt_username").String())
  392. } else {
  393. s_params = append(s_params, "")
  394. }
  395. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  396. ids, err := setting.SnowIds.NextId()
  397. if err != nil {
  398. ids = time.Now().UnixNano()
  399. //logging.Info("create SnowIds err", err)
  400. }
  401. s_params = append(s_params, ids)
  402. } else {
  403. if parammaps[strings.Trim(value, " ")] == nil {
  404. s_params = append(s_params, "")
  405. } else {
  406. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  407. }
  408. }
  409. }
  410. }
  411. } else if !r.Get("params").IsEmpty() {
  412. params := r.Get("params").Array()
  413. for _, v_params := range params {
  414. s_params = append(s_params, v_params)
  415. }
  416. }
  417. queryData, err := execDataBySqlT(sql, s_params)
  418. if err != nil {
  419. log.Error("PostDataByName err: ", err)
  420. } else {
  421. appG.Response(http.StatusOK, app.SUCCESS, queryData)
  422. }
  423. } else {
  424. appG.Response(http.StatusOK, app.SUCCESS, nil)
  425. }
  426. }
  427. func execDataBySqlT(sqlstr string, params []interface{}) (interface{}, error) {
  428. queryData, err := srv.ExecQueryT(sqlstr, params)
  429. return queryData, err
  430. }
  431. func GetRecuDataByName(r *ghttp.Request) {
  432. appG := app.Ghttp{C: r}
  433. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  434. //fsion := gofasion.NewFasion(string(dataByte))
  435. sqlnamestr := r.Get("name").String()
  436. parentidname := r.Get("idname").String()
  437. sql, p := srv.GetSqlByNameDB(sqlnamestr)
  438. params := make(map[string]interface{})
  439. s_params := make([]interface{}, 0)
  440. if !r.Get("params").IsEmpty() {
  441. for _, v_params := range r.Get("params").Array() {
  442. s_params = append(s_params, v_params)
  443. }
  444. params["params"] = s_params
  445. } else {
  446. jparams := r.Get("parammaps").Map()
  447. paramslist := strings.Split(p, ",")
  448. if len(paramslist) > 0 && p != "" {
  449. for _, value := range paramslist {
  450. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  451. if r.Get("jwt_username").String() != "" {
  452. params[value] = r.Get("jwt_username").String()
  453. } else {
  454. params[value] = ""
  455. }
  456. } else {
  457. params[value] = jparams[strings.Trim(value, " ")]
  458. }
  459. }
  460. }
  461. }
  462. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, !r.Get("params").IsEmpty())
  463. //logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  464. if err != nil {
  465. appG.Response(http.StatusOK, app.ERROR, err.Error())
  466. }
  467. appG.Response(http.StatusOK, app.SUCCESS, queryData)
  468. }
  469. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  470. paramslist := strings.Split(Params, ",")
  471. s_params := make([]interface{}, 0)
  472. if isList {
  473. for _, v_params := range thisParams["params"].([]interface{}) {
  474. s_params = append(s_params, v_params)
  475. }
  476. } else {
  477. paramslist := strings.Split(Params, ",")
  478. if len(paramslist) > 0 && Params != "" {
  479. for _, value := range paramslist {
  480. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  481. }
  482. }
  483. }
  484. queryData, err := srv.QueryByMapMenu(sql, 0, 0, s_params)
  485. for _, queryData_ := range queryData {
  486. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  487. if isList {
  488. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  489. if queryData_[idname] != nil {
  490. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  491. }
  492. } else {
  493. if queryData_[idname] != nil {
  494. thisParams[idname] = queryData_[idname].(interface{})
  495. }
  496. if len(paramslist) > 0 && Params != "" {
  497. s_params = make([]interface{}, 0)
  498. for _, value := range paramslist {
  499. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  500. }
  501. }
  502. }
  503. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  504. } else {
  505. }
  506. }
  507. return queryData, err
  508. }