f67e2f240e200d872ba82320b464d09af3663931.svn-base 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  1. package api
  2. import (
  3. "../../pkg/app"
  4. "../../pkg/e"
  5. "../../pkg/logging"
  6. "../../pkg/setting"
  7. "../../routers/restful"
  8. "bytes"
  9. "database/sql"
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "github.com/360EntSecGroup-Skylar/excelize"
  14. "github.com/Anderson-Lu/gofasion/gofasion"
  15. "github.com/gin-gonic/gin"
  16. "github.com/tealeg/xlsx"
  17. "github.com/xormplus/xorm"
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "path"
  22. "regexp"
  23. "strconv"
  24. "strings"
  25. "time"
  26. )
  27. // @Summary 根据SQL执行得到数据集
  28. // @Tags getData
  29. // @Accept json
  30. // @Produce json
  31. // @Param sql query string true "sql"
  32. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  33. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  34. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  35. // @Router /data/getdata [POST]
  36. func GetData(c *gin.Context) {
  37. appG := app.Gin{C: c}
  38. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  39. fsion := gofasion.NewFasion(string(dataByte))
  40. sqlstr := fsion.Get("sql").ValueStr()
  41. offset := fsion.Get("offset").ValueDefaultInt(0)
  42. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  43. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  44. queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
  45. if err != nil {
  46. appG.Response(http.StatusOK, e.ERROR, err.Error())
  47. } else {
  48. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  49. }
  50. }
  51. func Testpartitions(c *gin.Context) {
  52. appG := app.Gin{C: c}
  53. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  54. fsion := gofasion.NewFasion(string(dataByte))
  55. databaseName := fsion.Get("databaseName").ValueStr() //获取新数据库名称
  56. user := fsion.Get("user").ValueStr() //获取新数据库用户
  57. passWord := fsion.Get("passWord").ValueStr() //获取新数据库名称
  58. host := fsion.Get("host").ValueStr() //获取新数据库名称
  59. port := fsion.Get("port").ValueStr() //获取新数据库名称
  60. sqls, err1 := restful.Engine.SQL("SELECT * FROM sql_new ").QueryString() //获取新数据库表
  61. if sqls == nil || err1 != nil {
  62. appG.Response(http.StatusOK, e.SUCCESS, "失败:"+err1.Error())
  63. }
  64. sqlstrs := strings.Split(sqls[0]["sql"], ";") //分割sql
  65. connecting := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, "mysql")
  66. mydb, err := sql.Open("mysql", connecting) //连接新连接
  67. if err != nil {
  68. println("err1", err.Error())
  69. }
  70. _, err = mydb.Exec("CREATE DATABASE IF NOT EXISTS `" + databaseName + "`") // 创建数据库
  71. if err != nil {
  72. println("err2", err.Error())
  73. }
  74. mydb.Close()
  75. connecting = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, databaseName) //连接新数据库,创建表格
  76. mydb, err = sql.Open("mysql", connecting)
  77. if err != nil {
  78. println("err3", err.Error())
  79. }
  80. defer mydb.Close()
  81. if err == nil {
  82. mydb.SetMaxOpenConns(10)
  83. mydb.SetMaxIdleConns(10)
  84. }
  85. errmsg := ""
  86. if err == nil {
  87. //tx ,_ :=mydb.Begin()
  88. for _, sqlstr := range sqlstrs {
  89. //println("sqlstr---",sqlstr)
  90. if sqlstr == "" {
  91. continue
  92. }
  93. _, err = mydb.Exec(sqlstr)
  94. if err != nil {
  95. println("err4", err.Error())
  96. errmsg = err.Error()
  97. //err1 :=tx.Rollback()
  98. }
  99. }
  100. //_, err = mydb.Exec("CREATE TABLE `sql_new` (`sql` text DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;")
  101. //err =tx.Commit()
  102. }
  103. if errmsg == "" {
  104. appG.Response(http.StatusOK, e.SUCCESS, "成功"+databaseName)
  105. } else {
  106. appG.Response(http.StatusOK, e.SUCCESS, "失败"+errmsg)
  107. }
  108. }
  109. func TestData(c *gin.Context) {
  110. appG := app.Gin{C: c}
  111. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  112. fsion := gofasion.NewFasion(string(dataByte))
  113. sqlstr := fsion.Get("sql").ValueStr()
  114. token := ""
  115. if tempv, exists := c.Get("jwt_username"); exists {
  116. token = tempv.(string)
  117. }
  118. appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
  119. }
  120. // @Summary 根据APISQL表中SQL执行得到数据集
  121. // @Tags GetDataByName
  122. // @Accept json
  123. // @Produce json
  124. // @Param sqlname body string true "sqlname"
  125. // @Param offset body int true "offset"
  126. // @Param pagecount body int true "pagecount"
  127. // @Param returntype body string true "returntype"
  128. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  129. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  130. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  131. // @Router /data/GetDataByName [POST]
  132. func GetDataByName(c *gin.Context) {
  133. appG := app.Gin{C: c}
  134. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  135. fsion := gofasion.NewFasion(string(dataByte))
  136. sqlnamestr := fsion.Get("name").ValueStr()
  137. offset := fsion.Get("offset").ValueDefaultInt(0)
  138. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  139. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  140. tx := restful.Engine.NewSession()
  141. err := tx.Begin()
  142. defer func() {
  143. switch {
  144. case err != nil:
  145. logging.Error("tx.Begin 事务启动失败__error:", err)
  146. if tx != nil {
  147. tx.Rollback()
  148. }
  149. default:
  150. if tx != nil {
  151. err = tx.Commit()
  152. }
  153. }
  154. if tx != nil {
  155. tx.Close()
  156. }
  157. }()
  158. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  159. //fmt.Println("sql========\n",sqlnamestr,sql)
  160. if sql != "" {
  161. s_params := make([]interface{}, 0)
  162. if fsion.HasKey("parammaps") {
  163. parammaps := fsion.Get("parammaps")
  164. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  165. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  166. }
  167. paramslist := strings.Split(p, ",")
  168. if len(paramslist) > 0 && p != "" {
  169. for _, value := range paramslist {
  170. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  171. if tempv, exists := c.Get("jwt_username"); exists {
  172. s_params = append(s_params, tempv.(string))
  173. } else {
  174. s_params = append(s_params, "")
  175. }
  176. } else {
  177. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  178. }
  179. }
  180. }
  181. } else if fsion.HasKey("params") {
  182. params := fsion.Get("params").Array()
  183. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  184. for _, v_params := range params {
  185. s_params = append(s_params, v_params.ValueStr())
  186. }
  187. }
  188. if returntype == "xlsx" {
  189. var xlsxfile *xlsx.File
  190. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  191. //c.File("c:/dd.xlsx")
  192. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  193. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  194. c.Header("Content-Transfer-Encoding", "binary")
  195. _ = xlsxfile.Write(c.Writer)
  196. } else {
  197. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  198. //fmt.Printf("%v+\n",queryData)
  199. if err != nil {
  200. logging.Error("GetDataByName err: ", err)
  201. msg := geterrmsg(err.Error())
  202. appG.Response(http.StatusOK, e.ERROR, msg)
  203. } else {
  204. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  205. }
  206. }
  207. } else {
  208. appG.Response(http.StatusOK, e.SUCCESS, nil)
  209. }
  210. }
  211. func GetDataBySqlIN(c *gin.Context) {
  212. appG := app.Gin{C: c}
  213. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  214. fsion := gofasion.NewFasion(string(dataByte))
  215. sqlnamestr := fsion.Get("name").ValueStr()
  216. offset := fsion.Get("offset").ValueDefaultInt(0)
  217. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  218. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  219. tx := restful.Engine.NewSession()
  220. err := tx.Begin()
  221. defer func() {
  222. switch {
  223. case err != nil:
  224. logging.Error("tx.Begin 事务启动失败__error:", err)
  225. if tx != nil {
  226. tx.Rollback()
  227. }
  228. default:
  229. if tx != nil {
  230. err = tx.Commit()
  231. }
  232. }
  233. if tx != nil {
  234. tx.Close()
  235. }
  236. }()
  237. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  238. //fmt.Println("sql========\n",sqlnamestr,sql)
  239. if sql != "" {
  240. s_params := make([]interface{}, 0)
  241. if fsion.HasKey("parammaps") {
  242. parammaps := fsion.Get("parammaps")
  243. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  244. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  245. }
  246. paramslist := strings.Split(p, ",")
  247. if len(paramslist) > 0 && p != "" {
  248. for _, value := range paramslist {
  249. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  250. if tempv, exists := c.Get("jwt_username"); exists {
  251. s_params = append(s_params, tempv.(string))
  252. } else {
  253. s_params = append(s_params, "")
  254. }
  255. } else {
  256. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  257. }
  258. }
  259. }
  260. } else if fsion.HasKey("params") {
  261. params := fsion.Get("params").Array()
  262. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  263. for _, v_params := range params {
  264. s_params = append(s_params, v_params.ValueStr())
  265. }
  266. }
  267. if returntype == "xlsx" {
  268. var xlsxfile *xlsx.File
  269. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  270. //c.File("c:/dd.xlsx")
  271. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  272. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  273. c.Header("Content-Transfer-Encoding", "binary")
  274. _ = xlsxfile.Write(c.Writer)
  275. } else {
  276. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  277. //fmt.Printf("%v+\n",queryData)
  278. if err != nil {
  279. logging.Error("GetDataByName err: ", err)
  280. msg := geterrmsg(err.Error())
  281. appG.Response(http.StatusOK, e.ERROR, msg)
  282. } else {
  283. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  284. }
  285. }
  286. } else {
  287. appG.Response(http.StatusOK, e.SUCCESS, nil)
  288. }
  289. }
  290. func geterrmsg(errstr string) (msgstr string) {
  291. var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$") // 判断是否为汉字
  292. rs := []rune(errstr)
  293. if hzRegexp.MatchString(string(rs[0])) {
  294. msgstr = errstr
  295. return
  296. }
  297. if strings.Contains(errstr, "Duplicate entry") {
  298. strs := strings.Split(errstr, ` key '`)
  299. if len(strs) > 1 {
  300. if len(strs[len(strs)-1]) > 1 {
  301. errstr = "Duplicate entry :" + strs[len(strs)-1]
  302. }
  303. }
  304. }
  305. msg, err := restful.Engine.SQL("SELECT msg FROM err_msg where err = ? ", errstr).QueryString()
  306. if err != nil {
  307. msgstr = errstr
  308. return
  309. }
  310. if msg == nil || len(msg) == 0 {
  311. var param []interface{}
  312. param = append(param, errstr, errstr)
  313. go func() {
  314. tx := restful.Engine.NewSession()
  315. err := tx.Begin()
  316. defer func() {
  317. switch {
  318. case err != nil:
  319. logging.Error("tx.Begin 事务启动失败__error:", err)
  320. if tx != nil {
  321. tx.Rollback()
  322. }
  323. default:
  324. if tx != nil {
  325. err = tx.Commit()
  326. }
  327. }
  328. if tx != nil {
  329. tx.Close()
  330. }
  331. }()
  332. _, err = restful.ExecQueryT("insert into err_msg(err,msg) VALUES(?,?) ON DUPLICATE KEY UPDATE err=err", param, tx)
  333. if err != nil {
  334. logging.Error("insert into err_msg(err,msg) err", err)
  335. }
  336. }()
  337. msgstr = errstr
  338. } else {
  339. msgstr = msg[0]["msg"]
  340. }
  341. return
  342. }
  343. // @Summary 根据APISQL表中SQL执行得到数据集
  344. // @Tags GetDataByNameForm
  345. // @Accept json
  346. // @Produce json
  347. // @Param sqlname body json true "sqlname"
  348. // @Param offset query int true "offset"
  349. // @Param pagecount query int true "pagecount"
  350. // @Param returntype query string true "returntype"
  351. // @Param s formData json true "ss"
  352. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  353. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  354. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  355. // @Router /data/GetDataByNameForm [POST]
  356. func GetDataByNameForm(c *gin.Context) {
  357. appG := app.Gin{C: c}
  358. //sqlnamestr := c.PostForm("measureNumber")
  359. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  360. //fsion := gofasion.NewFasion(string(dataByte))
  361. //sqlnamestr := fsion.Get("name").ValueStr()
  362. //offset := fsion.Get("offset").ValueInt()
  363. //pagecount := fsion.Get("pagecount").ValueInt()
  364. //returntype := fsion.Get("returntype").ValueStr()
  365. //params := fsion.Get("params").Array()
  366. c.Request.ParseForm()
  367. params := c.Request.Form
  368. offset := 0
  369. pageSize_ := 0
  370. returntype := params.Get("page")
  371. if returntype == "" {
  372. returntype = "0"
  373. }
  374. if tempv, exists := c.Get("jwt_username"); exists {
  375. params.Set("JwtUserName", tempv.(string))
  376. } else {
  377. params.Set("JwtUserName", "")
  378. }
  379. sqlnamestr := params.Get("name")
  380. logging.Info("GetDataByNameForm ", c.Keys, sqlnamestr)
  381. s_params := make([]interface{}, 0)
  382. if returntype == "1" {
  383. pageNum, error := strconv.Atoi(params.Get("pageNum"))
  384. if error != nil {
  385. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
  386. }
  387. pageSize, error := strconv.Atoi(params.Get("pageSize"))
  388. if error != nil {
  389. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
  390. }
  391. pageSize_ = pageSize
  392. offset = pageNum
  393. }
  394. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  395. paramslist := strings.Split(p, ",")
  396. if len(paramslist) > 0 && p != "" {
  397. for _, value := range paramslist {
  398. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  399. }
  400. }
  401. if sql != "" {
  402. if returntype == "xlsx" {
  403. var xlsxfile *xlsx.File
  404. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  405. c.Header("Content-Type", "application/octet-stream")
  406. c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
  407. c.Header("Content-Transfer-Encoding", "binary")
  408. _ = xlsxfile.Write(c.Writer)
  409. } else {
  410. queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
  411. if err != nil {
  412. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  413. } else {
  414. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  415. }
  416. }
  417. } else {
  418. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  419. }
  420. }
  421. // @Summary 递归获取所有
  422. // @Tags menu
  423. // @Accept json
  424. // @Produce json
  425. // @Param Ton query string true "Ton"
  426. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  427. // @Router /authdata/GetRoleMenus [GET]
  428. func GetRecuDataByName(c *gin.Context) {
  429. appG := app.Gin{C: c}
  430. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  431. fsion := gofasion.NewFasion(string(dataByte))
  432. sqlnamestr := fsion.Get("name").ValueStr()
  433. parentidname := fsion.Get("idname").ValueStr()
  434. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  435. params := make(map[string]interface{})
  436. s_params := make([]interface{}, 0)
  437. if fsion.HasKey("params") {
  438. for _, v_params := range fsion.Get("params").Array() {
  439. s_params = append(s_params, v_params.ValueStr())
  440. }
  441. params["params"] = s_params
  442. } else {
  443. jparams := fsion.Get("parammaps")
  444. paramslist := strings.Split(p, ",")
  445. if len(paramslist) > 0 && p != "" {
  446. for _, value := range paramslist {
  447. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  448. if tempv, exists := c.Get("jwt_username"); exists {
  449. params[value] = tempv.(string)
  450. } else {
  451. params[value] = ""
  452. }
  453. } else {
  454. params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
  455. }
  456. }
  457. }
  458. }
  459. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
  460. logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  461. if err != nil {
  462. appG.Response(http.StatusOK, e.ERROR, err.Error())
  463. }
  464. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  465. }
  466. // @Summary 根据APISQL表中SQL执行得到数据集
  467. // @Tags GetDataByNames
  468. // @Accept json
  469. // @Produce json
  470. // @RequestBody sqlname query string true "sqlname"
  471. // @Param offset query int true "offset"
  472. // @Param pagecount query int true "pagecount"
  473. // @Param returntype query string true "returntype"
  474. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  475. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  476. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  477. // @Router /data/GetDataByNames [POST]
  478. func GetDataByNames(c *gin.Context) {
  479. appG := app.Gin{C: c}
  480. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  481. fsion := gofasion.NewFasion(string(dataByte))
  482. sqlParams := fsion.Array()
  483. queryData := make(map[string]interface{})
  484. for _, v := range sqlParams {
  485. fsion2 := gofasion.NewFasion(v.Json())
  486. logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
  487. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  488. if sql == "" {
  489. errorm := make(map[string]interface{})
  490. errorm["code"] = e.ERROR
  491. errorm["message"] = "sql没找到"
  492. queryData[fsion2.Get("name").ValueStr()] = errorm
  493. continue
  494. } else if sql != "" {
  495. offset := fsion2.Get("offset").ValueInt()
  496. pagecount := fsion2.Get("pagecount").ValueInt()
  497. returntype := fsion2.Get("returntype").ValueStr()
  498. s_params := make([]interface{}, 0)
  499. if fsion2.HasKey("parammaps") {
  500. parammaps := fsion2.Get("parammaps")
  501. paramslist := strings.Split(p, ",")
  502. if len(paramslist) > 0 && p != "" {
  503. for _, value := range paramslist {
  504. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  505. if tempv, exists := c.Get("jwt_username"); exists {
  506. s_params = append(s_params, tempv.(string))
  507. } else {
  508. s_params = append(s_params, "")
  509. }
  510. } else {
  511. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  512. }
  513. }
  514. }
  515. } else if fsion2.HasKey("params") {
  516. params := fsion2.Get("params").Array()
  517. for _, v_params := range params {
  518. s_params = append(s_params, v_params.ValueStr())
  519. }
  520. }
  521. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  522. if err != nil {
  523. errorm := make(map[string]interface{})
  524. errorm["code"] = e.ERROR
  525. errorm["message"] = err.Error()
  526. queryData[fsion2.Get("name").ValueStr()] = errorm
  527. logging.Error("GetDataByNames err: ", err)
  528. continue
  529. } else {
  530. queryData[fsion2.Get("name").ValueStr()] = queryData1
  531. }
  532. }
  533. }
  534. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  535. }
  536. // @Summary 根据APISQL表中SQL执行得到数据集
  537. // @Tags PostDataByNameForm
  538. // @Accept json
  539. // @Produce json
  540. // @Param sqlname query string true "sqlname"
  541. // @Param offset query int true "offset"
  542. // @Param pagecount query int true "pagecount"
  543. // @Param returntype query string true "returntype"
  544. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  545. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
  546. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  547. // @Router /data/PostDataByNameForm [POST]
  548. func PostDataByNameForm(c *gin.Context) {
  549. appG := app.Gin{C: c}
  550. c.Request.ParseForm()
  551. params := c.Request.Form
  552. sqlnamestr := params.Get("name")
  553. logging.Info("PostDataByNameForm ", c.Keys, sqlnamestr)
  554. s_params := make([]interface{}, 0)
  555. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  556. paramslist := strings.Split(p, ",")
  557. if len(paramslist) > 0 {
  558. for _, value := range paramslist {
  559. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  560. }
  561. }
  562. if sql != "" {
  563. queryData, err := execDataBySql(sql, s_params)
  564. if err != nil {
  565. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  566. } else {
  567. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  568. }
  569. } else {
  570. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  571. }
  572. }
  573. func GetReportform(c *gin.Context) {
  574. appG := app.Gin{C: c}
  575. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  576. fsion := gofasion.NewFasion(string(dataByte))
  577. sqlnamestr := fsion.Get("name").ValueStr()
  578. parammaps := fsion.Get("parammaps")
  579. logging.Info("GetReportform ", c.Keys, sqlnamestr, parammaps)
  580. tx := restful.Engine.NewSession()
  581. err := tx.Begin()
  582. defer func() {
  583. switch {
  584. case err != nil:
  585. fmt.Println("__error:", err)
  586. if tx != nil {
  587. tx.Rollback()
  588. }
  589. default:
  590. if tx != nil {
  591. err = tx.Commit()
  592. }
  593. }
  594. if tx != nil {
  595. tx.Close()
  596. }
  597. }()
  598. queryData, err := praseReportform(sqlnamestr, parammaps, tx)
  599. if err != nil{
  600. appG.Response(http.StatusOK, e.ERROR, err.Error())
  601. }else {
  602. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  603. }
  604. }
  605. func GetUpkeepPlan(c *gin.Context) {
  606. appG := app.Gin{C: c}
  607. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  608. fsion := gofasion.NewFasion(string(dataByte))
  609. sqlnamestr := fsion.Get("name").ValueStr()
  610. offset := fsion.Get("offset").ValueDefaultInt(0)
  611. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  612. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  613. month := fsion.Get("month").ValueDefaultStr("map")
  614. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  615. tx := restful.Engine.NewSession()
  616. err := tx.Begin()
  617. defer func() {
  618. switch {
  619. case err != nil:
  620. fmt.Println("__error:", err)
  621. if tx != nil {
  622. tx.Rollback()
  623. }
  624. default:
  625. if tx != nil {
  626. err = tx.Commit()
  627. }
  628. }
  629. if tx != nil {
  630. tx.Close()
  631. }
  632. }()
  633. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  634. if sql != "" {
  635. s_params := make([]interface{}, 0)
  636. if fsion.HasKey("parammaps") {
  637. parammaps := fsion.Get("parammaps")
  638. paramslist := strings.Split(p, ",")
  639. if len(paramslist) > 0 && p != "" {
  640. for _, value := range paramslist {
  641. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  642. if tempv, exists := c.Get("jwt_username"); exists {
  643. s_params = append(s_params, tempv.(string))
  644. } else {
  645. s_params = append(s_params, "")
  646. }
  647. } else {
  648. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  649. }
  650. }
  651. }
  652. } else if fsion.HasKey("params") {
  653. params := fsion.Get("params").Array()
  654. for _, v_params := range params {
  655. s_params = append(s_params, v_params.ValueStr())
  656. }
  657. }
  658. if returntype == "xlsx" {
  659. var xlsxfile *xlsx.File
  660. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  661. //c.File("c:/dd.xlsx")
  662. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  663. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  664. c.Header("Content-Transfer-Encoding", "binary")
  665. _ = xlsxfile.Write(c.Writer)
  666. } else {
  667. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  668. if err != nil {
  669. appG.Response(http.StatusOK, e.ERROR, err.Error())
  670. } else {
  671. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  672. if v, ok := value["id"]; ok {
  673. //month = "2020-03"
  674. seviceplans, _ := tx.SQL("SELECT * FROM seviceplan where eqId = ? and month=? ", v, month).QueryString() //获取保养计划
  675. if seviceplans != nil {
  676. for _, va := range seviceplans { //根据保养计划获取保养单
  677. //day,_ :=strconv.Atoi(va["day"])
  678. bigupkeeps, _ := tx.SQL("SELECT * FROM bigupkeep where seviceplanId = ?", va["id"]).QueryString() //获取保养计划中的保养单
  679. //buk := make(map[string]map[string]string)
  680. for _, bu := range bigupkeeps { //日期为key ,保养单为value 存到map中
  681. jstr := ""
  682. if bu["plantime"][8:9] == "0" {
  683. jstr = "m" + bu["plantime"][9:]
  684. } else {
  685. jstr = "m" + bu["plantime"][8:]
  686. }
  687. if bu["flowCompeleted"] < "2" && bu["plantime"] < time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  688. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#2" + "#" + bu["id"] + "#" + bu["pushbackId"]
  689. } else {
  690. if bu["flowCompeleted"] >= "2" {
  691. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#1" + "#" + bu["id"] + "#" + bu["pushbackId"]
  692. } else {
  693. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#0" + "#" + bu["id"] + "#" + bu["pushbackId"]
  694. }
  695. }
  696. }
  697. }
  698. }
  699. }
  700. }
  701. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  702. }
  703. }
  704. } else {
  705. appG.Response(http.StatusOK, e.SUCCESS, nil)
  706. }
  707. }
  708. func GetArrList(c *gin.Context) {
  709. appG := app.Gin{C: c}
  710. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  711. fsion := gofasion.NewFasion(string(dataByte))
  712. sqlnamestr := fsion.Get("name").ValueStr()
  713. sqlnamestr1 := fsion.Get("name1").ValueStr()
  714. offset := fsion.Get("offset").ValueDefaultInt(0)
  715. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  716. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  717. month := fsion.Get("month").ValueDefaultStr("map")
  718. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  719. tx := restful.Engine.NewSession()
  720. err := tx.Begin()
  721. defer func() {
  722. switch {
  723. case err != nil:
  724. fmt.Println("__error:", err)
  725. if tx != nil {
  726. tx.Rollback()
  727. }
  728. default:
  729. if tx != nil {
  730. err = tx.Commit()
  731. }
  732. }
  733. if tx != nil {
  734. tx.Close()
  735. }
  736. }()
  737. //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
  738. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  739. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  740. if sql != "" {
  741. s_params := make([]interface{}, 0)
  742. if fsion.HasKey("parammaps") {
  743. parammaps := fsion.Get("parammaps")
  744. paramslist := strings.Split(p, ",")
  745. if len(paramslist) > 0 && p != "" {
  746. for _, value := range paramslist {
  747. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  748. if tempv, exists := c.Get("jwt_username"); exists {
  749. s_params = append(s_params, tempv.(string))
  750. } else {
  751. s_params = append(s_params, "")
  752. }
  753. } else {
  754. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  755. }
  756. }
  757. }
  758. }
  759. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  760. if err != nil {
  761. appG.Response(http.StatusOK, e.ERROR, err.Error())
  762. } else {
  763. paramslist1 := strings.Split(p1, ",")
  764. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  765. s_params1 := make([]interface{}, 0)
  766. if len(paramslist1) > 0 && p1 != "" {
  767. for _, v := range paramslist1 {
  768. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  769. }
  770. }
  771. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  772. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  773. }
  774. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  775. }
  776. } else {
  777. appG.Response(http.StatusOK, e.SUCCESS, nil)
  778. }
  779. }
  780. func GetRJSBData(c *gin.Context) {
  781. appG := app.Gin{C: c}
  782. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  783. fsion := gofasion.NewFasion(string(dataByte))
  784. sqlnamestr := fsion.Get("name").ValueStr()
  785. sqlnamestr1 := fsion.Get("name1").ValueStr()
  786. offset := fsion.Get("offset").ValueDefaultInt(0)
  787. pagecount := fsion.Get("plancount").ValueDefaultInt(0)
  788. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  789. month := fsion.Get("month").ValueDefaultStr("map")
  790. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  791. tx := restful.Engine.NewSession()
  792. err := tx.Begin()
  793. defer func() {
  794. switch {
  795. case err != nil:
  796. fmt.Println("__error:", err)
  797. if tx != nil {
  798. tx.Rollback()
  799. }
  800. default:
  801. if tx != nil {
  802. err = tx.Commit()
  803. }
  804. }
  805. if tx != nil {
  806. tx.Close()
  807. }
  808. }()
  809. //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
  810. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  811. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  812. if sql != "" {
  813. s_params := make([]interface{}, 0)
  814. if fsion.HasKey("parammaps") {
  815. parammaps := fsion.Get("parammaps")
  816. paramslist := strings.Split(p, ",")
  817. if len(paramslist) > 0 && p != "" {
  818. for _, value := range paramslist {
  819. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  820. if tempv, exists := c.Get("jwt_username"); exists {
  821. s_params = append(s_params, tempv.(string))
  822. } else {
  823. s_params = append(s_params, "")
  824. }
  825. } else {
  826. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  827. }
  828. }
  829. }
  830. }
  831. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  832. if err != nil {
  833. appG.Response(http.StatusOK, e.ERROR, err.Error())
  834. } else {
  835. paramslist1 := strings.Split(p1, ",")
  836. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  837. s_params1 := make([]interface{}, 0)
  838. if len(paramslist1) > 0 && p1 != "" {
  839. for _, v := range paramslist1 {
  840. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  841. }
  842. }
  843. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  844. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  845. }
  846. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  847. }
  848. } else {
  849. appG.Response(http.StatusOK, e.SUCCESS, nil)
  850. }
  851. }
  852. func GetAccount(c *gin.Context) {
  853. appG := app.Gin{C: c}
  854. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  855. fsion := gofasion.NewFasion(string(dataByte))
  856. sqlnamestr := fsion.Get("name").ValueStr()
  857. offset := fsion.Get("offset").ValueDefaultInt(0)
  858. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  859. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  860. tx := restful.Engine.NewSession()
  861. err := tx.Begin()
  862. defer func() {
  863. switch {
  864. case err != nil:
  865. logging.Error("tx.Begin 事务启动失败__error:", err)
  866. if tx != nil {
  867. tx.Rollback()
  868. }
  869. default:
  870. if tx != nil {
  871. err = tx.Commit()
  872. }
  873. }
  874. if tx != nil {
  875. tx.Close()
  876. }
  877. }()
  878. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  879. //fmt.Println("sql========\n",sqlnamestr,sql)
  880. if sql != "" {
  881. s_params := make([]interface{}, 0)
  882. if fsion.HasKey("parammaps") {
  883. parammaps := fsion.Get("parammaps")
  884. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  885. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  886. }
  887. paramslist := strings.Split(p, ",")
  888. if len(paramslist) > 0 && p != "" {
  889. for _, value := range paramslist {
  890. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  891. if tempv, exists := c.Get("jwt_username"); exists {
  892. s_params = append(s_params, tempv.(string))
  893. } else {
  894. s_params = append(s_params, "")
  895. }
  896. } else {
  897. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  898. }
  899. }
  900. }
  901. } else if fsion.HasKey("params") {
  902. params := fsion.Get("params").Array()
  903. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  904. for _, v_params := range params {
  905. s_params = append(s_params, v_params.ValueStr())
  906. }
  907. }
  908. if returntype == "xlsx" {
  909. var xlsxfile *xlsx.File
  910. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  911. //c.File("c:/dd.xlsx")
  912. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  913. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  914. c.Header("Content-Transfer-Encoding", "binary")
  915. _ = xlsxfile.Write(c.Writer)
  916. } else {
  917. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  918. if err != nil {
  919. logging.Error("GetDataByName err: ", err)
  920. msg := geterrmsg(err.Error())
  921. appG.Response(http.StatusOK, e.ERROR, msg)
  922. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  923. } else {
  924. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  925. }
  926. }
  927. } else {
  928. appG.Response(http.StatusOK, e.SUCCESS, nil)
  929. }
  930. }
  931. // @Summary 根据APISQL表中SQL执行得到数据集
  932. // @Tags PostDataByName
  933. // @Accept json
  934. // @Produce json
  935. // @Param sqlname query string true "sqlname"
  936. // @Param offset query int true "offset"
  937. // @Param pagecount query int true "pagecount"
  938. // @Param returntype query string true "returntype"
  939. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  940. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  941. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  942. // @Router /data/PostDataByName [POST]
  943. func PostDataByName(c *gin.Context) {
  944. appG := app.Gin{C: c}
  945. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  946. fsion := gofasion.NewFasion(string(dataByte))
  947. sqlnamestr := fsion.Get("name").ValueStr()
  948. s_params := make([]interface{}, 0)
  949. tx := restful.Engine.NewSession()
  950. err := tx.Begin()
  951. defer func() {
  952. switch {
  953. case err != nil:
  954. fmt.Println("__error:", err)
  955. if tx != nil {
  956. tx.Rollback()
  957. }
  958. default:
  959. if tx != nil {
  960. err = tx.Commit()
  961. }
  962. }
  963. if tx != nil {
  964. tx.Close()
  965. }
  966. }()
  967. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  968. if sql != "" {
  969. if fsion.HasKey("parammaps") {
  970. parammaps := fsion.Get("parammaps")
  971. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  972. paramslist := strings.Split(p, ",")
  973. if len(paramslist) > 0 && p != "" {
  974. for _, value := range paramslist {
  975. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  976. if tempv, exists := c.Get("jwt_username"); exists {
  977. s_params = append(s_params, tempv.(string))
  978. } else {
  979. s_params = append(s_params, "")
  980. }
  981. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  982. ids, err := setting.SnowIds.NextId()
  983. if err != nil {
  984. ids = time.Now().UnixNano()
  985. logging.Info("create SnowIds err", err)
  986. }
  987. s_params = append(s_params, ids)
  988. } else {
  989. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  990. }
  991. }
  992. }
  993. } else if fsion.HasKey("params") {
  994. params := fsion.Get("params").Array()
  995. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  996. //ids,err := setting.SnowIds.NextId()
  997. //if err !=nil{
  998. // ids = time.Now().UnixNano()
  999. // logging.Info("create SnowIds err",err)
  1000. //}
  1001. //s_params = append(s_params, ids)
  1002. for _, v_params := range params {
  1003. s_params = append(s_params, v_params.ValueStr())
  1004. }
  1005. }
  1006. queryData, err := execDataBySqlT(sql, s_params, tx)
  1007. if err != nil {
  1008. logging.Error("PostDataByName err: ", err)
  1009. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1010. msg := geterrmsg(err.Error())
  1011. appG.Response(http.StatusOK, e.ERROR, msg)
  1012. } else {
  1013. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1014. }
  1015. } else {
  1016. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1017. }
  1018. }
  1019. func PostRJSBData(c *gin.Context) {
  1020. appG := app.Gin{C: c}
  1021. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1022. fsion := gofasion.NewFasion(string(dataByte))
  1023. logging.Info("PostRJSBData err: ", string(dataByte))
  1024. s_params := make([]interface{}, 0)
  1025. tx := restful.Engine.NewSession()
  1026. err := tx.Begin()
  1027. defer func() {
  1028. switch {
  1029. case err != nil:
  1030. fmt.Println("__error:", err)
  1031. if tx != nil {
  1032. tx.Rollback()
  1033. }
  1034. default:
  1035. if tx != nil {
  1036. err = tx.Commit()
  1037. }
  1038. }
  1039. if tx != nil {
  1040. tx.Close()
  1041. }
  1042. }()
  1043. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1044. sql1, p1 := restful.GetSqlByNameDBT("updated1YJ", tx)
  1045. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJ", tx)
  1046. sql3, p3 := restful.GetSqlByNameDBT("updated2YJ", tx)
  1047. if !fsion.HasKey("parammaps") {
  1048. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1049. return
  1050. }
  1051. parammaps := fsion.Get("parammaps")
  1052. // ==========================1========================
  1053. paramslist := strings.Split(p, ",")
  1054. if len(paramslist) > 0 && p != "" {
  1055. for _, value := range paramslist {
  1056. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1057. }
  1058. }
  1059. _, err = tx.SQL(sql, s_params...).Execute()
  1060. if err != nil {
  1061. fmt.Println(err)
  1062. logging.Error("PostRJSBData err: ", err)
  1063. msg := geterrmsg(err.Error())
  1064. appG.Response(http.StatusOK, e.ERROR, msg)
  1065. return
  1066. }
  1067. // ==========================2========================
  1068. s_params = make([]interface{}, 0)
  1069. paramslist = strings.Split(p1, ",")
  1070. if len(paramslist) > 0 && p1 != "" {
  1071. for _, value := range paramslist {
  1072. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1073. }
  1074. }
  1075. _, err = tx.SQL(sql1, s_params...).Execute()
  1076. if err != nil {
  1077. logging.Error("PostRJSBData1 err: ", err)
  1078. msg := geterrmsg(err.Error())
  1079. appG.Response(http.StatusOK, e.ERROR, msg)
  1080. return
  1081. }
  1082. s_params = make([]interface{}, 0)
  1083. // ==========================3========================
  1084. paramslist = strings.Split(p2, ",")
  1085. if len(paramslist) > 0 && p2 != "" {
  1086. for _, value := range paramslist {
  1087. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1088. }
  1089. }
  1090. _, err = tx.SQL(sql2, s_params...).Execute()
  1091. if err != nil {
  1092. logging.Error("PostRJSBData2 err: ", err)
  1093. msg := geterrmsg(err.Error())
  1094. appG.Response(http.StatusOK, e.ERROR, msg)
  1095. return
  1096. }
  1097. s_params = make([]interface{}, 0)
  1098. // ==========================4========================
  1099. paramslist = strings.Split(p3, ",")
  1100. if len(paramslist) > 0 && p3 != "" {
  1101. for _, value := range paramslist {
  1102. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1103. }
  1104. }
  1105. _, err = tx.SQL(sql3, s_params...).Execute()
  1106. if err != nil {
  1107. logging.Error("PostRJSBData3 err: ", err)
  1108. msg := geterrmsg(err.Error())
  1109. appG.Response(http.StatusOK, e.ERROR, msg)
  1110. return
  1111. }
  1112. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1113. }
  1114. func PostRJSBDatas(c *gin.Context) {
  1115. appG := app.Gin{C: c}
  1116. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1117. fsions := gofasion.NewFasion(string(dataByte)).Array()
  1118. logging.Info("PostRJSBDatas err: ", string(dataByte))
  1119. tx := restful.Engine.NewSession()
  1120. err := tx.Begin()
  1121. defer func() {
  1122. switch {
  1123. case err != nil:
  1124. fmt.Println("__error:", err)
  1125. if tx != nil {
  1126. tx.Rollback()
  1127. }
  1128. default:
  1129. if tx != nil {
  1130. err = tx.Commit()
  1131. }
  1132. }
  1133. if tx != nil {
  1134. tx.Close()
  1135. }
  1136. }()
  1137. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1138. sql1, p1 := restful.GetSqlByNameDBT("updated1YJV2", tx)
  1139. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJV2", tx)
  1140. sql3, p3 := restful.GetSqlByNameDBT("updated2YJV2", tx)
  1141. for _, fsion := range fsions {
  1142. if !fsion.HasKey("parammaps") {
  1143. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1144. return
  1145. }
  1146. parammaps := fsion.Get("parammaps")
  1147. s_params := make([]interface{}, 0)
  1148. // ==========================1========================
  1149. paramslist := strings.Split(p, ",")
  1150. if len(paramslist) > 0 && p != "" {
  1151. for _, value := range paramslist {
  1152. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1153. }
  1154. }
  1155. fmt.Println("}}}}}}}}}}}}}}}}}}}",s_params)
  1156. _, err = tx.SQL(sql, s_params...).Execute()
  1157. if err != nil {
  1158. fmt.Println(err)
  1159. logging.Error("PostRJSBData err: ", err)
  1160. msg := geterrmsg(err.Error())
  1161. appG.Response(http.StatusOK, e.ERROR, msg)
  1162. return
  1163. }
  1164. // ==========================2========================
  1165. s_params = make([]interface{}, 0)
  1166. paramslist = strings.Split(p1, ",")
  1167. if len(paramslist) > 0 && p1 != "" {
  1168. for _, value := range paramslist {
  1169. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1170. }
  1171. }
  1172. _, err = tx.SQL(sql1, s_params...).Execute()
  1173. if err != nil {
  1174. logging.Error("PostRJSBData1 err: ", err)
  1175. msg := geterrmsg(err.Error())
  1176. appG.Response(http.StatusOK, e.ERROR, msg)
  1177. return
  1178. }
  1179. s_params = make([]interface{}, 0)
  1180. // ==========================3========================
  1181. paramslist = strings.Split(p2, ",")
  1182. if len(paramslist) > 0 && p2 != "" {
  1183. for _, value := range paramslist {
  1184. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1185. }
  1186. }
  1187. _, err = tx.SQL(sql2, s_params...).Execute()
  1188. if err != nil {
  1189. logging.Error("PostRJSBData2 err: ", err)
  1190. msg := geterrmsg(err.Error())
  1191. appG.Response(http.StatusOK, e.ERROR, msg)
  1192. return
  1193. }
  1194. s_params = make([]interface{}, 0)
  1195. // ==========================4========================
  1196. paramslist = strings.Split(p3, ",")
  1197. if len(paramslist) > 0 && p3 != "" {
  1198. for _, value := range paramslist {
  1199. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1200. }
  1201. }
  1202. _, err = tx.SQL(sql3, s_params...).Execute()
  1203. if err != nil {
  1204. logging.Error("PostRJSBData3 err: ", err)
  1205. msg := geterrmsg(err.Error())
  1206. appG.Response(http.StatusOK, e.ERROR, msg)
  1207. return
  1208. }
  1209. }
  1210. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1211. }
  1212. // @Summary 根据APISQL表中SQL执行得到数据集
  1213. // @Tags ExecDataByConfig
  1214. // @Accept json
  1215. // @Produce json
  1216. // @Param sqlname query string true "sqlname"
  1217. // @Param offset query int true "offset"
  1218. // @Param pagecount query int true "pagecount"
  1219. // @Param returntype query string true "returntype"
  1220. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1221. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1222. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1223. // @Router /data/PostDataByName [POST]
  1224. func ExecDataByConfig(c *gin.Context) {
  1225. appG := app.Gin{C: c}
  1226. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1227. tempval_ := make(map[string]interface{})
  1228. tempCommon := make(map[string]interface{})
  1229. tempval := make([]map[string]interface{}, 0)
  1230. err := json.Unmarshal(dataByte, &tempval_)
  1231. logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  1232. if err != nil {
  1233. fmt.Println("error:", err)
  1234. } else {
  1235. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  1236. tempCommon = v
  1237. }
  1238. if v, ok := tempval_["data"].([]interface{}); ok {
  1239. for _, Tvalue := range v {
  1240. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1241. tempval = append(tempval, v1)
  1242. }
  1243. }
  1244. }
  1245. tx := restful.Engine.NewSession()
  1246. err := tx.Begin()
  1247. if err != nil {
  1248. logging.Error("tx.Begin 事务启动失败__error:", err)
  1249. }
  1250. defer func() {
  1251. switch {
  1252. case err != nil:
  1253. fmt.Println("__error:", err)
  1254. if tx != nil {
  1255. tx.Rollback()
  1256. }
  1257. default:
  1258. if tx != nil {
  1259. err = tx.Commit()
  1260. }
  1261. }
  1262. if tx != nil {
  1263. tx.Close()
  1264. }
  1265. }()
  1266. if err == nil {
  1267. if tempv, exists := c.Get("jwt_username"); exists {
  1268. tempCommon["jwt_username"] = tempv.(string)
  1269. }
  1270. for _, paramvalue := range tempval {
  1271. if _, ok := paramvalue["resultname"]; !ok {
  1272. paramvalue["resultname"] = paramvalue["name"]
  1273. }
  1274. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  1275. if err != nil {
  1276. logging.Error("ExecDataParam error:", err)
  1277. break
  1278. }
  1279. }
  1280. if err == nil {
  1281. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  1282. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  1283. } else {
  1284. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  1285. }
  1286. } else {
  1287. msg := geterrmsg(err.Error())
  1288. appG.Response(http.StatusOK, e.ERROR, msg)
  1289. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1290. }
  1291. }
  1292. }
  1293. if err != nil {
  1294. msg := geterrmsg(err.Error())
  1295. appG.Response(http.StatusOK, e.ERROR, msg)
  1296. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1297. }
  1298. }
  1299. func interfacetostring(val interface{}) string {
  1300. switch val.(type) {
  1301. case string:
  1302. ss := val.(string)
  1303. return ss
  1304. break
  1305. case int:
  1306. ss := val.(int)
  1307. return strconv.Itoa(ss)
  1308. break
  1309. case int64:
  1310. ss := val.(int64)
  1311. return strconv.FormatInt(ss, 10)
  1312. break
  1313. case float64:
  1314. ss := val.(float64)
  1315. return strconv.FormatFloat(ss, 'E', -1, 32)
  1316. break
  1317. }
  1318. return ""
  1319. }
  1320. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  1321. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1322. tempstrb := strings.IndexAny(valuestring, "@")
  1323. tempstre := strings.IndexAny(valuestring, ".")
  1324. if tempstrb > -1 && tempstre > -1 {
  1325. for _, ParamMapList := range ParamMap {
  1326. if v, ok := ParamMapList["resultname"]; ok {
  1327. if v == valuestring[tempstrb+1:tempstre] {
  1328. if v1, ok := ParamMapList["resultmaps"]; ok {
  1329. if newA, ok := v1.([]map[string]interface{}); ok {
  1330. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  1331. } else if newA, ok := v1.(map[string]interface{}); ok {
  1332. return interfacetostring(newA[valuestring[tempstre+1:]])
  1333. }
  1334. }
  1335. }
  1336. }
  1337. }
  1338. } else {
  1339. return valuestring
  1340. }
  1341. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  1342. return strconv.FormatInt(int64(valuestring), 10)
  1343. }
  1344. return ""
  1345. }
  1346. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  1347. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1348. tempstrb := strings.IndexAny(valuestring, "@")
  1349. tempstre := strings.IndexAny(valuestring, ".")
  1350. if tempstrb > -1 && tempstre > -1 {
  1351. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  1352. if s, ok := v.(string); ok {
  1353. return s
  1354. } else if s, ok := v.(float64); ok {
  1355. return strconv.FormatInt(int64(s), 10)
  1356. }
  1357. }
  1358. }
  1359. }
  1360. return ""
  1361. }
  1362. func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  1363. sqlname := fasion["name"].(string)
  1364. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  1365. if v, ok := fasion["type"]; ok {
  1366. sqltype = v.(string)
  1367. }
  1368. sql := ""
  1369. p := ""
  1370. s_params := make([]interface{}, 0)
  1371. if sqltype != "" {
  1372. sql, p = restful.GetSqlByName(sqlname, tx)
  1373. //sqlApi, _ := models.GetApisqlByName(sqlname)
  1374. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  1375. paramslist := strings.Split(p, ",")
  1376. if len(paramslist) > 0 && p != "" {
  1377. for _, value := range paramslist {
  1378. ss := ""
  1379. if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  1380. ids, err := setting.SnowIds.NextId()
  1381. if err != nil {
  1382. ids = time.Now().UnixNano()
  1383. logging.Info("create SnowIds err", err)
  1384. }
  1385. s_params = append(s_params, ids)
  1386. } else {
  1387. if ss == "" && CommonParamMap != nil {
  1388. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  1389. ss = valuestring
  1390. tempstrb := strings.IndexAny(valuestring, "@")
  1391. tempstre := strings.IndexAny(valuestring, ".")
  1392. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  1393. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  1394. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  1395. }
  1396. } else {
  1397. ss = ""
  1398. }
  1399. }
  1400. }
  1401. if ss == "" && ParentParamMap != nil {
  1402. ss = getParamValue_(v, value, ParentParamMap)
  1403. }
  1404. if ss == "" && sameParamMap != nil {
  1405. ss = getParamValue(v, value, sameParamMap)
  1406. }
  1407. if ss == "" && ParamMap != nil {
  1408. ss = getParamValue(v, value, ParamMap)
  1409. }
  1410. s_params = append(s_params, ss)
  1411. }
  1412. }
  1413. }
  1414. }
  1415. }
  1416. if sql == "" {
  1417. sqltype = ""
  1418. }
  1419. fmt.Printf("%v+ \n", s_params, sqlname)
  1420. if sqltype == "s1" {
  1421. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  1422. if v, ok := tempValParam.(map[string]interface{}); ok {
  1423. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1424. }
  1425. if err != nil {
  1426. return nil, err
  1427. }
  1428. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1429. if err != nil {
  1430. return nil, err
  1431. }
  1432. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  1433. } else if sqltype == "sm" {
  1434. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  1435. if v, ok := tempValParam.(map[string]interface{}); ok {
  1436. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1437. } else {
  1438. fmt.Println("sm", tempValParam)
  1439. }
  1440. if err != nil {
  1441. return nil, err
  1442. }
  1443. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1444. if err != nil {
  1445. return nil, err
  1446. }
  1447. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  1448. } else if sqltype == "v" {
  1449. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  1450. if v, ok := tempValParam.(map[string]interface{}); ok {
  1451. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1452. } else {
  1453. fmt.Println("v", tempValParam)
  1454. }
  1455. if err != nil {
  1456. return nil, err
  1457. }
  1458. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  1459. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  1460. }
  1461. } else if sqltype == "e" {
  1462. tempVal_arrary := make([]map[string]interface{}, 0)
  1463. tempVal_map := make(map[string]interface{})
  1464. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  1465. if err == nil {
  1466. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  1467. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  1468. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  1469. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  1470. return tempVal_arrary, err
  1471. }
  1472. return nil, err
  1473. } else {
  1474. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  1475. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1476. if err != nil {
  1477. return nil, err
  1478. }
  1479. return nil, err
  1480. }
  1481. }
  1482. return nil, nil
  1483. }
  1484. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *xorm.Session) error {
  1485. var err error
  1486. subarray, ok := fasion["children"]
  1487. if ok {
  1488. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  1489. tempVal_arrary := make([]map[string]interface{}, 0)
  1490. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  1491. tempVal_ := make(map[string]interface{})
  1492. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  1493. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  1494. } else {
  1495. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  1496. }
  1497. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  1498. if err != nil {
  1499. return err
  1500. }
  1501. tempVal_arrary = append(tempVal_arrary, tempVal_)
  1502. }
  1503. v.(map[string]interface{})["children"] = tempVal_arrary
  1504. }
  1505. }
  1506. return nil
  1507. }
  1508. // @Summary 删除图片
  1509. // @Tags RemovePicByName
  1510. // @Accept json
  1511. // @Produce json
  1512. // @Param sqlname query string true "sqlname"
  1513. // @Param offset query int true "offset"
  1514. // @Param pagecount query int true "pagecount"
  1515. // @Param returntype query string true "returntype"
  1516. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1517. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1518. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1519. // @Router /data/PostDataByName [POST]
  1520. func RemovePicByName(c *gin.Context) {
  1521. logging.Info("RemovePicByName ", c.Keys)
  1522. appG := app.Gin{C: c}
  1523. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1524. fsion := gofasion.NewFasion(string(dataByte))
  1525. sqlnamestr := fsion.Get("name").ValueStr()
  1526. picname := fsion.Get("picname").ValueStr()
  1527. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1528. params := fsion.Get("params").Array()
  1529. s_params := make([]interface{}, 0)
  1530. for _, v_params := range params {
  1531. s_params = append(s_params, v_params.ValueStr())
  1532. }
  1533. if sql != "" {
  1534. queryData, err := execDataBySql(sql, s_params)
  1535. if picname != "" {
  1536. // Destination
  1537. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  1538. err := os.Remove(distPath) //删除文件test.txt
  1539. if err != nil {
  1540. //如果删除失败则输出 file remove Error!
  1541. fmt.Println("file remove Error!")
  1542. //输出错误详细信息
  1543. fmt.Printf("%s", err)
  1544. } else {
  1545. //如果删除成功则输出 file remove OK!
  1546. fmt.Print("file remove OK!")
  1547. }
  1548. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  1549. err = os.Remove(distPath) //删除文件test.txt
  1550. if err != nil {
  1551. //如果删除失败则输出 file remove Error!
  1552. fmt.Println("file remove Error!")
  1553. //输出错误详细信息
  1554. fmt.Printf("%s", err)
  1555. } else {
  1556. //如果删除成功则输出 file remove OK!
  1557. fmt.Print("file remove OK!")
  1558. }
  1559. }
  1560. if err != nil {
  1561. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1562. } else {
  1563. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1564. }
  1565. } else {
  1566. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1567. }
  1568. }
  1569. // @Summary 删除文件
  1570. // @Tags RemoveFileByName
  1571. // @Accept json
  1572. // @Produce json
  1573. // @Param sqlname query string true "sqlname"
  1574. // @Param offset query int true "offset"
  1575. // @Param pagecount query int true "pagecount"
  1576. // @Param returntype query string true "returntype"
  1577. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1578. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1579. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1580. // @Router /data/PostDataByName [POST]
  1581. func RemoveFileByName(c *gin.Context) {
  1582. appG := app.Gin{C: c}
  1583. id := c.Param("id")
  1584. logging.Info("DownloadFile ", c.Keys, id)
  1585. eqdoc, err := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", id).QueryString()
  1586. if eqdoc == nil {
  1587. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1588. return
  1589. }
  1590. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  1591. err = os.Remove(distPath) //删除文件test.txt
  1592. if err != nil {
  1593. //如果删除失败则输出 file remove Error!
  1594. fmt.Println("file remove Error!")
  1595. //输出错误详细信息
  1596. fmt.Printf("%s", err)
  1597. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1598. return
  1599. }
  1600. fmt.Print("file remove OK!")
  1601. _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
  1602. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  1603. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1604. //fsion := gofasion.NewFasion(string(dataByte))
  1605. //sqlnamestr := fsion.Get("name").ValueStr()
  1606. //filename := fsion.Get("filename").ValueStr()
  1607. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1608. //params := fsion.Get("params").Array()
  1609. //s_params := make([]interface{}, 0)
  1610. //for _, v_params := range params {
  1611. // s_params = append(s_params, v_params.ValueStr())
  1612. //}
  1613. //if sql != "" {
  1614. // queryData, err := execDataBySql(sql, s_params)
  1615. //
  1616. // if filename != "" {
  1617. // // Destination
  1618. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  1619. //
  1620. // err := os.Remove(distPath) //删除文件test.txt
  1621. // if err != nil {
  1622. // //如果删除失败则输出 file remove Error!
  1623. // fmt.Println("file remove Error!")
  1624. // //输出错误详细信息
  1625. // fmt.Printf("%s", err)
  1626. // } else {
  1627. // //如果删除成功则输出 file remove OK!
  1628. // fmt.Print("file remove OK!")
  1629. // }
  1630. // }
  1631. // if err != nil {
  1632. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  1633. // } else {
  1634. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  1635. // }
  1636. //} else {
  1637. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  1638. //}
  1639. }
  1640. // @Summary 根据APISQL表中SQL执行得到数据集
  1641. // @Tags PostDataByName
  1642. // @Accept json
  1643. // @Produce json
  1644. // @Param sqlname query string true "sqlname"
  1645. // @Param offset query int true "offset"
  1646. // @Param pagecount query int true "pagecount"
  1647. // @Param returntype query string true "returntype"
  1648. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1649. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1650. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1651. // @Router /data/PostDataByName [POST]
  1652. func UpdateDataRelation(c *gin.Context) {
  1653. appG := app.Gin{C: c}
  1654. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1655. fsion := gofasion.NewFasion(string(dataByte))
  1656. tablename := fsion.Get("name").ValueStr()
  1657. dataname := fsion.Get("dataname").ValueStr()
  1658. datavalue := fsion.Get("datavalue").ValueStr()
  1659. valuename := fsion.Get("valuename").ValueStr()
  1660. logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  1661. values := fsion.Get("values").Array()
  1662. tx := restful.Engine.NewSession()
  1663. err := tx.Begin()
  1664. defer func() {
  1665. switch {
  1666. case err != nil:
  1667. fmt.Println("__error:", err)
  1668. if tx != nil {
  1669. tx.Rollback()
  1670. }
  1671. default:
  1672. if tx != nil {
  1673. err = tx.Commit()
  1674. }
  1675. }
  1676. if tx != nil {
  1677. tx.Close()
  1678. }
  1679. }()
  1680. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  1681. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  1682. d_params := make([]interface{}, 4)
  1683. d_params[0] = datavalue
  1684. s_params := ""
  1685. for _, v_values := range values {
  1686. d_params[1] = v_values.ValueStr()
  1687. if s_params == "" {
  1688. s_params = v_values.ValueStr()
  1689. } else {
  1690. s_params = s_params + "," + v_values.ValueStr()
  1691. }
  1692. d_params[2] = d_params[1]
  1693. d_params[3] = d_params[0]
  1694. _, err := execDataBySqlT(insertSqlstr, d_params, tx)
  1695. if err != nil {
  1696. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1697. break
  1698. }
  1699. }
  1700. if s_params != "" {
  1701. deleteSqlstr := "delete from " + tablename + " " +
  1702. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  1703. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  1704. if err != nil {
  1705. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1706. } else if err == nil {
  1707. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1708. }
  1709. } else {
  1710. deleteSqlstr := "delete from " + tablename + " " +
  1711. " where " + dataname + " = " + datavalue
  1712. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  1713. if err != nil {
  1714. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1715. } else if err == nil {
  1716. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1717. }
  1718. }
  1719. }
  1720. // @Summary 根据APISQL表中SQL执行得到数据集
  1721. // @Tags PostDatasByName
  1722. // @Accept json
  1723. // @Produce json
  1724. // @Param sqlname query string true "sqlname"
  1725. // @Param offset query int true "offset"
  1726. // @Param pagecount query int true "pagecount"
  1727. // @Param returntype query string true "returntype"
  1728. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1729. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1730. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1731. // @Router /data/PostDatasByName [POST]
  1732. func PostDataByNames(c *gin.Context) {
  1733. appG := app.Gin{C: c}
  1734. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1735. fsion := gofasion.NewFasion(string(dataByte))
  1736. sqlParams := fsion.Array()
  1737. queryData := make(map[string]interface{})
  1738. for _, v := range sqlParams {
  1739. fsion2 := gofasion.NewFasion(v.Json())
  1740. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  1741. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  1742. if sql == "" {
  1743. errorm := make(map[string]interface{})
  1744. errorm["code"] = e.ERROR
  1745. errorm["message"] = "sql没有找到"
  1746. errorm["param"] = ""
  1747. queryData[fsion2.Get("name").ValueStr()] = errorm
  1748. continue
  1749. } else if sql != "" {
  1750. s_params := make([]interface{}, 0)
  1751. if fsion2.HasKey("parammaps") {
  1752. parammaps := fsion2.Get("parammaps")
  1753. paramslist := strings.Split(p, ",")
  1754. if len(paramslist) > 0 && p != "" {
  1755. for _, value := range paramslist {
  1756. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1757. if tempv, exists := c.Get("jwt_username"); exists {
  1758. s_params = append(s_params, tempv.(string))
  1759. } else {
  1760. s_params = append(s_params, "")
  1761. }
  1762. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  1763. ids, err := setting.SnowIds.NextId()
  1764. if err != nil {
  1765. ids = time.Now().UnixNano()
  1766. logging.Info("create SnowIds err", err)
  1767. }
  1768. s_params = append(s_params, ids)
  1769. } else {
  1770. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1771. }
  1772. }
  1773. }
  1774. } else if fsion2.HasKey("params") {
  1775. params := fsion2.Get("params").Array()
  1776. //ids,err := setting.SnowIds.NextId()
  1777. //if err !=nil{
  1778. // ids = time.Now().UnixNano()
  1779. // logging.Info("create SnowIds err",err)
  1780. //}
  1781. //s_params = append(s_params, ids)
  1782. for _, v_params := range params {
  1783. s_params = append(s_params, v_params.ValueStr())
  1784. }
  1785. }
  1786. _, err := execDataBySql(sql, s_params)
  1787. if err != nil {
  1788. logging.Error("PostDataByName err: ", err)
  1789. errorm := make(map[string]interface{})
  1790. errorm["code"] = e.ERROR
  1791. errorm["message"] = err.Error()
  1792. errorm["param"] = s_params
  1793. queryData[fsion2.Get("name").ValueStr()] = errorm
  1794. continue
  1795. } else {
  1796. errorm := make(map[string]interface{})
  1797. errorm["code"] = e.SUCCESS
  1798. errorm["message"] = "success"
  1799. queryData[fsion2.Get("name").ValueStr()] = errorm
  1800. continue
  1801. }
  1802. } else {
  1803. errorm := make(map[string]interface{})
  1804. errorm["code"] = e.ERROR
  1805. errorm["message"] = "NO SQL"
  1806. errorm["param"] = ""
  1807. queryData[fsion2.Get("name").ValueStr()] = errorm
  1808. }
  1809. }
  1810. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1811. }
  1812. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  1813. if offset > 0 {
  1814. offset = offset - 1
  1815. }
  1816. //if pagecount == 0 {
  1817. // pagecount = setting.AppSetting.PageSize
  1818. //}else if pagecount>setting.AppSetting.PageSize{
  1819. // pagecount = setting.AppSetting.PageSize
  1820. //}
  1821. if returntype == "list" {
  1822. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  1823. return queryData, err
  1824. } else if returntype == "listlist" {
  1825. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  1826. return queryData, err
  1827. } else {
  1828. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  1829. return queryData, err
  1830. }
  1831. }
  1832. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  1833. if offset > 0 {
  1834. offset = offset - 1
  1835. }
  1836. //if pagecount == 0 {
  1837. // pagecount = setting.AppSetting.PageSize
  1838. //}else if pagecount>setting.AppSetting.PageSize{
  1839. // pagecount = setting.AppSetting.PageSize
  1840. //}
  1841. if returntype == "list" {
  1842. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  1843. return queryData, err
  1844. } else if returntype == "listlist" {
  1845. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  1846. return queryData, err
  1847. } else {
  1848. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  1849. return queryData, err
  1850. }
  1851. }
  1852. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  1853. paramslist := strings.Split(Params, ",")
  1854. s_params := make([]interface{}, 0)
  1855. if isList {
  1856. for _, v_params := range thisParams["params"].([]interface{}) {
  1857. s_params = append(s_params, v_params)
  1858. }
  1859. } else {
  1860. paramslist := strings.Split(Params, ",")
  1861. if len(paramslist) > 0 && Params != "" {
  1862. for _, value := range paramslist {
  1863. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1864. }
  1865. }
  1866. }
  1867. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  1868. for _, queryData_ := range queryData {
  1869. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  1870. if isList {
  1871. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  1872. if queryData_[idname] != nil {
  1873. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  1874. }
  1875. } else {
  1876. if queryData_[idname] != nil {
  1877. thisParams[idname] = queryData_[idname].(interface{})
  1878. }
  1879. if len(paramslist) > 0 && Params != "" {
  1880. s_params = make([]interface{}, 0)
  1881. for _, value := range paramslist {
  1882. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1883. }
  1884. }
  1885. }
  1886. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  1887. } else {
  1888. }
  1889. }
  1890. return queryData, err
  1891. }
  1892. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1893. queryData, err := restful.ExecQuery(sqlstr, params)
  1894. return queryData, err
  1895. }
  1896. func execDataBySqlT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  1897. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  1898. return queryData, err
  1899. }
  1900. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1901. queryData, err := restful.ExecQuery(sqlstr, params)
  1902. return queryData, err
  1903. }
  1904. // @Summary 导入excel文件到数据库,excel第一行作为参数
  1905. // @Tags ImportExcel
  1906. // @Accept json
  1907. // @Produce json
  1908. // @Param file query string true "文件名"
  1909. // @Param name header string true "执行sql的名字"
  1910. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1911. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1912. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1913. // @Router /data/ImportExcel [POST]
  1914. func ImportExcel(c *gin.Context) {
  1915. appG := app.Gin{C: c}
  1916. inputdata := ""
  1917. c.Request.ParseForm()
  1918. //tempv1, _ := c.Get("jwt_username")
  1919. //fmt.Println("=======================",tempv1)
  1920. params := c.Request.Form
  1921. file, _, err := c.Request.FormFile("file")
  1922. if err != nil {
  1923. appG.Response(http.StatusOK, e.ERROR, err)
  1924. return
  1925. }
  1926. sqlnamestrs := params.Get("name")
  1927. sheetname := params.Get("sheetname")
  1928. pastureid := params.Get("pastureid")
  1929. //importParams := params.Get("importParams")
  1930. logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestrs)
  1931. //paramslist := strings.Split(importParams, ",")
  1932. //paramslist = append(paramslist, "jwt_username")
  1933. requiredParams := params.Get("requiredParams")
  1934. dateParams := params.Get("dateParams")
  1935. numParams := params.Get("numParams")
  1936. dateParamslist := strings.Split(dateParams, ",")
  1937. requiredParamslist := strings.Split(requiredParams, ",")
  1938. numParamslist := strings.Split(numParams, ",")
  1939. queryData := make([]interface{}, 0)
  1940. sqlslist := strings.Split(sqlnamestrs, ",")
  1941. xlsx1, err := excelize.OpenReader(file)
  1942. if err != nil {
  1943. fmt.Println(err)
  1944. return
  1945. }
  1946. ok := 0
  1947. error := 0
  1948. isdone := true
  1949. rows, _ := xlsx1.GetRows(sheetname)
  1950. if len(rows) > 10000 { // 限制1000条
  1951. rows = rows[:10000]
  1952. }
  1953. for i, row := range rows {
  1954. if i > 0 {
  1955. entry := make(map[string]interface{})
  1956. outentry := make(map[string]interface{})
  1957. for j, colCell := range rows[0] {
  1958. for k, colCell1 := range row {
  1959. if j == k {
  1960. outentry[colCell] = colCell1
  1961. entry[colCell] = colCell1
  1962. }
  1963. }
  1964. }
  1965. inputdata = ""
  1966. a := ""
  1967. for _, v := range dateParamslist {
  1968. if v == ""{
  1969. continue
  1970. }
  1971. if d, ok := outentry[strings.Trim(v, " ")]; ok {
  1972. dtime,err := TransfTime(d.(string))
  1973. if err != nil {
  1974. a = strings.Trim(v, " ")
  1975. break
  1976. }
  1977. outentry[strings.Trim(v, " ")] = dtime
  1978. }
  1979. }
  1980. if a != "" {
  1981. outentry["error_msg"] = a + " 格式错误"
  1982. error++
  1983. queryData = append(queryData, outentry)
  1984. continue
  1985. }
  1986. a = ""
  1987. for _, v := range requiredParamslist {
  1988. if v == ""{
  1989. continue
  1990. }
  1991. if d, ok := outentry[strings.Trim(v, " ")]; !ok || d == "" {
  1992. a = strings.Trim(v, " ")
  1993. break
  1994. }
  1995. }
  1996. if a != "" {
  1997. outentry["error_msg"] = a + " 未填或格式错误"
  1998. error++
  1999. queryData = append(queryData, outentry)
  2000. continue
  2001. }
  2002. a = ""
  2003. for _, v := range numParamslist {
  2004. if v == ""{
  2005. continue
  2006. }
  2007. d, ok := outentry[strings.Trim(v, " ")]
  2008. if !ok || d == "" {
  2009. outentry[strings.Trim(v, " ")] = 0
  2010. }else {
  2011. var fl float64
  2012. switch d.(type) {
  2013. case string :
  2014. fl,err = strconv.ParseFloat(d.(string),64)
  2015. }
  2016. if err != nil || fl<0 {
  2017. a = strings.Trim(v, " ")
  2018. break
  2019. }
  2020. }
  2021. }
  2022. if a != "" {
  2023. outentry["error_msg"] = a + " 为数值类型且不可为负数"
  2024. error++
  2025. queryData = append(queryData, outentry)
  2026. continue
  2027. }
  2028. tx := restful.Engine.NewSession()
  2029. err := tx.Begin()
  2030. for _, sqlnamestr := range sqlslist {
  2031. sql, paramslists := restful.GetSqlByNameDB(sqlnamestr)
  2032. paramslist := strings.Split(paramslists, ",")
  2033. if len(paramslist) > 0 {
  2034. s_params := make([]interface{}, 0)
  2035. for _, value := range paramslist {
  2036. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2037. if tempv, exists := c.Get("jwt_username"); exists {
  2038. s_params = append(s_params, tempv.(string))
  2039. } else {
  2040. s_params = append(s_params, "")
  2041. }
  2042. }else if strings.ToLower(strings.Trim(value, " ")) == "pastureid"{
  2043. s_params = append(s_params, pastureid)
  2044. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2045. ids, err := setting.SnowIds.NextId()
  2046. if err != nil {
  2047. ids = time.Now().UnixNano()
  2048. logging.Info("create SnowIds err", err)
  2049. }
  2050. s_params = append(s_params, ids)
  2051. }else {
  2052. if outentry[strings.Trim(value, " ")] == nil {
  2053. s_params = append(s_params,"")
  2054. }else{
  2055. s_params = append(s_params, outentry[strings.Trim(value, " ")])
  2056. }
  2057. }
  2058. }
  2059. //fmt.Println("================",s_params)
  2060. outentry["input"] = inputdata
  2061. returnmsgmap1, err := tx.SQL(sql, s_params...).Query().List()
  2062. if err != nil {
  2063. if tx != nil {
  2064. tx.Rollback()
  2065. }
  2066. if tx != nil {
  2067. tx.Close()
  2068. }
  2069. msg := geterrmsg(err.Error())
  2070. outentry["error_msg"] = msg
  2071. error++
  2072. queryData = append(queryData, outentry)
  2073. isdone = true
  2074. break
  2075. } else{
  2076. if len(returnmsgmap1) > 0 {
  2077. if va,ok :=returnmsgmap1[0]["msg"] ; ok && va != "" {
  2078. if tx != nil {
  2079. tx.Rollback()
  2080. }
  2081. if tx != nil {
  2082. tx.Close()
  2083. }
  2084. msg := geterrmsg(va.(string))
  2085. outentry["error_msg"] = msg
  2086. error++
  2087. queryData = append(queryData, outentry)
  2088. isdone = true
  2089. break
  2090. }
  2091. }
  2092. isdone = false
  2093. }
  2094. }
  2095. }
  2096. switch {
  2097. case err != nil:
  2098. if tx != nil {
  2099. tx.Rollback()
  2100. }
  2101. default:
  2102. if tx != nil {
  2103. err = tx.Commit()
  2104. }
  2105. }
  2106. if tx != nil {
  2107. tx.Close()
  2108. }
  2109. }
  2110. if !isdone{
  2111. ok++
  2112. isdone = true
  2113. }
  2114. }
  2115. data := make(map[string]interface{})
  2116. data["result"] = queryData
  2117. data["success"] = ok
  2118. data["err_count"] = error
  2119. appG.Response(http.StatusOK, e.SUCCESS, data)
  2120. }
  2121. func TransfTime(timestr string) (string,error) {
  2122. if timestr == "" {
  2123. return time.Now().Format("2006-01-02"),nil
  2124. }
  2125. days, err := strconv.Atoi(timestr)
  2126. if err != nil {
  2127. if len(timestr) == 8 && strings.Count(timestr, "-") == 2 {
  2128. return "20" + timestr[6:] + "-" + timestr[:5],nil
  2129. }
  2130. _,err :=time.Parse("2006-01-02",timestr)
  2131. if err != nil{
  2132. t,err :=time.Parse("2006/01/02",timestr)
  2133. if err != nil{
  2134. t,err :=time.Parse("2006.01.02",timestr)
  2135. if err != nil {
  2136. return "",err
  2137. }else {
  2138. return t.Format("2006-01-02"),nil
  2139. }
  2140. }else {
  2141. return t.Format("2006-01-02"),nil
  2142. }
  2143. }else {
  2144. return timestr,nil
  2145. }
  2146. }
  2147. times, _ := time.Parse("2006-01-02", "1899-12-30")
  2148. times = times.AddDate(0, 0, days)
  2149. return times.Format("2006-01-02"),nil
  2150. }
  2151. func Createchart(c *gin.Context) {
  2152. appG := app.Gin{C: c}
  2153. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2154. fsion := gofasion.NewFasion(string(dataByte))
  2155. chart_name := fsion.Get("chart_name").ValueStr()
  2156. desc := fsion.Get("desc").ValueStr()
  2157. pastureid := fsion.Get("pastureid").ValueStr()
  2158. source_id := fsion.Get("source_id").ValueStr()
  2159. emp := fsion.Get("emp").ValueStr()
  2160. content := fsion.Get("content").Json()
  2161. //fmt.Println("=============",parammaW.Json())
  2162. var mapResult map[string]interface{}
  2163. err1 := json.Unmarshal([]byte(content), &mapResult)
  2164. if err1 != nil {
  2165. fmt.Println("JsonToMapDemo err: ", err1)
  2166. }
  2167. //fmt.Println("---------",mapResult)
  2168. tx := restful.Engine.NewSession()
  2169. err := tx.Begin()
  2170. defer func() {
  2171. switch {
  2172. case err != nil:
  2173. logging.Error("tx.Begin 事务启动失败__error:", err)
  2174. if tx != nil {
  2175. tx.Rollback()
  2176. }
  2177. default:
  2178. if tx != nil {
  2179. err = tx.Commit()
  2180. }
  2181. }
  2182. if tx != nil {
  2183. tx.Close()
  2184. }
  2185. }()
  2186. ids, err := setting.SnowIds.NextId()
  2187. if err != nil {
  2188. ids = time.Now().UnixNano()
  2189. logging.Info("create SnowIds err", err)
  2190. }
  2191. exec, err := tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  2192. ids, pastureid, chart_name, 0, emp, desc, source_id)
  2193. if err != nil {
  2194. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2195. } else {
  2196. appG.Response(http.StatusOK, e.SUCCESS, exec)
  2197. }
  2198. }
  2199. func Updatedashboard(c *gin.Context) {
  2200. appG := app.Gin{C: c}
  2201. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2202. fsion := gofasion.NewFasion(string(dataByte))
  2203. dashboard_id := fsion.Get("dashboard_id").ValueStr()
  2204. pastureid := fsion.Get("pastureid").ValueStr()
  2205. content := fsion.Get("content").Json()
  2206. //fmt.Println("=============",parammaW.Json())
  2207. var mapResult map[string]interface{}
  2208. err1 := json.Unmarshal([]byte(content), &mapResult)
  2209. if err1 != nil {
  2210. fmt.Println("JsonToMapDemo err: ", err1)
  2211. }
  2212. layout := mapResult["layout"]
  2213. fmt.Println("---------", layout)
  2214. tx := restful.Engine.NewSession()
  2215. err := tx.Begin()
  2216. defer func() {
  2217. switch {
  2218. case err != nil:
  2219. logging.Error("tx.Begin 事务启动失败__error:", err)
  2220. if tx != nil {
  2221. tx.Rollback()
  2222. }
  2223. default:
  2224. if tx != nil {
  2225. err = tx.Commit()
  2226. }
  2227. }
  2228. if tx != nil {
  2229. tx.Close()
  2230. }
  2231. }()
  2232. //fmt.Println("sql========\n",sqlnamestr,sql)
  2233. exec, err := tx.SQL("select content from dashboard where pastureid=? and id=?", pastureid, dashboard_id).Query().List()
  2234. if err != nil {
  2235. println("2", err.Error())
  2236. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2237. } else {
  2238. if exec != nil {
  2239. if exec[0]["content"] == nil {
  2240. mapR := make(map[string]interface{})
  2241. mapR["layout"] = layout
  2242. mjson, _ := json.Marshal(mapR)
  2243. mString := string(mjson)
  2244. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2245. if err3 != nil {
  2246. println("3", err3.Error())
  2247. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  2248. } else {
  2249. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  2250. }
  2251. } else {
  2252. var mapR map[string]interface{}
  2253. err2 := json.Unmarshal([]byte(exec[0]["content"].(string)), &mapR)
  2254. if err1 != nil {
  2255. fmt.Println("JsonToMapDemo err: ", err2)
  2256. }
  2257. mapR["layout"] = layout
  2258. mjson, _ := json.Marshal(mapR)
  2259. mString := string(mjson)
  2260. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2261. if err3 != nil {
  2262. println("3", err3.Error())
  2263. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  2264. } else {
  2265. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  2266. }
  2267. }
  2268. } else {
  2269. appG.Response(http.StatusOK, e.ERROR, "未找到对应的仪表盘")
  2270. }
  2271. }
  2272. }
  2273. func ExecSQL(c *gin.Context) {
  2274. appG := app.Gin{C: c}
  2275. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2276. fsion := gofasion.NewFasion(string(dataByte))
  2277. content := fsion.Get("content").Json()
  2278. pastureid := fsion.Get("pastureid").ValueStr()
  2279. //pastureid := fsion.Get("pastureid").ValueStr()
  2280. //println(pastureid,"==================")
  2281. //fmt.Println("=============",parammaW.Json())
  2282. var mapResult map[string]interface{}
  2283. err1 := json.Unmarshal([]byte(content), &mapResult)
  2284. if err1 != nil {
  2285. fmt.Println("JsonToMapDemo err: ", err1)
  2286. }
  2287. //fmt.Println("---------",mapResult)
  2288. sqlstr := "select "
  2289. groupstr := " GROUP BY "
  2290. for _, value := range mapResult["selectedDimension"].([]interface{}) {
  2291. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  2292. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  2293. }
  2294. for _, value := range mapResult["selectedCalcul"].([]interface{}) {
  2295. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  2296. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  2297. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  2298. } else {
  2299. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  2300. sqlstr = sqlstr + tem + ","
  2301. }
  2302. }
  2303. if _, ok := mapResult["selectedCalcul2"]; ok {
  2304. for _, value := range mapResult["selectedCalcul2"].([]interface{}) {
  2305. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  2306. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  2307. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  2308. } else {
  2309. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  2310. sqlstr = sqlstr + tem + ","
  2311. }
  2312. }
  2313. }
  2314. //
  2315. sqlstr = sqlstr[0 : len(sqlstr)-1]
  2316. sqlstr = sqlstr + " from " + mapResult["dataSrc"].(string)
  2317. //if len(mapResult["filters"].([]interface {}))>0{
  2318. sqlstr = sqlstr + " where pastureid=" + pastureid
  2319. //}
  2320. //
  2321. if len(mapResult["filters"].([]interface{})) > 0 {
  2322. sqlstr = sqlstr + " and "
  2323. }
  2324. println(len(mapResult["filters"].([]interface{})),sqlstr)
  2325. for _, value := range mapResult["filters"].([]interface{}) {
  2326. switch value.(map[string]interface{})["filterOperator"].(string) {
  2327. case ">", "=", "<", ">=", "<=", "!=", "like":
  2328. sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + value.(map[string]interface{})["filterOperator"].(string) + " " + value.(map[string]interface{})["value"].(map[string]interface{})["value1"].(string) + " AND "
  2329. case "IN":
  2330. sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + "("
  2331. for _, value1 := range value.(map[string]interface{})["value"].(map[string]interface{})["arrValue"].([]interface{}) {
  2332. sqlstr = sqlstr + value1.(string) + ","
  2333. }
  2334. sqlstr = sqlstr[0 : len(sqlstr)-1]
  2335. sqlstr = sqlstr + ")" + " AND "
  2336. case "BETWEEN":
  2337. sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + " " + value.(map[string]interface{})["value"].(map[string]interface{})["value1"].(string) + " and " + value.(map[string]interface{})["value"].(map[string]interface{})["value2"].(string) + " AND "
  2338. }
  2339. }
  2340. if len(mapResult["filters"].([]interface{})) > 0 {
  2341. sqlstr = sqlstr[0 : len(sqlstr)-4]
  2342. }
  2343. //
  2344. groupstr = groupstr[0 : len(groupstr)-1]
  2345. sqlstr = sqlstr + groupstr + " "
  2346. if len(mapResult["orderByStrs"].([]interface{})) > 0 {
  2347. sqlstr = sqlstr + " order by "
  2348. }
  2349. //
  2350. for key, value := range mapResult["orderByStrs"].([]interface{}) {
  2351. if key == len(mapResult["orderByStrs"].([]interface{}))-1 {
  2352. sqlstr = sqlstr + value.(string) + " "
  2353. } else {
  2354. sqlstr = sqlstr + value.(string) + ","
  2355. }
  2356. }
  2357. //
  2358. sqlstr = sqlstr + " limit " + strconv.Itoa(int(mapResult["limit"].(float64)))
  2359. //fmt.Println("====++---------",sqlstr)
  2360. tx := restful.Engine.NewSession()
  2361. err := tx.Begin()
  2362. defer func() {
  2363. switch {
  2364. case err != nil:
  2365. logging.Error("tx.Begin 事务启动失败__error:", err)
  2366. if tx != nil {
  2367. tx.Rollback()
  2368. }
  2369. default:
  2370. if tx != nil {
  2371. err = tx.Commit()
  2372. }
  2373. }
  2374. if tx != nil {
  2375. tx.Close()
  2376. }
  2377. }()
  2378. //fmt.Println("sql========\n",sqlnamestr,sql)
  2379. //ids,err := setting.SnowIds.NextId()
  2380. //if err !=nil{
  2381. // ids = time.Now().UnixNano()
  2382. // logging.Info("create SnowIds err",err)
  2383. //}
  2384. // exec,err :=tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  2385. //ids,pastureid,chart_name,0,emp,desc,source_id)
  2386. exec, err := tx.SQL(sqlstr).Query().List()
  2387. if err != nil {
  2388. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2389. } else {
  2390. appG.Response(http.StatusOK, e.SUCCESS, exec)
  2391. }
  2392. }
  2393. func Dashboards(c *gin.Context) {
  2394. appG := app.Gin{C: c}
  2395. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2396. fsion := gofasion.NewFasion(string(dataByte))
  2397. sqlnamestr := fsion.Get("name").ValueStr()
  2398. //sqlnamestr1 := fsion.Get("name1").ValueStr()
  2399. offset := fsion.Get("offset").ValueDefaultInt(0)
  2400. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2401. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  2402. month := fsion.Get("month").ValueDefaultStr("map")
  2403. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  2404. tx := restful.Engine.NewSession()
  2405. err := tx.Begin()
  2406. defer func() {
  2407. switch {
  2408. case err != nil:
  2409. fmt.Println("__error:", err)
  2410. if tx != nil {
  2411. tx.Rollback()
  2412. }
  2413. default:
  2414. if tx != nil {
  2415. err = tx.Commit()
  2416. }
  2417. }
  2418. if tx != nil {
  2419. tx.Close()
  2420. }
  2421. }()
  2422. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  2423. //sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  2424. if sql != "" {
  2425. s_params := make([]interface{}, 0)
  2426. if fsion.HasKey("parammaps") {
  2427. parammaps := fsion.Get("parammaps")
  2428. paramslist := strings.Split(p, ",")
  2429. if len(paramslist) > 0 && p != "" {
  2430. for _, value := range paramslist {
  2431. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2432. if tempv, exists := c.Get("jwt_username"); exists {
  2433. s_params = append(s_params, tempv.(string))
  2434. } else {
  2435. s_params = append(s_params, "")
  2436. }
  2437. } else {
  2438. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2439. }
  2440. }
  2441. }
  2442. }
  2443. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  2444. if err != nil {
  2445. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2446. } else {
  2447. //paramslist1 := strings.Split(p1, ",")
  2448. dids := make([]interface{}, 0)
  2449. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  2450. var mapResult map[string]interface{}
  2451. dids = append(dids, value["dashboard_id"])
  2452. if value["content"] != nil {
  2453. err1 := json.Unmarshal([]byte(value["content"].(string)), &mapResult)
  2454. if err1 != nil {
  2455. println("JsonToMapDemo1 err: ", err1)
  2456. }
  2457. //fmt.Printf("%v+\n",value["content"].(string))
  2458. temap := make(map[string]interface{})
  2459. tem := mapResult["layout"].([]interface{})
  2460. if v, ok := value["chartids"]; ok {
  2461. chartids := strings.Split(v.(string), ",")
  2462. for _, va := range chartids {
  2463. a := false
  2464. for _, v2 := range tem {
  2465. if v2.(map[string]interface{})["id"].(string) == va {
  2466. a = true
  2467. //println("va1======",va)
  2468. }
  2469. }
  2470. if !a {
  2471. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  2472. va + "\",\"id\":\"" + va +
  2473. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":100,\"yOffSet\":18}"
  2474. var mapTem map[string]interface{}
  2475. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  2476. if err1 != nil {
  2477. println("JsonToMapDemo err: ", err1)
  2478. }
  2479. tem = append(tem, mapTem)
  2480. }
  2481. }
  2482. temap["layout"] = tem
  2483. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  2484. }
  2485. } else {
  2486. temap := make(map[string]interface{})
  2487. tem := make([]interface{}, 0)
  2488. if v, ok := value["chartids"]; ok {
  2489. chartids := strings.Split(v.(string), ",")
  2490. for _, va := range chartids {
  2491. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  2492. va + "\",\"id\":\"" + va +
  2493. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":0,\"yOffSet\":18}"
  2494. var mapTem map[string]interface{}
  2495. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  2496. if err1 != nil {
  2497. println("JsonToMapDemo err: ", err1)
  2498. }
  2499. tem = append(tem, mapTem)
  2500. }
  2501. temap["layout"] = tem
  2502. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  2503. }
  2504. }
  2505. }
  2506. queryData.(map[string]interface{})["order"] = dids
  2507. queryData.(map[string]interface{})["dashboards"] = queryData.(map[string]interface{})["list"]
  2508. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2509. }
  2510. } else {
  2511. appG.Response(http.StatusOK, e.SUCCESS, nil)
  2512. }
  2513. }
  2514. func HttpPostData(url, RequestMethod, parammaps string, tx *xorm.Session) string {
  2515. fsion := gofasion.NewFasion(parammaps)
  2516. sqlnamestr := fsion.Get("name").ValueStr()
  2517. sqlnamestr1 := fsion.Get("name1").ValueStr()
  2518. offset := fsion.Get("offset").ValueDefaultInt(0)
  2519. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2520. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  2521. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  2522. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  2523. body := []byte("")
  2524. if sql != "" {
  2525. s_params := make([]interface{}, 0)
  2526. if fsion.HasKey("parammaps") {
  2527. parammaps := fsion.Get("parammaps")
  2528. paramslist := strings.Split(p, ",")
  2529. if len(paramslist) > 0 && p != "" {
  2530. for _, value := range paramslist {
  2531. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2532. }
  2533. }
  2534. }
  2535. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  2536. if err != nil {
  2537. } else {
  2538. paramslist1 := strings.Split(p1, ",")
  2539. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  2540. s_params1 := make([]interface{}, 0)
  2541. if len(paramslist1) > 0 && p1 != "" {
  2542. for _, v := range paramslist1 {
  2543. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  2544. }
  2545. }
  2546. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  2547. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  2548. body, _ = json.Marshal(queryData)
  2549. }
  2550. }
  2551. }
  2552. bs := bytes.NewBuffer(body)
  2553. client := &http.Client{}
  2554. //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}"))
  2555. req, err := http.NewRequest(RequestMethod,
  2556. url,
  2557. bs,
  2558. )
  2559. if err != nil {
  2560. }
  2561. req.Header.Set("Content-Type", "application/json;charset=utf-8")
  2562. req.Header.Set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY")
  2563. if err != nil {
  2564. // handle error
  2565. }
  2566. resp, err := client.Do(req)
  2567. if err != nil {
  2568. fmt.Println(err)
  2569. return ""
  2570. }
  2571. defer resp.Body.Close()
  2572. result_body, err := ioutil.ReadAll(resp.Body)
  2573. if err != nil {
  2574. fmt.Println(err)
  2575. return ""
  2576. }
  2577. fmt.Println(string(result_body))
  2578. return ""
  2579. }