df5d05a01c09deefe56433260c359193b733f018.svn-base 84 KB

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