db.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/360EntSecGroup-Skylar/excelize"
  7. "github.com/Anderson-Lu/gofasion/gofasion"
  8. "github.com/gin-gonic/gin"
  9. "github.com/jmoiron/sqlx"
  10. "github.com/kptyun/KPTCOMM/pkg/app"
  11. "github.com/kptyun/KPTCOMM/pkg/e"
  12. "github.com/kptyun/KPTCOMM/pkg/logging"
  13. "github.com/kptyun/KPTCOMM/routers/restful"
  14. "github.com/tealeg/xlsx"
  15. "io/ioutil"
  16. "net/http"
  17. "strconv"
  18. "strings"
  19. )
  20. // @Summary 根据SQL执行得到数据集
  21. // @Tags getData
  22. // @Accept json
  23. // @Produce json
  24. // @Param sql query string true "sql"
  25. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  26. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  27. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  28. // @Router /data/getdata [POST]
  29. func GetData(c *gin.Context) {
  30. appG := app.Gin{C: c}
  31. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  32. fsion := gofasion.NewFasion(string(dataByte))
  33. sqlstr := fsion.Get("sql").ValueStr()
  34. offset := fsion.Get("offset").ValueDefaultInt(0)
  35. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  36. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  37. queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
  38. if err != nil {
  39. appG.Response(http.StatusOK, e.ERROR, err.Error())
  40. } else {
  41. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  42. }
  43. }
  44. func TestData(c *gin.Context) {
  45. appG := app.Gin{C: c}
  46. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  47. fsion := gofasion.NewFasion(string(dataByte))
  48. sqlstr := fsion.Get("sql").ValueStr()
  49. token := ""
  50. if tempv, exists := c.Get("jwt_username"); exists {
  51. token = tempv.(string)
  52. }
  53. appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
  54. }
  55. // @Summary 根据APISQL表中SQL执行得到数据集
  56. // @Tags GetDataByName
  57. // @Accept json
  58. // @Produce json
  59. // @Param sqlname body string true "sqlname"
  60. // @Param offset body int true "offset"
  61. // @Param pagecount body int true "pagecount"
  62. // @Param returntype body string true "returntype"
  63. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  64. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  65. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  66. // @Router /data/GetDataByName [POST]
  67. func GetDataByName(c *gin.Context) {
  68. appG := app.Gin{C: c}
  69. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  70. fsion := gofasion.NewFasion(string(dataByte))
  71. sqlnamestr := fsion.Get("name").ValueStr()
  72. offset := fsion.Get("offset").ValueDefaultInt(0)
  73. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  74. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  75. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  76. if sql != "" {
  77. s_params := make([]interface{}, 0)
  78. if fsion.HasKey("parammaps") {
  79. parammaps := fsion.Get("parammaps")
  80. // logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  81. paramslist := strings.Split(p, ",")
  82. if len(paramslist) > 0 && p != "" {
  83. for _, value := range paramslist {
  84. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  85. if tempv, exists := c.Get("jwt_username"); exists {
  86. s_params = append(s_params, tempv.(string))
  87. } else {
  88. s_params = append(s_params, "")
  89. }
  90. } else {
  91. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  92. }
  93. }
  94. }
  95. } else if fsion.HasKey("params") {
  96. params := fsion.Get("params").Array()
  97. // logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  98. for _, v_params := range params {
  99. s_params = append(s_params, v_params.ValueStr())
  100. }
  101. }
  102. if returntype == "xlsx" {
  103. var xlsxfile *xlsx.File
  104. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  105. //c.File("c:/dd.xlsx")
  106. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  107. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  108. c.Header("Content-Transfer-Encoding", "binary")
  109. _ = xlsxfile.Write(c.Writer)
  110. } else {
  111. queryData, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  112. if err != nil {
  113. // logging.Error("GetDataByName err: ", err)
  114. appG.Response(http.StatusOK, e.ERROR, err.Error())
  115. } else {
  116. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  117. }
  118. }
  119. } else {
  120. appG.Response(http.StatusOK, e.SUCCESS, nil)
  121. }
  122. }
  123. // @Summary 根据APISQL表中SQL执行得到数据集
  124. // @Tags GetDataByNameForm
  125. // @Accept json
  126. // @Produce json
  127. // @Param sqlname body json true "sqlname"
  128. // @Param offset query int true "offset"
  129. // @Param pagecount query int true "pagecount"
  130. // @Param returntype query string true "returntype"
  131. // @Param s formData json true "ss"
  132. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  133. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  134. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  135. // @Router /data/GetDataByNameForm [POST]
  136. func GetDataByNameForm(c *gin.Context) {
  137. appG := app.Gin{C: c}
  138. //sqlnamestr := c.PostForm("measureNumber")
  139. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  140. //fsion := gofasion.NewFasion(string(dataByte))
  141. //sqlnamestr := fsion.Get("name").ValueStr()
  142. //offset := fsion.Get("offset").ValueInt()
  143. //pagecount := fsion.Get("pagecount").ValueInt()
  144. //returntype := fsion.Get("returntype").ValueStr()
  145. //params := fsion.Get("params").Array()
  146. c.Request.ParseForm()
  147. params := c.Request.Form
  148. offset := 0
  149. pageSize_ := 0
  150. returntype := params.Get("page")
  151. if returntype == "" {
  152. returntype = "0"
  153. }
  154. if tempv, exists := c.Get("jwt_username"); exists {
  155. params.Set("JwtUserName", tempv.(string))
  156. } else {
  157. params.Set("JwtUserName", "")
  158. }
  159. sqlnamestr := params.Get("name")
  160. logging.Info("GetDataByNameForm ", c.Keys, sqlnamestr)
  161. s_params := make([]interface{}, 0)
  162. if returntype == "1" {
  163. pageNum, error := strconv.Atoi(params.Get("pageNum"))
  164. if error != nil {
  165. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
  166. }
  167. pageSize, error := strconv.Atoi(params.Get("pageSize"))
  168. if error != nil {
  169. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
  170. }
  171. pageSize_ = pageSize
  172. offset = pageNum
  173. }
  174. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  175. paramslist := strings.Split(p, ",")
  176. if len(paramslist) > 0 && p != "" {
  177. for _, value := range paramslist {
  178. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  179. }
  180. }
  181. if sql != "" {
  182. if returntype == "xlsx" {
  183. var xlsxfile *xlsx.File
  184. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  185. c.Header("Content-Type", "application/octet-stream")
  186. c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
  187. c.Header("Content-Transfer-Encoding", "binary")
  188. _ = xlsxfile.Write(c.Writer)
  189. } else {
  190. queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
  191. if err != nil {
  192. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  193. } else {
  194. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  195. }
  196. }
  197. } else {
  198. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  199. }
  200. }
  201. // @Summary 递归获取所有
  202. // @Tags menu
  203. // @Accept json
  204. // @Produce json
  205. // @Param Ton query string true "Ton"
  206. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  207. // @Router /authdata/GetRoleMenus [GET]
  208. func GetRecuDataByName(c *gin.Context) {
  209. appG := app.Gin{C: c}
  210. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  211. fsion := gofasion.NewFasion(string(dataByte))
  212. sqlnamestr := fsion.Get("name").ValueStr()
  213. parentidname := fsion.Get("idname").ValueStr()
  214. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  215. params := make(map[string]interface{})
  216. s_params := make([]interface{}, 0)
  217. if fsion.HasKey("params") {
  218. for _, v_params := range fsion.Get("params").Array() {
  219. s_params = append(s_params, v_params.ValueStr())
  220. }
  221. params["params"] = s_params
  222. } else {
  223. jparams := fsion.Get("parammaps")
  224. paramslist := strings.Split(p, ",")
  225. if len(paramslist) > 0 && p != "" {
  226. for _, value := range paramslist {
  227. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  228. if tempv, exists := c.Get("jwt_username"); exists {
  229. params[value] = tempv.(string)
  230. } else {
  231. params[value] = ""
  232. }
  233. } else {
  234. params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
  235. }
  236. }
  237. }
  238. }
  239. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
  240. // logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  241. if err != nil {
  242. appG.Response(http.StatusOK, e.ERROR, err.Error())
  243. }
  244. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  245. }
  246. // @Summary 根据APISQL表中SQL执行得到数据集
  247. // @Tags GetDataByNames
  248. // @Accept json
  249. // @Produce json
  250. // @RequestBody sqlname query string true "sqlname"
  251. // @Param offset query int true "offset"
  252. // @Param pagecount query int true "pagecount"
  253. // @Param returntype query string true "returntype"
  254. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  255. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  256. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  257. // @Router /data/GetDataByNames [POST]
  258. func GetDataByNames(c *gin.Context) {
  259. appG := app.Gin{C: c}
  260. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  261. fsion := gofasion.NewFasion(string(dataByte))
  262. sqlParams := fsion.Array()
  263. queryData := make(map[string]interface{})
  264. for _, v := range sqlParams {
  265. fsion2 := gofasion.NewFasion(v.Json())
  266. // logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
  267. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  268. if sql == "" {
  269. errorm := make(map[string]interface{})
  270. errorm["code"] = e.ERROR
  271. errorm["message"] = "sql没找到"
  272. queryData[fsion2.Get("name").ValueStr()] = errorm
  273. continue
  274. } else if sql != "" {
  275. offset := fsion2.Get("offset").ValueInt()
  276. pagecount := fsion2.Get("pagecount").ValueInt()
  277. returntype := fsion2.Get("returntype").ValueStr()
  278. s_params := make([]interface{}, 0)
  279. if fsion2.HasKey("parammaps") {
  280. parammaps := fsion2.Get("parammaps")
  281. paramslist := strings.Split(p, ",")
  282. if len(paramslist) > 0 && p != "" {
  283. for _, value := range paramslist {
  284. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  285. if tempv, exists := c.Get("jwt_username"); exists {
  286. s_params = append(s_params, tempv.(string))
  287. } else {
  288. s_params = append(s_params, "")
  289. }
  290. } else {
  291. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  292. }
  293. }
  294. }
  295. } else if fsion2.HasKey("params") {
  296. params := fsion2.Get("params").Array()
  297. for _, v_params := range params {
  298. s_params = append(s_params, v_params.ValueStr())
  299. }
  300. }
  301. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  302. if err != nil {
  303. errorm := make(map[string]interface{})
  304. errorm["code"] = e.ERROR
  305. errorm["message"] = err.Error()
  306. queryData[fsion2.Get("name").ValueStr()] = errorm
  307. logging.Error("GetDataByNames err: ", err)
  308. continue
  309. } else {
  310. queryData[fsion2.Get("name").ValueStr()] = queryData1
  311. }
  312. }
  313. }
  314. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  315. }
  316. // @Summary 根据APISQL表中SQL执行得到数据集
  317. // @Tags PostDataByNameForm
  318. // @Accept json
  319. // @Produce json
  320. // @Param sqlname query string true "sqlname"
  321. // @Param offset query int true "offset"
  322. // @Param pagecount query int true "pagecount"
  323. // @Param returntype query string true "returntype"
  324. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  325. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
  326. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  327. // @Router /data/PostDataByNameForm [POST]
  328. func PostDataByNameForm(c *gin.Context) {
  329. appG := app.Gin{C: c}
  330. c.Request.ParseForm()
  331. params := c.Request.Form
  332. sqlnamestr := params.Get("name")
  333. logging.Info("PostDataByNameForm ", c.Keys, sqlnamestr)
  334. s_params := make([]interface{}, 0)
  335. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  336. paramslist := strings.Split(p, ",")
  337. if len(paramslist) > 0 {
  338. for _, value := range paramslist {
  339. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  340. }
  341. }
  342. if sql != "" {
  343. queryData, err := execDataBySql(sql, s_params)
  344. if err != nil {
  345. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  346. } else {
  347. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  348. }
  349. } else {
  350. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  351. }
  352. }
  353. // @Summary 根据APISQL表中SQL执行得到数据集
  354. // @Tags PostDataByName
  355. // @Accept json
  356. // @Produce json
  357. // @Param sqlname query string true "sqlname"
  358. // @Param offset query int true "offset"
  359. // @Param pagecount query int true "pagecount"
  360. // @Param returntype query string true "returntype"
  361. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  362. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  363. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  364. // @Router /data/PostDataByName [POST]
  365. func PostDataByName(c *gin.Context) {
  366. appG := app.Gin{C: c}
  367. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  368. fsion := gofasion.NewFasion(string(dataByte))
  369. sqlnamestr := fsion.Get("name").ValueStr()
  370. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  371. s_params := make([]interface{}, 0)
  372. if sql != "" {
  373. if fsion.HasKey("parammaps") {
  374. parammaps := fsion.Get("parammaps")
  375. // logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps)
  376. paramslist := strings.Split(p, ",")
  377. if len(paramslist) > 0 && p != "" {
  378. for _, value := range paramslist {
  379. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  380. if tempv, exists := c.Get("jwt_username"); exists {
  381. s_params = append(s_params, tempv.(string))
  382. } else {
  383. s_params = append(s_params, "")
  384. }
  385. } else {
  386. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  387. }
  388. }
  389. }
  390. } else if fsion.HasKey("params") {
  391. params := fsion.Get("params").Array()
  392. // logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  393. for _, v_params := range params {
  394. s_params = append(s_params, v_params.ValueStr())
  395. }
  396. }
  397. queryData, err := execDataBySql(sql, s_params)
  398. if err != nil {
  399. // logging.Error("PostDataByName err: ", err)
  400. appG.Response(http.StatusOK, e.ERROR, err.Error())
  401. } else {
  402. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  403. }
  404. } else {
  405. appG.Response(http.StatusOK, e.SUCCESS, nil)
  406. }
  407. }
  408. // @Summary 根据APISQL表中SQL执行得到数据集
  409. // @Tags ExecDataByConfig
  410. // @Accept json
  411. // @Produce json
  412. // @Param sqlname query string true "sqlname"
  413. // @Param offset query int true "offset"
  414. // @Param pagecount query int true "pagecount"
  415. // @Param returntype query string true "returntype"
  416. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  417. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  418. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  419. // @Router /data/PostDataByName [POST]
  420. func ExecDataByConfig(c *gin.Context) {
  421. appG := app.Gin{C: c}
  422. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  423. tempval_ := make(map[string]interface{})
  424. tempCommon := make(map[string]interface{})
  425. tempval := make([]map[string]interface{}, 0)
  426. err := json.Unmarshal(dataByte, &tempval_)
  427. logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  428. if err != nil {
  429. fmt.Println("error:", err)
  430. } else {
  431. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  432. tempCommon = v
  433. }
  434. if v, ok := tempval_["data"].([]interface{}); ok {
  435. for _, Tvalue := range v {
  436. if v1, ok := Tvalue.(map[string]interface{}); ok {
  437. tempval = append(tempval, v1)
  438. }
  439. }
  440. }
  441. tx, err := restful.Dbs.Beginx()
  442. if err != nil {
  443. fmt.Println("GetT error:", err)
  444. }
  445. defer func() {
  446. switch {
  447. case err != nil:
  448. fmt.Println("__error:", err)
  449. if tx != nil {
  450. tx.Rollback()
  451. }
  452. default:
  453. if tx != nil {
  454. err = tx.Commit()
  455. }
  456. }
  457. }()
  458. if err == nil {
  459. if tempv, exists := c.Get("jwt_username"); exists {
  460. tempCommon["jwt_username"] = tempv.(string)
  461. }
  462. for _, paramvalue := range tempval {
  463. if _, ok := paramvalue["resultname"]; !ok {
  464. paramvalue["resultname"] = paramvalue["name"]
  465. }
  466. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  467. if err != nil {
  468. fmt.Println("ExecDataParam error:", err)
  469. break
  470. }
  471. }
  472. if err == nil {
  473. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  474. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  475. } else {
  476. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  477. }
  478. } else {
  479. appG.Response(http.StatusOK, e.ERROR, err.Error())
  480. }
  481. }
  482. }
  483. if err != nil {
  484. appG.Response(http.StatusOK, e.ERROR, err.Error())
  485. }
  486. }
  487. func interfacetostring(val interface{}) string {
  488. switch val.(type) {
  489. case string:
  490. ss := val.(string)
  491. return ss
  492. break
  493. case int:
  494. ss := val.(int)
  495. return strconv.Itoa(ss)
  496. break
  497. case int64:
  498. ss := val.(int64)
  499. return strconv.FormatInt(ss, 10)
  500. break
  501. case float64:
  502. ss := val.(float64)
  503. return strconv.FormatFloat(ss, 'E', -1, 32)
  504. break
  505. }
  506. return ""
  507. }
  508. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  509. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  510. tempstrb := strings.IndexAny(valuestring, "@")
  511. tempstre := strings.IndexAny(valuestring, ".")
  512. if tempstrb > -1 && tempstre > -1 {
  513. for _, ParamMapList := range ParamMap {
  514. if v, ok := ParamMapList["resultname"]; ok {
  515. if v == valuestring[tempstrb+1:tempstre] {
  516. if v1, ok := ParamMapList["resultmaps"]; ok {
  517. if newA, ok := v1.([]map[string]interface{}); ok {
  518. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  519. } else if newA, ok := v1.(map[string]interface{}); ok {
  520. return interfacetostring(newA[valuestring[tempstre+1:]])
  521. }
  522. }
  523. }
  524. }
  525. }
  526. } else {
  527. return valuestring
  528. }
  529. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  530. return strconv.FormatInt(int64(valuestring), 10)
  531. }
  532. return ""
  533. }
  534. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  535. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  536. tempstrb := strings.IndexAny(valuestring, "@")
  537. tempstre := strings.IndexAny(valuestring, ".")
  538. if tempstrb > -1 && tempstre > -1 {
  539. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  540. if s, ok := v.(string); ok {
  541. return s
  542. } else if s, ok := v.(float64); ok {
  543. return strconv.FormatInt(int64(s), 10)
  544. }
  545. }
  546. }
  547. }
  548. return ""
  549. }
  550. func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}, tx *sqlx.Tx) ([]map[string]interface{}, error) {
  551. sqlname := fasion["name"].(string)
  552. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  553. if v, ok := fasion["type"]; ok {
  554. sqltype = v.(string)
  555. }
  556. sql := ""
  557. p := ""
  558. s_params := make([]interface{}, 0)
  559. if sqltype != "" {
  560. sql, p = restful.GetSqlByName(sqlname, tx)
  561. //sqlApi, _ := models.GetApisqlByName(sqlname)
  562. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  563. paramslist := strings.Split(p, ",")
  564. if len(paramslist) > 0 && p != "" {
  565. for _, value := range paramslist {
  566. ss := ""
  567. if ss == "" && CommonParamMap != nil {
  568. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  569. ss = valuestring
  570. tempstrb := strings.IndexAny(valuestring, "@")
  571. tempstre := strings.IndexAny(valuestring, ".")
  572. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  573. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  574. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  575. }
  576. } else {
  577. ss = ""
  578. }
  579. }
  580. }
  581. if ss == "" && ParentParamMap != nil {
  582. ss = getParamValue_(v, value, ParentParamMap)
  583. }
  584. if ss == "" && sameParamMap != nil {
  585. ss = getParamValue(v, value, sameParamMap)
  586. }
  587. if ss == "" && ParamMap != nil {
  588. ss = getParamValue(v, value, ParamMap)
  589. }
  590. s_params = append(s_params, ss)
  591. }
  592. }
  593. }
  594. }
  595. if sql == "" {
  596. sqltype = ""
  597. }
  598. if sqltype == "s1" {
  599. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  600. if v, ok := tempValParam.(map[string]interface{}); ok {
  601. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  602. }
  603. if err != nil {
  604. return nil, err
  605. }
  606. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  607. if err != nil {
  608. return nil, err
  609. }
  610. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  611. } else if sqltype == "sm" {
  612. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  613. if v, ok := tempValParam.(map[string]interface{}); ok {
  614. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  615. } else {
  616. fmt.Println("sm", tempValParam)
  617. }
  618. if err != nil {
  619. return nil, err
  620. }
  621. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  622. if err != nil {
  623. return nil, err
  624. }
  625. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  626. } else if sqltype == "v" {
  627. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  628. if v, ok := tempValParam.(map[string]interface{}); ok {
  629. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  630. } else {
  631. fmt.Println("v", tempValParam)
  632. }
  633. if err != nil {
  634. return nil, err
  635. }
  636. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  637. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  638. }
  639. } else if sqltype == "e" {
  640. tempVal_arrary := make([]map[string]interface{}, 0)
  641. tempVal_map := make(map[string]interface{})
  642. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  643. if err == nil {
  644. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  645. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  646. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  647. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  648. return tempVal_arrary, err
  649. }
  650. return nil, err
  651. } else {
  652. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  653. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  654. if err != nil {
  655. return nil, err
  656. }
  657. return nil, err
  658. }
  659. }
  660. return nil, nil
  661. }
  662. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *sqlx.Tx) error {
  663. var err error
  664. subarray, ok := fasion["children"]
  665. if ok {
  666. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  667. tempVal_arrary := make([]map[string]interface{}, 0)
  668. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  669. tempVal_ := make(map[string]interface{})
  670. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  671. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  672. } else {
  673. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  674. }
  675. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  676. if err != nil {
  677. return err
  678. }
  679. tempVal_arrary = append(tempVal_arrary, tempVal_)
  680. }
  681. v.(map[string]interface{})["children"] = tempVal_arrary
  682. }
  683. }
  684. return nil
  685. }
  686. //
  687. //// @Summary 删除图片
  688. //// @Tags RemovePicByName
  689. //// @Accept json
  690. //// @Produce json
  691. //// @Param sqlname query string true "sqlname"
  692. //// @Param offset query int true "offset"
  693. //// @Param pagecount query int true "pagecount"
  694. //// @Param returntype query string true "returntype"
  695. //// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  696. //// @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  697. //// @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  698. //// @Router /data/PostDataByName [POST]
  699. //func RemovePicByName(c *gin.Context) {
  700. // logging.Info("RemovePicByName ", c.Keys)
  701. // appG := app.Gin{C: c}
  702. // dataByte, _ := ioutil.ReadAll(c.Request.Body)
  703. // fsion := gofasion.NewFasion(string(dataByte))
  704. // sqlnamestr := fsion.Get("name").ValueStr()
  705. // picname := fsion.Get("picname").ValueStr()
  706. // sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  707. // params := fsion.Get("params").Array()
  708. // s_params := make([]interface{}, 0)
  709. // for _, v_params := range params {
  710. // s_params = append(s_params, v_params.ValueStr())
  711. // }
  712. // if sql != "" {
  713. // queryData, err := execDataBySql(sql, s_params)
  714. //
  715. // if picname != "" {
  716. // // Destination
  717. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  718. //
  719. // err := os.Remove(distPath) //删除文件test.txt
  720. // if err != nil {
  721. // //如果删除失败则输出 file remove Error!
  722. // fmt.Println("file remove Error!")
  723. // //输出错误详细信息
  724. // fmt.Printf("%s", err)
  725. // } else {
  726. // //如果删除成功则输出 file remove OK!
  727. // fmt.Print("file remove OK!")
  728. // }
  729. // distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  730. //
  731. // err = os.Remove(distPath) //删除文件test.txt
  732. // if err != nil {
  733. // //如果删除失败则输出 file remove Error!
  734. // fmt.Println("file remove Error!")
  735. // //输出错误详细信息
  736. // fmt.Printf("%s", err)
  737. // } else {
  738. // //如果删除成功则输出 file remove OK!
  739. // fmt.Print("file remove OK!")
  740. // }
  741. // }
  742. //
  743. // if err != nil {
  744. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  745. // } else {
  746. // appG.Response(http.StatusOK, e.SUCCESS, queryData)
  747. // }
  748. // } else {
  749. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  750. // }
  751. //}
  752. //
  753. //// @Summary 删除文件
  754. //// @Tags RemoveFileByName
  755. //// @Accept json
  756. //// @Produce json
  757. //// @Param sqlname query string true "sqlname"
  758. //// @Param offset query int true "offset"
  759. //// @Param pagecount query int true "pagecount"
  760. //// @Param returntype query string true "returntype"
  761. //// @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  762. //// @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  763. //// @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  764. //// @Router /data/PostDataByName [POST]
  765. //func RemoveFileByName(c *gin.Context) {
  766. // appG := app.Gin{C: c}
  767. // id := c.Param("id")
  768. // logging.Info("DownloadFile ", c.Keys, id)
  769. // restful.
  770. // eqdoc, err := restful.Dbs.Queryx("SELECT * FROM eq_doc where id = ? ", id)
  771. //
  772. // if eqdoc == nil {
  773. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  774. // return
  775. // }
  776. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  777. //
  778. // err = os.Remove(distPath) //删除文件test.txt
  779. // if err != nil {
  780. // //如果删除失败则输出 file remove Error!
  781. // fmt.Println("file remove Error!")
  782. // //输出错误详细信息
  783. // fmt.Printf("%s", err)
  784. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  785. // return
  786. // }
  787. // fmt.Print("file remove OK!")
  788. // _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
  789. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  790. //
  791. // //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  792. // //fsion := gofasion.NewFasion(string(dataByte))
  793. // //sqlnamestr := fsion.Get("name").ValueStr()
  794. // //filename := fsion.Get("filename").ValueStr()
  795. // //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  796. // //params := fsion.Get("params").Array()
  797. // //s_params := make([]interface{}, 0)
  798. // //for _, v_params := range params {
  799. // // s_params = append(s_params, v_params.ValueStr())
  800. // //}
  801. // //if sql != "" {
  802. // // queryData, err := execDataBySql(sql, s_params)
  803. // //
  804. // // if filename != "" {
  805. // // // Destination
  806. // // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  807. // //
  808. // // err := os.Remove(distPath) //删除文件test.txt
  809. // // if err != nil {
  810. // // //如果删除失败则输出 file remove Error!
  811. // // fmt.Println("file remove Error!")
  812. // // //输出错误详细信息
  813. // // fmt.Printf("%s", err)
  814. // // } else {
  815. // // //如果删除成功则输出 file remove OK!
  816. // // fmt.Print("file remove OK!")
  817. // // }
  818. // // }
  819. //
  820. // // if err != nil {
  821. // // appG.Response(http.StatusOK, e.ERROR, err.Error())
  822. // // } else {
  823. // // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  824. // // }
  825. // //} else {
  826. // // appG.Response(http.StatusOK, e.SUCCESS, nil)
  827. // //}
  828. //}
  829. // @Summary 根据APISQL表中SQL执行得到数据集
  830. // @Tags PostDataByName
  831. // @Accept json
  832. // @Produce json
  833. // @Param sqlname query string true "sqlname"
  834. // @Param offset query int true "offset"
  835. // @Param pagecount query int true "pagecount"
  836. // @Param returntype query string true "returntype"
  837. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  838. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  839. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  840. // @Router /data/PostDataByName [POST]
  841. func UpdateDataRelation(c *gin.Context) {
  842. appG := app.Gin{C: c}
  843. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  844. fsion := gofasion.NewFasion(string(dataByte))
  845. tablename := fsion.Get("name").ValueStr()
  846. dataname := fsion.Get("dataname").ValueStr()
  847. datavalue := fsion.Get("datavalue").ValueStr()
  848. valuename := fsion.Get("valuename").ValueStr()
  849. // logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  850. values := fsion.Get("values").Array()
  851. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  852. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  853. d_params := make([]interface{}, 4)
  854. d_params[0] = datavalue
  855. s_params := ""
  856. for _, v_values := range values {
  857. d_params[1] = v_values.ValueStr()
  858. if s_params == "" {
  859. s_params = v_values.ValueStr()
  860. } else {
  861. s_params = s_params + "," + v_values.ValueStr()
  862. }
  863. d_params[2] = d_params[1]
  864. d_params[3] = d_params[0]
  865. _, err := execDataBySql(insertSqlstr, d_params)
  866. if err != nil {
  867. appG.Response(http.StatusOK, e.ERROR, err.Error())
  868. break
  869. }
  870. }
  871. if s_params != "" {
  872. deleteSqlstr := "delete from " + tablename + " " +
  873. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  874. queryData, err := execDataBySql(deleteSqlstr, nil)
  875. if err != nil {
  876. appG.Response(http.StatusOK, e.ERROR, err.Error())
  877. } else if err == nil {
  878. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  879. }
  880. } else {
  881. deleteSqlstr := "delete from " + tablename + " " +
  882. " where " + dataname + " = " + datavalue
  883. queryData, err := execDataBySql(deleteSqlstr, nil)
  884. if err != nil {
  885. appG.Response(http.StatusOK, e.ERROR, err.Error())
  886. } else if err == nil {
  887. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  888. }
  889. }
  890. }
  891. // @Summary 根据APISQL表中SQL执行得到数据集
  892. // @Tags PostDatasByName
  893. // @Accept json
  894. // @Produce json
  895. // @Param sqlname query string true "sqlname"
  896. // @Param offset query int true "offset"
  897. // @Param pagecount query int true "pagecount"
  898. // @Param returntype query string true "returntype"
  899. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  900. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  901. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  902. // @Router /data/PostDatasByName [POST]
  903. func PostDataByNames(c *gin.Context) {
  904. appG := app.Gin{C: c}
  905. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  906. fsion := gofasion.NewFasion(string(dataByte))
  907. sqlParams := fsion.Array()
  908. queryData := make(map[string]interface{})
  909. for _, v := range sqlParams {
  910. fsion2 := gofasion.NewFasion(v.Json())
  911. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  912. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  913. if sql == "" {
  914. errorm := make(map[string]interface{})
  915. errorm["code"] = e.ERROR
  916. errorm["message"] = "sql没有找到"
  917. errorm["param"] = ""
  918. queryData[fsion2.Get("name").ValueStr()] = errorm
  919. continue
  920. } else if sql != "" {
  921. s_params := make([]interface{}, 0)
  922. if fsion2.HasKey("parammaps") {
  923. parammaps := fsion2.Get("parammaps")
  924. paramslist := strings.Split(p, ",")
  925. if len(paramslist) > 0 && p != "" {
  926. for _, value := range paramslist {
  927. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  928. if tempv, exists := c.Get("jwt_username"); exists {
  929. s_params = append(s_params, tempv.(string))
  930. } else {
  931. s_params = append(s_params, "")
  932. }
  933. } else {
  934. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  935. }
  936. }
  937. }
  938. } else if fsion2.HasKey("params") {
  939. params := fsion2.Get("params").Array()
  940. for _, v_params := range params {
  941. s_params = append(s_params, v_params.ValueStr())
  942. }
  943. }
  944. _, err := execDataBySql(sql, s_params)
  945. if err != nil {
  946. logging.Error("PostDataByName err: ", err)
  947. errorm := make(map[string]interface{})
  948. errorm["code"] = e.ERROR
  949. errorm["message"] = err.Error()
  950. errorm["param"] = s_params
  951. queryData[fsion2.Get("name").ValueStr()] = errorm
  952. continue
  953. } else {
  954. errorm := make(map[string]interface{})
  955. errorm["code"] = e.SUCCESS
  956. errorm["message"] = "success"
  957. queryData[fsion2.Get("name").ValueStr()] = errorm
  958. continue
  959. }
  960. } else {
  961. errorm := make(map[string]interface{})
  962. errorm["code"] = e.ERROR
  963. errorm["message"] = "NO SQL"
  964. errorm["param"] = ""
  965. queryData[fsion2.Get("name").ValueStr()] = errorm
  966. }
  967. }
  968. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  969. }
  970. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  971. if offset > 0 {
  972. offset = offset - 1
  973. }
  974. //if pagecount == 0 {
  975. // pagecount = setting.AppSetting.PageSize
  976. //}else if pagecount>setting.AppSetting.PageSize{
  977. // pagecount = setting.AppSetting.PageSize
  978. //}
  979. if returntype == "list" {
  980. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  981. return queryData, err
  982. } else if returntype == "listlist" {
  983. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  984. return queryData, err
  985. } else if returntype == "options" {
  986. queryData, err := restful.QueryByOption(sqlstr, offset, pagecount, params)
  987. return queryData, err
  988. } else {
  989. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  990. return queryData, err
  991. }
  992. }
  993. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  994. paramslist := strings.Split(Params, ",")
  995. s_params := make([]interface{}, 0)
  996. if isList {
  997. for _, v_params := range thisParams["params"].([]interface{}) {
  998. s_params = append(s_params, v_params)
  999. }
  1000. } else {
  1001. paramslist := strings.Split(Params, ",")
  1002. if len(paramslist) > 0 && Params != "" {
  1003. for _, value := range paramslist {
  1004. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1005. }
  1006. }
  1007. }
  1008. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  1009. for _, queryData_ := range queryData {
  1010. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  1011. if isList {
  1012. if queryData_[idname] != nil {
  1013. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  1014. }
  1015. } else {
  1016. if queryData_[idname] != nil {
  1017. thisParams[idname] = queryData_[idname].(interface{})
  1018. }
  1019. if len(paramslist) > 0 && Params != "" {
  1020. s_params = make([]interface{}, 0)
  1021. for _, value := range paramslist {
  1022. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1023. }
  1024. }
  1025. }
  1026. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  1027. } else {
  1028. }
  1029. }
  1030. return queryData, err
  1031. }
  1032. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1033. queryData, err := restful.ExecQuerySqlx(sqlstr, params)
  1034. return queryData, err
  1035. }
  1036. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1037. queryData, err := restful.ExecQuery(sqlstr, params)
  1038. return queryData, err
  1039. }
  1040. // @Summary 导入excel文件到数据库,excel第一行作为参数
  1041. // @Tags ImportExcel
  1042. // @Accept json
  1043. // @Produce json
  1044. // @Param file query string true "文件名"
  1045. // @Param name header string true "执行sql的名字"
  1046. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1047. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1048. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1049. // @Router /data/ImportExcel [POST]
  1050. func ImportExcel(c *gin.Context) {
  1051. appG := app.Gin{C: c}
  1052. inputdata := ""
  1053. c.Request.ParseForm()
  1054. //tempv1, _ := c.Get("jwt_username")
  1055. //fmt.Println("=======================",tempv1)
  1056. params := c.Request.Form
  1057. file, _, err := c.Request.FormFile("file")
  1058. if err != nil {
  1059. appG.Response(http.StatusOK, e.ERROR, err)
  1060. return
  1061. }
  1062. sqlnamestr := params.Get("name")
  1063. sheetname := params.Get("sheetname")
  1064. importParams := params.Get("importParams")
  1065. logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestr)
  1066. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1067. paramslist := strings.Split(importParams, ",")
  1068. paramslist = append(paramslist, "jwt_username")
  1069. queryData := make([]interface{}, 0)
  1070. xlsx1, err := excelize.OpenReader(file)
  1071. if err != nil {
  1072. fmt.Println(err)
  1073. return
  1074. }
  1075. ok := 0
  1076. error := 0
  1077. rows := xlsx1.GetRows(sheetname)
  1078. //fmt.Println("============================",rows)
  1079. for i, row := range rows {
  1080. fmt.Print(i, "\t")
  1081. if i > 0 {
  1082. if len(paramslist) > 0 {
  1083. s_params := make([]interface{}, 0)
  1084. entry := make(map[string]interface{})
  1085. outentry := make(map[string]interface{})
  1086. inputdata = ""
  1087. for _, value := range paramslist {
  1088. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1089. if tempv, exists := c.Get("jwt_username"); exists {
  1090. s_params = append(s_params, tempv.(string))
  1091. } else {
  1092. s_params = append(s_params, "")
  1093. }
  1094. } else {
  1095. for j, colCell := range rows[0] {
  1096. if strings.Trim(value, " ") == colCell {
  1097. for k, colCell1 := range row {
  1098. if j == k {
  1099. outentry[colCell] = colCell1
  1100. entry[colCell] = colCell1
  1101. //inputdata = inputdata + colCell + ":" + colCell1 + ";"
  1102. }
  1103. }
  1104. }
  1105. }
  1106. s_params = append(s_params, entry[strings.Trim(value, " ")])
  1107. }
  1108. }
  1109. outentry["input"] = inputdata
  1110. // returnmsg, err := execDataBySql(sqlApi.Sqlstr, s_params)
  1111. returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  1112. returnmsgmap, _ := returnmsg.(map[string]interface{})
  1113. returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  1114. if err != nil {
  1115. outentry["error_msg"] = err.Error()
  1116. error++
  1117. queryData = append(queryData, outentry)
  1118. } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  1119. ok++
  1120. } else {
  1121. outentry["error_msg"] = returnmsgmap1[0]["msg"]
  1122. error++
  1123. queryData = append(queryData, outentry)
  1124. }
  1125. }
  1126. }
  1127. }
  1128. data := make(map[string]interface{})
  1129. data["result"] = queryData
  1130. data["success"] = ok
  1131. data["err_count"] = error
  1132. appG.Response(http.StatusOK, e.SUCCESS, data)
  1133. }