e0c04afb2002bdfecb07d98c29e9de2d992b020e.svn-base 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373
  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. s_params := make([]interface{}, 0)
  1762. tx := restful.Engine.NewSession()
  1763. err := tx.Begin()
  1764. defer func() {
  1765. switch {
  1766. case err != nil:
  1767. fmt.Println("__error:", err)
  1768. if tx != nil {
  1769. tx.Rollback()
  1770. }
  1771. default:
  1772. if tx != nil {
  1773. err = tx.Commit()
  1774. }
  1775. }
  1776. if tx != nil {
  1777. tx.Close()
  1778. }
  1779. }()
  1780. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1781. sql1, p1 := restful.GetSqlByNameDBT("updated1YJ", tx)
  1782. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJ", tx)
  1783. sql3, p3 := restful.GetSqlByNameDBT("updated2YJ", tx)
  1784. if !fsion.HasKey("parammaps") {
  1785. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1786. return
  1787. }
  1788. parammaps := fsion.Get("parammaps")
  1789. // ==========================1========================
  1790. paramslist := strings.Split(p, ",")
  1791. if len(paramslist) > 0 && p != "" {
  1792. for _, value := range paramslist {
  1793. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1794. }
  1795. }
  1796. _, err = tx.SQL(sql, s_params...).Execute()
  1797. if err != nil {
  1798. fmt.Println(err)
  1799. logging.Error("PostRJSBData err: ", err)
  1800. msg := geterrmsg(err.Error())
  1801. appG.Response(http.StatusOK, e.ERROR, msg)
  1802. return
  1803. }
  1804. // ==========================2========================
  1805. s_params = make([]interface{}, 0)
  1806. paramslist = strings.Split(p1, ",")
  1807. if len(paramslist) > 0 && p1 != "" {
  1808. for _, value := range paramslist {
  1809. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1810. }
  1811. }
  1812. _, err = tx.SQL(sql1, s_params...).Execute()
  1813. if err != nil {
  1814. logging.Error("PostRJSBData1 err: ", err)
  1815. msg := geterrmsg(err.Error())
  1816. appG.Response(http.StatusOK, e.ERROR, msg)
  1817. return
  1818. }
  1819. s_params = make([]interface{}, 0)
  1820. // ==========================3========================
  1821. paramslist = strings.Split(p2, ",")
  1822. if len(paramslist) > 0 && p2 != "" {
  1823. for _, value := range paramslist {
  1824. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1825. }
  1826. }
  1827. _, err = tx.SQL(sql2, s_params...).Execute()
  1828. if err != nil {
  1829. logging.Error("PostRJSBData2 err: ", err)
  1830. msg := geterrmsg(err.Error())
  1831. appG.Response(http.StatusOK, e.ERROR, msg)
  1832. return
  1833. }
  1834. s_params = make([]interface{}, 0)
  1835. // ==========================4========================
  1836. paramslist = strings.Split(p3, ",")
  1837. if len(paramslist) > 0 && p3 != "" {
  1838. for _, value := range paramslist {
  1839. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1840. }
  1841. }
  1842. _, err = tx.SQL(sql3, s_params...).Execute()
  1843. if err != nil {
  1844. logging.Error("PostRJSBData3 err: ", err)
  1845. msg := geterrmsg(err.Error())
  1846. appG.Response(http.StatusOK, e.ERROR, msg)
  1847. return
  1848. }
  1849. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1850. }
  1851. func PostRJSBDatas(c *gin.Context) {
  1852. appG := app.Gin{C: c}
  1853. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1854. fsions := gofasion.NewFasion(string(dataByte)).Array()
  1855. tx := restful.Engine.NewSession()
  1856. err := tx.Begin()
  1857. defer func() {
  1858. switch {
  1859. case err != nil:
  1860. fmt.Println("__error:", err)
  1861. if tx != nil {
  1862. tx.Rollback()
  1863. }
  1864. default:
  1865. if tx != nil {
  1866. err = tx.Commit()
  1867. }
  1868. }
  1869. if tx != nil {
  1870. tx.Close()
  1871. }
  1872. }()
  1873. sql, p := restful.GetSqlByNameDBT("updatedownloadedplanYJ", tx)
  1874. sql1, p1 := restful.GetSqlByNameDBT("updated1YJV2", tx)
  1875. sql2, p2 := restful.GetSqlByNameDBT("updated1execYJV2", tx)
  1876. sql3, p3 := restful.GetSqlByNameDBT("updated2YJV2", tx)
  1877. for _, fsion := range fsions {
  1878. if !fsion.HasKey("parammaps") {
  1879. appG.Response(http.StatusOK, e.ERROR, "参数解析失败")
  1880. return
  1881. }
  1882. parammaps := fsion.Get("parammaps")
  1883. s_params := make([]interface{}, 0)
  1884. // ==========================1========================
  1885. paramslist := strings.Split(p, ",")
  1886. if len(paramslist) > 0 && p != "" {
  1887. for _, value := range paramslist {
  1888. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1889. }
  1890. }
  1891. fmt.Println("}}}}}}}}}}}}}}}}}}}",s_params)
  1892. _, err = tx.SQL(sql, s_params...).Execute()
  1893. if err != nil {
  1894. fmt.Println(err)
  1895. logging.Error("PostRJSBData err: ", err)
  1896. msg := geterrmsg(err.Error())
  1897. appG.Response(http.StatusOK, e.ERROR, msg)
  1898. return
  1899. }
  1900. // ==========================2========================
  1901. s_params = make([]interface{}, 0)
  1902. paramslist = strings.Split(p1, ",")
  1903. if len(paramslist) > 0 && p1 != "" {
  1904. for _, value := range paramslist {
  1905. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1906. }
  1907. }
  1908. _, err = tx.SQL(sql1, s_params...).Execute()
  1909. if err != nil {
  1910. logging.Error("PostRJSBData1 err: ", err)
  1911. msg := geterrmsg(err.Error())
  1912. appG.Response(http.StatusOK, e.ERROR, msg)
  1913. return
  1914. }
  1915. s_params = make([]interface{}, 0)
  1916. // ==========================3========================
  1917. paramslist = strings.Split(p2, ",")
  1918. if len(paramslist) > 0 && p2 != "" {
  1919. for _, value := range paramslist {
  1920. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1921. }
  1922. }
  1923. _, err = tx.SQL(sql2, s_params...).Execute()
  1924. if err != nil {
  1925. logging.Error("PostRJSBData2 err: ", err)
  1926. msg := geterrmsg(err.Error())
  1927. appG.Response(http.StatusOK, e.ERROR, msg)
  1928. return
  1929. }
  1930. s_params = make([]interface{}, 0)
  1931. // ==========================4========================
  1932. paramslist = strings.Split(p3, ",")
  1933. if len(paramslist) > 0 && p3 != "" {
  1934. for _, value := range paramslist {
  1935. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1936. }
  1937. }
  1938. _, err = tx.SQL(sql3, s_params...).Execute()
  1939. if err != nil {
  1940. logging.Error("PostRJSBData3 err: ", err)
  1941. msg := geterrmsg(err.Error())
  1942. appG.Response(http.StatusOK, e.ERROR, msg)
  1943. return
  1944. }
  1945. }
  1946. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1947. }
  1948. // @Summary 根据APISQL表中SQL执行得到数据集
  1949. // @Tags ExecDataByConfig
  1950. // @Accept json
  1951. // @Produce json
  1952. // @Param sqlname query string true "sqlname"
  1953. // @Param offset query int true "offset"
  1954. // @Param pagecount query int true "pagecount"
  1955. // @Param returntype query string true "returntype"
  1956. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1957. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1958. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1959. // @Router /data/PostDataByName [POST]
  1960. func ExecDataByConfig(c *gin.Context) {
  1961. appG := app.Gin{C: c}
  1962. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1963. tempval_ := make(map[string]interface{})
  1964. tempCommon := make(map[string]interface{})
  1965. tempval := make([]map[string]interface{}, 0)
  1966. err := json.Unmarshal(dataByte, &tempval_)
  1967. logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  1968. if err != nil {
  1969. fmt.Println("error:", err)
  1970. } else {
  1971. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  1972. tempCommon = v
  1973. }
  1974. if v, ok := tempval_["data"].([]interface{}); ok {
  1975. for _, Tvalue := range v {
  1976. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1977. tempval = append(tempval, v1)
  1978. }
  1979. }
  1980. }
  1981. tx := restful.Engine.NewSession()
  1982. err := tx.Begin()
  1983. if err != nil {
  1984. logging.Error("tx.Begin 事务启动失败__error:", err)
  1985. }
  1986. defer func() {
  1987. switch {
  1988. case err != nil:
  1989. fmt.Println("__error:", err)
  1990. if tx != nil {
  1991. tx.Rollback()
  1992. }
  1993. default:
  1994. if tx != nil {
  1995. err = tx.Commit()
  1996. }
  1997. }
  1998. if tx != nil {
  1999. tx.Close()
  2000. }
  2001. }()
  2002. if err == nil {
  2003. if tempv, exists := c.Get("jwt_username"); exists {
  2004. tempCommon["jwt_username"] = tempv.(string)
  2005. }
  2006. for _, paramvalue := range tempval {
  2007. if _, ok := paramvalue["resultname"]; !ok {
  2008. paramvalue["resultname"] = paramvalue["name"]
  2009. }
  2010. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  2011. if err != nil {
  2012. logging.Error("ExecDataParam error:", err)
  2013. break
  2014. }
  2015. }
  2016. if err == nil {
  2017. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  2018. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  2019. } else {
  2020. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  2021. }
  2022. } else {
  2023. msg := geterrmsg(err.Error())
  2024. appG.Response(http.StatusOK, e.ERROR, msg)
  2025. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  2026. }
  2027. }
  2028. }
  2029. if err != nil {
  2030. msg := geterrmsg(err.Error())
  2031. appG.Response(http.StatusOK, e.ERROR, msg)
  2032. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  2033. }
  2034. }
  2035. func interfacetostring(val interface{}) string {
  2036. switch val.(type) {
  2037. case string:
  2038. ss := val.(string)
  2039. return ss
  2040. break
  2041. case int:
  2042. ss := val.(int)
  2043. return strconv.Itoa(ss)
  2044. break
  2045. case int64:
  2046. ss := val.(int64)
  2047. return strconv.FormatInt(ss, 10)
  2048. break
  2049. case float64:
  2050. ss := val.(float64)
  2051. return strconv.FormatFloat(ss, 'E', -1, 32)
  2052. break
  2053. }
  2054. return ""
  2055. }
  2056. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  2057. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  2058. tempstrb := strings.IndexAny(valuestring, "@")
  2059. tempstre := strings.IndexAny(valuestring, ".")
  2060. if tempstrb > -1 && tempstre > -1 {
  2061. for _, ParamMapList := range ParamMap {
  2062. if v, ok := ParamMapList["resultname"]; ok {
  2063. if v == valuestring[tempstrb+1:tempstre] {
  2064. if v1, ok := ParamMapList["resultmaps"]; ok {
  2065. if newA, ok := v1.([]map[string]interface{}); ok {
  2066. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  2067. } else if newA, ok := v1.(map[string]interface{}); ok {
  2068. return interfacetostring(newA[valuestring[tempstre+1:]])
  2069. }
  2070. }
  2071. }
  2072. }
  2073. }
  2074. } else {
  2075. return valuestring
  2076. }
  2077. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  2078. return strconv.FormatInt(int64(valuestring), 10)
  2079. }
  2080. return ""
  2081. }
  2082. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  2083. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  2084. tempstrb := strings.IndexAny(valuestring, "@")
  2085. tempstre := strings.IndexAny(valuestring, ".")
  2086. if tempstrb > -1 && tempstre > -1 {
  2087. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  2088. if s, ok := v.(string); ok {
  2089. return s
  2090. } else if s, ok := v.(float64); ok {
  2091. return strconv.FormatInt(int64(s), 10)
  2092. }
  2093. }
  2094. }
  2095. }
  2096. return ""
  2097. }
  2098. 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) {
  2099. sqlname := fasion["name"].(string)
  2100. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  2101. if v, ok := fasion["type"]; ok {
  2102. sqltype = v.(string)
  2103. }
  2104. sql := ""
  2105. p := ""
  2106. s_params := make([]interface{}, 0)
  2107. if sqltype != "" {
  2108. sql, p = restful.GetSqlByName(sqlname, tx)
  2109. //sqlApi, _ := models.GetApisqlByName(sqlname)
  2110. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  2111. paramslist := strings.Split(p, ",")
  2112. if len(paramslist) > 0 && p != "" {
  2113. for _, value := range paramslist {
  2114. ss := ""
  2115. if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2116. ids, err := setting.SnowIds.NextId()
  2117. if err != nil {
  2118. ids = time.Now().UnixNano()
  2119. logging.Info("create SnowIds err", err)
  2120. }
  2121. s_params = append(s_params, ids)
  2122. } else {
  2123. if ss == "" && CommonParamMap != nil {
  2124. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  2125. ss = valuestring
  2126. tempstrb := strings.IndexAny(valuestring, "@")
  2127. tempstre := strings.IndexAny(valuestring, ".")
  2128. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  2129. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  2130. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  2131. }
  2132. } else {
  2133. ss = ""
  2134. }
  2135. }
  2136. }
  2137. if ss == "" && ParentParamMap != nil {
  2138. ss = getParamValue_(v, value, ParentParamMap)
  2139. }
  2140. if ss == "" && sameParamMap != nil {
  2141. ss = getParamValue(v, value, sameParamMap)
  2142. }
  2143. if ss == "" && ParamMap != nil {
  2144. ss = getParamValue(v, value, ParamMap)
  2145. }
  2146. s_params = append(s_params, ss)
  2147. }
  2148. }
  2149. }
  2150. }
  2151. }
  2152. if sql == "" {
  2153. sqltype = ""
  2154. }
  2155. fmt.Printf("%v+ \n", s_params, sqlname)
  2156. if sqltype == "s1" {
  2157. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2158. if v, ok := tempValParam.(map[string]interface{}); ok {
  2159. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2160. }
  2161. if err != nil {
  2162. return nil, err
  2163. }
  2164. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2165. if err != nil {
  2166. return nil, err
  2167. }
  2168. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2169. } else if sqltype == "sm" {
  2170. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  2171. if v, ok := tempValParam.(map[string]interface{}); ok {
  2172. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2173. } else {
  2174. fmt.Println("sm", tempValParam)
  2175. }
  2176. if err != nil {
  2177. return nil, err
  2178. }
  2179. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2180. if err != nil {
  2181. return nil, err
  2182. }
  2183. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2184. } else if sqltype == "v" {
  2185. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2186. if v, ok := tempValParam.(map[string]interface{}); ok {
  2187. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2188. } else {
  2189. fmt.Println("v", tempValParam)
  2190. }
  2191. if err != nil {
  2192. return nil, err
  2193. }
  2194. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  2195. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  2196. }
  2197. } else if sqltype == "e" {
  2198. tempVal_arrary := make([]map[string]interface{}, 0)
  2199. tempVal_map := make(map[string]interface{})
  2200. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  2201. if err == nil {
  2202. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  2203. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  2204. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  2205. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  2206. return tempVal_arrary, err
  2207. }
  2208. return nil, err
  2209. } else {
  2210. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  2211. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2212. if err != nil {
  2213. return nil, err
  2214. }
  2215. return nil, err
  2216. }
  2217. }
  2218. return nil, nil
  2219. }
  2220. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *xorm.Session) error {
  2221. var err error
  2222. subarray, ok := fasion["children"]
  2223. if ok {
  2224. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  2225. tempVal_arrary := make([]map[string]interface{}, 0)
  2226. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  2227. tempVal_ := make(map[string]interface{})
  2228. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  2229. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  2230. } else {
  2231. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  2232. }
  2233. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  2234. if err != nil {
  2235. return err
  2236. }
  2237. tempVal_arrary = append(tempVal_arrary, tempVal_)
  2238. }
  2239. v.(map[string]interface{})["children"] = tempVal_arrary
  2240. }
  2241. }
  2242. return nil
  2243. }
  2244. // @Summary 删除图片
  2245. // @Tags RemovePicByName
  2246. // @Accept json
  2247. // @Produce json
  2248. // @Param sqlname query string true "sqlname"
  2249. // @Param offset query int true "offset"
  2250. // @Param pagecount query int true "pagecount"
  2251. // @Param returntype query string true "returntype"
  2252. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2253. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2254. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2255. // @Router /data/PostDataByName [POST]
  2256. func RemovePicByName(c *gin.Context) {
  2257. logging.Info("RemovePicByName ", c.Keys)
  2258. appG := app.Gin{C: c}
  2259. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2260. fsion := gofasion.NewFasion(string(dataByte))
  2261. sqlnamestr := fsion.Get("name").ValueStr()
  2262. picname := fsion.Get("picname").ValueStr()
  2263. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2264. params := fsion.Get("params").Array()
  2265. s_params := make([]interface{}, 0)
  2266. for _, v_params := range params {
  2267. s_params = append(s_params, v_params.ValueStr())
  2268. }
  2269. if sql != "" {
  2270. queryData, err := execDataBySql(sql, s_params)
  2271. if picname != "" {
  2272. // Destination
  2273. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  2274. err := os.Remove(distPath) //删除文件test.txt
  2275. if err != nil {
  2276. //如果删除失败则输出 file remove Error!
  2277. fmt.Println("file remove Error!")
  2278. //输出错误详细信息
  2279. fmt.Printf("%s", err)
  2280. } else {
  2281. //如果删除成功则输出 file remove OK!
  2282. fmt.Print("file remove OK!")
  2283. }
  2284. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  2285. err = os.Remove(distPath) //删除文件test.txt
  2286. if err != nil {
  2287. //如果删除失败则输出 file remove Error!
  2288. fmt.Println("file remove Error!")
  2289. //输出错误详细信息
  2290. fmt.Printf("%s", err)
  2291. } else {
  2292. //如果删除成功则输出 file remove OK!
  2293. fmt.Print("file remove OK!")
  2294. }
  2295. }
  2296. if err != nil {
  2297. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2298. } else {
  2299. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2300. }
  2301. } else {
  2302. appG.Response(http.StatusOK, e.SUCCESS, nil)
  2303. }
  2304. }
  2305. // @Summary 删除文件
  2306. // @Tags RemoveFileByName
  2307. // @Accept json
  2308. // @Produce json
  2309. // @Param sqlname query string true "sqlname"
  2310. // @Param offset query int true "offset"
  2311. // @Param pagecount query int true "pagecount"
  2312. // @Param returntype query string true "returntype"
  2313. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2314. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2315. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2316. // @Router /data/PostDataByName [POST]
  2317. func RemoveFileByName(c *gin.Context) {
  2318. appG := app.Gin{C: c}
  2319. id := c.Param("id")
  2320. logging.Info("DownloadFile ", c.Keys, id)
  2321. eqdoc, err := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", id).QueryString()
  2322. if eqdoc == nil {
  2323. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2324. return
  2325. }
  2326. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  2327. err = os.Remove(distPath) //删除文件test.txt
  2328. if err != nil {
  2329. //如果删除失败则输出 file remove Error!
  2330. fmt.Println("file remove Error!")
  2331. //输出错误详细信息
  2332. fmt.Printf("%s", err)
  2333. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2334. return
  2335. }
  2336. fmt.Print("file remove OK!")
  2337. _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
  2338. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2339. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2340. //fsion := gofasion.NewFasion(string(dataByte))
  2341. //sqlnamestr := fsion.Get("name").ValueStr()
  2342. //filename := fsion.Get("filename").ValueStr()
  2343. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2344. //params := fsion.Get("params").Array()
  2345. //s_params := make([]interface{}, 0)
  2346. //for _, v_params := range params {
  2347. // s_params = append(s_params, v_params.ValueStr())
  2348. //}
  2349. //if sql != "" {
  2350. // queryData, err := execDataBySql(sql, s_params)
  2351. //
  2352. // if filename != "" {
  2353. // // Destination
  2354. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  2355. //
  2356. // err := os.Remove(distPath) //删除文件test.txt
  2357. // if err != nil {
  2358. // //如果删除失败则输出 file remove Error!
  2359. // fmt.Println("file remove Error!")
  2360. // //输出错误详细信息
  2361. // fmt.Printf("%s", err)
  2362. // } else {
  2363. // //如果删除成功则输出 file remove OK!
  2364. // fmt.Print("file remove OK!")
  2365. // }
  2366. // }
  2367. // if err != nil {
  2368. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  2369. // } else {
  2370. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2371. // }
  2372. //} else {
  2373. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  2374. //}
  2375. }
  2376. // @Summary 根据APISQL表中SQL执行得到数据集
  2377. // @Tags PostDataByName
  2378. // @Accept json
  2379. // @Produce json
  2380. // @Param sqlname query string true "sqlname"
  2381. // @Param offset query int true "offset"
  2382. // @Param pagecount query int true "pagecount"
  2383. // @Param returntype query string true "returntype"
  2384. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2385. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2386. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2387. // @Router /data/PostDataByName [POST]
  2388. func UpdateDataRelation(c *gin.Context) {
  2389. appG := app.Gin{C: c}
  2390. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2391. fsion := gofasion.NewFasion(string(dataByte))
  2392. tablename := fsion.Get("name").ValueStr()
  2393. dataname := fsion.Get("dataname").ValueStr()
  2394. datavalue := fsion.Get("datavalue").ValueStr()
  2395. valuename := fsion.Get("valuename").ValueStr()
  2396. logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  2397. values := fsion.Get("values").Array()
  2398. tx := restful.Engine.NewSession()
  2399. err := tx.Begin()
  2400. defer func() {
  2401. switch {
  2402. case err != nil:
  2403. fmt.Println("__error:", err)
  2404. if tx != nil {
  2405. tx.Rollback()
  2406. }
  2407. default:
  2408. if tx != nil {
  2409. err = tx.Commit()
  2410. }
  2411. }
  2412. if tx != nil {
  2413. tx.Close()
  2414. }
  2415. }()
  2416. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  2417. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  2418. d_params := make([]interface{}, 4)
  2419. d_params[0] = datavalue
  2420. s_params := ""
  2421. for _, v_values := range values {
  2422. d_params[1] = v_values.ValueStr()
  2423. if s_params == "" {
  2424. s_params = v_values.ValueStr()
  2425. } else {
  2426. s_params = s_params + "," + v_values.ValueStr()
  2427. }
  2428. d_params[2] = d_params[1]
  2429. d_params[3] = d_params[0]
  2430. _, err := execDataBySqlT(insertSqlstr, d_params, tx)
  2431. if err != nil {
  2432. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2433. break
  2434. }
  2435. }
  2436. if s_params != "" {
  2437. deleteSqlstr := "delete from " + tablename + " " +
  2438. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  2439. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2440. if err != nil {
  2441. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2442. } else if err == nil {
  2443. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2444. }
  2445. } else {
  2446. deleteSqlstr := "delete from " + tablename + " " +
  2447. " where " + dataname + " = " + datavalue
  2448. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2449. if err != nil {
  2450. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2451. } else if err == nil {
  2452. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2453. }
  2454. }
  2455. }
  2456. // @Summary 根据APISQL表中SQL执行得到数据集
  2457. // @Tags PostDatasByName
  2458. // @Accept json
  2459. // @Produce json
  2460. // @Param sqlname query string true "sqlname"
  2461. // @Param offset query int true "offset"
  2462. // @Param pagecount query int true "pagecount"
  2463. // @Param returntype query string true "returntype"
  2464. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2465. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2466. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2467. // @Router /data/PostDatasByName [POST]
  2468. func PostDataByNames(c *gin.Context) {
  2469. appG := app.Gin{C: c}
  2470. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2471. fsion := gofasion.NewFasion(string(dataByte))
  2472. sqlParams := fsion.Array()
  2473. queryData := make(map[string]interface{})
  2474. for _, v := range sqlParams {
  2475. fsion2 := gofasion.NewFasion(v.Json())
  2476. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  2477. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  2478. if sql == "" {
  2479. errorm := make(map[string]interface{})
  2480. errorm["code"] = e.ERROR
  2481. errorm["message"] = "sql没有找到"
  2482. errorm["param"] = ""
  2483. queryData[fsion2.Get("name").ValueStr()] = errorm
  2484. continue
  2485. } else if sql != "" {
  2486. s_params := make([]interface{}, 0)
  2487. if fsion2.HasKey("parammaps") {
  2488. parammaps := fsion2.Get("parammaps")
  2489. paramslist := strings.Split(p, ",")
  2490. if len(paramslist) > 0 && p != "" {
  2491. for _, value := range paramslist {
  2492. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2493. if tempv, exists := c.Get("jwt_username"); exists {
  2494. s_params = append(s_params, tempv.(string))
  2495. } else {
  2496. s_params = append(s_params, "")
  2497. }
  2498. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2499. ids, err := setting.SnowIds.NextId()
  2500. if err != nil {
  2501. ids = time.Now().UnixNano()
  2502. logging.Info("create SnowIds err", err)
  2503. }
  2504. s_params = append(s_params, ids)
  2505. } else {
  2506. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2507. }
  2508. }
  2509. }
  2510. } else if fsion2.HasKey("params") {
  2511. params := fsion2.Get("params").Array()
  2512. //ids,err := setting.SnowIds.NextId()
  2513. //if err !=nil{
  2514. // ids = time.Now().UnixNano()
  2515. // logging.Info("create SnowIds err",err)
  2516. //}
  2517. //s_params = append(s_params, ids)
  2518. for _, v_params := range params {
  2519. s_params = append(s_params, v_params.ValueStr())
  2520. }
  2521. }
  2522. _, err := execDataBySql(sql, s_params)
  2523. if err != nil {
  2524. logging.Error("PostDataByName err: ", err)
  2525. errorm := make(map[string]interface{})
  2526. errorm["code"] = e.ERROR
  2527. errorm["message"] = err.Error()
  2528. errorm["param"] = s_params
  2529. queryData[fsion2.Get("name").ValueStr()] = errorm
  2530. continue
  2531. } else {
  2532. errorm := make(map[string]interface{})
  2533. errorm["code"] = e.SUCCESS
  2534. errorm["message"] = "success"
  2535. queryData[fsion2.Get("name").ValueStr()] = errorm
  2536. continue
  2537. }
  2538. } else {
  2539. errorm := make(map[string]interface{})
  2540. errorm["code"] = e.ERROR
  2541. errorm["message"] = "NO SQL"
  2542. errorm["param"] = ""
  2543. queryData[fsion2.Get("name").ValueStr()] = errorm
  2544. }
  2545. }
  2546. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2547. }
  2548. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2549. if offset > 0 {
  2550. offset = offset - 1
  2551. }
  2552. //if pagecount == 0 {
  2553. // pagecount = setting.AppSetting.PageSize
  2554. //}else if pagecount>setting.AppSetting.PageSize{
  2555. // pagecount = setting.AppSetting.PageSize
  2556. //}
  2557. if returntype == "list" {
  2558. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  2559. return queryData, err
  2560. } else if returntype == "listlist" {
  2561. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  2562. return queryData, err
  2563. } else {
  2564. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  2565. return queryData, err
  2566. }
  2567. }
  2568. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  2569. if offset > 0 {
  2570. offset = offset - 1
  2571. }
  2572. //if pagecount == 0 {
  2573. // pagecount = setting.AppSetting.PageSize
  2574. //}else if pagecount>setting.AppSetting.PageSize{
  2575. // pagecount = setting.AppSetting.PageSize
  2576. //}
  2577. if returntype == "list" {
  2578. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  2579. return queryData, err
  2580. } else if returntype == "listlist" {
  2581. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  2582. return queryData, err
  2583. } else {
  2584. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  2585. return queryData, err
  2586. }
  2587. }
  2588. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  2589. paramslist := strings.Split(Params, ",")
  2590. s_params := make([]interface{}, 0)
  2591. if isList {
  2592. for _, v_params := range thisParams["params"].([]interface{}) {
  2593. s_params = append(s_params, v_params)
  2594. }
  2595. } else {
  2596. paramslist := strings.Split(Params, ",")
  2597. if len(paramslist) > 0 && Params != "" {
  2598. for _, value := range paramslist {
  2599. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2600. }
  2601. }
  2602. }
  2603. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  2604. for _, queryData_ := range queryData {
  2605. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  2606. if isList {
  2607. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  2608. if queryData_[idname] != nil {
  2609. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  2610. }
  2611. } else {
  2612. if queryData_[idname] != nil {
  2613. thisParams[idname] = queryData_[idname].(interface{})
  2614. }
  2615. if len(paramslist) > 0 && Params != "" {
  2616. s_params = make([]interface{}, 0)
  2617. for _, value := range paramslist {
  2618. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2619. }
  2620. }
  2621. }
  2622. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  2623. } else {
  2624. }
  2625. }
  2626. return queryData, err
  2627. }
  2628. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2629. queryData, err := restful.ExecQuery(sqlstr, params)
  2630. return queryData, err
  2631. }
  2632. func execDataBySqlT(sqlstr string, params []interface{}, tx *xorm.Session) (interface{}, error) {
  2633. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  2634. return queryData, err
  2635. }
  2636. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2637. queryData, err := restful.ExecQuery(sqlstr, params)
  2638. return queryData, err
  2639. }
  2640. // @Summary 导入excel文件到数据库,excel第一行作为参数
  2641. // @Tags ImportExcel
  2642. // @Accept json
  2643. // @Produce json
  2644. // @Param file query string true "文件名"
  2645. // @Param name header string true "执行sql的名字"
  2646. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2647. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2648. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2649. // @Router /data/ImportExcel [POST]
  2650. func ImportExcel(c *gin.Context) {
  2651. appG := app.Gin{C: c}
  2652. inputdata := ""
  2653. c.Request.ParseForm()
  2654. //tempv1, _ := c.Get("jwt_username")
  2655. //fmt.Println("=======================",tempv1)
  2656. params := c.Request.Form
  2657. file, _, err := c.Request.FormFile("file")
  2658. if err != nil {
  2659. appG.Response(http.StatusOK, e.ERROR, err)
  2660. return
  2661. }
  2662. sqlnamestrs := params.Get("name")
  2663. sheetname := params.Get("sheetname")
  2664. pastureid := params.Get("pastureid")
  2665. //importParams := params.Get("importParams")
  2666. logging.Info("ImportExcel ", c.Keys, c.Request.RemoteAddr, sqlnamestrs)
  2667. //paramslist := strings.Split(importParams, ",")
  2668. //paramslist = append(paramslist, "jwt_username")
  2669. requiredParams := params.Get("requiredParams")
  2670. dateParams := params.Get("dateParams")
  2671. numParams := params.Get("numParams")
  2672. dateParamslist := strings.Split(dateParams, ",")
  2673. requiredParamslist := strings.Split(requiredParams, ",")
  2674. numParamslist := strings.Split(numParams, ",")
  2675. queryData := make([]interface{}, 0)
  2676. sqlslist := strings.Split(sqlnamestrs, ",")
  2677. xlsx1, err := excelize.OpenReader(file)
  2678. if err != nil {
  2679. fmt.Println(err)
  2680. return
  2681. }
  2682. ok := 0
  2683. error := 0
  2684. isdone := true
  2685. rows, _ := xlsx1.GetRows(sheetname)
  2686. if len(rows) > 10000 { // 限制1000条
  2687. rows = rows[:10000]
  2688. }
  2689. for i, row := range rows {
  2690. if i > 0 {
  2691. entry := make(map[string]interface{})
  2692. outentry := make(map[string]interface{})
  2693. for j, colCell := range rows[0] {
  2694. for k, colCell1 := range row {
  2695. if j == k {
  2696. outentry[colCell] = colCell1
  2697. entry[colCell] = colCell1
  2698. }
  2699. }
  2700. }
  2701. inputdata = ""
  2702. a := ""
  2703. for _, v := range dateParamslist {
  2704. if v == ""{
  2705. continue
  2706. }
  2707. if d, ok := outentry[strings.Trim(v, " ")]; ok {
  2708. dtime,err := TransfTime(d.(string))
  2709. if err != nil {
  2710. a = strings.Trim(v, " ")
  2711. break
  2712. }
  2713. outentry[strings.Trim(v, " ")] = dtime
  2714. }
  2715. }
  2716. if a != "" {
  2717. outentry["error_msg"] = a + " 格式错误"
  2718. error++
  2719. queryData = append(queryData, outentry)
  2720. continue
  2721. }
  2722. a = ""
  2723. for _, v := range requiredParamslist {
  2724. if v == ""{
  2725. continue
  2726. }
  2727. if d, ok := outentry[strings.Trim(v, " ")]; !ok || d == "" {
  2728. a = strings.Trim(v, " ")
  2729. break
  2730. }
  2731. }
  2732. if a != "" {
  2733. outentry["error_msg"] = a + " 未填或格式错误"
  2734. error++
  2735. queryData = append(queryData, outentry)
  2736. continue
  2737. }
  2738. a = ""
  2739. for _, v := range numParamslist {
  2740. if v == ""{
  2741. continue
  2742. }
  2743. d, ok := outentry[strings.Trim(v, " ")]
  2744. if !ok || d == "" {
  2745. outentry[strings.Trim(v, " ")] = 0
  2746. }else {
  2747. var fl float64
  2748. switch d.(type) {
  2749. case string :
  2750. fl,err = strconv.ParseFloat(d.(string),64)
  2751. }
  2752. if err != nil || fl<0 {
  2753. a = strings.Trim(v, " ")
  2754. break
  2755. }
  2756. }
  2757. }
  2758. if a != "" {
  2759. outentry["error_msg"] = a + " 为数值类型且不可为负数"
  2760. error++
  2761. queryData = append(queryData, outentry)
  2762. continue
  2763. }
  2764. tx := restful.Engine.NewSession()
  2765. err := tx.Begin()
  2766. for _, sqlnamestr := range sqlslist {
  2767. sql, paramslists := restful.GetSqlByNameDB(sqlnamestr)
  2768. paramslist := strings.Split(paramslists, ",")
  2769. if len(paramslist) > 0 {
  2770. s_params := make([]interface{}, 0)
  2771. for _, value := range paramslist {
  2772. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2773. if tempv, exists := c.Get("jwt_username"); exists {
  2774. s_params = append(s_params, tempv.(string))
  2775. } else {
  2776. s_params = append(s_params, "")
  2777. }
  2778. }else if strings.ToLower(strings.Trim(value, " ")) == "pastureid"{
  2779. s_params = append(s_params, pastureid)
  2780. } else if strings.ToLower(strings.Trim(value, " ")) == "snowid" {
  2781. ids, err := setting.SnowIds.NextId()
  2782. if err != nil {
  2783. ids = time.Now().UnixNano()
  2784. logging.Info("create SnowIds err", err)
  2785. }
  2786. s_params = append(s_params, ids)
  2787. }else {
  2788. if outentry[strings.Trim(value, " ")] == nil {
  2789. s_params = append(s_params,"")
  2790. }else{
  2791. s_params = append(s_params, outentry[strings.Trim(value, " ")])
  2792. }
  2793. }
  2794. }
  2795. //fmt.Println("================",s_params)
  2796. outentry["input"] = inputdata
  2797. returnmsgmap1, err := tx.SQL(sql, s_params...).Query().List()
  2798. if err != nil {
  2799. if tx != nil {
  2800. tx.Rollback()
  2801. }
  2802. if tx != nil {
  2803. tx.Close()
  2804. }
  2805. msg := geterrmsg(err.Error())
  2806. outentry["error_msg"] = msg
  2807. error++
  2808. queryData = append(queryData, outentry)
  2809. isdone = true
  2810. break
  2811. } else{
  2812. if len(returnmsgmap1) > 0 {
  2813. if va,ok :=returnmsgmap1[0]["msg"] ; ok && va != "" {
  2814. if tx != nil {
  2815. tx.Rollback()
  2816. }
  2817. if tx != nil {
  2818. tx.Close()
  2819. }
  2820. msg := geterrmsg(va.(string))
  2821. outentry["error_msg"] = msg
  2822. error++
  2823. queryData = append(queryData, outentry)
  2824. isdone = true
  2825. break
  2826. }
  2827. }
  2828. isdone = false
  2829. }
  2830. }
  2831. }
  2832. switch {
  2833. case err != nil:
  2834. if tx != nil {
  2835. tx.Rollback()
  2836. }
  2837. default:
  2838. if tx != nil {
  2839. err = tx.Commit()
  2840. }
  2841. }
  2842. if tx != nil {
  2843. tx.Close()
  2844. }
  2845. }
  2846. if !isdone{
  2847. ok++
  2848. isdone = true
  2849. }
  2850. }
  2851. data := make(map[string]interface{})
  2852. data["result"] = queryData
  2853. data["success"] = ok
  2854. data["err_count"] = error
  2855. appG.Response(http.StatusOK, e.SUCCESS, data)
  2856. }
  2857. func TransfTime(timestr string) (string,error) {
  2858. if timestr == "" {
  2859. return time.Now().Format("2006-01-02"),nil
  2860. }
  2861. days, err := strconv.Atoi(timestr)
  2862. if err != nil {
  2863. if len(timestr) == 8 && strings.Count(timestr, "-") == 2 {
  2864. return "20" + timestr[6:] + "-" + timestr[:5],nil
  2865. }
  2866. _,err :=time.Parse("2006-01-02",timestr)
  2867. if err != nil{
  2868. t,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. return "",err
  2873. }else {
  2874. return t.Format("2006-01-02"),nil
  2875. }
  2876. }else {
  2877. return t.Format("2006-01-02"),nil
  2878. }
  2879. }else {
  2880. return timestr,nil
  2881. }
  2882. }
  2883. times, _ := time.Parse("2006-01-02", "1899-12-30")
  2884. times = times.AddDate(0, 0, days)
  2885. return times.Format("2006-01-02"),nil
  2886. }
  2887. func Createchart(c *gin.Context) {
  2888. appG := app.Gin{C: c}
  2889. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2890. fsion := gofasion.NewFasion(string(dataByte))
  2891. chart_name := fsion.Get("chart_name").ValueStr()
  2892. desc := fsion.Get("desc").ValueStr()
  2893. pastureid := fsion.Get("pastureid").ValueStr()
  2894. source_id := fsion.Get("source_id").ValueStr()
  2895. emp := fsion.Get("emp").ValueStr()
  2896. content := fsion.Get("content").Json()
  2897. //fmt.Println("=============",parammaW.Json())
  2898. var mapResult map[string]interface{}
  2899. err1 := json.Unmarshal([]byte(content), &mapResult)
  2900. if err1 != nil {
  2901. fmt.Println("JsonToMapDemo err: ", err1)
  2902. }
  2903. //fmt.Println("---------",mapResult)
  2904. tx := restful.Engine.NewSession()
  2905. err := tx.Begin()
  2906. defer func() {
  2907. switch {
  2908. case err != nil:
  2909. logging.Error("tx.Begin 事务启动失败__error:", err)
  2910. if tx != nil {
  2911. tx.Rollback()
  2912. }
  2913. default:
  2914. if tx != nil {
  2915. err = tx.Commit()
  2916. }
  2917. }
  2918. if tx != nil {
  2919. tx.Close()
  2920. }
  2921. }()
  2922. ids, err := setting.SnowIds.NextId()
  2923. if err != nil {
  2924. ids = time.Now().UnixNano()
  2925. logging.Info("create SnowIds err", err)
  2926. }
  2927. exec, err := tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  2928. ids, pastureid, chart_name, 0, emp, desc, source_id)
  2929. if err != nil {
  2930. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2931. } else {
  2932. appG.Response(http.StatusOK, e.SUCCESS, exec)
  2933. }
  2934. }
  2935. func Updatedashboard(c *gin.Context) {
  2936. appG := app.Gin{C: c}
  2937. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2938. fsion := gofasion.NewFasion(string(dataByte))
  2939. dashboard_id := fsion.Get("dashboard_id").ValueStr()
  2940. pastureid := fsion.Get("pastureid").ValueStr()
  2941. content := fsion.Get("content").Json()
  2942. //fmt.Println("=============",parammaW.Json())
  2943. var mapResult map[string]interface{}
  2944. err1 := json.Unmarshal([]byte(content), &mapResult)
  2945. if err1 != nil {
  2946. fmt.Println("JsonToMapDemo err: ", err1)
  2947. }
  2948. layout := mapResult["layout"]
  2949. fmt.Println("---------", layout)
  2950. tx := restful.Engine.NewSession()
  2951. err := tx.Begin()
  2952. defer func() {
  2953. switch {
  2954. case err != nil:
  2955. logging.Error("tx.Begin 事务启动失败__error:", err)
  2956. if tx != nil {
  2957. tx.Rollback()
  2958. }
  2959. default:
  2960. if tx != nil {
  2961. err = tx.Commit()
  2962. }
  2963. }
  2964. if tx != nil {
  2965. tx.Close()
  2966. }
  2967. }()
  2968. //fmt.Println("sql========\n",sqlnamestr,sql)
  2969. exec, err := tx.SQL("select content from dashboard where pastureid=? and id=?", pastureid, dashboard_id).Query().List()
  2970. if err != nil {
  2971. println("2", err.Error())
  2972. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2973. } else {
  2974. if exec != nil {
  2975. if exec[0]["content"] == nil {
  2976. mapR := make(map[string]interface{})
  2977. mapR["layout"] = layout
  2978. mjson, _ := json.Marshal(mapR)
  2979. mString := string(mjson)
  2980. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2981. if err3 != nil {
  2982. println("3", err3.Error())
  2983. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  2984. } else {
  2985. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  2986. }
  2987. } else {
  2988. var mapR map[string]interface{}
  2989. err2 := json.Unmarshal([]byte(exec[0]["content"].(string)), &mapR)
  2990. if err1 != nil {
  2991. fmt.Println("JsonToMapDemo err: ", err2)
  2992. }
  2993. mapR["layout"] = layout
  2994. mjson, _ := json.Marshal(mapR)
  2995. mString := string(mjson)
  2996. exec1, err3 := tx.Exec("update dashboard set content=? where pastureid=? and id=?", mString, pastureid, dashboard_id)
  2997. if err3 != nil {
  2998. println("3", err3.Error())
  2999. appG.Response(http.StatusOK, e.ERROR, err3.Error())
  3000. } else {
  3001. appG.Response(http.StatusOK, e.SUCCESS, exec1)
  3002. }
  3003. }
  3004. } else {
  3005. appG.Response(http.StatusOK, e.ERROR, "未找到对应的仪表盘")
  3006. }
  3007. }
  3008. }
  3009. func ExecSQL(c *gin.Context) {
  3010. appG := app.Gin{C: c}
  3011. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3012. fsion := gofasion.NewFasion(string(dataByte))
  3013. content := fsion.Get("content").Json()
  3014. pastureid := fsion.Get("pastureid").ValueStr()
  3015. //pastureid := fsion.Get("pastureid").ValueStr()
  3016. //println(pastureid,"==================")
  3017. //fmt.Println("=============",parammaW.Json())
  3018. var mapResult map[string]interface{}
  3019. err1 := json.Unmarshal([]byte(content), &mapResult)
  3020. if err1 != nil {
  3021. fmt.Println("JsonToMapDemo err: ", err1)
  3022. }
  3023. //fmt.Println("---------",mapResult)
  3024. sqlstr := "select "
  3025. groupstr := " GROUP BY "
  3026. for _, value := range mapResult["selectedDimension"].([]interface{}) {
  3027. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3028. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3029. }
  3030. for _, value := range mapResult["selectedCalcul"].([]interface{}) {
  3031. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  3032. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3033. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3034. } else {
  3035. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  3036. sqlstr = sqlstr + tem + ","
  3037. }
  3038. }
  3039. if _, ok := mapResult["selectedCalcul2"]; ok {
  3040. for _, value := range mapResult["selectedCalcul2"].([]interface{}) {
  3041. if value.(map[string]interface{})["calculFunc"].(string) == "none" {
  3042. sqlstr = sqlstr + value.(map[string]interface{})["Column"].(string) + ","
  3043. groupstr = groupstr + value.(map[string]interface{})["Column"].(string) + ","
  3044. } else {
  3045. tem := value.(map[string]interface{})["calculFunc"].(string) + "(" + value.(map[string]interface{})["Column"].(string) + ") as " + value.(map[string]interface{})["Column"].(string)
  3046. sqlstr = sqlstr + tem + ","
  3047. }
  3048. }
  3049. }
  3050. //
  3051. sqlstr = sqlstr[0 : len(sqlstr)-1]
  3052. sqlstr = sqlstr + " from " + mapResult["dataSrc"].(string)
  3053. //if len(mapResult["filters"].([]interface {}))>0{
  3054. sqlstr = sqlstr + " where pastureid=" + pastureid
  3055. //}
  3056. //
  3057. for _, value := range mapResult["filters"].([]interface{}) {
  3058. switch value.(map[string]interface{})["filterOperator"].(string) {
  3059. case ">", "=", "<", ">=", "<=", "!=", "like":
  3060. 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 "
  3061. case "IN":
  3062. sqlstr = sqlstr + value.(map[string]interface{})["filteCol"].(string) + " " + value.(map[string]interface{})["filterOperator"].(string) + "("
  3063. for _, value1 := range value.(map[string]interface{})["value"].(map[string]interface{})["arrValue"].([]interface{}) {
  3064. sqlstr = sqlstr + value1.(string) + ","
  3065. }
  3066. sqlstr = sqlstr[0 : len(sqlstr)-1]
  3067. sqlstr = sqlstr + ")" + " AND "
  3068. case "BETWEEN":
  3069. 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 "
  3070. }
  3071. }
  3072. if len(mapResult["filters"].([]interface{})) > 0 {
  3073. sqlstr = sqlstr[0 : len(sqlstr)-4]
  3074. }
  3075. //
  3076. groupstr = groupstr[0 : len(groupstr)-1]
  3077. sqlstr = sqlstr + groupstr + " "
  3078. if len(mapResult["orderByStrs"].([]interface{})) > 0 {
  3079. sqlstr = sqlstr + " order by "
  3080. }
  3081. //
  3082. for key, value := range mapResult["orderByStrs"].([]interface{}) {
  3083. if key == len(mapResult["orderByStrs"].([]interface{}))-1 {
  3084. sqlstr = sqlstr + value.(string) + " "
  3085. } else {
  3086. sqlstr = sqlstr + value.(string) + ","
  3087. }
  3088. }
  3089. //
  3090. sqlstr = sqlstr + " limit " + strconv.Itoa(int(mapResult["limit"].(float64)))
  3091. //fmt.Println("====++---------",sqlstr)
  3092. tx := restful.Engine.NewSession()
  3093. err := tx.Begin()
  3094. defer func() {
  3095. switch {
  3096. case err != nil:
  3097. logging.Error("tx.Begin 事务启动失败__error:", err)
  3098. if tx != nil {
  3099. tx.Rollback()
  3100. }
  3101. default:
  3102. if tx != nil {
  3103. err = tx.Commit()
  3104. }
  3105. }
  3106. if tx != nil {
  3107. tx.Close()
  3108. }
  3109. }()
  3110. //fmt.Println("sql========\n",sqlnamestr,sql)
  3111. //ids,err := setting.SnowIds.NextId()
  3112. //if err !=nil{
  3113. // ids = time.Now().UnixNano()
  3114. // logging.Info("create SnowIds err",err)
  3115. //}
  3116. // exec,err :=tx.Exec("insert into chart(id,pastureid,cname,statue,emp,opratetime,content,sourceid)VALUEs(?,?,?,?,?,?,?)",
  3117. //ids,pastureid,chart_name,0,emp,desc,source_id)
  3118. exec, err := tx.SQL(sqlstr).Query().List()
  3119. if err != nil {
  3120. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3121. } else {
  3122. appG.Response(http.StatusOK, e.SUCCESS, exec)
  3123. }
  3124. }
  3125. func Dashboards(c *gin.Context) {
  3126. appG := app.Gin{C: c}
  3127. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3128. fsion := gofasion.NewFasion(string(dataByte))
  3129. sqlnamestr := fsion.Get("name").ValueStr()
  3130. //sqlnamestr1 := fsion.Get("name1").ValueStr()
  3131. offset := fsion.Get("offset").ValueDefaultInt(0)
  3132. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  3133. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  3134. month := fsion.Get("month").ValueDefaultStr("map")
  3135. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  3136. tx := restful.Engine.NewSession()
  3137. err := tx.Begin()
  3138. defer func() {
  3139. switch {
  3140. case err != nil:
  3141. fmt.Println("__error:", err)
  3142. if tx != nil {
  3143. tx.Rollback()
  3144. }
  3145. default:
  3146. if tx != nil {
  3147. err = tx.Commit()
  3148. }
  3149. }
  3150. if tx != nil {
  3151. tx.Close()
  3152. }
  3153. }()
  3154. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  3155. //sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  3156. if sql != "" {
  3157. s_params := make([]interface{}, 0)
  3158. if fsion.HasKey("parammaps") {
  3159. parammaps := fsion.Get("parammaps")
  3160. paramslist := strings.Split(p, ",")
  3161. if len(paramslist) > 0 && p != "" {
  3162. for _, value := range paramslist {
  3163. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  3164. if tempv, exists := c.Get("jwt_username"); exists {
  3165. s_params = append(s_params, tempv.(string))
  3166. } else {
  3167. s_params = append(s_params, "")
  3168. }
  3169. } else {
  3170. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  3171. }
  3172. }
  3173. }
  3174. }
  3175. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  3176. if err != nil {
  3177. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3178. } else {
  3179. //paramslist1 := strings.Split(p1, ",")
  3180. dids := make([]interface{}, 0)
  3181. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  3182. var mapResult map[string]interface{}
  3183. dids = append(dids, value["dashboard_id"])
  3184. if value["content"] != nil {
  3185. err1 := json.Unmarshal([]byte(value["content"].(string)), &mapResult)
  3186. if err1 != nil {
  3187. println("JsonToMapDemo1 err: ", err1)
  3188. }
  3189. //fmt.Printf("%v+\n",value["content"].(string))
  3190. temap := make(map[string]interface{})
  3191. tem := mapResult["layout"].([]interface{})
  3192. if v, ok := value["chartids"]; ok {
  3193. chartids := strings.Split(v.(string), ",")
  3194. for _, va := range chartids {
  3195. a := false
  3196. for _, v2 := range tem {
  3197. if v2.(map[string]interface{})["id"].(string) == va {
  3198. a = true
  3199. //println("va1======",va)
  3200. }
  3201. }
  3202. if !a {
  3203. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  3204. va + "\",\"id\":\"" + va +
  3205. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":100,\"yOffSet\":18}"
  3206. var mapTem map[string]interface{}
  3207. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  3208. if err1 != nil {
  3209. println("JsonToMapDemo err: ", err1)
  3210. }
  3211. tem = append(tem, mapTem)
  3212. }
  3213. }
  3214. temap["layout"] = tem
  3215. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  3216. }
  3217. } else {
  3218. temap := make(map[string]interface{})
  3219. tem := make([]interface{}, 0)
  3220. if v, ok := value["chartids"]; ok {
  3221. chartids := strings.Split(v.(string), ",")
  3222. for _, va := range chartids {
  3223. laytem := "{\"bottomLine\":[[3,18],[15,18]],\"h\":9,\"i\":\"" +
  3224. va + "\",\"id\":\"" + va +
  3225. "\",\"moved\":false,\"topLine\":[[3,9],[15,9]],\"w\":12,\"x\":0,\"xOffSet\":15,\"y\":0,\"yOffSet\":18}"
  3226. var mapTem map[string]interface{}
  3227. err1 := json.Unmarshal([]byte(laytem), &mapTem)
  3228. if err1 != nil {
  3229. println("JsonToMapDemo err: ", err1)
  3230. }
  3231. tem = append(tem, mapTem)
  3232. }
  3233. temap["layout"] = tem
  3234. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["content"] = temap
  3235. }
  3236. }
  3237. }
  3238. queryData.(map[string]interface{})["order"] = dids
  3239. queryData.(map[string]interface{})["dashboards"] = queryData.(map[string]interface{})["list"]
  3240. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  3241. }
  3242. } else {
  3243. appG.Response(http.StatusOK, e.SUCCESS, nil)
  3244. }
  3245. }
  3246. //type Nur struct {
  3247. // Avgmonthage float64 `json : "avgmonthage"` //月龄
  3248. // Bw float64 `json : "bw"` //体重
  3249. // Dayw float64 `json : "dayw"` //日增重
  3250. // Fetal float64 `json : "fetal"` //胎次
  3251. // Avgdim float64 `json : "avgdim"` //平均泌乳天数
  3252. // Dayspre float64 `json : "dayspre"` //怀孕天数
  3253. // Product float64 `json : "product"` //产奶量
  3254. // Fat float64 `json : "fat"` //乳脂率
  3255. // Pro float64 `json : "pro"` //乳蛋白率
  3256. // Lactose float64 `json : "lactose"` //乳糖率
  3257. // Source string `json : "source"` //来源
  3258. // Tem float64 `json : "tem"` //温度
  3259. // Cowsum float64 `json : "cowsum"` //牛头数
  3260. // Cowclass float64 `json : "cowclass"` //牲畜类别
  3261. // Cowclassid float64 `json : "cowclassid"` //牲畜类别id
  3262. // Bigcowclass string `json : "bigcowclass"` //牲畜大类
  3263. //}
  3264. type Nur struct {
  3265. Avgmonthage float64 //月龄
  3266. Bw float64 //体重
  3267. Dayw float64 //日增重
  3268. Fetal float64 //胎次
  3269. Avgdim float64 //平均泌乳天数
  3270. Dayspre float64 //怀孕天数
  3271. Product float64 //产奶量
  3272. Fat float64 //乳脂率
  3273. Pro float64 //乳蛋白率
  3274. Lactose float64 //乳糖率
  3275. Source string //来源
  3276. Tem float64 //温度
  3277. Cowsum float64 //牛头数
  3278. Cowclass string //牲畜类别
  3279. Cowclassid float64 //牲畜类别id
  3280. Bigcowclass string //牲畜大类
  3281. }
  3282. func GETNurJudge(c *gin.Context) {
  3283. appG := app.Gin{C: c}
  3284. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3285. fsion := gofasion.NewFasion(string(dataByte))
  3286. nurstr := fsion.Get("parammaps").Json()
  3287. parammaps := make(map[string]interface{})
  3288. nur := Nur{}
  3289. _ = json.Unmarshal([]byte(nurstr), &nur) //需要json 的类型相同
  3290. _ = json.Unmarshal([]byte(nurstr), &parammaps) //需要json 的类型相同
  3291. tx := restful.Engine.NewSession()
  3292. err := tx.Begin()
  3293. defer func() {
  3294. switch {
  3295. case err != nil:
  3296. fmt.Println("__error:", err)
  3297. if tx != nil {
  3298. tx.Rollback()
  3299. }
  3300. default:
  3301. if tx != nil {
  3302. err = tx.Commit()
  3303. }
  3304. }
  3305. if tx != nil {
  3306. tx.Close()
  3307. }
  3308. }()
  3309. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? "+
  3310. "and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  3311. resnur, err := CalNUR(formulas, &nur)
  3312. if err != nil {
  3313. logging.Error("GETNurJudge err: ", err)
  3314. msg := geterrmsg(err.Error())
  3315. appG.Response(http.StatusOK, e.ERROR, msg)
  3316. }
  3317. resf, err := FeedproviderV2(parammaps, tx)
  3318. reslist := make([]map[string]interface{}, 21)
  3319. if len(resf) > 1 {
  3320. for key, _ := range resf[0] {
  3321. //println(key)
  3322. str, sort := switchname(key)
  3323. res := make(map[string]interface{}, 0)
  3324. res["standard"] = str
  3325. res["cowneed"] = resnur[key] // 奶牛需要
  3326. res["ftpro"] = resf[0][key] // 配方提供
  3327. res["hprov"] = resf[1][key] // tmr料
  3328. res["sprov"] = resf[2][key] //采食量
  3329. reslist[sort] = res
  3330. }
  3331. }
  3332. if err != nil {
  3333. logging.Error("GETNurJudge err: ", err)
  3334. msg := geterrmsg(err.Error())
  3335. appG.Response(http.StatusOK, e.ERROR, msg)
  3336. } else {
  3337. appG.Response(http.StatusOK, e.SUCCESS, reslist)
  3338. }
  3339. }
  3340. func GETNurJudgeRport(c *gin.Context) {
  3341. appG := app.Gin{C: c}
  3342. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  3343. fsion := gofasion.NewFasion(string(dataByte))
  3344. nurstr := fsion.Get("parammaps").Json()
  3345. parammaps := make(map[string]interface{})
  3346. _ = json.Unmarshal([]byte(nurstr), &parammaps)
  3347. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  3348. if err != nil {
  3349. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3350. return
  3351. }
  3352. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  3353. if err != nil {
  3354. appG.Response(http.StatusOK, e.ERROR, err.Error())
  3355. return
  3356. }
  3357. if stopTime.Sub(startTime) > 31*24*time.Hour {
  3358. appG.Response(http.StatusOK, e.ERROR, errors.New("日期范围过大"))
  3359. return
  3360. }
  3361. tx := restful.Engine.NewSession()
  3362. err = tx.Begin()
  3363. defer func() {
  3364. switch {
  3365. case err != nil:
  3366. fmt.Println("__error:", err)
  3367. if tx != nil {
  3368. tx.Rollback()
  3369. }
  3370. default:
  3371. if tx != nil {
  3372. err = tx.Commit()
  3373. }
  3374. }
  3375. if tx != nil {
  3376. tx.Close()
  3377. }
  3378. }()
  3379. //CulNurReportCow("getbmdetaillist",parammaps,tx)
  3380. res, err := CulNurReportFT(parammaps, tx)
  3381. if err != nil {
  3382. logging.Error("GETNurJudgeRport err: ", err)
  3383. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  3384. msg := geterrmsg(err.Error())
  3385. appG.Response(http.StatusOK, e.ERROR, msg)
  3386. } else {
  3387. appG.Response(http.StatusOK, e.SUCCESS, res)
  3388. }
  3389. }
  3390. func CulNurReportCow(sqlname string, parammaps map[string]interface{}, tx *xorm.Session) (map[string]interface{}, error) {
  3391. var nur Nur
  3392. sql, p := restful.GetSqlByNameDBT(sqlname, tx)
  3393. s_params := make([]interface{}, 0)
  3394. paramslist := strings.Split(p, ",")
  3395. if len(paramslist) > 0 && p != "" {
  3396. for _, value := range paramslist {
  3397. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3398. }
  3399. }
  3400. res, err := tx.SQL(sql, s_params...).Query().List()
  3401. if len(res) > 0 {
  3402. resstr, _ := json.Marshal(res[0])
  3403. err = json.Unmarshal(resstr, &nur)
  3404. if err != nil {
  3405. return nil, err
  3406. }
  3407. }
  3408. formulas, _ := tx.SQL("select * from tb_formula_d where cowclass = ? and if (?='',''='', nutrition=? ) ORDER BY nutrition", nur.Bigcowclass, nur.Source, nur.Source).Query().List()
  3409. //println(len(formulas))
  3410. resnur, err := CalNUR(formulas, &nur)
  3411. if err != nil {
  3412. return nil, err
  3413. }
  3414. return resnur, err
  3415. }
  3416. func CulNurReportFT(parammaps map[string]interface{}, tx *xorm.Session) (res []map[string]interface{}, err error) {
  3417. 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",
  3418. parammaps["pastureid"], parammaps["startTime"], parammaps["stopTime"], parammaps["ftid"]).Query().List()
  3419. //println(parammaps["pastureid"].(string), parammaps["startTime"].(string), parammaps["stopTime"].(string), parammaps["ftid"].(string))
  3420. if err != nil {
  3421. return nil, err
  3422. }
  3423. bartem := ""
  3424. //println("barids", len(barids))
  3425. dates := make([]string, 0)
  3426. for key, value := range barids {
  3427. if key == 0 {
  3428. bartem = value["barid"].(string)
  3429. parammaps["barid"] = value["barid"]
  3430. }
  3431. if bartem != value["barid"].(string) || key == len(barids)-1 || key == 0{
  3432. if key == len(barids)-1 || key == 0{
  3433. dates = append(dates, value["date"].(string))
  3434. }
  3435. parammaps["date"] = value["date"]
  3436. parammaps["barid"] = value["barid"]
  3437. barres, err := FeedproviderV3(parammaps, dates, tx)
  3438. if err != nil {
  3439. return nil, err
  3440. }
  3441. resnur, err := CulNurReportCow("getbmdetaillist", parammaps, tx)
  3442. if err != nil {
  3443. return nil, err
  3444. }
  3445. restem := make(map[string]interface{}, 0)
  3446. for k, _ := range barres[0] {
  3447. //println("wwwwwww",k)
  3448. restem["barid"] = value["barid"].(string)
  3449. restem["barname"] = value["barname"].(string)
  3450. restem["ccount"] = value["ccount"].(int64)
  3451. restem[k+"-nur"] = resnur[k]
  3452. restem[k+"-FT"] = barres[0][k]
  3453. restem[k+"-H"] = barres[1][k]
  3454. restem[k+"-S"] = barres[2][k]
  3455. }
  3456. res = append(res, restem)
  3457. if err != nil {
  3458. return nil, err
  3459. }
  3460. bartem = value["barid"].(string)
  3461. dates = make([]string, 0)
  3462. parammaps["barid"] = value["barid"]
  3463. }
  3464. dates = append(dates, value["date"].(string))
  3465. }
  3466. return res, err
  3467. }
  3468. func switchname(str string) (string, int) {
  3469. tstr := ""
  3470. sort := 0
  3471. switch str {
  3472. case "dry":
  3473. tstr = "干物质(kg)"
  3474. sort = 0
  3475. case "nm":
  3476. tstr = "产奶净能(MJ)"
  3477. sort = 1
  3478. case "nuint":
  3479. tstr = "奶牛能量单位(NND)"
  3480. sort = 2
  3481. case "cp":
  3482. tstr = "粗蛋白(g)"
  3483. sort = 3
  3484. case "ca":
  3485. tstr = " 钙(g)"
  3486. sort = 4
  3487. case "p":
  3488. tstr = "磷(g)"
  3489. sort = 5
  3490. case "nmd":
  3491. tstr = "产奶净能(MCal/DM)"
  3492. sort = 6
  3493. case "cpd":
  3494. tstr = "粗蛋白(%DM)"
  3495. sort = 7
  3496. case "fat":
  3497. tstr = "脂肪"
  3498. sort = 8
  3499. case "starch":
  3500. tstr = "淀粉"
  3501. sort = 9
  3502. case "ndf":
  3503. tstr = "NDF(%DM)"
  3504. sort = 10
  3505. case "cndf":
  3506. tstr = "粗料中的NDF(%DM)"
  3507. sort = 11
  3508. case "adf":
  3509. tstr = "ADF(%DM)"
  3510. sort = 12
  3511. case "cad":
  3512. tstr = " 钙(%DM)"
  3513. sort = 13
  3514. case "pd":
  3515. tstr = "磷(%DM)"
  3516. sort = 14
  3517. case "trans35f":
  3518. tstr = "(3.5)饲料转化效率(kg/DM)"
  3519. sort = 15
  3520. case "trans4f":
  3521. tstr = "(4.0)饲料转化效率(kg/DM)"
  3522. sort = 16
  3523. case "jcrate":
  3524. tstr = "精粗比"
  3525. sort = 17
  3526. case "uprice":
  3527. tstr = "日粮成本(元)"
  3528. sort = 18
  3529. case "upriced":
  3530. tstr = "干物质成本(元/公斤)"
  3531. sort = 19
  3532. case "kprice":
  3533. tstr = "公斤奶成本(元) "
  3534. sort = 20
  3535. default:
  3536. tstr = str
  3537. sort = 21
  3538. }
  3539. return tstr, sort
  3540. }
  3541. func FeedproviderV2(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]float64, error) {
  3542. startTime, err := time.Parse("2006-01-02", parammaps["startTime"].(string))
  3543. if err != nil {
  3544. return nil, err
  3545. }
  3546. stopTime, err := time.Parse("2006-01-02", parammaps["stopTime"].(string))
  3547. if err != nil {
  3548. return nil, err
  3549. }
  3550. if stopTime.Sub(startTime) > 31*24*time.Hour {
  3551. return nil, errors.New("日期范围过大")
  3552. }
  3553. dates := make([]string, 0)
  3554. for stopTime.Sub(startTime) >= 0 {
  3555. dates = append(dates, startTime.Format("2006-01-02"))
  3556. startTime = startTime.Add(24 * time.Hour)
  3557. }
  3558. resall, err := FeedproviderV3(parammaps, dates, tx)
  3559. return resall, err
  3560. }
  3561. func FeedproviderV3(parammaps map[string]interface{}, dates []string, tx *xorm.Session) ([]map[string]float64, error) {
  3562. res := make(map[string]float64, 0)
  3563. resH := make(map[string]float64, 0)
  3564. resS := make(map[string]float64, 0)
  3565. i := 0
  3566. for _, date := range dates {
  3567. i++
  3568. //println(date)
  3569. parammaps["date"] = date
  3570. restem, err := Feedprovider(parammaps, tx)
  3571. if err != nil {
  3572. return nil, err
  3573. }
  3574. restemSH, err := SHprovider(parammaps, tx)
  3575. if err != nil {
  3576. return nil, err
  3577. }
  3578. var h, s float64
  3579. if len(restemSH) > 1 {
  3580. switch restemSH[0]["actualweightminus"].(type) {
  3581. case string:
  3582. h, _ = strconv.ParseFloat(restemSH[0]["actualweightminus"].(string), 64)
  3583. }
  3584. switch restemSH[1]["actualweightminus"].(type) {
  3585. case string:
  3586. s, _ = strconv.ParseFloat(restemSH[1]["actualweightminus"].(string), 64)
  3587. }
  3588. }
  3589. if len(restem) > 0 {
  3590. b, _ := strconv.ParseFloat(restem[0]["jcrate"].(string), 64)
  3591. res["jcrate"] = res["jcrate"] + b
  3592. resH["jcrate"] = resH["jcrate"] + h*b
  3593. resS["jcrate"] = resS["jcrate"] + s*b
  3594. b, _ = strconv.ParseFloat(restem[0]["cpd"].(string), 64)
  3595. res["cpd"] = res["cpd"] + b
  3596. resH["cpd"] = resH["cpd"] + h*b
  3597. resS["cpd"] = resS["cpd"] + s*b
  3598. b, _ = strconv.ParseFloat(restem[0]["adf"].(string), 64)
  3599. res["adf"] = res["adf"] + b
  3600. resH["adf"] = resH["adf"] + h*b
  3601. resS["adf"] = resS["adf"] + s*b
  3602. b, _ = strconv.ParseFloat(restem[0]["ca"].(string), 64)
  3603. res["ca"] = res["ca"] + b
  3604. resH["ca"] = resH["ca"] + h*b
  3605. resS["ca"] = resS["ca"] + s*b
  3606. b, _ = strconv.ParseFloat(restem[0]["cad"].(string), 64)
  3607. res["cad"] = res["cad"] + b
  3608. resH["cad"] = resH["cad"] + h*b
  3609. resS["cad"] = resS["cad"] + s*b
  3610. //b,_ =strconv.ParseFloat(restem[0]["cdry"].(string),64)
  3611. //res["cdry"] = res["cdry"]+b
  3612. //
  3613. //resH["cdry"] = resH["cdry"]+ h*b
  3614. //resS["cdry"] = resS["cdry"]+ s*b
  3615. b, _ = strconv.ParseFloat(restem[0]["cndf"].(string), 64)
  3616. res["cndf"] = res["cndf"] + b
  3617. resH["cndf"] = resH["cndf"] + h*b
  3618. resS["cndf"] = resS["cndf"] + s*b
  3619. b, _ = strconv.ParseFloat(restem[0]["cp"].(string), 64)
  3620. res["cp"] = res["cp"] + b
  3621. resH["cp"] = resH["cp"] + h*b
  3622. resS["cp"] = resS["cp"] + s*b
  3623. b, _ = strconv.ParseFloat(restem[0]["dry"].(string), 64)
  3624. res["dry"] = res["dry"] + b
  3625. resH["dry"] = resH["dry"] + h*b
  3626. resS["dry"] = resS["dry"] + s*b
  3627. b, _ = strconv.ParseFloat(restem[0]["fat"].(string), 64)
  3628. res["fat"] = res["fat"] + b
  3629. resH["fat"] = resH["fat"] + h*b
  3630. resS["fat"] = resS["fat"] + s*b
  3631. b, _ = strconv.ParseFloat(restem[0]["kprice"].(string), 64)
  3632. res["kprice"] = res["kprice"] + b
  3633. resH["kprice"] = resH["kprice"] + h*b
  3634. resS["kprice"] = resS["kprice"] + s*b
  3635. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  3636. res["nm"] = res["nm"] + b
  3637. resH["nm"] = resH["nm"] + h*b
  3638. resS["nm"] = resS["nm"] + s*b
  3639. b, _ = strconv.ParseFloat(restem[0]["ndf"].(string), 64)
  3640. res["ndf"] = res["ndf"] + b
  3641. resH["ndf"] = resH["ndf"] + h*b
  3642. resS["ndf"] = resS["ndf"] + s*b
  3643. b, _ = strconv.ParseFloat(restem[0]["nm"].(string), 64)
  3644. res["nm"] = res["nm"] + b
  3645. resH["nm"] = resH["nm"] + h*b
  3646. resS["nm"] = resS["nm"] + s*b
  3647. b, _ = strconv.ParseFloat(restem[0]["nmd"].(string), 64)
  3648. res["nmd"] = res["nmd"] + b
  3649. resH["nmd"] = resH["nmd"] + h*b
  3650. resS["nmd"] = resS["nmd"] + s*b
  3651. b, _ = strconv.ParseFloat(restem[0]["nuint"].(string), 64)
  3652. res["nuint"] = res["nuint"] + b
  3653. resH["nuint"] = resH["nuint"] + h*b
  3654. resS["nuint"] = resS["nuint"] + s*b
  3655. b, _ = strconv.ParseFloat(restem[0]["p"].(string), 64)
  3656. res["p"] = res["p"] + b
  3657. resH["p"] = resH["p"] + h*b
  3658. resS["p"] = resS["p"] + s*b
  3659. b, _ = strconv.ParseFloat(restem[0]["pd"].(string), 64)
  3660. res["pd"] = res["pd"] + b
  3661. resH["pd"] = resH["pd"] + h*b
  3662. resS["pd"] = resS["pd"] + s*b
  3663. b, _ = strconv.ParseFloat(restem[0]["starch"].(string), 64)
  3664. res["starch"] = res["starch"] + b
  3665. resH["starch"] = resH["starch"] + h*b
  3666. resS["starch"] = resS["starch"] + s*b
  3667. b, _ = strconv.ParseFloat(restem[0]["trans4f"].(string), 64)
  3668. res["trans4f"] = res["trans4f"] + b
  3669. resH["trans4f"] = resH["trans4f"] + h*b
  3670. resS["trans4f"] = resS["trans4f"] + s*b
  3671. b, _ = strconv.ParseFloat(restem[0]["trans35f"].(string), 64)
  3672. res["trans35f"] = res["trans35f"] + b
  3673. resH["trans35f"] = resH["trans35f"] + h*b
  3674. resS["trans35f"] = resS["trans35f"] + s*b
  3675. b, _ = strconv.ParseFloat(restem[0]["uprice"].(string), 64)
  3676. res["uprice"] = res["uprice"] + b
  3677. resH["uprice"] = resH["uprice"] + h*b
  3678. resS["uprice"] = resS["uprice"] + s*b
  3679. b, _ = strconv.ParseFloat(restem[0]["upriced"].(string), 64)
  3680. res["upriced"] = res["upriced"] + b
  3681. resH["upriced"] = resH["upriced"] + h*b
  3682. resS["upriced"] = resS["upriced"] + s*b
  3683. }
  3684. }
  3685. for key, _ := range res {
  3686. resf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", res[key]/float64(i)), 64)
  3687. resHf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resH[key]/float64(i)), 64)
  3688. resSf, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", resS[key]/float64(i)), 64)
  3689. res[key] = resf
  3690. resH[key] = resHf
  3691. resS[key] = resSf
  3692. }
  3693. resAll := make([]map[string]float64, 0)
  3694. resAll = append(resAll, res, resH, resS)
  3695. return resAll, nil
  3696. }
  3697. func Feedprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  3698. sql, p := restful.GetSqlByNameDBT(parammaps["name"].(string), tx)
  3699. s_params := make([]interface{}, 0)
  3700. paramslist := strings.Split(p, ",")
  3701. if len(paramslist) > 0 && p != "" {
  3702. for _, value := range paramslist {
  3703. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3704. }
  3705. }
  3706. res, err := tx.SQL(sql, s_params...).Query().List()
  3707. return res, err
  3708. }
  3709. func SHprovider(parammaps map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  3710. sql, p := restful.GetSqlByNameDBT(parammaps["name1"].(string), tx)
  3711. //println(parammaps["name1"].(string),sql)
  3712. s_params := make([]interface{}, 0)
  3713. paramslist := strings.Split(p, ",")
  3714. if len(paramslist) > 0 && p != "" {
  3715. for _, value := range paramslist {
  3716. s_params = append(s_params, parammaps[strings.Trim(value, " ")])
  3717. }
  3718. }
  3719. res, err := tx.SQL(sql, s_params...).Query().List()
  3720. return res, err
  3721. }
  3722. func CalNUR(formulas []map[string]interface{}, nur *Nur) (map[string]interface{}, error) {
  3723. //formulaADF := make([]map[string]interface{},1)//ADF
  3724. //formulaNDF := make([]map[string]interface{},1)//NDF
  3725. //formulaNFC := make([]map[string]interface{},1)//NFC
  3726. //formulacprotedm := make([]map[string]interface{},1)//小肠可消化粗蛋白
  3727. //formuladry := make([]map[string]interface{},1) //干物质
  3728. //formulastarch := make([]map[string]interface{},1)//淀粉
  3729. //formulap := make([]map[string]interface{},1)//磷
  3730. //formulacNDF := make([]map[string]interface{},1)//粗料中的NDF %DM
  3731. //formulajcrate := make([]map[string]interface{},1)//粗料比例
  3732. //formulacprote := make([]map[string]interface{},1)//粗蛋白
  3733. //formulaenergy := make([]map[string]interface{},1)//能量
  3734. //formulafat := make([]map[string]interface{},1)//脂肪
  3735. //formulaga := make([]map[string]interface{},1)//钙
  3736. //formulafeedrate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质4.0
  3737. //formulafeed35rate := make([]map[string]interface{},1)//饲料转化效率,标准乳/干物质3.5
  3738. resmap := make(map[string]float64)
  3739. resmapstr := make(map[string]string)
  3740. resAll := make(map[string]interface{})
  3741. for _, formula := range formulas {
  3742. if formula["type"].(int64) == 0 {
  3743. resmapstr[formula["note"].(string)] = formula["formula"].(string)
  3744. continue
  3745. }
  3746. res, err := PraseFormula(formula["formula"].(string), nur)
  3747. if err != nil {
  3748. logging.Error("PraseFormula err", err.Error(), formula["nutrition"].(string))
  3749. //println("PraseFormula err", err.Error(), formula["nutrition"].(string))
  3750. }
  3751. switch res.(type) {
  3752. case string:
  3753. resmapstr[formula["note"].(string)] = res.(string)
  3754. case float64, int, float32, int64, int32, int16, int8:
  3755. var rest float64
  3756. switch res.(type) {
  3757. case float64:
  3758. rest = res.(float64)
  3759. case int:
  3760. rest = float64(res.(int))
  3761. case int64:
  3762. rest = float64(res.(int64))
  3763. case int32:
  3764. rest = float64(res.(int32))
  3765. case int16:
  3766. rest = float64(res.(int16))
  3767. case int8:
  3768. rest = float64(res.(int8))
  3769. }
  3770. //fmt.Printf("==============oo %+v %+v %+v\n", res,rest,formula["nutrition"].(string))
  3771. switch formula["nutrition"].(string) {
  3772. case "ADF %DM":
  3773. resmap["adf"] = resmap["adf"] + rest
  3774. case "NDF %DM":
  3775. resmap["ndf"] = resmap["ndf"] + rest
  3776. case "NFC %DM":
  3777. resmap["nfc"] = resmap["nfc"] + rest
  3778. //case "小肠可消化粗蛋白":
  3779. // resmap["cprotedm"] = resmap["cprotedm"] + rest
  3780. case "干物质":
  3781. resmap["dry"] = resmap["dry"] + rest
  3782. case "淀粉":
  3783. resmap["starch"] = resmap["starch"] + rest
  3784. case "磷":
  3785. resmap["p"] = resmap["p"] + rest
  3786. case "粗料中的NDF %DM":
  3787. resmap["cndf"] = resmap["cndf"] + rest
  3788. case "粗料比例":
  3789. resmap["jcrate"] = resmap["jcrate"] + rest
  3790. case "粗蛋白":
  3791. resmap["cp"] = resmap["cp"] + rest
  3792. case "能量":
  3793. resmap["nm"] = resmap["nm"] + rest
  3794. case "脂肪":
  3795. resmap["fat"] = resmap["fat"] + rest
  3796. case "钙":
  3797. resmap["ca"] = resmap["ca"] + rest
  3798. case "饲料转化效率,标准乳/干物质":
  3799. resmap["trans4f"] = resmap["trans4f"] + rest
  3800. case "饲料转化效率,标准乳/干物质3.5":
  3801. resmap["trans35f"] = resmap["trans35f"] + rest
  3802. }
  3803. default:
  3804. fmt.Printf("===============%T \n", res)
  3805. }
  3806. }
  3807. if resmap["dry"] == 0 { //粗蛋白%dm = 粗蛋白/干物质
  3808. resmap["cpd"] = 0
  3809. resmap["cad"] = 0
  3810. resmap["pd"] = 0
  3811. resmap["nuint"] = 0
  3812. resmap["nmd"] = 0
  3813. } else {
  3814. resmap["cpd"] = resmap["cp"] / resmap["dry"] / 10
  3815. resmap["cad"] = resmap["ca"] / resmap["dry"] / 10
  3816. resmap["pd"] = resmap["p"] / resmap["dry"] / 10
  3817. resmap["nuint"] = resmap["nm"] / 3.138
  3818. resmap["nmd"] = resmap["nm"] * 0.2389 / resmap["dry"]
  3819. }
  3820. for key, value := range resmap {
  3821. va, _ := strconv.ParseFloat(fmt.Sprintf("%.3f", value), 64)
  3822. resAll[key] = va
  3823. }
  3824. for key, value := range resmapstr {
  3825. //println(key, value)
  3826. resAll[key] = value
  3827. }
  3828. resAll["uprice"] = ""
  3829. resAll["upriced"] = ""
  3830. resAll["kprice"] = ""
  3831. return resAll, nil
  3832. }
  3833. func PraseFormula(formula string, nur *Nur) (interface{}, error) {
  3834. str := formula
  3835. str = strings.ReplaceAll(str, "日增重", strconv.FormatFloat(nur.Dayw, 'f', -1, 64))
  3836. //str = strings.ReplaceAll(str, "体重", nur.Bw)
  3837. str = strings.ReplaceAll(str, "怀孕天数", strconv.FormatFloat(nur.Dayspre, 'f', -1, 64))
  3838. str = strings.ReplaceAll(str, "产量", strconv.FormatFloat(nur.Product, 'f', -1, 64))
  3839. str = strings.ReplaceAll(str, "乳脂率", strconv.FormatFloat(nur.Fat, 'f', -1, 64))
  3840. str = strings.ReplaceAll(str, "乳蛋白率", strconv.FormatFloat(nur.Pro, 'f', -1, 64))
  3841. str = strings.ReplaceAll(str, "乳糖率", strconv.FormatFloat(nur.Lactose, 'f', -1, 64))
  3842. str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64)) // 3.5乳脂率
  3843. //str = strings.ReplaceAll(str, "标准乳", strconv.FormatFloat(0.35*nur.Product+18.57*nur.Fat/100*nur.Product, 'f', -1, 64))
  3844. //println("标准乳--",strconv.FormatFloat(0.4*nur.Product+15*nur.Fat/100*nur.Product, 'f', -1, 64))
  3845. str = strings.ReplaceAll(str, "泌乳天数", strconv.FormatFloat(nur.Avgdim, 'f', -1, 64))
  3846. str = strings.ReplaceAll(str, "月龄", strconv.FormatFloat(nur.Avgmonthage, 'f', -1, 64))
  3847. str = strings.ReplaceAll(str, "温度", strconv.FormatFloat(nur.Tem, 'f', -1, 64))
  3848. str = strings.ReplaceAll(str, "胎次", strconv.FormatFloat(nur.Fetal, 'f', -1, 64))
  3849. str = strings.ReplaceAll(str, "体重", strconv.FormatFloat(nur.Bw, 'f', -1, 64))
  3850. str = strings.ReplaceAll(str, "Math.pow", "Mathpow")
  3851. str = strings.ReplaceAll(str, "Math.round", "Mathround")
  3852. str = strings.ReplaceAll(str, "Math.log", "Mathlog")
  3853. str = strings.ReplaceAll(str, "Math.exp", "Mathexp")
  3854. //str := "(体重>200?4.2/0.55*Math.pow(体重,0.75):3.5/0.55*Math.pow(体重,0.75))"
  3855. functions := map[string]govaluate.ExpressionFunction{
  3856. "strlen": func(args ...interface{}) (interface{}, error) {
  3857. length := len(args[0].(string))
  3858. return (float64)(length), nil
  3859. },
  3860. "Mathpow": func(args ...interface{}) (interface{}, error) {
  3861. c := math.Pow(args[0].(float64), args[1].(float64))
  3862. return (float64)(c), nil
  3863. },
  3864. "Mathround": func(args ...interface{}) (interface{}, error) {
  3865. c := math.Round(args[0].(float64))
  3866. return (float64)(c), nil
  3867. },
  3868. "Mathlog": func(args ...interface{}) (interface{}, error) {
  3869. c := math.Log(args[0].(float64))
  3870. return (float64)(c), nil
  3871. },
  3872. "Mathexp": func(args ...interface{}) (interface{}, error) {
  3873. c := math.Exp(args[0].(float64))
  3874. return (float64)(c), nil
  3875. },
  3876. }
  3877. expression, err := govaluate.NewEvaluableExpressionWithFunctions(str, functions)
  3878. if err != nil {
  3879. return 0, err
  3880. }
  3881. //fmt.Printf("== %+v\n", str)
  3882. result, err := expression.Evaluate(nil)
  3883. //fmt.Printf("== %+v\n", result)
  3884. if err != nil || result == nil {
  3885. return 0, err
  3886. }
  3887. return result, err
  3888. }
  3889. func HttpPostData(url, RequestMethod, parammaps string, tx *xorm.Session) string {
  3890. fsion := gofasion.NewFasion(parammaps)
  3891. sqlnamestr := fsion.Get("name").ValueStr()
  3892. sqlnamestr1 := fsion.Get("name1").ValueStr()
  3893. offset := fsion.Get("offset").ValueDefaultInt(0)
  3894. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  3895. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  3896. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  3897. sql1, p1 := restful.GetSqlByNameDBT(sqlnamestr1, tx)
  3898. body := []byte("")
  3899. if sql != "" {
  3900. s_params := make([]interface{}, 0)
  3901. if fsion.HasKey("parammaps") {
  3902. parammaps := fsion.Get("parammaps")
  3903. paramslist := strings.Split(p, ",")
  3904. if len(paramslist) > 0 && p != "" {
  3905. for _, value := range paramslist {
  3906. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  3907. }
  3908. }
  3909. }
  3910. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  3911. if err != nil {
  3912. } else {
  3913. paramslist1 := strings.Split(p1, ",")
  3914. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  3915. s_params1 := make([]interface{}, 0)
  3916. if len(paramslist1) > 0 && p1 != "" {
  3917. for _, v := range paramslist1 {
  3918. s_params1 = append(s_params1, value[strings.ToLower(strings.Trim(v, " "))])
  3919. }
  3920. }
  3921. arr, _ := tx.SQL(sql1, s_params1...).QueryString() //获取
  3922. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index]["arrList"] = arr
  3923. body, _ = json.Marshal(queryData)
  3924. }
  3925. }
  3926. }
  3927. bs := bytes.NewBuffer(body)
  3928. client := &http.Client{}
  3929. //body1 := bytes.NewBuffer([]byte("{\"username\":\"00028\",\"password\":\"123456\"}"))
  3930. req, err := http.NewRequest(RequestMethod,
  3931. url,
  3932. bs,
  3933. )
  3934. if err != nil {
  3935. }
  3936. req.Header.Set("Content-Type", "application/json;charset=utf-8")
  3937. req.Header.Set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6IjAwMDI4IiwicGFzc3dvcmQiOiJlMTBhZGMzOTQ5YmE1OWFiYmU1NmUwNTdmMjBmODgzZSIsImV4cCI6MTYxOTM4NjU0MiwiaXNzIjoiaHR0cHM6Ly9naXRodWIuY29tL2twdHl1bi9nby1hZG1pbi8ifQ.sJwnE9HTOVnDNOaeHqOQzi7cSydYkWS4uQHxtjY7UMY")
  3938. if err != nil {
  3939. // handle error
  3940. }
  3941. resp, err := client.Do(req)
  3942. if err != nil {
  3943. fmt.Println(err)
  3944. return ""
  3945. }
  3946. defer resp.Body.Close()
  3947. result_body, err := ioutil.ReadAll(resp.Body)
  3948. if err != nil {
  3949. fmt.Println(err)
  3950. return ""
  3951. }
  3952. fmt.Println(string(result_body))
  3953. return ""
  3954. }