1eeb2309bb369f78200ad096da5c99f25d608482.svn-base 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378
  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/Knetic/govaluate"
  16. "github.com/gin-gonic/gin"
  17. "github.com/tealeg/xlsx"
  18. "github.com/xormplus/xorm"
  19. "io/ioutil"
  20. "math"
  21. "net/http"
  22. "os"
  23. "path"
  24. "regexp"
  25. "sort"
  26. "strconv"
  27. "strings"
  28. "time"
  29. )
  30. // @Summary 根据SQL执行得到数据集
  31. // @Tags getData
  32. // @Accept json
  33. // @Produce json
  34. // @Param sql query string true "sql"
  35. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  36. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  37. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  38. // @Router /data/getdata [POST]
  39. func GetData(c *gin.Context) {
  40. appG := app.Gin{C: c}
  41. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  42. fsion := gofasion.NewFasion(string(dataByte))
  43. sqlstr := fsion.Get("sql").ValueStr()
  44. offset := fsion.Get("offset").ValueDefaultInt(0)
  45. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  46. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  47. queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
  48. if err != nil {
  49. appG.Response(http.StatusOK, e.ERROR, err.Error())
  50. } else {
  51. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  52. }
  53. }
  54. func Testpartitions(c *gin.Context) {
  55. appG := app.Gin{C: c}
  56. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  57. fsion := gofasion.NewFasion(string(dataByte))
  58. databaseName := fsion.Get("databaseName").ValueStr() //获取新数据库名称
  59. user := fsion.Get("user").ValueStr() //获取新数据库用户
  60. passWord := fsion.Get("passWord").ValueStr() //获取新数据库名称
  61. host := fsion.Get("host").ValueStr() //获取新数据库名称
  62. port := fsion.Get("port").ValueStr() //获取新数据库名称
  63. sqls, err1 := restful.Engine.SQL("SELECT * FROM sql_new ").QueryString() //获取新数据库表
  64. if sqls == nil || err1 != nil {
  65. appG.Response(http.StatusOK, e.SUCCESS, "失败:"+err1.Error())
  66. }
  67. sqlstrs := strings.Split(sqls[0]["sql"], ";") //分割sql
  68. connecting := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, "mysql")
  69. mydb, err := sql.Open("mysql", connecting) //连接新连接
  70. if err != nil {
  71. println("err1", err.Error())
  72. }
  73. _, err = mydb.Exec("CREATE DATABASE IF NOT EXISTS `" + databaseName + "`") // 创建数据库
  74. if err != nil {
  75. println("err2", err.Error())
  76. }
  77. mydb.Close()
  78. connecting = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, databaseName) //连接新数据库,创建表格
  79. mydb, err = sql.Open("mysql", connecting)
  80. if err != nil {
  81. println("err3", err.Error())
  82. }
  83. defer mydb.Close()
  84. if err == nil {
  85. mydb.SetMaxOpenConns(10)
  86. mydb.SetMaxIdleConns(10)
  87. }
  88. errmsg := ""
  89. if err == nil {
  90. //tx ,_ :=mydb.Begin()
  91. for _, sqlstr := range sqlstrs {
  92. //println("sqlstr---",sqlstr)
  93. if sqlstr == "" {
  94. continue
  95. }
  96. _, err = mydb.Exec(sqlstr)
  97. if err != nil {
  98. println("err4", err.Error())
  99. errmsg = err.Error()
  100. //err1 :=tx.Rollback()
  101. }
  102. }
  103. //_, err = mydb.Exec("CREATE TABLE `sql_new` (`sql` text DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;")
  104. //err =tx.Commit()
  105. }
  106. if errmsg == "" {
  107. appG.Response(http.StatusOK, e.SUCCESS, "成功"+databaseName)
  108. } else {
  109. appG.Response(http.StatusOK, e.SUCCESS, "失败"+errmsg)
  110. }
  111. }
  112. func TestData(c *gin.Context) {
  113. appG := app.Gin{C: c}
  114. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  115. fsion := gofasion.NewFasion(string(dataByte))
  116. sqlstr := fsion.Get("sql").ValueStr()
  117. token := ""
  118. if tempv, exists := c.Get("jwt_username"); exists {
  119. token = tempv.(string)
  120. }
  121. appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
  122. }
  123. // @Summary 根据APISQL表中SQL执行得到数据集
  124. // @Tags GetDataByName
  125. // @Accept json
  126. // @Produce json
  127. // @Param sqlname body string true "sqlname"
  128. // @Param offset body int true "offset"
  129. // @Param pagecount body int true "pagecount"
  130. // @Param returntype body string true "returntype"
  131. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  132. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  133. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  134. // @Router /data/GetDataByName [POST]
  135. func GetDataByName(c *gin.Context) {
  136. appG := app.Gin{C: c}
  137. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  138. fsion := gofasion.NewFasion(string(dataByte))
  139. sqlnamestr := fsion.Get("name").ValueStr()
  140. offset := fsion.Get("offset").ValueDefaultInt(0)
  141. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  142. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  143. tx := restful.Engine.NewSession()
  144. err := tx.Begin()
  145. defer func() {
  146. switch {
  147. case err != nil:
  148. logging.Error("tx.Begin 事务启动失败__error:", err)
  149. if tx != nil {
  150. tx.Rollback()
  151. }
  152. default:
  153. if tx != nil {
  154. err = tx.Commit()
  155. }
  156. }
  157. if tx != nil {
  158. tx.Close()
  159. }
  160. }()
  161. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  162. //fmt.Println("sql========\n",sqlnamestr,sql)
  163. if sql != "" {
  164. s_params := make([]interface{}, 0)
  165. if fsion.HasKey("parammaps") {
  166. parammaps := fsion.Get("parammaps")
  167. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  168. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  169. }
  170. paramslist := strings.Split(p, ",")
  171. if len(paramslist) > 0 && p != "" {
  172. for _, value := range paramslist {
  173. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  174. if tempv, exists := c.Get("jwt_username"); exists {
  175. s_params = append(s_params, tempv.(string))
  176. } else {
  177. s_params = append(s_params, "")
  178. }
  179. } else {
  180. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  181. }
  182. }
  183. }
  184. } else if fsion.HasKey("params") {
  185. params := fsion.Get("params").Array()
  186. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  187. for _, v_params := range params {
  188. s_params = append(s_params, v_params.ValueStr())
  189. }
  190. }
  191. if returntype == "xlsx" {
  192. var xlsxfile *xlsx.File
  193. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  194. //c.File("c:/dd.xlsx")
  195. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  196. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  197. c.Header("Content-Transfer-Encoding", "binary")
  198. _ = xlsxfile.Write(c.Writer)
  199. } else {
  200. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  201. //fmt.Printf("%v+\n",queryData)
  202. if err != nil {
  203. logging.Error("GetDataByName err: ", err)
  204. msg := geterrmsg(err.Error())
  205. appG.Response(http.StatusOK, e.ERROR, msg)
  206. } else {
  207. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  208. }
  209. }
  210. } else {
  211. appG.Response(http.StatusOK, e.SUCCESS, nil)
  212. }
  213. }
  214. func GetDataBySqlIN(c *gin.Context) {
  215. appG := app.Gin{C: c}
  216. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  217. fsion := gofasion.NewFasion(string(dataByte))
  218. sqlnamestr := fsion.Get("name").ValueStr()
  219. offset := fsion.Get("offset").ValueDefaultInt(0)
  220. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  221. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  222. tx := restful.Engine.NewSession()
  223. err := tx.Begin()
  224. defer func() {
  225. switch {
  226. case err != nil:
  227. logging.Error("tx.Begin 事务启动失败__error:", err)
  228. if tx != nil {
  229. tx.Rollback()
  230. }
  231. default:
  232. if tx != nil {
  233. err = tx.Commit()
  234. }
  235. }
  236. if tx != nil {
  237. tx.Close()
  238. }
  239. }()
  240. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  241. //fmt.Println("sql========\n",sqlnamestr,sql)
  242. if sql != "" {
  243. s_params := make([]interface{}, 0)
  244. if fsion.HasKey("parammaps") {
  245. parammaps := fsion.Get("parammaps")
  246. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  247. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  248. }
  249. paramslist := strings.Split(p, ",")
  250. if len(paramslist) > 0 && p != "" {
  251. for _, value := range paramslist {
  252. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  253. if tempv, exists := c.Get("jwt_username"); exists {
  254. s_params = append(s_params, tempv.(string))
  255. } else {
  256. s_params = append(s_params, "")
  257. }
  258. } else {
  259. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  260. }
  261. }
  262. }
  263. } else if fsion.HasKey("params") {
  264. params := fsion.Get("params").Array()
  265. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  266. for _, v_params := range params {
  267. s_params = append(s_params, v_params.ValueStr())
  268. }
  269. }
  270. if returntype == "xlsx" {
  271. var xlsxfile *xlsx.File
  272. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  273. //c.File("c:/dd.xlsx")
  274. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  275. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  276. c.Header("Content-Transfer-Encoding", "binary")
  277. _ = xlsxfile.Write(c.Writer)
  278. } else {
  279. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  280. //fmt.Printf("%v+\n",queryData)
  281. if err != nil {
  282. logging.Error("GetDataByName err: ", err)
  283. msg := geterrmsg(err.Error())
  284. appG.Response(http.StatusOK, e.ERROR, msg)
  285. } else {
  286. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  287. }
  288. }
  289. } else {
  290. appG.Response(http.StatusOK, e.SUCCESS, nil)
  291. }
  292. }
  293. func geterrmsg(errstr string) (msgstr string) {
  294. var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$") // 判断是否为汉字
  295. rs := []rune(errstr)
  296. if hzRegexp.MatchString(string(rs[0])) {
  297. msgstr = errstr
  298. return
  299. }
  300. if strings.Contains(errstr, "Duplicate entry") {
  301. strs := strings.Split(errstr, ` key '`)
  302. if len(strs) > 1 {
  303. if len(strs[len(strs)-1]) > 1 {
  304. errstr = "Duplicate entry :" + strs[len(strs)-1]
  305. }
  306. }
  307. }
  308. msg, err := restful.Engine.SQL("SELECT msg FROM err_msg where err = ? ", errstr).QueryString()
  309. if err != nil {
  310. msgstr = errstr
  311. return
  312. }
  313. if msg == nil || len(msg) == 0 {
  314. var param []interface{}
  315. param = append(param, errstr, errstr)
  316. go func() {
  317. tx := restful.Engine.NewSession()
  318. err := tx.Begin()
  319. defer func() {
  320. switch {
  321. case err != nil:
  322. logging.Error("tx.Begin 事务启动失败__error:", err)
  323. if tx != nil {
  324. tx.Rollback()
  325. }
  326. default:
  327. if tx != nil {
  328. err = tx.Commit()
  329. }
  330. }
  331. if tx != nil {
  332. tx.Close()
  333. }
  334. }()
  335. _, err = restful.ExecQueryT("insert into err_msg(err,msg) VALUES(?,?) ON DUPLICATE KEY UPDATE err=err", param, tx)
  336. if err != nil {
  337. logging.Error("insert into err_msg(err,msg) err", err)
  338. }
  339. }()
  340. msgstr = errstr
  341. } else {
  342. msgstr = msg[0]["msg"]
  343. }
  344. return
  345. }
  346. // @Summary 根据APISQL表中SQL执行得到数据集
  347. // @Tags GetDataByNameForm
  348. // @Accept json
  349. // @Produce json
  350. // @Param sqlname body json true "sqlname"
  351. // @Param offset query int true "offset"
  352. // @Param pagecount query int true "pagecount"
  353. // @Param returntype query string true "returntype"
  354. // @Param s formData json true "ss"
  355. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  356. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  357. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  358. // @Router /data/GetDataByNameForm [POST]
  359. func GetDataByNameForm(c *gin.Context) {
  360. appG := app.Gin{C: c}
  361. //sqlnamestr := c.PostForm("measureNumber")
  362. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  363. //fsion := gofasion.NewFasion(string(dataByte))
  364. //sqlnamestr := fsion.Get("name").ValueStr()
  365. //offset := fsion.Get("offset").ValueInt()
  366. //pagecount := fsion.Get("pagecount").ValueInt()
  367. //returntype := fsion.Get("returntype").ValueStr()
  368. //params := fsion.Get("params").Array()
  369. c.Request.ParseForm()
  370. params := c.Request.Form
  371. offset := 0
  372. pageSize_ := 0
  373. returntype := params.Get("page")
  374. if returntype == "" {
  375. returntype = "0"
  376. }
  377. if tempv, exists := c.Get("jwt_username"); exists {
  378. params.Set("JwtUserName", tempv.(string))
  379. } else {
  380. params.Set("JwtUserName", "")
  381. }
  382. sqlnamestr := params.Get("name")
  383. logging.Info("GetDataByNameForm ", c.Keys, sqlnamestr)
  384. s_params := make([]interface{}, 0)
  385. if returntype == "1" {
  386. pageNum, error := strconv.Atoi(params.Get("pageNum"))
  387. if error != nil {
  388. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
  389. }
  390. pageSize, error := strconv.Atoi(params.Get("pageSize"))
  391. if error != nil {
  392. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
  393. }
  394. pageSize_ = pageSize
  395. offset = pageNum
  396. }
  397. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  398. paramslist := strings.Split(p, ",")
  399. if len(paramslist) > 0 && p != "" {
  400. for _, value := range paramslist {
  401. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  402. }
  403. }
  404. if sql != "" {
  405. if returntype == "xlsx" {
  406. var xlsxfile *xlsx.File
  407. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  408. c.Header("Content-Type", "application/octet-stream")
  409. c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
  410. c.Header("Content-Transfer-Encoding", "binary")
  411. _ = xlsxfile.Write(c.Writer)
  412. } else {
  413. queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
  414. if err != nil {
  415. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  416. } else {
  417. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  418. }
  419. }
  420. } else {
  421. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  422. }
  423. }
  424. // @Summary 递归获取所有
  425. // @Tags menu
  426. // @Accept json
  427. // @Produce json
  428. // @Param Ton query string true "Ton"
  429. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  430. // @Router /authdata/GetRoleMenus [GET]
  431. func GetRecuDataByName(c *gin.Context) {
  432. appG := app.Gin{C: c}
  433. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  434. fsion := gofasion.NewFasion(string(dataByte))
  435. sqlnamestr := fsion.Get("name").ValueStr()
  436. parentidname := fsion.Get("idname").ValueStr()
  437. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  438. params := make(map[string]interface{})
  439. s_params := make([]interface{}, 0)
  440. if fsion.HasKey("params") {
  441. for _, v_params := range fsion.Get("params").Array() {
  442. s_params = append(s_params, v_params.ValueStr())
  443. }
  444. params["params"] = s_params
  445. } else {
  446. jparams := fsion.Get("parammaps")
  447. paramslist := strings.Split(p, ",")
  448. if len(paramslist) > 0 && p != "" {
  449. for _, value := range paramslist {
  450. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  451. if tempv, exists := c.Get("jwt_username"); exists {
  452. params[value] = tempv.(string)
  453. } else {
  454. params[value] = ""
  455. }
  456. } else {
  457. params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
  458. }
  459. }
  460. }
  461. }
  462. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
  463. logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  464. if err != nil {
  465. appG.Response(http.StatusOK, e.ERROR, err.Error())
  466. }
  467. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  468. }
  469. // @Summary 根据APISQL表中SQL执行得到数据集
  470. // @Tags GetDataByNames
  471. // @Accept json
  472. // @Produce json
  473. // @RequestBody sqlname query string true "sqlname"
  474. // @Param offset query int true "offset"
  475. // @Param pagecount query int true "pagecount"
  476. // @Param returntype query string true "returntype"
  477. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  478. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  479. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  480. // @Router /data/GetDataByNames [POST]
  481. func GetDataByNames(c *gin.Context) {
  482. appG := app.Gin{C: c}
  483. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  484. fsion := gofasion.NewFasion(string(dataByte))
  485. sqlParams := fsion.Array()
  486. queryData := make(map[string]interface{})
  487. for _, v := range sqlParams {
  488. fsion2 := gofasion.NewFasion(v.Json())
  489. logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
  490. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  491. if sql == "" {
  492. errorm := make(map[string]interface{})
  493. errorm["code"] = e.ERROR
  494. errorm["message"] = "sql没找到"
  495. queryData[fsion2.Get("name").ValueStr()] = errorm
  496. continue
  497. } else if sql != "" {
  498. offset := fsion2.Get("offset").ValueInt()
  499. pagecount := fsion2.Get("pagecount").ValueInt()
  500. returntype := fsion2.Get("returntype").ValueStr()
  501. s_params := make([]interface{}, 0)
  502. if fsion2.HasKey("parammaps") {
  503. parammaps := fsion2.Get("parammaps")
  504. paramslist := strings.Split(p, ",")
  505. if len(paramslist) > 0 && p != "" {
  506. for _, value := range paramslist {
  507. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  508. if tempv, exists := c.Get("jwt_username"); exists {
  509. s_params = append(s_params, tempv.(string))
  510. } else {
  511. s_params = append(s_params, "")
  512. }
  513. } else {
  514. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  515. }
  516. }
  517. }
  518. } else if fsion2.HasKey("params") {
  519. params := fsion2.Get("params").Array()
  520. for _, v_params := range params {
  521. s_params = append(s_params, v_params.ValueStr())
  522. }
  523. }
  524. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  525. if err != nil {
  526. errorm := make(map[string]interface{})
  527. errorm["code"] = e.ERROR
  528. errorm["message"] = err.Error()
  529. queryData[fsion2.Get("name").ValueStr()] = errorm
  530. logging.Error("GetDataByNames err: ", err)
  531. continue
  532. } else {
  533. queryData[fsion2.Get("name").ValueStr()] = queryData1
  534. }
  535. }
  536. }
  537. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  538. }
  539. // @Summary 根据APISQL表中SQL执行得到数据集
  540. // @Tags PostDataByNameForm
  541. // @Accept json
  542. // @Produce json
  543. // @Param sqlname query string true "sqlname"
  544. // @Param offset query int true "offset"
  545. // @Param pagecount query int true "pagecount"
  546. // @Param returntype query string true "returntype"
  547. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  548. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
  549. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  550. // @Router /data/PostDataByNameForm [POST]
  551. func PostDataByNameForm(c *gin.Context) {
  552. appG := app.Gin{C: c}
  553. c.Request.ParseForm()
  554. params := c.Request.Form
  555. sqlnamestr := params.Get("name")
  556. logging.Info("PostDataByNameForm ", c.Keys, sqlnamestr)
  557. s_params := make([]interface{}, 0)
  558. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  559. paramslist := strings.Split(p, ",")
  560. if len(paramslist) > 0 {
  561. for _, value := range paramslist {
  562. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  563. }
  564. }
  565. if sql != "" {
  566. queryData, err := execDataBySql(sql, s_params)
  567. if err != nil {
  568. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  569. } else {
  570. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  571. }
  572. } else {
  573. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  574. }
  575. }
  576. func GetReportform(c *gin.Context) {
  577. appG := app.Gin{C: c}
  578. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  579. fsion := gofasion.NewFasion(string(dataByte))
  580. sqlnamestr := fsion.Get("name").ValueStr()
  581. parammaps := fsion.Get("parammaps")
  582. logging.Info("GetReportform ", c.Keys, sqlnamestr, parammaps)
  583. tx := restful.Engine.NewSession()
  584. err := tx.Begin()
  585. defer func() {
  586. switch {
  587. case err != nil:
  588. fmt.Println("__error:", err)
  589. if tx != nil {
  590. tx.Rollback()
  591. }
  592. default:
  593. if tx != nil {
  594. err = tx.Commit()
  595. }
  596. }
  597. if tx != nil {
  598. tx.Close()
  599. }
  600. }()
  601. queryData, _ := praseReportform(sqlnamestr, parammaps, tx)
  602. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  603. }
  604. func praseReportform(sqlnamestr string, parammaps *gofasion.Fasion, tx *xorm.Session) (queryData map[string]interface{}, err error) {
  605. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  606. s_params := make([]interface{}, 0)
  607. if sql != "" {
  608. paramslist := strings.Split(p, ",")
  609. if len(paramslist) > 0 && p != "" {
  610. for _, value := range paramslist {
  611. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  612. }
  613. }
  614. }
  615. queryList, _ := tx.SQL(sql, s_params...).Query().List()
  616. temstr := ""
  617. switch sqlnamestr {
  618. case "getAccuracyAllHL", "getAccuracyAllQX", "getAccuracyAllJH", "getFeedEffMR", "getFeedEffHL", "getHomepageCB", "getAccuraa", "judgenurFTReportDetail":
  619. temstr = sqlnamestr
  620. default:
  621. temstr = sqlnamestr[:10]
  622. }
  623. switch temstr {
  624. case "getAccurac", "getFeedEff":
  625. queryData, err = getAccuracy(queryList) // 不定参数
  626. case "getprocess":
  627. //queryData, err = getAccuracyV3v1(queryList)
  628. queryData, err = getAccuracyV3(queryList)
  629. case "getHomepro":
  630. queryData, err = getAccuracyV3(queryList)
  631. case "getAccuracyAllHL", "getAccuracyAllQX", "getAccuracyAllJH", "getFeedEffMR", "getFeedEffHL", "getHomepageCB", "getAccuraa":
  632. queryData, err = getAccuracyV2(queryList) //固定维度 日期
  633. case "getMateria":
  634. queryData, err = getAccuracyV4(queryList)
  635. case "getPriceAn":
  636. queryData, err = getAccuracyV5(queryList)
  637. case "getJT1Accu":
  638. queryData, err = getAccuracyV6(queryList) //固定维度 牧场
  639. case "judgenurFT":
  640. queryData, err = getAccuracyV7(queryList)
  641. case "judgenurFTReportDetail":
  642. queryData, err = getAccuracyV8(queryList)
  643. default:
  644. queryData, err = nil, nil
  645. }
  646. return
  647. //switch sqlnamestr {
  648. //case "getAccuracyAllFT", "getAccuracyAllNQ", "getAccuracyAllCC", "getAccuracyAllLS","getFeedEffSL","getFeedEffZH","getFeedEffCB","getFeedEffCBLS","getFeedEffCBFT":
  649. // queryData, err = getAccuracy(queryList)
  650. //case "getprocessAnalysisTB","getHomeprocessAnalysis":
  651. // queryData, err = getAccuracyV3(queryList)
  652. //case "getAccuracyAllHL", "getAccuracyAllQX","getAccuracyAllJH","getFeedEffMR","getFeedEffHL","getHomepageCB":
  653. // queryData, err = getAccuracyV2(queryList)
  654. //default:
  655. // queryData, err = nil, nil
  656. //}
  657. //return
  658. }
  659. func getAccuracyV8(queryList []map[string]interface{}) (map[string]interface{}, error) {
  660. queryData := make(map[string]interface{})
  661. chartData := make(map[string]interface{})
  662. //list := make(map[string]interface{})
  663. data := []interface{}{}
  664. for i := 0; i < len(queryList); i++ {
  665. dry, _ := strconv.ParseFloat(queryList[i]["dry"].(string), 64)
  666. Hrate, _ := strconv.ParseFloat(queryList[i]["Hrate"].(string), 64)
  667. Srate, _ := strconv.ParseFloat(queryList[i]["Srate"].(string), 64)
  668. trans35f, _ := strconv.ParseFloat(queryList[i]["trans35f"].(string), 64)
  669. jcrate, _ := strconv.ParseFloat(queryList[i]["jcrate"].(string), 64)
  670. uprice, _ := strconv.ParseFloat(queryList[i]["uprice"].(string), 64)
  671. kprice, _ := strconv.ParseFloat(queryList[i]["dry"].(string), 64)
  672. adf, _ := strconv.ParseFloat(queryList[i]["adf"].(string), 64)
  673. ca, _ := strconv.ParseFloat(queryList[i]["ca"].(string), 64)
  674. cdry, _ := strconv.ParseFloat(queryList[i]["cdry"].(string), 64)
  675. cad, _ := strconv.ParseFloat(queryList[i]["cad"].(string), 64)
  676. cndf, _ := strconv.ParseFloat(queryList[i]["cndf"].(string), 64)
  677. cp, _ := strconv.ParseFloat(queryList[i]["cp"].(string), 64)
  678. cpd, _ := strconv.ParseFloat(queryList[i]["cpd"].(string), 64)
  679. fat, _ := strconv.ParseFloat(queryList[i]["fat"].(string), 64)
  680. ndf, _ := strconv.ParseFloat(queryList[i]["ndf"].(string), 64)
  681. nm, _ := strconv.ParseFloat(queryList[i]["nm"].(string), 64)
  682. nmd, _ := strconv.ParseFloat(queryList[i]["nmd"].(string), 64)
  683. nuint, _ := strconv.ParseFloat(queryList[i]["nuint"].(string), 64)
  684. p, _ := strconv.ParseFloat(queryList[i]["p"].(string), 64)
  685. pd, _ := strconv.ParseFloat(queryList[i]["pd"].(string), 64)
  686. starch, _ := strconv.ParseFloat(queryList[i]["starch"].(string), 64)
  687. trans4f, _ := strconv.ParseFloat(queryList[i]["trans4f"].(string), 64)
  688. list := make(map[string]interface{})
  689. list["ft"] = dry
  690. list["index"] = "干物质(kg)"
  691. list["tmr"] = TransFloat(dry * Hrate)
  692. list["cyl"] = TransFloat(dry * Srate)
  693. data = append(data, list)
  694. list = make(map[string]interface{})
  695. list["ft"] = jcrate
  696. list["index"] = "粗精比(%)"
  697. list["tmr"] = TransFloat(jcrate * Hrate)
  698. list["cyl"] = TransFloat(jcrate * Srate)
  699. data = append(data, list)
  700. list = make(map[string]interface{})
  701. list["ft"] = uprice
  702. list["index"] = "日粮成本(元)"
  703. list["tmr"] = TransFloat(uprice * Hrate)
  704. list["cyl"] = TransFloat(uprice * Srate)
  705. data = append(data, list)
  706. list = make(map[string]interface{})
  707. list["ft"] = kprice
  708. list["index"] = "公斤奶成本"
  709. list["tmr"] = TransFloat(kprice * Hrate)
  710. list["cyl"] = TransFloat(kprice * Srate)
  711. data = append(data, list)
  712. list = make(map[string]interface{})
  713. list["ft"] = cdry
  714. list["index"] = "粗料中的干物质"
  715. list["tmr"] = TransFloat(cdry * Hrate)
  716. list["cyl"] = TransFloat(cdry * Srate)
  717. data = append(data, list)
  718. list = make(map[string]interface{})
  719. list["ft"] = nm
  720. list["index"] = " 产奶净能(MJ)"
  721. list["tmr"] = TransFloat(nm * Hrate)
  722. list["cyl"] = TransFloat(nm * Srate)
  723. data = append(data, list)
  724. list = make(map[string]interface{})
  725. list["ft"] = nuint
  726. list["index"] = "奶牛能量单位(NND)"
  727. list["tmr"] = TransFloat(nuint * Hrate)
  728. list["cyl"] = TransFloat(nuint * Srate)
  729. data = append(data, list)
  730. list = make(map[string]interface{})
  731. list["ft"] = cp
  732. list["index"] = "粗蛋白(g)"
  733. list["tmr"] = TransFloat(cp * Hrate)
  734. list["cyl"] = TransFloat(cp * Srate)
  735. data = append(data, list)
  736. list = make(map[string]interface{})
  737. list["ft"] = cpd
  738. list["index"] = "粗蛋白(%DM)"
  739. list["tmr"] = TransFloat(cpd * Hrate)
  740. list["cyl"] = TransFloat(cpd * Srate)
  741. data = append(data, list)
  742. list = make(map[string]interface{})
  743. list["ft"] = ca
  744. list["index"] = "钙(g)"
  745. list["tmr"] = TransFloat(ca * Hrate)
  746. list["cyl"] = TransFloat(ca * Srate)
  747. data = append(data, list)
  748. list = make(map[string]interface{})
  749. list["ft"] = p
  750. list["index"] = "磷(g)"
  751. list["tmr"] = TransFloat(p * Hrate)
  752. list["cyl"] = TransFloat(p * Srate)
  753. data = append(data, list)
  754. list = make(map[string]interface{})
  755. list["ft"] = nmd
  756. list["index"] = "产奶净能(Mcal/DM)"
  757. list["tmr"] = TransFloat(nmd * Hrate)
  758. list["cyl"] = TransFloat(nmd * Srate)
  759. data = append(data, list)
  760. list = make(map[string]interface{})
  761. list["ft"] = fat
  762. list["index"] = "脂肪"
  763. list["tmr"] = TransFloat(fat * Hrate)
  764. list["cyl"] = TransFloat(fat * Srate)
  765. data = append(data, list)
  766. list = make(map[string]interface{})
  767. list["ft"] = starch
  768. list["index"] = "淀粉"
  769. list["tmr"] = TransFloat(starch * Hrate)
  770. list["cyl"] = TransFloat(starch * Srate)
  771. data = append(data, list)
  772. list = make(map[string]interface{})
  773. list["ft"] = ndf
  774. list["index"] = "NDF(%DM)"
  775. list["tmr"] = TransFloat(ndf * Hrate)
  776. list["cyl"] = TransFloat(ndf * Srate)
  777. data = append(data, list)
  778. list = make(map[string]interface{})
  779. list["ft"] = cndf
  780. list["index"] = "粗料中的NDF(%DM)"
  781. list["tmr"] = TransFloat(cndf * Hrate)
  782. list["cyl"] = TransFloat(cndf * Srate)
  783. data = append(data, list)
  784. list = make(map[string]interface{})
  785. list["ft"] = adf
  786. list["index"] = " ADF(%DM)"
  787. list["tmr"] = TransFloat(adf * Hrate)
  788. list["cyl"] = TransFloat(adf * Srate)
  789. data = append(data, list)
  790. list = make(map[string]interface{})
  791. list["ft"] = cad
  792. list["index"] = "钙(%DM)"
  793. list["tmr"] = TransFloat(cad * Hrate)
  794. list["cyl"] = TransFloat(cad * Srate)
  795. data = append(data, list)
  796. list = make(map[string]interface{})
  797. list["ft"] = pd
  798. list["index"] = " 磷(%DM)"
  799. list["tmr"] = TransFloat(pd * Hrate)
  800. list["cyl"] = TransFloat(pd * Srate)
  801. data = append(data, list)
  802. list = make(map[string]interface{})
  803. list["ft"] = trans35f
  804. list["index"] = "(3.5)FCR(kg/DM)"
  805. list["tmr"] = TransFloat(trans35f * Hrate)
  806. list["cyl"] = TransFloat(trans35f * Srate)
  807. data = append(data, list)
  808. list = make(map[string]interface{})
  809. list["ft"] = trans4f
  810. list["index"] = "(4.0)FCR(kg/DM)"
  811. list["tmr"] = TransFloat(trans4f * Hrate)
  812. list["cyl"] = TransFloat(trans4f * Srate)
  813. data = append(data, list)
  814. }
  815. chartData["data"] = data
  816. queryData["list"] = chartData
  817. queryData["code"] = 0
  818. queryData["message"] = "ok"
  819. queryData["data"] = queryList
  820. return queryData, nil
  821. }
  822. func TransFloat(flo float64)(float64){
  823. hf,_ :=strconv.ParseFloat(fmt.Sprintf("%.3f", float64(flo)), 64)
  824. return hf
  825. }
  826. func getAccuracyV7(queryList []map[string]interface{}) (map[string]interface{}, error) {
  827. queryData := make(map[string]interface{})
  828. chartData := make(map[string]interface{})
  829. //list := make(map[string]interface{})
  830. data := []interface{}{}
  831. for i := 0; i < len(queryList); i++ {
  832. dry, _ := strconv.ParseFloat(queryList[i]["dry"].(string), 64)
  833. Hrate, _ := strconv.ParseFloat(queryList[i]["Hrate"].(string), 64)
  834. Srate, _ := strconv.ParseFloat(queryList[i]["Srate"].(string), 64)
  835. trans35f, _ := strconv.ParseFloat(queryList[i]["trans35f"].(string), 64)
  836. jcrate, _ := strconv.ParseFloat(queryList[i]["jcrate"].(string), 64)
  837. uprice, _ := strconv.ParseFloat(queryList[i]["uprice"].(string), 64)
  838. kprice, _ := strconv.ParseFloat(queryList[i]["dry"].(string), 64)
  839. list := make(map[string]interface{})
  840. list["tname"] = queryList[i]["tname"]
  841. list["ftid"] = queryList[i]["ftid"]
  842. list["ft"] = dry
  843. list["index"] = "干物质(kg)"
  844. list["tmr"] = TransFloat(dry * Hrate)
  845. list["cyl"] = TransFloat(dry * Srate)
  846. data = append(data, list)
  847. list = make(map[string]interface{})
  848. list["tname"] = queryList[i]["tname"]
  849. list["ftid"] = queryList[i]["ftid"]
  850. list["ft"] = trans35f
  851. list["index"] = "(3.5)FCR(kg/DM)"
  852. list["tmr"] = TransFloat(trans35f * Hrate)
  853. list["cyl"] = TransFloat(trans35f * Srate)
  854. data = append(data, list)
  855. list = make(map[string]interface{})
  856. list["tname"] = queryList[i]["tname"]
  857. list["ftid"] = queryList[i]["ftid"]
  858. list["ft"] = jcrate
  859. list["index"] = "粗精比(%)"
  860. list["tmr"] = TransFloat(jcrate * Hrate)
  861. list["cyl"] = TransFloat(jcrate * Srate)
  862. data = append(data, list)
  863. list = make(map[string]interface{})
  864. list["tname"] = queryList[i]["tname"]
  865. list["ftid"] = queryList[i]["ftid"]
  866. list["ft"] = uprice
  867. list["index"] = "日粮成本(元)"
  868. list["tmr"] = TransFloat(uprice * Hrate)
  869. list["cyl"] = TransFloat(uprice * Srate)
  870. data = append(data, list)
  871. list = make(map[string]interface{})
  872. list["tname"] = queryList[i]["tname"]
  873. list["ftid"] = queryList[i]["ftid"]
  874. list["ft"] = kprice
  875. list["index"] = "公斤奶成本"
  876. list["tmr"] = TransFloat(kprice * Hrate)
  877. list["cyl"] = TransFloat(kprice * Srate)
  878. data = append(data, list)
  879. }
  880. chartData["data"] = data
  881. queryData["list"] = chartData
  882. queryData["code"] = 0
  883. queryData["message"] = "ok"
  884. queryData["data"] = queryList
  885. return queryData, nil
  886. }
  887. func getAccuracyV6(queryList []map[string]interface{}) (map[string]interface{}, error) {
  888. queryData := make(map[string]interface{})
  889. chartData := make(map[string]interface{})
  890. data := []interface{}{}
  891. data1 := []interface{}{}
  892. data2 := []interface{}{}
  893. data3 := []interface{}{}
  894. data4 := []interface{}{}
  895. for i := 0; i < len(queryList); i++ {
  896. data = append(data, queryList[i]["牧场"])
  897. data1 = append(data1, queryList[i]["field1"])
  898. data2 = append(data2, queryList[i]["field2"])
  899. data3 = append(data3, queryList[i]["field3"])
  900. data4 = append(data4, queryList[i]["field4"])
  901. }
  902. chartData["data1"] = data
  903. chartData["data2"] = data1
  904. chartData["data3"] = data2
  905. chartData["data4"] = data3
  906. chartData["data5"] = data4
  907. queryData["list"] = chartData
  908. queryData["code"] = 0
  909. queryData["message"] = "ok"
  910. queryData["data"] = queryList
  911. return queryData, nil
  912. }
  913. func getAccuracyV5(queryList []map[string]interface{}) (map[string]interface{}, error) {
  914. queryData := make(map[string]interface{})
  915. chartData := make(map[string]interface{})
  916. datamap1 := make(map[string]interface{})
  917. datamap2 := make(map[string]interface{})
  918. temsmap := make(map[interface{}]interface{})
  919. mapc2 := make(map[interface{}]interface{})
  920. data3 := []interface{}{}
  921. datamap2child := make(map[string]interface{})
  922. data2 := []interface{}{}
  923. data1 := []interface{}{}
  924. var dtem interface{}
  925. var temstr interface{}
  926. var typestr interface{}
  927. count2 := 0
  928. a := 0
  929. for i := 0; i < len(queryList); i++ {
  930. if i == 0 {
  931. temstr = queryList[i]["fname"]
  932. typestr = queryList[i]["typestr"]
  933. }
  934. if temstr == queryList[i]["fname"] {
  935. temsmap[queryList[i]["feedname"]] = 1
  936. }
  937. if _, ok := mapc2[queryList[i]["feedname"]]; !ok {
  938. mapc2[queryList[i]["feedname"]] = count2
  939. mapc2[count2] = queryList[i]["feedname"]
  940. count2++
  941. }
  942. }
  943. //for i := 0; i<count2 ; i++ {
  944. // reslist := make([]map[string]interface{},1)
  945. // res := make(map[string]interface{})
  946. // res["fname"] = "分界线"
  947. // res["feedname"] = mapc2[i]
  948. // res["饲料名称"] = mapc2[i]
  949. // reslist[0] = res
  950. // queryList = append(reslist,queryList...)
  951. //}
  952. for i := 0; i < count2; i++ { // 将所有饲料种类新增为第一行 测试
  953. if _, ok := temsmap[mapc2[i]]; !ok {
  954. reslist := make([]map[string]interface{}, 1)
  955. res := make(map[string]interface{})
  956. res["fname"] = temstr
  957. res["feedname"] = mapc2[i]
  958. res["饲料名称"] = mapc2[i]
  959. res["typestr"] = typestr
  960. reslist[0] = res
  961. queryList = append(reslist, queryList...)
  962. }
  963. }
  964. for i := 0; i < len(queryList); i++ {
  965. a = mapc2[queryList[i]["feedname"]].(int) * 2
  966. if i == 0 {
  967. datamap2["label"] = "饲料名称"
  968. switch queryList[i]["typestr"].(type) {
  969. case string:
  970. datamap2child["label"] = queryList[i]["typestr"].(string) + "/饲料参数"
  971. default:
  972. datamap2child["label"] = "/饲料参数"
  973. }
  974. datamap2child["prop"] = "A"
  975. data3 = append(data3, datamap2child)
  976. datamap2["children"] = data3
  977. data2 = append(data2, datamap2)
  978. data3 = []interface{}{}
  979. datamap2child = make(map[string]interface{})
  980. datamap2 = make(map[string]interface{})
  981. dtem = queryList[i]["fname"]
  982. }
  983. if dtem != queryList[i]["fname"] && i != 0 {
  984. data1 = append(data1, datamap1)
  985. dtem = queryList[i]["fname"]
  986. datamap1 = make(map[string]interface{})
  987. }
  988. if queryList[i]["fname"] == temstr {
  989. datamap2["label"] = queryList[i]["feedname"]
  990. datamap2child["label"] = "理论"
  991. datamap2child["prop"] = "a" + strconv.Itoa(a)
  992. data3 = append(data3, datamap2child)
  993. datamap2child = make(map[string]interface{})
  994. datamap2child["label"] = "实际"
  995. datamap2child["prop"] = "a" + strconv.Itoa(a+1)
  996. data3 = append(data3, datamap2child)
  997. datamap2child = make(map[string]interface{})
  998. datamap2["children"] = data3
  999. data2 = append(data2, datamap2)
  1000. data3 = []interface{}{}
  1001. datamap2 = make(map[string]interface{})
  1002. datamap2child = make(map[string]interface{})
  1003. }
  1004. datamap1["A"] = queryList[i]["fname"]
  1005. datamap1["a"+strconv.Itoa(a)] = queryList[i]["lweight"]
  1006. datamap1["a"+strconv.Itoa(a+1)] = queryList[i]["actualweightminus"]
  1007. datamap2["label"] = queryList[i]["饲料名称"]
  1008. a = a + 2
  1009. }
  1010. data1 = append(data1, datamap1)
  1011. chartData["data1"] = data1
  1012. chartData["data2"] = data2
  1013. queryData["list"] = chartData
  1014. queryData["code"] = 0
  1015. queryData["message"] = "ok"
  1016. queryData["data"] = queryList
  1017. return queryData, nil
  1018. }
  1019. func getAccuracyV4(queryList []map[string]interface{}) (map[string]interface{}, error) {
  1020. queryData := make(map[string]interface{})
  1021. chartData := make(map[string]interface{})
  1022. datamap1 := make(map[string]interface{})
  1023. datamap2 := make(map[string]interface{})
  1024. temsmap := make(map[interface{}]interface{})
  1025. mapc2 := make(map[interface{}]interface{})
  1026. mapc3 := make(map[interface{}]interface{})
  1027. data3 := []interface{}{}
  1028. datamap2child := make(map[string]interface{})
  1029. data2 := []interface{}{}
  1030. data1 := []interface{}{}
  1031. a := 0
  1032. var dtem interface{}
  1033. count2 := 0
  1034. var tem interface{}
  1035. var temstr interface{}
  1036. var typestr interface{}
  1037. for i := 0; i < len(queryList); i++ { // 获取所有饲料种类
  1038. if i == 0 {
  1039. temstr = queryList[i]["fname"]
  1040. typestr = queryList[i]["typestr"]
  1041. }
  1042. if temstr == queryList[i]["fname"] {
  1043. temsmap[queryList[i]["feedname"]] = 1
  1044. }
  1045. if _, ok := mapc2[queryList[i]["feedname"]]; !ok {
  1046. mapc2[queryList[i]["feedname"]] = count2 // 编号
  1047. mapc2[count2] = queryList[i]["feedname"]
  1048. count2++
  1049. }
  1050. }
  1051. for i := 0; i < count2; i++ { // 将所有饲料种类新增为第一行 测试
  1052. if _, ok := temsmap[mapc2[i]]; !ok {
  1053. reslist := make([]map[string]interface{}, 1)
  1054. res := make(map[string]interface{})
  1055. res["fname"] = temstr
  1056. res["feedname"] = mapc2[i]
  1057. res["饲料名称"] = mapc2[i]
  1058. res["typestr"] = typestr
  1059. reslist[0] = res
  1060. queryList = append(reslist, queryList...)
  1061. }
  1062. }
  1063. //for i := 0; i<count2 ; i++ {
  1064. // reslist := make([]map[string]interface{},1)
  1065. // res := make(map[string]interface{})
  1066. // res["fname"] = "分界线"
  1067. // res["feedname"] = mapc2[i]
  1068. // res["饲料名称"] = mapc2[i]
  1069. // res["typestr"] =typestr
  1070. // reslist[0] = res
  1071. // queryList = append(reslist,queryList...)
  1072. //}
  1073. for i := 0; i < len(queryList); i++ {
  1074. if i == 0 {
  1075. tem = queryList[i]["fname"]
  1076. }
  1077. if queryList[i]["fname"] == tem {
  1078. mapc3[queryList[i]["feedname"]] = i
  1079. }
  1080. }
  1081. for i := 0; i < len(queryList); i++ {
  1082. a = mapc2[queryList[i]["feedname"]].(int) * 4
  1083. if i == 0 {
  1084. datamap2["label"] = "饲料名称"
  1085. switch queryList[i]["typestr"].(type) {
  1086. case string:
  1087. datamap2child["label"] = queryList[i]["typestr"].(string) + "/饲料参数"
  1088. default:
  1089. datamap2child["label"] = "/饲料参数"
  1090. }
  1091. datamap2child["prop"] = "A"
  1092. data3 = append(data3, datamap2child)
  1093. datamap2["children"] = data3
  1094. data2 = append(data2, datamap2)
  1095. data3 = []interface{}{}
  1096. datamap2child = make(map[string]interface{})
  1097. datamap2 = make(map[string]interface{})
  1098. dtem = queryList[i]["fname"]
  1099. }
  1100. if dtem != queryList[i]["fname"] && i != 0 {
  1101. data1 = append(data1, datamap1)
  1102. dtem = queryList[i]["fname"]
  1103. datamap1 = make(map[string]interface{})
  1104. }
  1105. if queryList[i]["fname"] == temstr {
  1106. datamap2["label"] = queryList[i]["feedname"]
  1107. datamap2child["label"] = "理论"
  1108. datamap2child["prop"] = "a" + strconv.Itoa(a)
  1109. data3 = append(data3, datamap2child)
  1110. datamap2child = make(map[string]interface{})
  1111. datamap2child["label"] = "实际"
  1112. datamap2child["prop"] = "a" + strconv.Itoa(a+1)
  1113. data3 = append(data3, datamap2child)
  1114. datamap2child = make(map[string]interface{})
  1115. datamap2child["label"] = "误差值"
  1116. datamap2child["prop"] = "a" + strconv.Itoa(a+2)
  1117. data3 = append(data3, datamap2child)
  1118. datamap2child = make(map[string]interface{})
  1119. datamap2child["label"] = "误差率"
  1120. datamap2child["prop"] = "a" + strconv.Itoa(a+3)
  1121. data3 = append(data3, datamap2child)
  1122. datamap2["children"] = data3
  1123. data2 = append(data2, datamap2)
  1124. data3 = []interface{}{}
  1125. datamap2 = make(map[string]interface{})
  1126. datamap2child = make(map[string]interface{})
  1127. }
  1128. datamap1["A"] = queryList[i]["fname"]
  1129. datamap1["a"+strconv.Itoa(a)] = queryList[i]["lweight"]
  1130. datamap1["a"+strconv.Itoa(a+1)] = queryList[i]["actualweightminus"]
  1131. datamap1["a"+strconv.Itoa(a+2)] = queryList[i]["diff"]
  1132. datamap1["a"+strconv.Itoa(a+3)] = queryList[i]["diffrate"]
  1133. datamap2["label"] = queryList[i]["饲料名称"]
  1134. }
  1135. data1 = append(data1, datamap1)
  1136. chartData["data1"] = data1
  1137. chartData["data2"] = data2
  1138. queryData["list"] = chartData
  1139. queryData["code"] = 0
  1140. queryData["message"] = "ok"
  1141. queryData["data"] = queryList
  1142. return queryData, nil
  1143. }
  1144. func getAccuracyV3(queryList []map[string]interface{}) (map[string]interface{}, error) {
  1145. queryData := make(map[string]interface{})
  1146. chartData := make(map[string]interface{})
  1147. data := []interface{}{}
  1148. data1 := []interface{}{}
  1149. data2 := []interface{}{}
  1150. data3 := []interface{}{}
  1151. data4 := []interface{}{}
  1152. datatem := []interface{}{}
  1153. t1 := make(map[interface{}]string, 0)
  1154. for i := 0; i < len(queryList); i++ {
  1155. data = append(data, queryList[i]["mtime"])
  1156. data1 = append(data1, queryList[i]["lweight"])
  1157. data2 = append(data2, queryList[i]["weight"])
  1158. //data3 = append(data3, queryList[i]["field3"])
  1159. //data4 = append(data4, queryList[i]["field4"])
  1160. if _, ok := t1[queryList[i]["id"]]; !ok {
  1161. datatem = append(datatem, queryList[i]["startstime"], queryList[i]["lastactualweight"])
  1162. data3 = append(data3, datatem)
  1163. datatem = []interface{}{}
  1164. datatem = append(datatem, queryList[i]["intime"], queryList[i]["actualweight"])
  1165. data3 = append(data3, datatem)
  1166. datatem = []interface{}{}
  1167. }
  1168. t1[queryList[i]["id"]] = "1"
  1169. }
  1170. chartData["data1"] = data
  1171. chartData["data2"] = data1
  1172. chartData["data3"] = data2
  1173. chartData["data4"] = data3
  1174. chartData["data5"] = data4
  1175. queryData["list"] = chartData
  1176. queryData["code"] = 0
  1177. queryData["message"] = "ok"
  1178. queryData["data"] = queryList
  1179. return queryData, nil
  1180. }
  1181. func getAccuracyV3v1(queryList []map[string]interface{}) (map[string]interface{}, error) {
  1182. queryData := make(map[string]interface{})
  1183. chartData := make(map[string]interface{})
  1184. data := []interface{}{}
  1185. data1 := []interface{}{}
  1186. data2 := []interface{}{}
  1187. data3 := []interface{}{}
  1188. data4 := []interface{}{}
  1189. datatem := []interface{}{}
  1190. //t1 := make(map[interface{}]string, 0)
  1191. jsonstr := ""
  1192. jsonmap := make([]map[string]interface{},0)
  1193. if len(queryList)>0{
  1194. queryjsonList, _ := restful.Engine.SQL("select weightjson from recweightmuster where pid = ? and pastureid = ?" ,queryList[0]["pid"],queryList[0]["pastureid"]).Query().List()
  1195. if len(queryjsonList)>0{
  1196. jsonstr = queryjsonList[0]["weightjson"].(string)
  1197. }
  1198. }
  1199. err := json.Unmarshal([]byte(jsonstr),&jsonmap)
  1200. if err != nil{
  1201. logging.Error("json.Unmarshal err",err)
  1202. }
  1203. for i := 0; i < len(queryList); i++ {
  1204. datatem = append(datatem, queryList[i]["startstime"], queryList[i]["lastactualweight"])
  1205. data3 = append(data3, datatem)
  1206. datatem = []interface{}{}
  1207. datatem = append(datatem, queryList[i]["intime"], queryList[i]["actualweight"])
  1208. data3 = append(data3, datatem)
  1209. datatem = []interface{}{}
  1210. }
  1211. j := 0
  1212. for i := 0; i < len(jsonmap); i++ {
  1213. if jsonmap[i]["mt"].(string)>queryList[j]["intime"].(string){
  1214. if j < len(queryList)-1{
  1215. j++
  1216. }
  1217. }
  1218. data1 = append(data1, queryList[j]["lweight"])
  1219. data = append(data, jsonmap[i]["mt"])
  1220. data2 = append(data2, jsonmap[i]["w"])
  1221. }
  1222. chartData["data1"] = data
  1223. chartData["data2"] = data1
  1224. chartData["data3"] = data2
  1225. chartData["data4"] = data3
  1226. chartData["data5"] = data4
  1227. queryData["list"] = chartData
  1228. queryData["code"] = 0
  1229. queryData["message"] = "ok"
  1230. queryData["data"] = queryList
  1231. return queryData, nil
  1232. }
  1233. func getAccuracyV2(queryList []map[string]interface{}) (map[string]interface{}, error) {
  1234. queryData := make(map[string]interface{})
  1235. chartData := make(map[string]interface{})
  1236. data := []interface{}{}
  1237. data1 := []interface{}{}
  1238. data2 := []interface{}{}
  1239. data3 := []interface{}{}
  1240. data4 := []interface{}{}
  1241. for i := 0; i < len(queryList); i++ {
  1242. data = append(data, queryList[i]["日期"])
  1243. data1 = append(data1, queryList[i]["field1"])
  1244. data2 = append(data2, queryList[i]["field2"])
  1245. data3 = append(data3, queryList[i]["field3"])
  1246. data4 = append(data4, queryList[i]["field4"])
  1247. }
  1248. chartData["data1"] = data
  1249. chartData["data2"] = data1
  1250. chartData["data3"] = data2
  1251. chartData["data4"] = data3
  1252. chartData["data5"] = data4
  1253. queryData["list"] = chartData
  1254. queryData["code"] = 0
  1255. queryData["message"] = "ok"
  1256. queryData["data"] = queryList
  1257. return queryData, nil
  1258. }
  1259. func getAccuracy(queryList []map[string]interface{}) (map[string]interface{}, error) {
  1260. queryData := make(map[string]interface{})
  1261. chartData := make(map[string]interface{})
  1262. t1 := make(map[interface{}]interface{}, 0)
  1263. t2 := make(map[interface{}]interface{}, 0)
  1264. data := make(map[string][]interface{}, 0)
  1265. t := make([]map[string][]interface{}, 0)
  1266. e1 := []interface{}{}
  1267. e2 := []string{}
  1268. for _, value := range queryList {
  1269. if _, ok := t1[value["名称"]]; !ok {
  1270. e1 = append(e1, value["名称"])
  1271. }
  1272. if _, ok := t2[value["日期"]]; !ok {
  1273. switch value["日期"].(type) {
  1274. case string:
  1275. e2 = append(e2, value["日期"].(string))
  1276. }
  1277. }
  1278. t1[value["名称"]] = value["名称"]
  1279. t2[value["日期"]] = value["日期"]
  1280. }
  1281. sort.Strings(e2)
  1282. var b interface{}
  1283. a := 0
  1284. for i := 0; i < len(queryList); i++ {
  1285. if i == 0 {
  1286. b = queryList[i]["名称"]
  1287. }
  1288. if b != queryList[i]["名称"] {
  1289. if a < len(e2)-1 {
  1290. for k := 0; k < len(e2)-a; k++ {
  1291. data["data"] = append(data["data"], "")
  1292. }
  1293. }
  1294. t = append(t, data)
  1295. data = make(map[string][]interface{}, 0)
  1296. b = queryList[i]["名称"]
  1297. a = 0
  1298. }
  1299. switch queryList[i]["日期"].(type) {
  1300. case string:
  1301. dates, _ := time.Parse("2006-01-02", queryList[i]["日期"].(string))
  1302. dates1 := ""
  1303. if a < len(e2) -1{
  1304. dates1 = e2[a]
  1305. }else {
  1306. dates1 = e2[len(e2)-1]
  1307. }
  1308. if a < len(e2)-1 {
  1309. if e2[a] != queryList[i]["日期"].(string) {
  1310. if e2[a] < queryList[i]["日期"].(string) {
  1311. dates1 = queryList[i]["日期"].(string)
  1312. dates, _ = time.Parse("2006-01-02", e2[a])
  1313. for ; a < len(e2)-1; a++ {
  1314. if e2[a] >= queryList[i]["日期"].(string) {
  1315. break
  1316. }
  1317. }
  1318. }
  1319. for ; dates.Format("2006-01-02") < dates1; dates = dates.Add(24 * time.Hour) {
  1320. data["data"] = append(data["data"], "")
  1321. }
  1322. }
  1323. }
  1324. }
  1325. data["data"] = append(data["data"], queryList[i]["准确率"])
  1326. a++
  1327. }
  1328. if a < len(e2)-1 {
  1329. for k := 0; k < len(e2)-a; k++ {
  1330. data["data"] = append(data["data"], "")
  1331. }
  1332. }
  1333. t = append(t, data)
  1334. chartData["data1"] = e1
  1335. chartData["data2"] = e2
  1336. chartData["data3"] = t
  1337. queryData["list"] = chartData
  1338. queryData["code"] = 0
  1339. queryData["message"] = "ok"
  1340. queryData["data"] = queryList
  1341. return queryData, nil
  1342. }
  1343. func GetUpkeepPlan(c *gin.Context) {
  1344. appG := app.Gin{C: c}
  1345. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1346. fsion := gofasion.NewFasion(string(dataByte))
  1347. sqlnamestr := fsion.Get("name").ValueStr()
  1348. offset := fsion.Get("offset").ValueDefaultInt(0)
  1349. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1350. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1351. month := fsion.Get("month").ValueDefaultStr("map")
  1352. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  1353. tx := restful.Engine.NewSession()
  1354. err := tx.Begin()
  1355. defer func() {
  1356. switch {
  1357. case err != nil:
  1358. fmt.Println("__error:", err)
  1359. if tx != nil {
  1360. tx.Rollback()
  1361. }
  1362. default:
  1363. if tx != nil {
  1364. err = tx.Commit()
  1365. }
  1366. }
  1367. if tx != nil {
  1368. tx.Close()
  1369. }
  1370. }()
  1371. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1372. if sql != "" {
  1373. s_params := make([]interface{}, 0)
  1374. if fsion.HasKey("parammaps") {
  1375. parammaps := fsion.Get("parammaps")
  1376. paramslist := strings.Split(p, ",")
  1377. if len(paramslist) > 0 && p != "" {
  1378. for _, value := range paramslist {
  1379. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1380. if tempv, exists := c.Get("jwt_username"); exists {
  1381. s_params = append(s_params, tempv.(string))
  1382. } else {
  1383. s_params = append(s_params, "")
  1384. }
  1385. } else {
  1386. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1387. }
  1388. }
  1389. }
  1390. } else if fsion.HasKey("params") {
  1391. params := fsion.Get("params").Array()
  1392. for _, v_params := range params {
  1393. s_params = append(s_params, v_params.ValueStr())
  1394. }
  1395. }
  1396. if returntype == "xlsx" {
  1397. var xlsxfile *xlsx.File
  1398. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  1399. //c.File("c:/dd.xlsx")
  1400. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1401. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  1402. c.Header("Content-Transfer-Encoding", "binary")
  1403. _ = xlsxfile.Write(c.Writer)
  1404. } else {
  1405. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1406. if err != nil {
  1407. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1408. } else {
  1409. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  1410. if v, ok := value["id"]; ok {
  1411. //month = "2020-03"
  1412. seviceplans, _ := tx.SQL("SELECT * FROM seviceplan where eqId = ? and month=? ", v, month).QueryString() //获取保养计划
  1413. if seviceplans != nil {
  1414. for _, va := range seviceplans { //根据保养计划获取保养单
  1415. //day,_ :=strconv.Atoi(va["day"])
  1416. bigupkeeps, _ := tx.SQL("SELECT * FROM bigupkeep where seviceplanId = ?", va["id"]).QueryString() //获取保养计划中的保养单
  1417. //buk := make(map[string]map[string]string)
  1418. for _, bu := range bigupkeeps { //日期为key ,保养单为value 存到map中
  1419. jstr := ""
  1420. if bu["plantime"][8:9] == "0" {
  1421. jstr = "m" + bu["plantime"][9:]
  1422. } else {
  1423. jstr = "m" + bu["plantime"][8:]
  1424. }
  1425. if bu["flowCompeleted"] < "2" && bu["plantime"] < time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  1426. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#2" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1427. } else {
  1428. if bu["flowCompeleted"] >= "2" {
  1429. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#1" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1430. } else {
  1431. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#0" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1432. }
  1433. }
  1434. }
  1435. }
  1436. }
  1437. }
  1438. }
  1439. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1440. }
  1441. }
  1442. } else {
  1443. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1444. }
  1445. }
  1446. func GetArrList(c *gin.Context) {
  1447. appG := app.Gin{C: c}
  1448. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1449. fsion := gofasion.NewFasion(string(dataByte))
  1450. sqlnamestr := fsion.Get("name").ValueStr()
  1451. sqlnamestr1 := fsion.Get("name1").ValueStr()
  1452. offset := fsion.Get("offset").ValueDefaultInt(0)
  1453. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1454. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1455. month := fsion.Get("month").ValueDefaultStr("map")
  1456. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  1457. tx := restful.Engine.NewSession()
  1458. err := tx.Begin()
  1459. defer func() {
  1460. switch {
  1461. case err != nil:
  1462. fmt.Println("__error:", err)
  1463. if tx != nil {
  1464. tx.Rollback()
  1465. }
  1466. default:
  1467. if tx != nil {
  1468. err = tx.Commit()
  1469. }
  1470. }
  1471. if tx != nil {
  1472. tx.Close()
  1473. }
  1474. }()
  1475. //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
  1476. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1477. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  1478. if sql != "" {
  1479. s_params := make([]interface{}, 0)
  1480. if fsion.HasKey("parammaps") {
  1481. parammaps := fsion.Get("parammaps")
  1482. paramslist := strings.Split(p, ",")
  1483. if len(paramslist) > 0 && p != "" {
  1484. for _, value := range paramslist {
  1485. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1486. if tempv, exists := c.Get("jwt_username"); exists {
  1487. s_params = append(s_params, tempv.(string))
  1488. } else {
  1489. s_params = append(s_params, "")
  1490. }
  1491. } else {
  1492. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1493. }
  1494. }
  1495. }
  1496. }
  1497. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1498. if err != nil {
  1499. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1500. } else {
  1501. paramslist1 := strings.Split(p1, ",")
  1502. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  1503. s_params1 := make([]interface{}, 0)
  1504. if len(paramslist1) > 0 && p1 != "" {
  1505. for _, v := range paramslist1 {
  1506. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  1507. }
  1508. }
  1509. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  1510. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  1511. }
  1512. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1513. }
  1514. } else {
  1515. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1516. }
  1517. }
  1518. func GetRJSBData(c *gin.Context) {
  1519. appG := app.Gin{C: c}
  1520. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1521. fsion := gofasion.NewFasion(string(dataByte))
  1522. sqlnamestr := fsion.Get("name").ValueStr()
  1523. sqlnamestr1 := fsion.Get("name1").ValueStr()
  1524. offset := fsion.Get("offset").ValueDefaultInt(0)
  1525. pagecount := fsion.Get("plancount").ValueDefaultInt(0)
  1526. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1527. month := fsion.Get("month").ValueDefaultStr("map")
  1528. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  1529. tx := restful.Engine.NewSession()
  1530. err := tx.Begin()
  1531. defer func() {
  1532. switch {
  1533. case err != nil:
  1534. fmt.Println("__error:", err)
  1535. if tx != nil {
  1536. tx.Rollback()
  1537. }
  1538. default:
  1539. if tx != nil {
  1540. err = tx.Commit()
  1541. }
  1542. }
  1543. if tx != nil {
  1544. tx.Close()
  1545. }
  1546. }()
  1547. //STR :=Httppostdata("http://192.168.1.104:8081/authdata/GetDataBySqlIN","POST",string(dataByte),tx)
  1548. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1549. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  1550. if sql != "" {
  1551. s_params := make([]interface{}, 0)
  1552. if fsion.HasKey("parammaps") {
  1553. parammaps := fsion.Get("parammaps")
  1554. paramslist := strings.Split(p, ",")
  1555. if len(paramslist) > 0 && p != "" {
  1556. for _, value := range paramslist {
  1557. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1558. if tempv, exists := c.Get("jwt_username"); exists {
  1559. s_params = append(s_params, tempv.(string))
  1560. } else {
  1561. s_params = append(s_params, "")
  1562. }
  1563. } else {
  1564. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1565. }
  1566. }
  1567. }
  1568. }
  1569. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1570. if err != nil {
  1571. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1572. } else {
  1573. paramslist1 := strings.Split(p1, ",")
  1574. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  1575. s_params1 := make([]interface{}, 0)
  1576. if len(paramslist1) > 0 && p1 != "" {
  1577. for _, v := range paramslist1 {
  1578. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  1579. }
  1580. }
  1581. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  1582. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  1583. }
  1584. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1585. }
  1586. } else {
  1587. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1588. }
  1589. }
  1590. func GetAccount(c *gin.Context) {
  1591. appG := app.Gin{C: c}
  1592. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1593. fsion := gofasion.NewFasion(string(dataByte))
  1594. sqlnamestr := fsion.Get("name").ValueStr()
  1595. offset := fsion.Get("offset").ValueDefaultInt(0)
  1596. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1597. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1598. tx := restful.Engine.NewSession()
  1599. err := tx.Begin()
  1600. defer func() {
  1601. switch {
  1602. case err != nil:
  1603. logging.Error("tx.Begin 事务启动失败__error:", err)
  1604. if tx != nil {
  1605. tx.Rollback()
  1606. }
  1607. default:
  1608. if tx != nil {
  1609. err = tx.Commit()
  1610. }
  1611. }
  1612. if tx != nil {
  1613. tx.Close()
  1614. }
  1615. }()
  1616. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1617. //fmt.Println("sql========\n",sqlnamestr,sql)
  1618. if sql != "" {
  1619. s_params := make([]interface{}, 0)
  1620. if fsion.HasKey("parammaps") {
  1621. parammaps := fsion.Get("parammaps")
  1622. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  1623. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  1624. }
  1625. paramslist := strings.Split(p, ",")
  1626. if len(paramslist) > 0 && p != "" {
  1627. for _, value := range paramslist {
  1628. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1629. if tempv, exists := c.Get("jwt_username"); exists {
  1630. s_params = append(s_params, tempv.(string))
  1631. } else {
  1632. s_params = append(s_params, "")
  1633. }
  1634. } else {
  1635. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1636. }
  1637. }
  1638. }
  1639. } else if fsion.HasKey("params") {
  1640. params := fsion.Get("params").Array()
  1641. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  1642. for _, v_params := range params {
  1643. s_params = append(s_params, v_params.ValueStr())
  1644. }
  1645. }
  1646. if returntype == "xlsx" {
  1647. var xlsxfile *xlsx.File
  1648. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  1649. //c.File("c:/dd.xlsx")
  1650. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1651. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  1652. c.Header("Content-Transfer-Encoding", "binary")
  1653. _ = xlsxfile.Write(c.Writer)
  1654. } else {
  1655. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1656. if err != nil {
  1657. logging.Error("GetDataByName err: ", err)
  1658. msg := geterrmsg(err.Error())
  1659. appG.Response(http.StatusOK, e.ERROR, msg)
  1660. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1661. } else {
  1662. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1663. }
  1664. }
  1665. } else {
  1666. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1667. }
  1668. }
  1669. // @Summary 根据APISQL表中SQL执行得到数据集
  1670. // @Tags PostDataByName
  1671. // @Accept json
  1672. // @Produce json
  1673. // @Param sqlname query string true "sqlname"
  1674. // @Param offset query int true "offset"
  1675. // @Param pagecount query int true "pagecount"
  1676. // @Param returntype query string true "returntype"
  1677. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1678. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1679. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1680. // @Router /data/PostDataByName [POST]
  1681. func PostDataByName(c *gin.Context) {
  1682. appG := app.Gin{C: c}
  1683. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1684. fsion := gofasion.NewFasion(string(dataByte))
  1685. sqlnamestr := fsion.Get("name").ValueStr()
  1686. s_params := make([]interface{}, 0)
  1687. tx := restful.Engine.NewSession()
  1688. err := tx.Begin()
  1689. defer func() {
  1690. switch {
  1691. case err != nil:
  1692. fmt.Println("__error:", err)
  1693. if tx != nil {
  1694. tx.Rollback()
  1695. }
  1696. default:
  1697. if tx != nil {
  1698. err = tx.Commit()
  1699. }
  1700. }
  1701. if tx != nil {
  1702. tx.Close()
  1703. }
  1704. }()
  1705. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1706. if sql != "" {
  1707. if fsion.HasKey("parammaps") {
  1708. parammaps := fsion.Get("parammaps")
  1709. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  1710. paramslist := strings.Split(p, ",")
  1711. if len(paramslist) > 0 && p != "" {
  1712. for _, value := range paramslist {
  1713. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1714. if tempv, exists := c.Get("jwt_username"); exists {
  1715. s_params = append(s_params, tempv.(string))
  1716. } else {
  1717. s_params = append(s_params, "")
  1718. }
  1719. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  1720. ids, err := setting.SnowIds.NextId()
  1721. if err != nil {
  1722. ids = time.Now().UnixNano()
  1723. logging.Info("create SnowIds err", err)
  1724. }
  1725. s_params = append(s_params, ids)
  1726. } else {
  1727. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1728. }
  1729. }
  1730. }
  1731. } else if fsion.HasKey("params") {
  1732. params := fsion.Get("params").Array()
  1733. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  1734. //ids,err := setting.SnowIds.NextId()
  1735. //if err !=nil{
  1736. // ids = time.Now().UnixNano()
  1737. // logging.Info("create SnowIds err",err)
  1738. //}
  1739. //s_params = append(s_params, ids)
  1740. for _, v_params := range params {
  1741. s_params = append(s_params, v_params.ValueStr())
  1742. }
  1743. }
  1744. queryData, err := execDataBySqlT(sql, s_params, tx)
  1745. if err != nil {
  1746. logging.Error("PostDataByName err: ", err)
  1747. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1748. msg := geterrmsg(err.Error())
  1749. appG.Response(http.StatusOK, e.ERROR, msg)
  1750. } else {
  1751. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1752. }
  1753. } else {
  1754. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1755. }
  1756. }
  1757. func PostRJSBData(c *gin.Context) {
  1758. appG := app.Gin{C: c}
  1759. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1760. fsion := gofasion.NewFasion(string(dataByte))
  1761. logging.Info("PostRJSBData err: ", string(dataByte))
  1762. s_params := make([]interface{}, 0)
  1763. tx := restful.Engine.NewSession()
  1764. err := tx.Begin()
  1765. defer func() {
  1766. switch {
  1767. case err != nil:
  1768. fmt.Println("__error:", err)
  1769. if tx != nil {
  1770. tx.Rollback()
  1771. }
  1772. default:
  1773. if tx != nil {
  1774. err = tx.Commit()
  1775. }
  1776. }
  1777. if tx != nil {
  1778. tx.Close()
  1779. }
  1780. }()
  1781. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1782. sql1, p1 := restful.GetSqlByNameDBT("updated1YJ", tx)
  1783. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJ", tx)
  1784. sql3, p3 := restful.GetSqlByNameDBT("updated2YJ", tx)
  1785. if !fsion.HasKey("parammaps") {
  1786. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1787. return
  1788. }
  1789. parammaps := fsion.Get("parammaps")
  1790. // ==========================1========================
  1791. paramslist := strings.Split(p, ",")
  1792. if len(paramslist) > 0 && p != "" {
  1793. for _, value := range paramslist {
  1794. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1795. }
  1796. }
  1797. _, err = tx.SQL(sql, s_params...).Execute()
  1798. if err != nil {
  1799. fmt.Println(err)
  1800. logging.Error("PostRJSBData err: ", err)
  1801. msg := geterrmsg(err.Error())
  1802. appG.Response(http.StatusOK, e.ERROR, msg)
  1803. return
  1804. }
  1805. // ==========================2========================
  1806. s_params = make([]interface{}, 0)
  1807. paramslist = strings.Split(p1, ",")
  1808. if len(paramslist) > 0 && p1 != "" {
  1809. for _, value := range paramslist {
  1810. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1811. }
  1812. }
  1813. _, err = tx.SQL(sql1, s_params...).Execute()
  1814. if err != nil {
  1815. logging.Error("PostRJSBData1 err: ", err)
  1816. msg := geterrmsg(err.Error())
  1817. appG.Response(http.StatusOK, e.ERROR, msg)
  1818. return
  1819. }
  1820. s_params = make([]interface{}, 0)
  1821. // ==========================3========================
  1822. paramslist = strings.Split(p2, ",")
  1823. if len(paramslist) > 0 && p2 != "" {
  1824. for _, value := range paramslist {
  1825. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1826. }
  1827. }
  1828. _, err = tx.SQL(sql2, s_params...).Execute()
  1829. if err != nil {
  1830. logging.Error("PostRJSBData2 err: ", err)
  1831. msg := geterrmsg(err.Error())
  1832. appG.Response(http.StatusOK, e.ERROR, msg)
  1833. return
  1834. }
  1835. s_params = make([]interface{}, 0)
  1836. // ==========================4========================
  1837. paramslist = strings.Split(p3, ",")
  1838. if len(paramslist) > 0 && p3 != "" {
  1839. for _, value := range paramslist {
  1840. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1841. }
  1842. }
  1843. _, err = tx.SQL(sql3, s_params...).Execute()
  1844. if err != nil {
  1845. logging.Error("PostRJSBData3 err: ", err)
  1846. msg := geterrmsg(err.Error())
  1847. appG.Response(http.StatusOK, e.ERROR, msg)
  1848. return
  1849. }
  1850. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1851. }
  1852. func PostRJSBDatas(c *gin.Context) {
  1853. appG := app.Gin{C: c}
  1854. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1855. fsions := gofasion.NewFasion(string(dataByte)).Array()
  1856. logging.Info("PostRJSBDatas err: ", string(dataByte))
  1857. tx := restful.Engine.NewSession()
  1858. err := tx.Begin()
  1859. defer func() {
  1860. switch {
  1861. case err != nil:
  1862. fmt.Println("__error:", err)
  1863. if tx != nil {
  1864. tx.Rollback()
  1865. }
  1866. default:
  1867. if tx != nil {
  1868. err = tx.Commit()
  1869. }
  1870. }
  1871. if tx != nil {
  1872. tx.Close()
  1873. }
  1874. }()
  1875. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1876. sql1, p1 := restful.GetSqlByNameDBT("updated1YJV2", tx)
  1877. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJV2", tx)
  1878. sql3, p3 := restful.GetSqlByNameDBT("updated2YJV2", tx)
  1879. for _, fsion := range fsions {
  1880. if !fsion.HasKey("parammaps") {
  1881. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1882. return
  1883. }
  1884. parammaps := fsion.Get("parammaps")
  1885. s_params := make([]interface{}, 0)
  1886. // ==========================1========================
  1887. paramslist := strings.Split(p, ",")
  1888. if len(paramslist) > 0 && p != "" {
  1889. for _, value := range paramslist {
  1890. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1891. }
  1892. }
  1893. fmt.Println("}}}}}}}}}}}}}}}}}}}",s_params)
  1894. _, err = tx.SQL(sql, s_params...).Execute()
  1895. if err != nil {
  1896. fmt.Println(err)
  1897. logging.Error("PostRJSBData err: ", err)
  1898. msg := geterrmsg(err.Error())
  1899. appG.Response(http.StatusOK, e.ERROR, msg)
  1900. return
  1901. }
  1902. // ==========================2========================
  1903. s_params = make([]interface{}, 0)
  1904. paramslist = strings.Split(p1, ",")
  1905. if len(paramslist) > 0 && p1 != "" {
  1906. for _, value := range paramslist {
  1907. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1908. }
  1909. }
  1910. _, err = tx.SQL(sql1, s_params...).Execute()
  1911. if err != nil {
  1912. logging.Error("PostRJSBData1 err: ", err)
  1913. msg := geterrmsg(err.Error())
  1914. appG.Response(http.StatusOK, e.ERROR, msg)
  1915. return
  1916. }
  1917. s_params = make([]interface{}, 0)
  1918. // ==========================3========================
  1919. paramslist = strings.Split(p2, ",")
  1920. if len(paramslist) > 0 && p2 != "" {
  1921. for _, value := range paramslist {
  1922. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1923. }
  1924. }
  1925. _, err = tx.SQL(sql2, s_params...).Execute()
  1926. if err != nil {
  1927. logging.Error("PostRJSBData2 err: ", err)
  1928. msg := geterrmsg(err.Error())
  1929. appG.Response(http.StatusOK, e.ERROR, msg)
  1930. return
  1931. }
  1932. s_params = make([]interface{}, 0)
  1933. // ==========================4========================
  1934. paramslist = strings.Split(p3, ",")
  1935. if len(paramslist) > 0 && p3 != "" {
  1936. for _, value := range paramslist {
  1937. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1938. }
  1939. }
  1940. _, err = tx.SQL(sql3, s_params...).Execute()
  1941. if err != nil {
  1942. logging.Error("PostRJSBData3 err: ", err)
  1943. msg := geterrmsg(err.Error())
  1944. appG.Response(http.StatusOK, e.ERROR, msg)
  1945. return
  1946. }
  1947. }
  1948. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1949. }
  1950. // @Summary 根据APISQL表中SQL执行得到数据集
  1951. // @Tags ExecDataByConfig
  1952. // @Accept json
  1953. // @Produce json
  1954. // @Param sqlname query string true "sqlname"
  1955. // @Param offset query int true "offset"
  1956. // @Param pagecount query int true "pagecount"
  1957. // @Param returntype query string true "returntype"
  1958. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1959. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1960. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1961. // @Router /data/PostDataByName [POST]
  1962. func ExecDataByConfig(c *gin.Context) {
  1963. appG := app.Gin{C: c}
  1964. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1965. tempval_ := make(map[string]interface{})
  1966. tempCommon := make(map[string]interface{})
  1967. tempval := make([]map[string]interface{}, 0)
  1968. err := json.Unmarshal(dataByte, &tempval_)
  1969. logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  1970. if err != nil {
  1971. fmt.Println("error:", err)
  1972. } else {
  1973. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  1974. tempCommon = v
  1975. }
  1976. if v, ok := tempval_["data"].([]interface{}); ok {
  1977. for _, Tvalue := range v {
  1978. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1979. tempval = append(tempval, v1)
  1980. }
  1981. }
  1982. }
  1983. tx := restful.Engine.NewSession()
  1984. err := tx.Begin()
  1985. if err != nil {
  1986. logging.Error("tx.Begin 事务启动失败__error:", err)
  1987. }
  1988. defer func() {
  1989. switch {
  1990. case err != nil:
  1991. fmt.Println("__error:", err)
  1992. if tx != nil {
  1993. tx.Rollback()
  1994. }
  1995. default:
  1996. if tx != nil {
  1997. err = tx.Commit()
  1998. }
  1999. }
  2000. if tx != nil {
  2001. tx.Close()
  2002. }
  2003. }()
  2004. if err == nil {
  2005. if tempv, exists := c.Get("jwt_username"); exists {
  2006. tempCommon["jwt_username"] = tempv.(string)
  2007. }
  2008. for _, paramvalue := range tempval {
  2009. if _, ok := paramvalue["resultname"]; !ok {
  2010. paramvalue["resultname"] = paramvalue["name"]
  2011. }
  2012. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  2013. if err != nil {
  2014. logging.Error("ExecDataParam error:", err)
  2015. break
  2016. }
  2017. }
  2018. if err == nil {
  2019. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  2020. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  2021. } else {
  2022. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  2023. }
  2024. } else {
  2025. msg := geterrmsg(err.Error())
  2026. appG.Response(http.StatusOK, e.ERROR, msg)
  2027. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  2028. }
  2029. }
  2030. }
  2031. if err != nil {
  2032. msg := geterrmsg(err.Error())
  2033. appG.Response(http.StatusOK, e.ERROR, msg)
  2034. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  2035. }
  2036. }
  2037. func interfacetostring(val interface{}) string {
  2038. switch val.(type) {
  2039. case string:
  2040. ss := val.(string)
  2041. return ss
  2042. break
  2043. case int:
  2044. ss := val.(int)
  2045. return strconv.Itoa(ss)
  2046. break
  2047. case int64:
  2048. ss := val.(int64)
  2049. return strconv.FormatInt(ss, 10)
  2050. break
  2051. case float64:
  2052. ss := val.(float64)
  2053. return strconv.FormatFloat(ss, 'E', -1, 32)
  2054. break
  2055. }
  2056. return ""
  2057. }
  2058. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  2059. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  2060. tempstrb := strings.IndexAny(valuestring, "@")
  2061. tempstre := strings.IndexAny(valuestring, ".")
  2062. if tempstrb > -1 && tempstre > -1 {
  2063. for _, ParamMapList := range ParamMap {
  2064. if v, ok := ParamMapList["resultname"]; ok {
  2065. if v == valuestring[tempstrb+1:tempstre] {
  2066. if v1, ok := ParamMapList["resultmaps"]; ok {
  2067. if newA, ok := v1.([]map[string]interface{}); ok {
  2068. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  2069. } else if newA, ok := v1.(map[string]interface{}); ok {
  2070. return interfacetostring(newA[valuestring[tempstre+1:]])
  2071. }
  2072. }
  2073. }
  2074. }
  2075. }
  2076. } else {
  2077. return valuestring
  2078. }
  2079. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  2080. return strconv.FormatInt(int64(valuestring), 10)
  2081. }
  2082. return ""
  2083. }
  2084. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  2085. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  2086. tempstrb := strings.IndexAny(valuestring, "@")
  2087. tempstre := strings.IndexAny(valuestring, ".")
  2088. if tempstrb > -1 && tempstre > -1 {
  2089. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  2090. if s, ok := v.(string); ok {
  2091. return s
  2092. } else if s, ok := v.(float64); ok {
  2093. return strconv.FormatInt(int64(s), 10)
  2094. }
  2095. }
  2096. }
  2097. }
  2098. return ""
  2099. }
  2100. 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) {
  2101. sqlname := fasion["name"].(string)
  2102. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  2103. if v, ok := fasion["type"]; ok {
  2104. sqltype = v.(string)
  2105. }
  2106. sql := ""
  2107. p := ""
  2108. s_params := make([]interface{}, 0)
  2109. if sqltype != "" {
  2110. sql, p = restful.GetSqlByName(sqlname, tx)
  2111. //sqlApi, _ := models.GetApisqlByName(sqlname)
  2112. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  2113. paramslist := strings.Split(p, ",")
  2114. if len(paramslist) > 0 && p != "" {
  2115. for _, value := range paramslist {
  2116. ss := ""
  2117. if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2118. ids, err := setting.SnowIds.NextId()
  2119. if err != nil {
  2120. ids = time.Now().UnixNano()
  2121. logging.Info("create SnowIds err", err)
  2122. }
  2123. s_params = append(s_params, ids)
  2124. } else {
  2125. if ss == "" && CommonParamMap != nil {
  2126. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  2127. ss = valuestring
  2128. tempstrb := strings.IndexAny(valuestring, "@")
  2129. tempstre := strings.IndexAny(valuestring, ".")
  2130. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  2131. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  2132. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  2133. }
  2134. } else {
  2135. ss = ""
  2136. }
  2137. }
  2138. }
  2139. if ss == "" && ParentParamMap != nil {
  2140. ss = getParamValue_(v, value, ParentParamMap)
  2141. }
  2142. if ss == "" && sameParamMap != nil {
  2143. ss = getParamValue(v, value, sameParamMap)
  2144. }
  2145. if ss == "" && ParamMap != nil {
  2146. ss = getParamValue(v, value, ParamMap)
  2147. }
  2148. s_params = append(s_params, ss)
  2149. }
  2150. }
  2151. }
  2152. }
  2153. }
  2154. if sql == "" {
  2155. sqltype = ""
  2156. }
  2157. fmt.Printf("%v+ \n", s_params, sqlname)
  2158. if sqltype == "s1" {
  2159. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2160. if v, ok := tempValParam.(map[string]interface{}); ok {
  2161. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2162. }
  2163. if err != nil {
  2164. return nil, err
  2165. }
  2166. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2167. if err != nil {
  2168. return nil, err
  2169. }
  2170. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2171. } else if sqltype == "sm" {
  2172. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  2173. if v, ok := tempValParam.(map[string]interface{}); ok {
  2174. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2175. } else {
  2176. fmt.Println("sm", tempValParam)
  2177. }
  2178. if err != nil {
  2179. return nil, err
  2180. }
  2181. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2182. if err != nil {
  2183. return nil, err
  2184. }
  2185. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2186. } else if sqltype == "v" {
  2187. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2188. if v, ok := tempValParam.(map[string]interface{}); ok {
  2189. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2190. } else {
  2191. fmt.Println("v", tempValParam)
  2192. }
  2193. if err != nil {
  2194. return nil, err
  2195. }
  2196. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  2197. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  2198. }
  2199. } else if sqltype == "e" {
  2200. tempVal_arrary := make([]map[string]interface{}, 0)
  2201. tempVal_map := make(map[string]interface{})
  2202. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  2203. if err == nil {
  2204. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  2205. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  2206. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  2207. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  2208. return tempVal_arrary, err
  2209. }
  2210. return nil, err
  2211. } else {
  2212. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  2213. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2214. if err != nil {
  2215. return nil, err
  2216. }
  2217. return nil, err
  2218. }
  2219. }
  2220. return nil, nil
  2221. }
  2222. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *xorm.Session) error {
  2223. var err error
  2224. subarray, ok := fasion["children"]
  2225. if ok {
  2226. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  2227. tempVal_arrary := make([]map[string]interface{}, 0)
  2228. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  2229. tempVal_ := make(map[string]interface{})
  2230. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  2231. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  2232. } else {
  2233. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  2234. }
  2235. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  2236. if err != nil {
  2237. return err
  2238. }
  2239. tempVal_arrary = append(tempVal_arrary, tempVal_)
  2240. }
  2241. v.(map[string]interface{})["children"] = tempVal_arrary
  2242. }
  2243. }
  2244. return nil
  2245. }
  2246. // @Summary 删除图片
  2247. // @Tags RemovePicByName
  2248. // @Accept json
  2249. // @Produce json
  2250. // @Param sqlname query string true "sqlname"
  2251. // @Param offset query int true "offset"
  2252. // @Param pagecount query int true "pagecount"
  2253. // @Param returntype query string true "returntype"
  2254. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2255. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2256. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2257. // @Router /data/PostDataByName [POST]
  2258. func RemovePicByName(c *gin.Context) {
  2259. logging.Info("RemovePicByName ", c.Keys)
  2260. appG := app.Gin{C: c}
  2261. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2262. fsion := gofasion.NewFasion(string(dataByte))
  2263. sqlnamestr := fsion.Get("name").ValueStr()
  2264. picname := fsion.Get("picname").ValueStr()
  2265. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2266. params := fsion.Get("params").Array()
  2267. s_params := make([]interface{}, 0)
  2268. for _, v_params := range params {
  2269. s_params = append(s_params, v_params.ValueStr())
  2270. }
  2271. if sql != "" {
  2272. queryData, err := execDataBySql(sql, s_params)
  2273. if picname != "" {
  2274. // Destination
  2275. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  2276. err := os.Remove(distPath) //删除文件test.txt
  2277. if err != nil {
  2278. //如果删除失败则输出 file remove Error!
  2279. fmt.Println("file remove Error!")
  2280. //输出错误详细信息
  2281. fmt.Printf("%s", err)
  2282. } else {
  2283. //如果删除成功则输出 file remove OK!
  2284. fmt.Print("file remove OK!")
  2285. }
  2286. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  2287. err = os.Remove(distPath) //删除文件test.txt
  2288. if err != nil {
  2289. //如果删除失败则输出 file remove Error!
  2290. fmt.Println("file remove Error!")
  2291. //输出错误详细信息
  2292. fmt.Printf("%s", err)
  2293. } else {
  2294. //如果删除成功则输出 file remove OK!
  2295. fmt.Print("file remove OK!")
  2296. }
  2297. }
  2298. if err != nil {
  2299. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2300. } else {
  2301. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2302. }
  2303. } else {
  2304. appG.Response(http.StatusOK, e.SUCCESS, nil)
  2305. }
  2306. }
  2307. // @Summary 删除文件
  2308. // @Tags RemoveFileByName
  2309. // @Accept json
  2310. // @Produce json
  2311. // @Param sqlname query string true "sqlname"
  2312. // @Param offset query int true "offset"
  2313. // @Param pagecount query int true "pagecount"
  2314. // @Param returntype query string true "returntype"
  2315. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2316. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2317. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2318. // @Router /data/PostDataByName [POST]
  2319. func RemoveFileByName(c *gin.Context) {
  2320. appG := app.Gin{C: c}
  2321. id := c.Param("id")
  2322. logging.Info("DownloadFile ", c.Keys, id)
  2323. eqdoc, err := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", id).QueryString()
  2324. if eqdoc == nil {
  2325. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2326. return
  2327. }
  2328. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  2329. err = os.Remove(distPath) //删除文件test.txt
  2330. if err != nil {
  2331. //如果删除失败则输出 file remove Error!
  2332. fmt.Println("file remove Error!")
  2333. //输出错误详细信息
  2334. fmt.Printf("%s", err)
  2335. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2336. return
  2337. }
  2338. fmt.Print("file remove OK!")
  2339. _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
  2340. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2341. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2342. //fsion := gofasion.NewFasion(string(dataByte))
  2343. //sqlnamestr := fsion.Get("name").ValueStr()
  2344. //filename := fsion.Get("filename").ValueStr()
  2345. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2346. //params := fsion.Get("params").Array()
  2347. //s_params := make([]interface{}, 0)
  2348. //for _, v_params := range params {
  2349. // s_params = append(s_params, v_params.ValueStr())
  2350. //}
  2351. //if sql != "" {
  2352. // queryData, err := execDataBySql(sql, s_params)
  2353. //
  2354. // if filename != "" {
  2355. // // Destination
  2356. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  2357. //
  2358. // err := os.Remove(distPath) //删除文件test.txt
  2359. // if err != nil {
  2360. // //如果删除失败则输出 file remove Error!
  2361. // fmt.Println("file remove Error!")
  2362. // //输出错误详细信息
  2363. // fmt.Printf("%s", err)
  2364. // } else {
  2365. // //如果删除成功则输出 file remove OK!
  2366. // fmt.Print("file remove OK!")
  2367. // }
  2368. // }
  2369. // if err != nil {
  2370. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  2371. // } else {
  2372. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2373. // }
  2374. //} else {
  2375. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  2376. //}
  2377. }
  2378. // @Summary 根据APISQL表中SQL执行得到数据集
  2379. // @Tags PostDataByName
  2380. // @Accept json
  2381. // @Produce json
  2382. // @Param sqlname query string true "sqlname"
  2383. // @Param offset query int true "offset"
  2384. // @Param pagecount query int true "pagecount"
  2385. // @Param returntype query string true "returntype"
  2386. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2387. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2388. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2389. // @Router /data/PostDataByName [POST]
  2390. func UpdateDataRelation(c *gin.Context) {
  2391. appG := app.Gin{C: c}
  2392. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2393. fsion := gofasion.NewFasion(string(dataByte))
  2394. tablename := fsion.Get("name").ValueStr()
  2395. dataname := fsion.Get("dataname").ValueStr()
  2396. datavalue := fsion.Get("datavalue").ValueStr()
  2397. valuename := fsion.Get("valuename").ValueStr()
  2398. logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  2399. values := fsion.Get("values").Array()
  2400. tx := restful.Engine.NewSession()
  2401. err := tx.Begin()
  2402. defer func() {
  2403. switch {
  2404. case err != nil:
  2405. fmt.Println("__error:", err)
  2406. if tx != nil {
  2407. tx.Rollback()
  2408. }
  2409. default:
  2410. if tx != nil {
  2411. err = tx.Commit()
  2412. }
  2413. }
  2414. if tx != nil {
  2415. tx.Close()
  2416. }
  2417. }()
  2418. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  2419. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  2420. d_params := make([]interface{}, 4)
  2421. d_params[0] = datavalue
  2422. s_params := ""
  2423. for _, v_values := range values {
  2424. d_params[1] = v_values.ValueStr()
  2425. if s_params == "" {
  2426. s_params = v_values.ValueStr()
  2427. } else {
  2428. s_params = s_params + "," + v_values.ValueStr()
  2429. }
  2430. d_params[2] = d_params[1]
  2431. d_params[3] = d_params[0]
  2432. _, err := execDataBySqlT(insertSqlstr, d_params, tx)
  2433. if err != nil {
  2434. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2435. break
  2436. }
  2437. }
  2438. if s_params != "" {
  2439. deleteSqlstr := "delete from " + tablename + " " +
  2440. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  2441. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2442. if err != nil {
  2443. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2444. } else if err == nil {
  2445. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2446. }
  2447. } else {
  2448. deleteSqlstr := "delete from " + tablename + " " +
  2449. " where " + dataname + " = " + datavalue
  2450. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2451. if err != nil {
  2452. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2453. } else if err == nil {
  2454. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2455. }
  2456. }
  2457. }
  2458. // @Summary 根据APISQL表中SQL执行得到数据集
  2459. // @Tags PostDatasByName
  2460. // @Accept json
  2461. // @Produce json
  2462. // @Param sqlname query string true "sqlname"
  2463. // @Param offset query int true "offset"
  2464. // @Param pagecount query int true "pagecount"
  2465. // @Param returntype query string true "returntype"
  2466. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2467. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2468. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2469. // @Router /data/PostDatasByName [POST]
  2470. func PostDataByNames(c *gin.Context) {
  2471. appG := app.Gin{C: c}
  2472. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2473. fsion := gofasion.NewFasion(string(dataByte))
  2474. sqlParams := fsion.Array()
  2475. queryData := make(map[string]interface{})
  2476. for _, v := range sqlParams {
  2477. fsion2 := gofasion.NewFasion(v.Json())
  2478. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  2479. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  2480. if sql == "" {
  2481. errorm := make(map[string]interface{})
  2482. errorm["code"] = e.ERROR
  2483. errorm["message"] = "sql没有找到"
  2484. errorm["param"] = ""
  2485. queryData[fsion2.Get("name").ValueStr()] = errorm
  2486. continue
  2487. } else if sql != "" {
  2488. s_params := make([]interface{}, 0)
  2489. if fsion2.HasKey("parammaps") {
  2490. parammaps := fsion2.Get("parammaps")
  2491. paramslist := strings.Split(p, ",")
  2492. if len(paramslist) > 0 && p != "" {
  2493. for _, value := range paramslist {
  2494. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2495. if tempv, exists := c.Get("jwt_username"); exists {
  2496. s_params = append(s_params, tempv.(string))
  2497. } else {
  2498. s_params = append(s_params, "")
  2499. }
  2500. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2501. ids, err := setting.SnowIds.NextId()
  2502. if err != nil {
  2503. ids = time.Now().UnixNano()
  2504. logging.Info("create SnowIds err", err)
  2505. }
  2506. s_params = append(s_params, ids)
  2507. } else {
  2508. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2509. }
  2510. }
  2511. }
  2512. } else if fsion2.HasKey("params") {
  2513. params := fsion2.Get("params").Array()
  2514. //ids,err := setting.SnowIds.NextId()
  2515. //if err !=nil{
  2516. // ids = time.Now().UnixNano()
  2517. // logging.Info("create SnowIds err",err)
  2518. //}
  2519. //s_params = append(s_params, ids)
  2520. for _, v_params := range params {
  2521. s_params = append(s_params, v_params.ValueStr())
  2522. }
  2523. }
  2524. _, err := execDataBySql(sql, s_params)
  2525. if err != nil {
  2526. logging.Error("PostDataByName err: ", err)
  2527. errorm := make(map[string]interface{})
  2528. errorm["code"] = e.ERROR
  2529. errorm["message"] = err.Error()
  2530. errorm["param"] = s_params
  2531. queryData[fsion2.Get("name").ValueStr()] = errorm
  2532. continue
  2533. } else {
  2534. errorm := make(map[string]interface{})
  2535. errorm["code"] = e.SUCCESS
  2536. errorm["message"] = "success"
  2537. queryData[fsion2.Get("name").ValueStr()] = errorm
  2538. continue
  2539. }
  2540. } else {
  2541. errorm := make(map[string]interface{})
  2542. errorm["code"] = e.ERROR
  2543. errorm["message"] = "NO SQL"
  2544. errorm["param"] = ""
  2545. queryData[fsion2.Get("name").ValueStr()] = errorm
  2546. }
  2547. }
  2548. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2549. }
  2550. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2551. if offset > 0 {
  2552. offset = offset - 1
  2553. }
  2554. //if pagecount == 0 {
  2555. // pagecount = setting.AppSetting.PageSize
  2556. //}else if pagecount>setting.AppSetting.PageSize{
  2557. // pagecount = setting.AppSetting.PageSize
  2558. //}
  2559. if returntype == "list" {
  2560. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  2561. return queryData, err
  2562. } else if returntype == "listlist" {
  2563. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  2564. return queryData, err
  2565. } else {
  2566. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  2567. return queryData, err
  2568. }
  2569. }
  2570. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  2571. if offset > 0 {
  2572. offset = offset - 1
  2573. }
  2574. //if pagecount == 0 {
  2575. // pagecount = setting.AppSetting.PageSize
  2576. //}else if pagecount>setting.AppSetting.PageSize{
  2577. // pagecount = setting.AppSetting.PageSize
  2578. //}
  2579. if returntype == "list" {
  2580. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  2581. return queryData, err
  2582. } else if returntype == "listlist" {
  2583. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  2584. return queryData, err
  2585. } else {
  2586. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  2587. return queryData, err
  2588. }
  2589. }
  2590. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  2591. paramslist := strings.Split(Params, ",")
  2592. s_params := make([]interface{}, 0)
  2593. if isList {
  2594. for _, v_params := range thisParams["params"].([]interface{}) {
  2595. s_params = append(s_params, v_params)
  2596. }
  2597. } else {
  2598. paramslist := strings.Split(Params, ",")
  2599. if len(paramslist) > 0 && Params != "" {
  2600. for _, value := range paramslist {
  2601. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2602. }
  2603. }
  2604. }
  2605. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  2606. for _, queryData_ := range queryData {
  2607. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  2608. if isList {
  2609. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  2610. if queryData_[idname] != nil {
  2611. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  2612. }
  2613. } else {
  2614. if queryData_[idname] != nil {
  2615. thisParams[idname] = queryData_[idname].(interface{})
  2616. }
  2617. if len(paramslist) > 0 && Params != "" {
  2618. s_params = make([]interface{}, 0)
  2619. for _, value := range paramslist {
  2620. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2621. }
  2622. }
  2623. }
  2624. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  2625. } else {
  2626. }
  2627. }
  2628. return queryData, err
  2629. }
  2630. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2631. queryData, err := restful.ExecQuery(sqlstr, params)
  2632. return queryData, err
  2633. }
  2634. func execDataBySqlT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  2635. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  2636. return queryData, err
  2637. }
  2638. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2639. queryData, err := restful.ExecQuery(sqlstr, params)
  2640. return queryData, err
  2641. }
  2642. // @Summary 导入excel文件到数据库,excel第一行作为参数
  2643. // @Tags ImportExcel
  2644. // @Accept json
  2645. // @Produce json
  2646. // @Param file query string true "文件名"
  2647. // @Param name header string true "执行sql的名字"
  2648. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2649. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2650. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2651. // @Router /data/ImportExcel [POST]
  2652. func ImportExcel(c *gin.Context) {
  2653. appG := app.Gin{C: c}
  2654. inputdata := ""
  2655. c.Request.ParseForm()
  2656. //tempv1, _ := c.Get("jwt_username")
  2657. //fmt.Println("=======================",tempv1)
  2658. params := c.Request.Form
  2659. file, _, err := c.Request.FormFile("file")
  2660. if err != nil {
  2661. appG.Response(http.StatusOK, e.ERROR, err)
  2662. return
  2663. }
  2664. sqlnamestrs := params.Get("name")
  2665. sheetname := params.Get("sheetname")
  2666. pastureid := params.Get("pastureid")
  2667. //importParams := params.Get("importParams")
  2668. logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestrs)
  2669. //paramslist := strings.Split(importParams, ",")
  2670. //paramslist = append(paramslist, "jwt_username")
  2671. requiredParams := params.Get("requiredParams")
  2672. dateParams := params.Get("dateParams")
  2673. numParams := params.Get("numParams")
  2674. dateParamslist := strings.Split(dateParams, ",")
  2675. requiredParamslist := strings.Split(requiredParams, ",")
  2676. numParamslist := strings.Split(numParams, ",")
  2677. queryData := make([]interface{}, 0)
  2678. sqlslist := strings.Split(sqlnamestrs, ",")
  2679. xlsx1, err := excelize.OpenReader(file)
  2680. if err != nil {
  2681. fmt.Println(err)
  2682. return
  2683. }
  2684. ok := 0
  2685. error := 0
  2686. isdone := true
  2687. rows, _ := xlsx1.GetRows(sheetname)
  2688. if len(rows) > 10000 { // 限制1000条
  2689. rows = rows[:10000]
  2690. }
  2691. for i, row := range rows {
  2692. if i > 0 {
  2693. entry := make(map[string]interface{})
  2694. outentry := make(map[string]interface{})
  2695. for j, colCell := range rows[0] {
  2696. for k, colCell1 := range row {
  2697. if j == k {
  2698. outentry[colCell] = colCell1
  2699. entry[colCell] = colCell1
  2700. }
  2701. }
  2702. }
  2703. inputdata = ""
  2704. a := ""
  2705. for _, v := range dateParamslist {
  2706. if v == ""{
  2707. continue
  2708. }
  2709. if d, ok := outentry[strings.Trim(v, " ")]; ok {
  2710. dtime,err := TransfTime(d.(string))
  2711. if err != nil {
  2712. a = strings.Trim(v, " ")
  2713. break
  2714. }
  2715. outentry[strings.Trim(v, " ")] = dtime
  2716. }
  2717. }
  2718. if a != "" {
  2719. outentry["error_msg"] = a + " 格式错误"
  2720. error++
  2721. queryData = append(queryData, outentry)
  2722. continue
  2723. }
  2724. a = ""
  2725. for _, v := range requiredParamslist {
  2726. if v == ""{
  2727. continue
  2728. }
  2729. if d, ok := outentry[strings.Trim(v, " ")]; !ok || d == "" {
  2730. a = strings.Trim(v, " ")
  2731. break
  2732. }
  2733. }
  2734. if a != "" {
  2735. outentry["error_msg"] = a + " 未填或格式错误"
  2736. error++
  2737. queryData = append(queryData, outentry)
  2738. continue
  2739. }
  2740. a = ""
  2741. for _, v := range numParamslist {
  2742. if v == ""{
  2743. continue
  2744. }
  2745. d, ok := outentry[strings.Trim(v, " ")]
  2746. if !ok || d == "" {
  2747. outentry[strings.Trim(v, " ")] = 0
  2748. }else {
  2749. var fl float64
  2750. switch d.(type) {
  2751. case string :
  2752. fl,err = strconv.ParseFloat(d.(string),64)
  2753. }
  2754. if err != nil || fl<0 {
  2755. a = strings.Trim(v, " ")
  2756. break
  2757. }
  2758. }
  2759. }
  2760. if a != "" {
  2761. outentry["error_msg"] = a + " 为数值类型且不可为负数"
  2762. error++
  2763. queryData = append(queryData, outentry)
  2764. continue
  2765. }
  2766. tx := restful.Engine.NewSession()
  2767. err := tx.Begin()
  2768. for _, sqlnamestr := range sqlslist {
  2769. sql, paramslists := restful.GetSqlByNameDB(sqlnamestr)
  2770. paramslist := strings.Split(paramslists, ",")
  2771. if len(paramslist) > 0 {
  2772. s_params := make([]interface{}, 0)
  2773. for _, value := range paramslist {
  2774. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2775. if tempv, exists := c.Get("jwt_username"); exists {
  2776. s_params = append(s_params, tempv.(string))
  2777. } else {
  2778. s_params = append(s_params, "")
  2779. }
  2780. }else if strings.ToLower(strings.Trim(value, " ")) == "pastureid"{
  2781. s_params = append(s_params, pastureid)
  2782. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2783. ids, err := setting.SnowIds.NextId()
  2784. if err != nil {
  2785. ids = time.Now().UnixNano()
  2786. logging.Info("create SnowIds err", err)
  2787. }
  2788. s_params = append(s_params, ids)
  2789. }else {
  2790. if outentry[strings.Trim(value, " ")] == nil {
  2791. s_params = append(s_params,"")
  2792. }else{
  2793. s_params = append(s_params, outentry[strings.Trim(value, " ")])
  2794. }
  2795. }
  2796. }
  2797. //fmt.Println("================",s_params)
  2798. outentry["input"] = inputdata
  2799. returnmsgmap1, err := tx.SQL(sql, s_params...).Query().List()
  2800. if err != nil {
  2801. if tx != nil {
  2802. tx.Rollback()
  2803. }
  2804. if tx != nil {
  2805. tx.Close()
  2806. }
  2807. msg := geterrmsg(err.Error())
  2808. outentry["error_msg"] = msg
  2809. error++
  2810. queryData = append(queryData, outentry)
  2811. isdone = true
  2812. break
  2813. } else{
  2814. if len(returnmsgmap1) > 0 {
  2815. if va,ok :=returnmsgmap1[0]["msg"] ; ok && va != "" {
  2816. if tx != nil {
  2817. tx.Rollback()
  2818. }
  2819. if tx != nil {
  2820. tx.Close()
  2821. }
  2822. msg := geterrmsg(va.(string))
  2823. outentry["error_msg"] = msg
  2824. error++
  2825. queryData = append(queryData, outentry)
  2826. isdone = true
  2827. break
  2828. }
  2829. }
  2830. isdone = false
  2831. }
  2832. }
  2833. }
  2834. switch {
  2835. case err != nil:
  2836. if tx != nil {
  2837. tx.Rollback()
  2838. }
  2839. default:
  2840. if tx != nil {
  2841. err = tx.Commit()
  2842. }
  2843. }
  2844. if tx != nil {
  2845. tx.Close()
  2846. }
  2847. }
  2848. if !isdone{
  2849. ok++
  2850. isdone = true
  2851. }
  2852. }
  2853. data := make(map[string]interface{})
  2854. data["result"] = queryData
  2855. data["success"] = ok
  2856. data["err_count"] = error
  2857. appG.Response(http.StatusOK, e.SUCCESS, data)
  2858. }
  2859. func TransfTime(timestr string) (string,error) {
  2860. if timestr == "" {
  2861. return time.Now().Format("2006-01-02"),nil
  2862. }
  2863. days, err := strconv.Atoi(timestr)
  2864. if err != nil {
  2865. if len(timestr) == 8 && strings.Count(timestr, "-") == 2 {
  2866. return "20" + timestr[6:] + "-" + timestr[:5],nil
  2867. }
  2868. _,err :=time.Parse("2006-01-02",timestr)
  2869. if err != nil{
  2870. t,err :=time.Parse("2006/01/02",timestr)
  2871. if err != nil{
  2872. t,err :=time.Parse("2006.01.02",timestr)
  2873. if err != nil {
  2874. return "",err
  2875. }else {
  2876. return t.Format("2006-01-02"),nil
  2877. }
  2878. }else {
  2879. return t.Format("2006-01-02"),nil
  2880. }
  2881. }else {
  2882. return timestr,nil
  2883. }
  2884. }
  2885. times, _ := time.Parse("2006-01-02", "1899-12-30")
  2886. times = times.AddDate(0, 0, days)
  2887. return times.Format("2006-01-02"),nil
  2888. }
  2889. func Createchart(c *gin.Context) {
  2890. appG := app.Gin{C: c}
  2891. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2892. fsion := gofasion.NewFasion(string(dataByte))
  2893. chart_name := fsion.Get("chart_name").ValueStr()
  2894. desc := fsion.Get("desc").ValueStr()
  2895. pastureid := fsion.Get("pastureid").ValueStr()
  2896. source_id := fsion.Get("source_id").ValueStr()
  2897. emp := fsion.Get("emp").ValueStr()
  2898. content := fsion.Get("content").Json()
  2899. //fmt.Println("=============",parammaW.Json())
  2900. var mapResult map[string]interface{}
  2901. err1 := json.Unmarshal([]byte(content), &mapResult)
  2902. if err1 != nil {
  2903. fmt.Println("JsonToMapDemo err: ", err1)
  2904. }
  2905. //fmt.Println("---------",mapResult)
  2906. tx := restful.Engine.NewSession()
  2907. err := tx.Begin()
  2908. defer func() {
  2909. switch {
  2910. case err != nil:
  2911. logging.Error("tx.Begin 事务启动失败__error:", err)
  2912. if tx != nil {
  2913. tx.Rollback()
  2914. }
  2915. default:
  2916. if tx != nil {
  2917. err = tx.Commit()
  2918. }
  2919. }
  2920. if tx != nil {
  2921. tx.Close()
  2922. }
  2923. }()
  2924. ids, err := setting.SnowIds.NextId()
  2925. if err != nil {
  2926. ids = time.Now().UnixNano()
  2927. logging.Info("create SnowIds err", err)
  2928. }
  2929. exec, err := tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  2930. ids, pastureid, chart_name, 0, emp, desc, source_id)
  2931. if err != nil {
  2932. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2933. } else {
  2934. appG.Response(http.StatusOK, e.SUCCESS, exec)
  2935. }
  2936. }
  2937. func Updatedashboard(c *gin.Context) {
  2938. appG := app.Gin{C: c}
  2939. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2940. fsion := gofasion.NewFasion(string(dataByte))
  2941. dashboard_id := fsion.Get("dashboard_id").ValueStr()
  2942. pastureid := fsion.Get("pastureid").ValueStr()
  2943. content := fsion.Get("content").Json()
  2944. //fmt.Println("=============",parammaW.Json())
  2945. var mapResult map[string]interface{}
  2946. err1 := json.Unmarshal([]byte(content), &mapResult)
  2947. if err1 != nil {
  2948. fmt.Println("JsonToMapDemo err: ", err1)
  2949. }
  2950. layout := mapResult["layout"]
  2951. fmt.Println("---------", layout)
  2952. tx := restful.Engine.NewSession()
  2953. err := tx.Begin()
  2954. defer func() {
  2955. switch {
  2956. case err != nil:
  2957. logging.Error("tx.Begin 事务启动失败__error:", err)
  2958. if tx != nil {
  2959. tx.Rollback()
  2960. }
  2961. default:
  2962. if tx != nil {
  2963. err = tx.Commit()
  2964. }
  2965. }
  2966. if tx != nil {
  2967. tx.Close()
  2968. }
  2969. }()
  2970. //fmt.Println("sql========\n",sqlnamestr,sql)
  2971. exec, err := tx.SQL("select content from dashboard where pastureid=? and id=?", pastureid, dashboard_id).Query().List()
  2972. if err != nil {
  2973. println("2", err.Error())
  2974. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2975. } else {
  2976. if exec != nil {
  2977. if exec[0]["content"] == nil {
  2978. mapR := make(map[string]interface{})
  2979. mapR["layout"] = layout
  2980. mjson, _ := json.Marshal(mapR)
  2981. mString := string(mjson)
  2982. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2983. if err3 != nil {
  2984. println("3", err3.Error())
  2985. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  2986. } else {
  2987. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  2988. }
  2989. } else {
  2990. var mapR map[string]interface{}
  2991. err2 := json.Unmarshal([]byte(exec[0]["content"].(string)), &mapR)
  2992. if err1 != nil {
  2993. fmt.Println("JsonToMapDemo err: ", err2)
  2994. }
  2995. mapR["layout"] = layout
  2996. mjson, _ := json.Marshal(mapR)
  2997. mString := string(mjson)
  2998. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2999. if err3 != nil {
  3000. println("3", err3.Error())
  3001. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  3002. } else {
  3003. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  3004. }
  3005. }
  3006. } else {
  3007. appG.Response(http.StatusOK, e.ERROR, "未找到对应的仪表盘")
  3008. }
  3009. }
  3010. }
  3011. func ExecSQL(c *gin.Context) {
  3012. appG := app.Gin{C: c}
  3013. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3014. fsion := gofasion.NewFasion(string(dataByte))
  3015. content := fsion.Get("content").Json()
  3016. pastureid := fsion.Get("pastureid").ValueStr()
  3017. //pastureid := fsion.Get("pastureid").ValueStr()
  3018. //println(pastureid,"==================")
  3019. //fmt.Println("=============",parammaW.Json())
  3020. var mapResult map[string]interface{}
  3021. err1 := json.Unmarshal([]byte(content), &mapResult)
  3022. if err1 != nil {
  3023. fmt.Println("JsonToMapDemo err: ", err1)
  3024. }
  3025. //fmt.Println("---------",mapResult)
  3026. sqlstr := "select "
  3027. groupstr := " GROUP BY "
  3028. for _, value := range mapResult["selectedDimension"].([]interface{}) {
  3029. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3030. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3031. }
  3032. for _, value := range mapResult["selectedCalcul"].([]interface{}) {
  3033. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  3034. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3035. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3036. } else {
  3037. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  3038. sqlstr = sqlstr + tem + ","
  3039. }
  3040. }
  3041. if _, ok := mapResult["selectedCalcul2"]; ok {
  3042. for _, value := range mapResult["selectedCalcul2"].([]interface{}) {
  3043. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  3044. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3045. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3046. } else {
  3047. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  3048. sqlstr = sqlstr + tem + ","
  3049. }
  3050. }
  3051. }
  3052. //
  3053. sqlstr = sqlstr[0 : len(sqlstr)-1]
  3054. sqlstr = sqlstr + " from " + mapResult["dataSrc"].(string)
  3055. //if len(mapResult["filters"].([]interface {}))>0{
  3056. sqlstr = sqlstr + " where pastureid=" + pastureid
  3057. //}
  3058. //
  3059. if len(mapResult["filters"].([]interface{})) > 0 {
  3060. sqlstr = sqlstr + " and "
  3061. }
  3062. println(len(mapResult["filters"].([]interface{})),sqlstr)
  3063. for _, value := range mapResult["filters"].([]interface{}) {
  3064. switch value.(map[string]interface{})["filterOperator"].(string) {
  3065. case ">", "=", "<", ">=", "<=", "!=", "like":
  3066. 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 "
  3067. case "IN":
  3068. sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + "("
  3069. for _, value1 := range value.(map[string]interface{})["value"].(map[string]interface{})["arrValue"].([]interface{}) {
  3070. sqlstr = sqlstr + value1.(string) + ","
  3071. }
  3072. sqlstr = sqlstr[0 : len(sqlstr)-1]
  3073. sqlstr = sqlstr + ")" + " AND "
  3074. case "BETWEEN":
  3075. 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 "
  3076. }
  3077. }
  3078. if len(mapResult["filters"].([]interface{})) > 0 {
  3079. sqlstr = sqlstr[0 : len(sqlstr)-4]
  3080. }
  3081. //
  3082. groupstr = groupstr[0 : len(groupstr)-1]
  3083. sqlstr = sqlstr + groupstr + " "
  3084. if len(mapResult["orderByStrs"].([]interface{})) > 0 {
  3085. sqlstr = sqlstr + " order by "
  3086. }
  3087. //
  3088. for key, value := range mapResult["orderByStrs"].([]interface{}) {
  3089. if key == len(mapResult["orderByStrs"].([]interface{}))-1 {
  3090. sqlstr = sqlstr + value.(string) + " "
  3091. } else {
  3092. sqlstr = sqlstr + value.(string) + ","
  3093. }
  3094. }
  3095. //
  3096. sqlstr = sqlstr + " limit " + strconv.Itoa(int(mapResult["limit"].(float64)))
  3097. //fmt.Println("====++---------",sqlstr)
  3098. tx := restful.Engine.NewSession()
  3099. err := tx.Begin()
  3100. defer func() {
  3101. switch {
  3102. case err != nil:
  3103. logging.Error("tx.Begin 事务启动失败__error:", err)
  3104. if tx != nil {
  3105. tx.Rollback()
  3106. }
  3107. default:
  3108. if tx != nil {
  3109. err = tx.Commit()
  3110. }
  3111. }
  3112. if tx != nil {
  3113. tx.Close()
  3114. }
  3115. }()
  3116. //fmt.Println("sql========\n",sqlnamestr,sql)
  3117. //ids,err := setting.SnowIds.NextId()
  3118. //if err !=nil{
  3119. // ids = time.Now().UnixNano()
  3120. // logging.Info("create SnowIds err",err)
  3121. //}
  3122. // exec,err :=tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  3123. //ids,pastureid,chart_name,0,emp,desc,source_id)
  3124. exec, err := tx.SQL(sqlstr).Query().List()
  3125. if err != nil {
  3126. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3127. } else {
  3128. appG.Response(http.StatusOK, e.SUCCESS, exec)
  3129. }
  3130. }
  3131. func Dashboards(c *gin.Context) {
  3132. appG := app.Gin{C: c}
  3133. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3134. fsion := gofasion.NewFasion(string(dataByte))
  3135. sqlnamestr := fsion.Get("name").ValueStr()
  3136. //sqlnamestr1 := fsion.Get("name1").ValueStr()
  3137. offset := fsion.Get("offset").ValueDefaultInt(0)
  3138. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  3139. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  3140. month := fsion.Get("month").ValueDefaultStr("map")
  3141. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  3142. tx := restful.Engine.NewSession()
  3143. err := tx.Begin()
  3144. defer func() {
  3145. switch {
  3146. case err != nil:
  3147. fmt.Println("__error:", err)
  3148. if tx != nil {
  3149. tx.Rollback()
  3150. }
  3151. default:
  3152. if tx != nil {
  3153. err = tx.Commit()
  3154. }
  3155. }
  3156. if tx != nil {
  3157. tx.Close()
  3158. }
  3159. }()
  3160. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  3161. //sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  3162. if sql != "" {
  3163. s_params := make([]interface{}, 0)
  3164. if fsion.HasKey("parammaps") {
  3165. parammaps := fsion.Get("parammaps")
  3166. paramslist := strings.Split(p, ",")
  3167. if len(paramslist) > 0 && p != "" {
  3168. for _, value := range paramslist {
  3169. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  3170. if tempv, exists := c.Get("jwt_username"); exists {
  3171. s_params = append(s_params, tempv.(string))
  3172. } else {
  3173. s_params = append(s_params, "")
  3174. }
  3175. } else {
  3176. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  3177. }
  3178. }
  3179. }
  3180. }
  3181. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  3182. if err != nil {
  3183. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3184. } else {
  3185. //paramslist1 := strings.Split(p1, ",")
  3186. dids := make([]interface{}, 0)
  3187. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  3188. var mapResult map[string]interface{}
  3189. dids = append(dids, value["dashboard_id"])
  3190. if value["content"] != nil {
  3191. err1 := json.Unmarshal([]byte(value["content"].(string)), &mapResult)
  3192. if err1 != nil {
  3193. println("JsonToMapDemo1 err: ", err1)
  3194. }
  3195. //fmt.Printf("%v+\n",value["content"].(string))
  3196. temap := make(map[string]interface{})
  3197. tem := mapResult["layout"].([]interface{})
  3198. if v, ok := value["chartids"]; ok {
  3199. chartids := strings.Split(v.(string), ",")
  3200. for _, va := range chartids {
  3201. a := false
  3202. for _, v2 := range tem {
  3203. if v2.(map[string]interface{})["id"].(string) == va {
  3204. a = true
  3205. //println("va1======",va)
  3206. }
  3207. }
  3208. if !a {
  3209. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  3210. va + "\",\"id\":\"" + va +
  3211. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":100,\"yOffSet\":18}"
  3212. var mapTem map[string]interface{}
  3213. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  3214. if err1 != nil {
  3215. println("JsonToMapDemo err: ", err1)
  3216. }
  3217. tem = append(tem, mapTem)
  3218. }
  3219. }
  3220. temap["layout"] = tem
  3221. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  3222. }
  3223. } else {
  3224. temap := make(map[string]interface{})
  3225. tem := make([]interface{}, 0)
  3226. if v, ok := value["chartids"]; ok {
  3227. chartids := strings.Split(v.(string), ",")
  3228. for _, va := range chartids {
  3229. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  3230. va + "\",\"id\":\"" + va +
  3231. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":0,\"yOffSet\":18}"
  3232. var mapTem map[string]interface{}
  3233. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  3234. if err1 != nil {
  3235. println("JsonToMapDemo err: ", err1)
  3236. }
  3237. tem = append(tem, mapTem)
  3238. }
  3239. temap["layout"] = tem
  3240. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  3241. }
  3242. }
  3243. }
  3244. queryData.(map[string]interface{})["order"] = dids
  3245. queryData.(map[string]interface{})["dashboards"] = queryData.(map[string]interface{})["list"]
  3246. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  3247. }
  3248. } else {
  3249. appG.Response(http.StatusOK, e.SUCCESS, nil)
  3250. }
  3251. }
  3252. //type Nur struct {
  3253. // Avgmonthage float64 `json : "avgmonthage"` //月龄
  3254. // Bw float64 `json : "bw"` //体重
  3255. // Dayw float64 `json : "dayw"` //日增重
  3256. // Fetal float64 `json : "fetal"` //胎次
  3257. // Avgdim float64 `json : "avgdim"` //平均泌乳天数
  3258. // Dayspre float64 `json : "dayspre"` //怀孕天数
  3259. // Product float64 `json : "product"` //产奶量
  3260. // Fat float64 `json : "fat"` //乳脂率
  3261. // Pro float64 `json : "pro"` //乳蛋白率
  3262. // Lactose float64 `json : "lactose"` //乳糖率
  3263. // Source string `json : "source"` //来源
  3264. // Tem float64 `json : "tem"` //温度
  3265. // Cowsum float64 `json : "cowsum"` //牛头数
  3266. // Cowclass float64 `json : "cowclass"` //牲畜类别
  3267. // Cowclassid float64 `json : "cowclassid"` //牲畜类别id
  3268. // Bigcowclass string `json : "bigcowclass"` //牲畜大类
  3269. //}
  3270. type Nur struct {
  3271. Avgmonthage float64 //月龄
  3272. Bw float64 //体重
  3273. Dayw float64 //日增重
  3274. Fetal float64 //胎次
  3275. Avgdim float64 //平均泌乳天数
  3276. Dayspre float64 //怀孕天数
  3277. Product float64 //产奶量
  3278. Fat float64 //乳脂率
  3279. Pro float64 //乳蛋白率
  3280. Lactose float64 //乳糖率
  3281. Source string //来源
  3282. Tem float64 //温度
  3283. Cowsum float64 //牛头数
  3284. Cowclass string //牲畜类别
  3285. Cowclassid float64 //牲畜类别id
  3286. Bigcowclass string //牲畜大类
  3287. }
  3288. func GETNurJudge(c *gin.Context) {
  3289. appG := app.Gin{C: c}
  3290. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3291. fsion := gofasion.NewFasion(string(dataByte))
  3292. nurstr := fsion.Get("parammaps").Json()
  3293. parammaps := make(map[string]interface{})
  3294. nur := Nur{}
  3295. _ = json.Unmarshal([]byte(nurstr), &nur) //需要json 的类型相同
  3296. _ = json.Unmarshal([]byte(nurstr), &parammaps) //需要json 的类型相同
  3297. tx := restful.Engine.NewSession()
  3298. err := tx.Begin()
  3299. defer func() {
  3300. switch {
  3301. case err != nil:
  3302. fmt.Println("__error:", err)
  3303. if tx != nil {
  3304. tx.Rollback()
  3305. }
  3306. default:
  3307. if tx != nil {
  3308. err = tx.Commit()
  3309. }
  3310. }
  3311. if tx != nil {
  3312. tx.Close()
  3313. }
  3314. }()
  3315. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? "+
  3316. "and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  3317. resnur, err := CalNUR(formulas, &nur)
  3318. if err != nil {
  3319. logging.Error("GETNurJudge err: ", err)
  3320. msg := geterrmsg(err.Error())
  3321. appG.Response(http.StatusOK, e.ERROR, msg)
  3322. }
  3323. resf, err := FeedproviderV2(parammaps, tx)
  3324. reslist := make([]map[string]interface{}, 21)
  3325. if len(resf) > 1 {
  3326. for key, _ := range resf[0] {
  3327. //println(key)
  3328. str, sort := switchname(key)
  3329. res := make(map[string]interface{}, 0)
  3330. res["standard"] = str
  3331. res["cowneed"] = resnur[key] // 奶牛需要
  3332. res["ftpro"] = resf[0][key] // 配方提供
  3333. res["hprov"] = resf[1][key] // tmr料
  3334. res["sprov"] = resf[2][key] //采食量
  3335. reslist[sort] = res
  3336. }
  3337. }
  3338. if err != nil {
  3339. logging.Error("GETNurJudge err: ", err)
  3340. msg := geterrmsg(err.Error())
  3341. appG.Response(http.StatusOK, e.ERROR, msg)
  3342. } else {
  3343. appG.Response(http.StatusOK, e.SUCCESS, reslist)
  3344. }
  3345. }
  3346. func GETNurJudgeRport(c *gin.Context) {
  3347. appG := app.Gin{C: c}
  3348. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3349. fsion := gofasion.NewFasion(string(dataByte))
  3350. nurstr := fsion.Get("parammaps").Json()
  3351. parammaps := make(map[string]interface{})
  3352. _ = json.Unmarshal([]byte(nurstr), &parammaps)
  3353. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  3354. if err != nil {
  3355. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3356. return
  3357. }
  3358. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  3359. if err != nil {
  3360. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3361. return
  3362. }
  3363. if stopTime.Sub(startTime) > 31*24*time.Hour {
  3364. appG.Response(http.StatusOK, e.ERROR, errors.New("日期范围过大"))
  3365. return
  3366. }
  3367. tx := restful.Engine.NewSession()
  3368. err = tx.Begin()
  3369. defer func() {
  3370. switch {
  3371. case err != nil:
  3372. fmt.Println("__error:", err)
  3373. if tx != nil {
  3374. tx.Rollback()
  3375. }
  3376. default:
  3377. if tx != nil {
  3378. err = tx.Commit()
  3379. }
  3380. }
  3381. if tx != nil {
  3382. tx.Close()
  3383. }
  3384. }()
  3385. //CulNurReportCow("getbmdetaillist",parammaps,tx)
  3386. res, err := CulNurReportFT(parammaps, tx)
  3387. if err != nil {
  3388. logging.Error("GETNurJudgeRport err: ", err)
  3389. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  3390. msg := geterrmsg(err.Error())
  3391. appG.Response(http.StatusOK, e.ERROR, msg)
  3392. } else {
  3393. appG.Response(http.StatusOK, e.SUCCESS, res)
  3394. }
  3395. }
  3396. func CulNurReportCow(sqlname string, parammaps map[string]interface{}, tx *xorm.Session) (map[string]interface{}, error) {
  3397. var nur Nur
  3398. sql, p := restful.GetSqlByNameDBT(sqlname, tx)
  3399. s_params := make([]interface{}, 0)
  3400. paramslist := strings.Split(p, ",")
  3401. if len(paramslist) > 0 && p != "" {
  3402. for _, value := range paramslist {
  3403. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3404. }
  3405. }
  3406. res, err := tx.SQL(sql, s_params...).Query().List()
  3407. if len(res) > 0 {
  3408. resstr, _ := json.Marshal(res[0])
  3409. err = json.Unmarshal(resstr, &nur)
  3410. if err != nil {
  3411. return nil, err
  3412. }
  3413. }
  3414. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  3415. //println(len(formulas))
  3416. resnur, err := CalNUR(formulas, &nur)
  3417. if err != nil {
  3418. return nil, err
  3419. }
  3420. return resnur, err
  3421. }
  3422. func CulNurReportFT(parammaps map[string]interface{}, tx *xorm.Session) (res []map[string]interface{}, err error) {
  3423. barids, err := tx.SQL("SELECT trim(fp.barid) barid,fp.ccount,DATE_FORMAT(fp.date, '%Y-%m-%d')date,fp.barname FROM feedpdate fp WHERE fp.pastureid = ? AND fp.date BETWEEN ? AND ? AND fp.`ftid` = ? GROUP BY fp.`barid` order by fp.barid",
  3424. parammaps["pastureid"], parammaps["startTime"], parammaps["stopTime"], parammaps["ftid"]).Query().List()
  3425. //println(parammaps["pastureid"].(string), parammaps["startTime"].(string), parammaps["stopTime"].(string), parammaps["ftid"].(string))
  3426. if err != nil {
  3427. return nil, err
  3428. }
  3429. bartem := ""
  3430. //println("barids", len(barids))
  3431. dates := make([]string, 0)
  3432. for key, value := range barids {
  3433. if key == 0 {
  3434. bartem = value["barid"].(string)
  3435. parammaps["barid"] = value["barid"]
  3436. }
  3437. if bartem != value["barid"].(string) || key == len(barids)-1 || key == 0{
  3438. if key == len(barids)-1 || key == 0{
  3439. dates = append(dates, value["date"].(string))
  3440. }
  3441. parammaps["date"] = value["date"]
  3442. parammaps["barid"] = value["barid"]
  3443. barres, err := FeedproviderV3(parammaps, dates, tx)
  3444. if err != nil {
  3445. return nil, err
  3446. }
  3447. resnur, err := CulNurReportCow("getbmdetaillist", parammaps, tx)
  3448. if err != nil {
  3449. return nil, err
  3450. }
  3451. restem := make(map[string]interface{}, 0)
  3452. for k, _ := range barres[0] {
  3453. //println("wwwwwww",k)
  3454. restem["barid"] = value["barid"].(string)
  3455. restem["barname"] = value["barname"].(string)
  3456. restem["ccount"] = value["ccount"].(int64)
  3457. restem[k+"-nur"] = resnur[k]
  3458. restem[k+"-FT"] = barres[0][k]
  3459. restem[k+"-H"] = barres[1][k]
  3460. restem[k+"-S"] = barres[2][k]
  3461. }
  3462. res = append(res, restem)
  3463. if err != nil {
  3464. return nil, err
  3465. }
  3466. bartem = value["barid"].(string)
  3467. dates = make([]string, 0)
  3468. parammaps["barid"] = value["barid"]
  3469. }
  3470. dates = append(dates, value["date"].(string))
  3471. }
  3472. return res, err
  3473. }
  3474. func switchname(str string) (string, int) {
  3475. tstr := ""
  3476. sort := 0
  3477. switch str {
  3478. case "dry":
  3479. tstr = "干物质(kg)"
  3480. sort = 0
  3481. case "nm":
  3482. tstr = "产奶净能(MJ)"
  3483. sort = 1
  3484. case "nuint":
  3485. tstr = "奶牛能量单位(NND)"
  3486. sort = 2
  3487. case "cp":
  3488. tstr = "粗蛋白(g)"
  3489. sort = 3
  3490. case "ca":
  3491. tstr = " 钙(g)"
  3492. sort = 4
  3493. case "p":
  3494. tstr = "磷(g)"
  3495. sort = 5
  3496. case "nmd":
  3497. tstr = "产奶净能(MCal/DM)"
  3498. sort = 6
  3499. case "cpd":
  3500. tstr = "粗蛋白(%DM)"
  3501. sort = 7
  3502. case "fat":
  3503. tstr = "脂肪"
  3504. sort = 8
  3505. case "starch":
  3506. tstr = "淀粉"
  3507. sort = 9
  3508. case "ndf":
  3509. tstr = "NDF(%DM)"
  3510. sort = 10
  3511. case "cndf":
  3512. tstr = "粗料中的NDF(%DM)"
  3513. sort = 11
  3514. case "adf":
  3515. tstr = "ADF(%DM)"
  3516. sort = 12
  3517. case "cad":
  3518. tstr = " 钙(%DM)"
  3519. sort = 13
  3520. case "pd":
  3521. tstr = "磷(%DM)"
  3522. sort = 14
  3523. case "trans35f":
  3524. tstr = "(3.5)饲料转化效率(kg/DM)"
  3525. sort = 15
  3526. case "trans4f":
  3527. tstr = "(4.0)饲料转化效率(kg/DM)"
  3528. sort = 16
  3529. case "jcrate":
  3530. tstr = "精粗比"
  3531. sort = 17
  3532. case "uprice":
  3533. tstr = "日粮成本(元)"
  3534. sort = 18
  3535. case "upriced":
  3536. tstr = "干物质成本(元/公斤)"
  3537. sort = 19
  3538. case "kprice":
  3539. tstr = "公斤奶成本(元) "
  3540. sort = 20
  3541. default:
  3542. tstr = str
  3543. sort = 21
  3544. }
  3545. return tstr, sort
  3546. }
  3547. func FeedproviderV2(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]float64, error) {
  3548. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  3549. if err != nil {
  3550. return nil, err
  3551. }
  3552. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  3553. if err != nil {
  3554. return nil, err
  3555. }
  3556. if stopTime.Sub(startTime) > 31*24*time.Hour {
  3557. return nil, errors.New("日期范围过大")
  3558. }
  3559. dates := make([]string, 0)
  3560. for stopTime.Sub(startTime) >= 0 {
  3561. dates = append(dates, startTime.Format("2006-01-02"))
  3562. startTime = startTime.Add(24 * time.Hour)
  3563. }
  3564. resall, err := FeedproviderV3(parammaps, dates, tx)
  3565. return resall, err
  3566. }
  3567. func FeedproviderV3(parammaps map[string]interface{}, dates []string, tx *xorm.Session) ([]map[string]float64, error) {
  3568. res := make(map[string]float64, 0)
  3569. resH := make(map[string]float64, 0)
  3570. resS := make(map[string]float64, 0)
  3571. i := 0
  3572. for _, date := range dates {
  3573. i++
  3574. //println(date)
  3575. parammaps["date"] = date
  3576. restem, err := Feedprovider(parammaps, tx)
  3577. if err != nil {
  3578. return nil, err
  3579. }
  3580. restemSH, err := SHprovider(parammaps, tx)
  3581. if err != nil {
  3582. return nil, err
  3583. }
  3584. var h, s float64
  3585. if len(restemSH) > 1 {
  3586. switch restemSH[0]["actualweightminus"].(type) {
  3587. case string:
  3588. h, _ = strconv.ParseFloat(restemSH[0]["actualweightminus"].(string), 64)
  3589. }
  3590. switch restemSH[1]["actualweightminus"].(type) {
  3591. case string:
  3592. s, _ = strconv.ParseFloat(restemSH[1]["actualweightminus"].(string), 64)
  3593. }
  3594. }
  3595. if len(restem) > 0 {
  3596. b, _ := strconv.ParseFloat(restem[0]["jcrate"].(string), 64)
  3597. res["jcrate"] = res["jcrate"] + b
  3598. resH["jcrate"] = resH["jcrate"] + h*b
  3599. resS["jcrate"] = resS["jcrate"] + s*b
  3600. b, _ = strconv.ParseFloat(restem[0]["cpd"].(string), 64)
  3601. res["cpd"] = res["cpd"] + b
  3602. resH["cpd"] = resH["cpd"] + h*b
  3603. resS["cpd"] = resS["cpd"] + s*b
  3604. b, _ = strconv.ParseFloat(restem[0]["adf"].(string), 64)
  3605. res["adf"] = res["adf"] + b
  3606. resH["adf"] = resH["adf"] + h*b
  3607. resS["adf"] = resS["adf"] + s*b
  3608. b, _ = strconv.ParseFloat(restem[0]["ca"].(string), 64)
  3609. res["ca"] = res["ca"] + b
  3610. resH["ca"] = resH["ca"] + h*b
  3611. resS["ca"] = resS["ca"] + s*b
  3612. b, _ = strconv.ParseFloat(restem[0]["cad"].(string), 64)
  3613. res["cad"] = res["cad"] + b
  3614. resH["cad"] = resH["cad"] + h*b
  3615. resS["cad"] = resS["cad"] + s*b
  3616. //b,_ =strconv.ParseFloat(restem[0]["cdry"].(string),64)
  3617. //res["cdry"] = res["cdry"]+b
  3618. //
  3619. //resH["cdry"] = resH["cdry"]+ h*b
  3620. //resS["cdry"] = resS["cdry"]+ s*b
  3621. b, _ = strconv.ParseFloat(restem[0]["cndf"].(string), 64)
  3622. res["cndf"] = res["cndf"] + b
  3623. resH["cndf"] = resH["cndf"] + h*b
  3624. resS["cndf"] = resS["cndf"] + s*b
  3625. b, _ = strconv.ParseFloat(restem[0]["cp"].(string), 64)
  3626. res["cp"] = res["cp"] + b
  3627. resH["cp"] = resH["cp"] + h*b
  3628. resS["cp"] = resS["cp"] + s*b
  3629. b, _ = strconv.ParseFloat(restem[0]["dry"].(string), 64)
  3630. res["dry"] = res["dry"] + b
  3631. resH["dry"] = resH["dry"] + h*b
  3632. resS["dry"] = resS["dry"] + s*b
  3633. b, _ = strconv.ParseFloat(restem[0]["fat"].(string), 64)
  3634. res["fat"] = res["fat"] + b
  3635. resH["fat"] = resH["fat"] + h*b
  3636. resS["fat"] = resS["fat"] + s*b
  3637. b, _ = strconv.ParseFloat(restem[0]["kprice"].(string), 64)
  3638. res["kprice"] = res["kprice"] + b
  3639. resH["kprice"] = resH["kprice"] + h*b
  3640. resS["kprice"] = resS["kprice"] + s*b
  3641. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  3642. res["nm"] = res["nm"] + b
  3643. resH["nm"] = resH["nm"] + h*b
  3644. resS["nm"] = resS["nm"] + s*b
  3645. b, _ = strconv.ParseFloat(restem[0]["ndf"].(string), 64)
  3646. res["ndf"] = res["ndf"] + b
  3647. resH["ndf"] = resH["ndf"] + h*b
  3648. resS["ndf"] = resS["ndf"] + s*b
  3649. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  3650. res["nm"] = res["nm"] + b
  3651. resH["nm"] = resH["nm"] + h*b
  3652. resS["nm"] = resS["nm"] + s*b
  3653. b, _ = strconv.ParseFloat(restem[0]["nmd"].(string), 64)
  3654. res["nmd"] = res["nmd"] + b
  3655. resH["nmd"] = resH["nmd"] + h*b
  3656. resS["nmd"] = resS["nmd"] + s*b
  3657. b, _ = strconv.ParseFloat(restem[0]["nuint"].(string), 64)
  3658. res["nuint"] = res["nuint"] + b
  3659. resH["nuint"] = resH["nuint"] + h*b
  3660. resS["nuint"] = resS["nuint"] + s*b
  3661. b, _ = strconv.ParseFloat(restem[0]["p"].(string), 64)
  3662. res["p"] = res["p"] + b
  3663. resH["p"] = resH["p"] + h*b
  3664. resS["p"] = resS["p"] + s*b
  3665. b, _ = strconv.ParseFloat(restem[0]["pd"].(string), 64)
  3666. res["pd"] = res["pd"] + b
  3667. resH["pd"] = resH["pd"] + h*b
  3668. resS["pd"] = resS["pd"] + s*b
  3669. b, _ = strconv.ParseFloat(restem[0]["starch"].(string), 64)
  3670. res["starch"] = res["starch"] + b
  3671. resH["starch"] = resH["starch"] + h*b
  3672. resS["starch"] = resS["starch"] + s*b
  3673. b, _ = strconv.ParseFloat(restem[0]["trans4f"].(string), 64)
  3674. res["trans4f"] = res["trans4f"] + b
  3675. resH["trans4f"] = resH["trans4f"] + h*b
  3676. resS["trans4f"] = resS["trans4f"] + s*b
  3677. b, _ = strconv.ParseFloat(restem[0]["trans35f"].(string), 64)
  3678. res["trans35f"] = res["trans35f"] + b
  3679. resH["trans35f"] = resH["trans35f"] + h*b
  3680. resS["trans35f"] = resS["trans35f"] + s*b
  3681. b, _ = strconv.ParseFloat(restem[0]["uprice"].(string), 64)
  3682. res["uprice"] = res["uprice"] + b
  3683. resH["uprice"] = resH["uprice"] + h*b
  3684. resS["uprice"] = resS["uprice"] + s*b
  3685. b, _ = strconv.ParseFloat(restem[0]["upriced"].(string), 64)
  3686. res["upriced"] = res["upriced"] + b
  3687. resH["upriced"] = resH["upriced"] + h*b
  3688. resS["upriced"] = resS["upriced"] + s*b
  3689. }
  3690. }
  3691. for key, _ := range res {
  3692. resf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", res[key]/float64(i)), 64)
  3693. resHf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resH[key]/float64(i)), 64)
  3694. resSf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resS[key]/float64(i)), 64)
  3695. res[key] = resf
  3696. resH[key] = resHf
  3697. resS[key] = resSf
  3698. }
  3699. resAll := make([]map[string]float64, 0)
  3700. resAll = append(resAll, res, resH, resS)
  3701. return resAll, nil
  3702. }
  3703. func Feedprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  3704. sql, p := restful.GetSqlByNameDBT(parammaps["name"].(string), tx)
  3705. s_params := make([]interface{}, 0)
  3706. paramslist := strings.Split(p, ",")
  3707. if len(paramslist) > 0 && p != "" {
  3708. for _, value := range paramslist {
  3709. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3710. }
  3711. }
  3712. res, err := tx.SQL(sql, s_params...).Query().List()
  3713. return res, err
  3714. }
  3715. func SHprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  3716. sql, p := restful.GetSqlByNameDBT(parammaps["name1"].(string), tx)
  3717. //println(parammaps["name1"].(string),sql)
  3718. s_params := make([]interface{}, 0)
  3719. paramslist := strings.Split(p, ",")
  3720. if len(paramslist) > 0 && p != "" {
  3721. for _, value := range paramslist {
  3722. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3723. }
  3724. }
  3725. res, err := tx.SQL(sql, s_params...).Query().List()
  3726. return res, err
  3727. }
  3728. func CalNUR(formulas []map[string]interface{}, nur *Nur) (map[string]interface{}, error) {
  3729. //formulaADF := make([]map[string]interface{},1)//ADF
  3730. //formulaNDF := make([]map[string]interface{},1)//NDF
  3731. //formulaNFC := make([]map[string]interface{},1)//NFC
  3732. //formulacprotedm := make([]map[string]interface{},1)//小肠可消化粗蛋白
  3733. //formuladry := make([]map[string]interface{},1) //干物质
  3734. //formulastarch := make([]map[string]interface{},1)//淀粉
  3735. //formulap := make([]map[string]interface{},1)//磷
  3736. //formulacNDF := make([]map[string]interface{},1)//粗料中的NDF %DM
  3737. //formulajcrate := make([]map[string]interface{},1)//粗料比例
  3738. //formulacprote := make([]map[string]interface{},1)//粗蛋白
  3739. //formulaenergy := make([]map[string]interface{},1)//能量
  3740. //formulafat := make([]map[string]interface{},1)//脂肪
  3741. //formulaga := make([]map[string]interface{},1)//钙
  3742. //formulafeedrate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质4.0
  3743. //formulafeed35rate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质3.5
  3744. resmap := make(map[string]float64)
  3745. resmapstr := make(map[string]string)
  3746. resAll := make(map[string]interface{})
  3747. for _, formula := range formulas {
  3748. if formula["type"].(int64) == 0 {
  3749. resmapstr[formula["note"].(string)] = formula["formula"].(string)
  3750. continue
  3751. }
  3752. res, err := PraseFormula(formula["formula"].(string), nur)
  3753. if err != nil {
  3754. logging.Error("PraseFormula err", err.Error(), formula["nutrition"].(string))
  3755. //println("PraseFormula err", err.Error(), formula["nutrition"].(string))
  3756. }
  3757. switch res.(type) {
  3758. case string:
  3759. resmapstr[formula["note"].(string)] = res.(string)
  3760. case float64, int, float32, int64, int32, int16, int8:
  3761. var rest float64
  3762. switch res.(type) {
  3763. case float64:
  3764. rest = res.(float64)
  3765. case int:
  3766. rest = float64(res.(int))
  3767. case int64:
  3768. rest = float64(res.(int64))
  3769. case int32:
  3770. rest = float64(res.(int32))
  3771. case int16:
  3772. rest = float64(res.(int16))
  3773. case int8:
  3774. rest = float64(res.(int8))
  3775. }
  3776. //fmt.Printf("==============oo %+v %+v %+v\n", res,rest,formula["nutrition"].(string))
  3777. switch formula["nutrition"].(string) {
  3778. case "ADF %DM":
  3779. resmap["adf"] = resmap["adf"] + rest
  3780. case "NDF %DM":
  3781. resmap["ndf"] = resmap["ndf"] + rest
  3782. case "NFC %DM":
  3783. resmap["nfc"] = resmap["nfc"] + rest
  3784. //case "小肠可消化粗蛋白":
  3785. // resmap["cprotedm"] = resmap["cprotedm"] + rest
  3786. case "干物质":
  3787. resmap["dry"] = resmap["dry"] + rest
  3788. case "淀粉":
  3789. resmap["starch"] = resmap["starch"] + rest
  3790. case "磷":
  3791. resmap["p"] = resmap["p"] + rest
  3792. case "粗料中的NDF %DM":
  3793. resmap["cndf"] = resmap["cndf"] + rest
  3794. case "粗料比例":
  3795. resmap["jcrate"] = resmap["jcrate"] + rest
  3796. case "粗蛋白":
  3797. resmap["cp"] = resmap["cp"] + rest
  3798. case "能量":
  3799. resmap["nm"] = resmap["nm"] + rest
  3800. case "脂肪":
  3801. resmap["fat"] = resmap["fat"] + rest
  3802. case "钙":
  3803. resmap["ca"] = resmap["ca"] + rest
  3804. case "饲料转化效率,标准乳/干物质":
  3805. resmap["trans4f"] = resmap["trans4f"] + rest
  3806. case "饲料转化效率,标准乳/干物质3.5":
  3807. resmap["trans35f"] = resmap["trans35f"] + rest
  3808. }
  3809. default:
  3810. fmt.Printf("===============%T \n", res)
  3811. }
  3812. }
  3813. if resmap["dry"] == 0 { //粗蛋白%dm = 粗蛋白/干物质
  3814. resmap["cpd"] = 0
  3815. resmap["cad"] = 0
  3816. resmap["pd"] = 0
  3817. resmap["nuint"] = 0
  3818. resmap["nmd"] = 0
  3819. } else {
  3820. resmap["cpd"] = resmap["cp"] / resmap["dry"] / 10
  3821. resmap["cad"] = resmap["ca"] / resmap["dry"] / 10
  3822. resmap["pd"] = resmap["p"] / resmap["dry"] / 10
  3823. resmap["nuint"] = resmap["nm"] / 3.138
  3824. resmap["nmd"] = resmap["nm"] * 0.2389 / resmap["dry"]
  3825. }
  3826. for key, value := range resmap {
  3827. va, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", value), 64)
  3828. resAll[key] = va
  3829. }
  3830. for key, value := range resmapstr {
  3831. //println(key, value)
  3832. resAll[key] = value
  3833. }
  3834. resAll["uprice"] = ""
  3835. resAll["upriced"] = ""
  3836. resAll["kprice"] = ""
  3837. return resAll, nil
  3838. }
  3839. func PraseFormula(formula string, nur *Nur) (interface{}, error) {
  3840. str := formula
  3841. str = strings.ReplaceAll(str, "日增重", strconv.FormatFloat(nur.Dayw, 'f', -1, 64))
  3842. //str = strings.ReplaceAll(str, "体重", nur.Bw)
  3843. str = strings.ReplaceAll(str, "怀孕天数", strconv.FormatFloat(nur.Dayspre, 'f', -1, 64))
  3844. str = strings.ReplaceAll(str, "产量", strconv.FormatFloat(nur.Product, 'f', -1, 64))
  3845. str = strings.ReplaceAll(str, "乳脂率", strconv.FormatFloat(nur.Fat, 'f', -1, 64))
  3846. str = strings.ReplaceAll(str, "乳蛋白率", strconv.FormatFloat(nur.Pro, 'f', -1, 64))
  3847. str = strings.ReplaceAll(str, "乳糖率", strconv.FormatFloat(nur.Lactose, 'f', -1, 64))
  3848. str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64)) // 3.5乳脂率
  3849. //str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+18.57*nur.Fat/100*nur.Product, 'f', -1, 64))
  3850. //println("标准乳--",strconv.FormatFloat(0.4*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64))
  3851. str = strings.ReplaceAll(str, "泌乳天数", strconv.FormatFloat(nur.Avgdim, 'f', -1, 64))
  3852. str = strings.ReplaceAll(str, "月龄", strconv.FormatFloat(nur.Avgmonthage, 'f', -1, 64))
  3853. str = strings.ReplaceAll(str, "温度", strconv.FormatFloat(nur.Tem, 'f', -1, 64))
  3854. str = strings.ReplaceAll(str, "胎次", strconv.FormatFloat(nur.Fetal, 'f', -1, 64))
  3855. str = strings.ReplaceAll(str, "体重", strconv.FormatFloat(nur.Bw, 'f', -1, 64))
  3856. str = strings.ReplaceAll(str, "Math.pow", "Mathpow")
  3857. str = strings.ReplaceAll(str, "Math.round", "Mathround")
  3858. str = strings.ReplaceAll(str, "Math.log", "Mathlog")
  3859. str = strings.ReplaceAll(str, "Math.exp", "Mathexp")
  3860. //str := "(体重>200?4.2/0.55*Math.pow(体重,0.75):3.5/0.55*Math.pow(体重,0.75))"
  3861. functions := map[string]govaluate.ExpressionFunction{
  3862. "strlen": func(args ...interface{}) (interface{}, error) {
  3863. length := len(args[0].(string))
  3864. return (float64)(length), nil
  3865. },
  3866. "Mathpow": func(args ...interface{}) (interface{}, error) {
  3867. c := math.Pow(args[0].(float64), args[1].(float64))
  3868. return (float64)(c), nil
  3869. },
  3870. "Mathround": func(args ...interface{}) (interface{}, error) {
  3871. c := math.Round(args[0].(float64))
  3872. return (float64)(c), nil
  3873. },
  3874. "Mathlog": func(args ...interface{}) (interface{}, error) {
  3875. c := math.Log(args[0].(float64))
  3876. return (float64)(c), nil
  3877. },
  3878. "Mathexp": func(args ...interface{}) (interface{}, error) {
  3879. c := math.Exp(args[0].(float64))
  3880. return (float64)(c), nil
  3881. },
  3882. }
  3883. expression, err := govaluate.NewEvaluableExpressionWithFunctions(str, functions)
  3884. if err != nil {
  3885. return 0, err
  3886. }
  3887. //fmt.Printf("== %+v\n", str)
  3888. result, err := expression.Evaluate(nil)
  3889. //fmt.Printf("== %+v\n", result)
  3890. if err != nil || result == nil {
  3891. return 0, err
  3892. }
  3893. return result, err
  3894. }
  3895. func HttpPostData(url, RequestMethod, parammaps string, tx *xorm.Session) string {
  3896. fsion := gofasion.NewFasion(parammaps)
  3897. sqlnamestr := fsion.Get("name").ValueStr()
  3898. sqlnamestr1 := fsion.Get("name1").ValueStr()
  3899. offset := fsion.Get("offset").ValueDefaultInt(0)
  3900. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  3901. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  3902. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  3903. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  3904. body := []byte("")
  3905. if sql != "" {
  3906. s_params := make([]interface{}, 0)
  3907. if fsion.HasKey("parammaps") {
  3908. parammaps := fsion.Get("parammaps")
  3909. paramslist := strings.Split(p, ",")
  3910. if len(paramslist) > 0 && p != "" {
  3911. for _, value := range paramslist {
  3912. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  3913. }
  3914. }
  3915. }
  3916. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  3917. if err != nil {
  3918. } else {
  3919. paramslist1 := strings.Split(p1, ",")
  3920. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  3921. s_params1 := make([]interface{}, 0)
  3922. if len(paramslist1) > 0 && p1 != "" {
  3923. for _, v := range paramslist1 {
  3924. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  3925. }
  3926. }
  3927. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  3928. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  3929. body, _ = json.Marshal(queryData)
  3930. }
  3931. }
  3932. }
  3933. bs := bytes.NewBuffer(body)
  3934. client := &http.Client{}
  3935. //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}"))
  3936. req, err := http.NewRequest(RequestMethod,
  3937. url,
  3938. bs,
  3939. )
  3940. if err != nil {
  3941. }
  3942. req.Header.Set("Content-Type", "application/json;charset=utf-8")
  3943. req.Header.Set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY")
  3944. if err != nil {
  3945. // handle error
  3946. }
  3947. resp, err := client.Do(req)
  3948. if err != nil {
  3949. fmt.Println(err)
  3950. return ""
  3951. }
  3952. defer resp.Body.Close()
  3953. result_body, err := ioutil.ReadAll(resp.Body)
  3954. if err != nil {
  3955. fmt.Println(err)
  3956. return ""
  3957. }
  3958. fmt.Println(string(result_body))
  3959. return ""
  3960. }