db.go 85 KB

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