db.go 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263
  1. package api
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "math"
  9. "net/http"
  10. "os"
  11. "path"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/360EntSecGroup-Skylar/excelize"
  17. "github.com/jmoiron/sqlx"
  18. "github.com/pkg/errors"
  19. // "github.com/360EntSecGroup-Skylar/excelize"
  20. "github.com/Anderson-Lu/gofasion/gofasion"
  21. "github.com/axetroy/go-fs"
  22. "github.com/gin-gonic/gin"
  23. "github.com/tealeg/xlsx"
  24. // "github.com/xormplus/xorm"
  25. "kpt.xdmy/apiserver/routers/restful"
  26. "kpt.xdmy/pkg/app"
  27. "kpt.xdmy/pkg/e"
  28. "kpt.xdmy/pkg/logging"
  29. "kpt.xdmy/pkg/setting"
  30. "kpt.xdmy/pkg/util"
  31. )
  32. // @Summary 根据SQL执行得到数据集
  33. // @Tags getData
  34. // @Accept json
  35. // @Produce json
  36. // @Param sql query string true "sql"
  37. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  38. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  39. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  40. // @Router /data/getdata [POST]
  41. func GetData(c *gin.Context) {
  42. appG := app.Gin{C: c}
  43. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  44. fsion := gofasion.NewFasion(string(dataByte))
  45. sqlstr := fsion.Get("sql").ValueStr()
  46. offset := fsion.Get("offset").ValueDefaultInt(0)
  47. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  48. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  49. queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
  50. if err != nil {
  51. appG.Response(http.StatusOK, e.ERROR, err.Error())
  52. } else {
  53. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  54. }
  55. }
  56. func Testpartitions(c *gin.Context) {
  57. appG := app.Gin{C: c}
  58. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  59. fsion := gofasion.NewFasion(string(dataByte))
  60. databaseName := fsion.Get("databaseName").ValueStr() //获取新数据库名称
  61. user := fsion.Get("user").ValueStr() //获取新数据库用户
  62. passWord := fsion.Get("passWord").ValueStr() //获取新数据库名称
  63. host := fsion.Get("host").ValueStr() //获取新数据库名称
  64. port := fsion.Get("port").ValueStr()
  65. sqlstr := "SELECT sql FROM sql_new "
  66. var sqlname string
  67. err1 := restful.BasicList(sqlstr, nil, &sqlname)
  68. if err1 != nil {
  69. appG.Response(http.StatusOK, e.SUCCESS, "失败:"+err1.Error())
  70. }
  71. sqlstrs := strings.Split(sqlname, ";") //分割sql
  72. // sqlstrs := strings.Split(p["sql"].(string), ";") //分割sql
  73. connecting := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, "mysql")
  74. mydb, err := sql.Open("mysql", connecting) //连接新连接
  75. if err != nil {
  76. println("err1", err.Error())
  77. }
  78. _, err = mydb.Exec("CREATE DATABASE IF NOT EXISTS `" + databaseName + "`") // 创建数据库
  79. if err != nil {
  80. println("err2", err.Error())
  81. }
  82. mydb.Close()
  83. connecting = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8", user, passWord, host, port, databaseName) //连接新数据库,创建表格
  84. mydb, err = sql.Open("mysql", connecting)
  85. if err != nil {
  86. println("err3", err.Error())
  87. }
  88. defer mydb.Close()
  89. if err == nil {
  90. mydb.SetMaxOpenConns(10)
  91. mydb.SetMaxIdleConns(10)
  92. }
  93. errmsg := ""
  94. if err == nil {
  95. //tx ,_ :=mydb.Begin()
  96. for _, sqlstr := range sqlstrs {
  97. //println("sqlstr---",sqlstr)
  98. if sqlstr == "" {
  99. continue
  100. }
  101. _, err = mydb.Exec(sqlstr)
  102. if err != nil {
  103. println("err4", err.Error())
  104. errmsg = err.Error()
  105. }
  106. }
  107. }
  108. if errmsg == "" {
  109. appG.Response(http.StatusOK, e.SUCCESS, "成功"+databaseName)
  110. } else {
  111. appG.Response(http.StatusOK, e.SUCCESS, "失败"+errmsg)
  112. }
  113. }
  114. func TestData(c *gin.Context) {
  115. appG := app.Gin{C: c}
  116. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  117. fsion := gofasion.NewFasion(string(dataByte))
  118. sqlstr := fsion.Get("sql").ValueStr()
  119. token := ""
  120. if tempv, exists := c.Get("jwt_username"); exists {
  121. token = tempv.(string)
  122. }
  123. appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
  124. }
  125. // @Summary 根据APISQL表中SQL执行得到数据集
  126. // @Tags GetDataByName
  127. // @Accept json
  128. // @Produce json
  129. // @Param sqlname body string true "sqlname"
  130. // @Param offset body int true "offset"
  131. // @Param pagecount body int true "pagecount"
  132. // @Param returntype body string true "returntype"
  133. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  134. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  135. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  136. // @Router /data/GetDataByName [POST]
  137. func GetDataByName(c *gin.Context) {
  138. appG := app.Gin{C: c}
  139. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  140. fsion := gofasion.NewFasion(string(dataByte))
  141. sqlnamestr := fsion.Get("name").ValueStr()
  142. offset := fsion.Get("offset").ValueDefaultInt(0)
  143. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  144. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  145. getTotal := fsion.Get("getTotal").ValueStr()
  146. paramsTest := fsion.Get("parammaps")
  147. fmt.Println(paramsTest.Get("partClass").ValueStr())
  148. sql, p := restful.GetSqlByNameDBT(sqlnamestr, nil)
  149. if sql != "" {
  150. s_params := make([]interface{}, 0)
  151. if fsion.HasKey("parammaps") {
  152. parammaps := fsion.Get("parammaps")
  153. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  154. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  155. }
  156. paramslist := strings.Split(p, ",")
  157. if len(paramslist) > 0 && p != "" {
  158. for _, value := range paramslist {
  159. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  160. if tempv, exists := c.Get("jwt_username"); exists {
  161. s_params = append(s_params, tempv.(string))
  162. } else {
  163. s_params = append(s_params, "")
  164. }
  165. } else {
  166. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  167. }
  168. }
  169. }
  170. } else if fsion.HasKey("params") {
  171. params := fsion.Get("params").Array()
  172. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  173. for _, v_params := range params {
  174. s_params = append(s_params, v_params.ValueStr())
  175. }
  176. }
  177. if returntype == "xlsx" {
  178. var xlsxfile *xlsx.File
  179. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  180. //c.File("c:/dd.xlsx")
  181. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  182. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  183. c.Header("Content-Transfer-Encoding", "binary")
  184. _ = xlsxfile.Write(c.Writer)
  185. } else {
  186. // queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  187. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
  188. if getTotal == "total3" {
  189. totalData := make(map[string]interface{})
  190. queryData1 := queryData.(map[string]interface{})
  191. totalData["total3"] = queryData1["total"]
  192. appG.Response(http.StatusOK, e.SUCCESS, totalData)
  193. return
  194. }
  195. if err != nil {
  196. logging.Error("GetDataByName err: ", err)
  197. msg := geterrmsg(err.Error())
  198. appG.Response(http.StatusOK, e.ERROR, msg)
  199. } else {
  200. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  201. }
  202. }
  203. } else {
  204. appG.Response(http.StatusOK, e.SUCCESS, nil)
  205. }
  206. defer c.Request.Body.Close()
  207. }
  208. // @Summary 根据APISQL表中SQL执行得到数据集
  209. // @Tags GetDataByName
  210. // @Accept json
  211. // @Produce json
  212. // @Param sqlname body string true "sqlname"
  213. // @Param offset body int true "offset"
  214. // @Param pagecount body int true "pagecount"
  215. // @Param returntype body string true "returntype"
  216. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  217. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  218. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  219. // @Router /data/GetDataByName [POST]
  220. func EasSync(c *gin.Context) {
  221. appG := app.Gin{C: c}
  222. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  223. fsion := gofasion.NewFasion(string(dataByte))
  224. typeIn := fsion.Get("typeIn").ValueDefaultInt(0)
  225. billCode := fsion.Get("billCode").ValueStr()
  226. logging.Error("EasSync date: ", typeIn, billCode)
  227. //typeInstr := fsion.Get("typeIn").ValueStr()
  228. //println(typeInstr,typeIn)
  229. // 用于判断备件干什么的和eas同步
  230. err := util.EASBillSync(typeIn, billCode, "1")
  231. if err != nil {
  232. logging.Error("EasSync err: ", err)
  233. appG.Response(http.StatusOK, e.ERROR, err.Error())
  234. } else {
  235. appG.Response(http.StatusOK, e.SUCCESS, "成功")
  236. }
  237. }
  238. func GetDataByNameOPEN(c *gin.Context) {
  239. appG := app.Gin{C: c}
  240. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  241. fsion := gofasion.NewFasion(string(dataByte))
  242. sqlnamestr := fsion.Get("name").ValueStr()
  243. sqlnamestr = sqlnamestr + "OPEN"
  244. offset := fsion.Get("offset").ValueDefaultInt(0)
  245. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  246. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  247. sql, p := restful.GetSqlByNameDBT(sqlnamestr, nil)
  248. //fmt.Println("sql========\n",sqlnamestr,sql)
  249. if sql != "" {
  250. s_params := make([]interface{}, 0)
  251. if fsion.HasKey("parammaps") {
  252. parammaps := fsion.Get("parammaps")
  253. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  254. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  255. }
  256. paramslist := strings.Split(p, ",")
  257. if len(paramslist) > 0 && p != "" {
  258. for _, value := range paramslist {
  259. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  260. if tempv, exists := c.Get("jwt_username"); exists {
  261. s_params = append(s_params, tempv.(string))
  262. } else {
  263. s_params = append(s_params, "")
  264. }
  265. } else {
  266. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  267. }
  268. }
  269. }
  270. } else if fsion.HasKey("params") {
  271. params := fsion.Get("params").Array()
  272. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  273. for _, v_params := range params {
  274. s_params = append(s_params, v_params.ValueStr())
  275. }
  276. }
  277. if returntype == "xlsx" {
  278. var xlsxfile *xlsx.File
  279. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  280. //c.File("c:/dd.xlsx")
  281. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  282. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  283. c.Header("Content-Transfer-Encoding", "binary")
  284. _ = xlsxfile.Write(c.Writer)
  285. } else {
  286. // queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  287. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
  288. if err != nil {
  289. logging.Error("GetDataByName err: ", err)
  290. msg := geterrmsg(err.Error())
  291. appG.Response(http.StatusOK, e.ERROR, msg)
  292. } else {
  293. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  294. }
  295. }
  296. } else {
  297. appG.Response(http.StatusOK, e.SUCCESS, nil)
  298. }
  299. }
  300. func geterrmsg(errstr string) (msgstr string) {
  301. var hzRegexp = regexp.MustCompile("^[\u4e00-\u9fa5]$") // 判断是否为汉字
  302. rs := []rune(errstr)
  303. if len(rs) == 0 {
  304. msgstr = errstr
  305. return
  306. }
  307. if hzRegexp.MatchString(string(rs[0])) {
  308. msgstr = errstr
  309. return
  310. }
  311. if strings.Contains(errstr, "Duplicate entry") {
  312. strs := strings.Split(errstr, ` key '`)
  313. if len(strs) > 1 {
  314. if len(strs[len(strs)-1]) > 1 {
  315. errstr = "Duplicate entry :" + strs[len(strs)-1]
  316. }
  317. }
  318. }
  319. msg, err := restful.MapStrList("SELECT msg FROM err_msg where err = ? ", errstr)
  320. if err != nil {
  321. msgstr = errstr
  322. return
  323. }
  324. if msg == nil || len(msg) == 0 {
  325. var param []interface{}
  326. param = append(param, errstr, errstr)
  327. go func() {
  328. tx, err := restful.Dbs.Beginx()
  329. defer func() {
  330. switch {
  331. case err != nil:
  332. logging.Error("tx.Begin 事务启动失败__error:", err)
  333. if tx != nil {
  334. tx.Rollback()
  335. }
  336. default:
  337. if tx != nil {
  338. err = tx.Commit()
  339. }
  340. }
  341. if tx != nil {
  342. }
  343. }()
  344. _, err = restful.ExecQueryT("insert into err_msg(err,msg) VALUES(?,?) ON DUPLICATE KEY UPDATE err=err", param, tx)
  345. if err != nil {
  346. logging.Error("insert into err_msg(err,msg) err", err)
  347. }
  348. }()
  349. msgstr = errstr
  350. } else {
  351. msgstr = msg[0]["msg"]
  352. }
  353. return
  354. }
  355. // @Summary 根据APISQL表中SQL执行得到数据集
  356. // @Tags GetDataByNameForm
  357. // @Accept json
  358. // @Produce json
  359. // @Param sqlname body json true "sqlname"
  360. // @Param offset query int true "offset"
  361. // @Param pagecount query int true "pagecount"
  362. // @Param returntype query string true "returntype"
  363. // @Param s formData json true "ss"
  364. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  365. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  366. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  367. // @Router /data/GetDataByNameForm [POST]
  368. func GetDataByNameForm(c *gin.Context) {
  369. appG := app.Gin{C: c}
  370. //sqlnamestr := c.PostForm("measureNumber")
  371. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  372. //fsion := gofasion.NewFasion(string(dataByte))
  373. //sqlnamestr := fsion.Get("name").ValueStr()
  374. //offset := fsion.Get("offset").ValueInt()
  375. //pagecount := fsion.Get("pagecount").ValueInt()
  376. //returntype := fsion.Get("returntype").ValueStr()
  377. //params := fsion.Get("params").Array()
  378. c.Request.ParseForm()
  379. params := c.Request.Form
  380. offset := 0
  381. pageSize_ := 0
  382. returntype := params.Get("page")
  383. if returntype == "" {
  384. returntype = "0"
  385. }
  386. if tempv, exists := c.Get("jwt_username"); exists {
  387. params.Set("JwtUserName", tempv.(string))
  388. } else {
  389. params.Set("JwtUserName", "")
  390. }
  391. sqlnamestr := params.Get("name")
  392. logging.Info("GetDataByNameForm ", c.Keys, sqlnamestr)
  393. s_params := make([]interface{}, 0)
  394. if returntype == "1" {
  395. pageNum, error := strconv.Atoi(params.Get("pageNum"))
  396. if error != nil {
  397. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
  398. }
  399. pageSize, error := strconv.Atoi(params.Get("pageSize"))
  400. if error != nil {
  401. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
  402. }
  403. pageSize_ = pageSize
  404. offset = pageNum
  405. }
  406. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  407. paramslist := strings.Split(p, ",")
  408. if len(paramslist) > 0 && p != "" {
  409. for _, value := range paramslist {
  410. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  411. }
  412. }
  413. if sql != "" {
  414. if returntype == "xlsx" {
  415. var xlsxfile *xlsx.File
  416. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  417. c.Header("Content-Type", "application/octet-stream")
  418. c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
  419. c.Header("Content-Transfer-Encoding", "binary")
  420. _ = xlsxfile.Write(c.Writer)
  421. } else {
  422. queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
  423. if err != nil {
  424. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  425. } else {
  426. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  427. }
  428. }
  429. } else {
  430. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  431. }
  432. }
  433. // @Summary 递归获取所有
  434. // @Tags menu
  435. // @Accept json
  436. // @Produce json
  437. // @Param Ton query string true "Ton"
  438. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  439. // @Router /authdata/GetRoleMenus [GET]
  440. func GetRecuDataByName(c *gin.Context) {
  441. appG := app.Gin{C: c}
  442. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  443. fsion := gofasion.NewFasion(string(dataByte))
  444. sqlnamestr := fsion.Get("name").ValueStr()
  445. parentidname := fsion.Get("idname").ValueStr()
  446. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  447. params := make(map[string]interface{})
  448. s_params := make([]interface{}, 0)
  449. if fsion.HasKey("params") {
  450. for _, v_params := range fsion.Get("params").Array() {
  451. s_params = append(s_params, v_params.ValueStr())
  452. }
  453. params["params"] = s_params
  454. } else {
  455. jparams := fsion.Get("parammaps")
  456. paramslist := strings.Split(p, ",")
  457. if len(paramslist) > 0 && p != "" {
  458. for _, value := range paramslist {
  459. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  460. if tempv, exists := c.Get("jwt_username"); exists {
  461. params[value] = tempv.(string)
  462. } else {
  463. params[value] = ""
  464. }
  465. } else {
  466. params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
  467. }
  468. }
  469. }
  470. }
  471. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
  472. logging.Info("sqlname parammaps", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  473. if err != nil {
  474. appG.Response(http.StatusOK, e.ERROR, err.Error())
  475. }
  476. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  477. }
  478. // @Summary 根据APISQL表中SQL执行得到数据集
  479. // @Tags GetDataByNames
  480. // @Accept json
  481. // @Produce json
  482. // @RequestBody sqlname query string true "sqlname"
  483. // @Param offset query int true "offset"
  484. // @Param pagecount query int true "pagecount"
  485. // @Param returntype query string true "returntype"
  486. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  487. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  488. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  489. // @Router /data/GetDataByNames [POST]
  490. func GetDataByNames(c *gin.Context) {
  491. appG := app.Gin{C: c}
  492. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  493. fsion := gofasion.NewFasion(string(dataByte))
  494. sqlParams := fsion.Array()
  495. queryData := make(map[string]interface{})
  496. for _, v := range sqlParams {
  497. fsion2 := gofasion.NewFasion(v.Json())
  498. logging.Info("GetDataByNames", c.Keys, fsion2.Get("name").ValueStr(), c.Request.RemoteAddr)
  499. // p是sql表中的sql语句中的变量差数格式
  500. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  501. if sql == "" {
  502. errorm := make(map[string]interface{})
  503. errorm["code"] = e.ERROR
  504. errorm["message"] = "sql没找到"
  505. queryData[fsion2.Get("name").ValueStr()] = errorm
  506. continue
  507. } else if sql != "" {
  508. offset := fsion2.Get("offset").ValueInt()
  509. pagecount := fsion2.Get("pagecount").ValueInt()
  510. returntype := fsion2.Get("returntype").ValueStr()
  511. // s_params 是用来存储sql中需要的参数
  512. s_params := make([]interface{}, 0)
  513. // 这边是判断是传过来的是参数格式如果是map格式的是需要在apisql表中查询params字段
  514. if fsion2.HasKey("parammaps") {
  515. parammaps := fsion2.Get("parammaps")
  516. paramslist := strings.Split(p, ",")
  517. if len(paramslist) > 0 && p != "" {
  518. for _, value := range paramslist {
  519. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  520. if tempv, exists := c.Get("jwt_username"); exists {
  521. s_params = append(s_params, tempv.(string))
  522. } else {
  523. s_params = append(s_params, "")
  524. }
  525. } else {
  526. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  527. }
  528. }
  529. }
  530. } else if fsion2.HasKey("params") {
  531. params := fsion2.Get("params").Array()
  532. for _, v_params := range params {
  533. s_params = append(s_params, v_params.ValueStr())
  534. }
  535. }
  536. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  537. if err != nil {
  538. errorm := make(map[string]interface{})
  539. errorm["code"] = e.ERROR
  540. errorm["message"] = err.Error()
  541. queryData[fsion2.Get("name").ValueStr()] = errorm
  542. logging.Error("GetDataByNames err: ", err)
  543. continue
  544. } else {
  545. queryData[fsion2.Get("name").ValueStr()] = queryData1
  546. }
  547. }
  548. }
  549. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  550. }
  551. // @Summary 根据APISQL表中SQL执行得到数据集
  552. // @Tags PostDataByNameForm
  553. // @Accept json
  554. // @Produce json
  555. // @Param sqlname query string true "sqlname"
  556. // @Param offset query int true "offset"
  557. // @Param pagecount query int true "pagecount"
  558. // @Param returntype query string true "returntype"
  559. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  560. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
  561. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  562. // @Router /data/PostDataByNameForm [POST]
  563. func PostDataByNameForm(c *gin.Context) {
  564. appG := app.Gin{C: c}
  565. c.Request.ParseForm()
  566. params := c.Request.Form
  567. sqlnamestr := params.Get("name")
  568. logging.Info("PostDataByNameForm ", c.Keys, sqlnamestr)
  569. s_params := make([]interface{}, 0)
  570. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  571. paramslist := strings.Split(p, ",")
  572. if len(paramslist) > 0 {
  573. for _, value := range paramslist {
  574. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  575. }
  576. }
  577. if sql != "" {
  578. queryData, err := execDataBySql(sql, s_params)
  579. if err != nil {
  580. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  581. } else {
  582. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  583. }
  584. } else {
  585. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  586. }
  587. }
  588. func GetReportform(c *gin.Context) {
  589. appG := app.Gin{C: c}
  590. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  591. fsion := gofasion.NewFasion(string(dataByte))
  592. sqlnamestr := fsion.Get("name").ValueStr()
  593. parammaps := fsion.Get("parammaps")
  594. logging.Info("GetReportform ", c.Keys, sqlnamestr, parammaps)
  595. queryData, _ := praseReportform(sqlnamestr, parammaps)
  596. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  597. }
  598. func praseReportform(sqlnamestr string, parammaps *gofasion.Fasion) (queryData map[string]interface{}, err error) {
  599. switch sqlnamestr {
  600. case "getpSumNow", "getpSumNowWater", "getpSumNowElec", "getpSumNowDiesel":
  601. queryData, err = getpSumNow(sqlnamestr, parammaps)
  602. case "getPastureSumMonth", "getPastureSumMonthWater", "getPastureSumMonthElec", "getPastureSumMonthDiesel":
  603. queryData, err = getPastureSumMonth(sqlnamestr, parammaps)
  604. case "getPastureSumDayWater", "getPastureSumDayElec", "getPastureSumDayDiesel":
  605. queryData, err = getPastureSumDay(sqlnamestr, parammaps)
  606. case "getPastureSumDay":
  607. queryData, err = getPastureSumDay1(sqlnamestr, parammaps)
  608. case "geteqclassSumMonth", "geteqclassSumMonthWater", "geteqclassSumMonthElec", "geteqclassSumMonthDiesel":
  609. queryData, err = geteqclassSumMonth(sqlnamestr, parammaps)
  610. case "geteCowWaterList", "geteCowElecList", "geteCowDieselList", "geteCowMaintainList", "geteCowAllList":
  611. queryData, err = geteCow(sqlnamestr, parammaps)
  612. case "geteCowWaterMonthList", "geteCowElecMonthList", "geteCowDieselMonthList", "geteCowMaintainMonthList", "geteCowAllMonthList":
  613. queryData, err = geteCowMonth(sqlnamestr, parammaps)
  614. case "getEqRunSurveyListPasture":
  615. queryData, err = getEqRunSurveyListPasture(sqlnamestr, parammaps)
  616. case "geteqclassFeeSum", "geteqclassFeeSumPasture":
  617. queryData, err = geteqclassFeeSum(sqlnamestr, parammaps)
  618. case "getEqRunSurveyListDay":
  619. queryData, err = getEqRunSurveyListDay(sqlnamestr, parammaps)
  620. case "getReportEqCostYear":
  621. queryData, err = geteqCostYear(sqlnamestr, parammaps)
  622. case "getReportEqCostMonth":
  623. queryData, err = geteqCostMonth(sqlnamestr, parammaps)
  624. case "homepagePastureInfor":
  625. queryData, err = homepagePastureInfor(sqlnamestr, parammaps)
  626. case "homepageEqstatus":
  627. queryData, err = homepageEqstatus(sqlnamestr, parammaps)
  628. case "homepageEqRunstatueListPasture", "homepageEqRunstatueListEqclass":
  629. queryData, err = homepageEqRunstatue(sqlnamestr, parammaps)
  630. case "homepageFeeWX", "homepageFeeWater", "homepageFeeElec", "homepageFeeDiesel":
  631. queryData, err = homepageFee(sqlnamestr, parammaps)
  632. default:
  633. queryData, err = nil, nil
  634. }
  635. return
  636. }
  637. func homepageFee(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  638. queryData := make(map[string]interface{})
  639. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  640. pastureName := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  641. pSumNow, err := restful.MapStrList(sqlNow, pastureName, pastureName, pastureName, pastureName, pastureName, pastureName, pastureName, pastureName, pastureName, pastureName)
  642. if err != nil {
  643. log.Default().Printf("homepageFee queryx: %v", err)
  644. }
  645. typeStr := parammaps.Get(strings.Trim("type", " ")).ValueStr()
  646. list1s := make([]string, 1)
  647. list2s := make([]string, 1)
  648. list3s := make([]string, 1)
  649. list4s := make([]string, 1)
  650. list5s := make([]string, 1)
  651. list6s := make([]string, 1)
  652. list7s := make([]string, 1)
  653. list8s := make([]string, 1)
  654. list1s[0] = typeStr
  655. for _, value := range pSumNow {
  656. list2s[0] = value["monthBudget"]
  657. list3s[0] = value["sumPrice"]
  658. list4s[0] = value["monthBudgetL"]
  659. list5s[0] = value["sumPriceL"]
  660. list6s[0] = value["monthBudget"]
  661. list7s[0] = value["sumPrice"]
  662. list8s[0] = value["cowS"]
  663. }
  664. queryData["data1"] = list1s
  665. queryData["data2"] = list2s
  666. queryData["data3"] = list3s
  667. queryData["data4"] = list4s
  668. queryData["data5"] = list5s
  669. queryData["data6"] = list6s
  670. queryData["data7"] = list7s
  671. queryData["data8"] = list8s
  672. queryData["code"] = 0
  673. queryData["message"] = "ok"
  674. return queryData, nil
  675. }
  676. func homepageEqRunstatue(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  677. queryData := make(map[string]interface{})
  678. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  679. pastureName := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  680. typeStr := parammaps.Get(strings.Trim("type", " ")).ValueStr()
  681. pSumNow, _ := restful.MapStrList(sqlNow, pastureName)
  682. list1s := make([]string, 0)
  683. list2s := make([]string, 0)
  684. list3s := make([]string, 1)
  685. list3s[0] = typeStr
  686. for _, value := range pSumNow {
  687. list1s = append(list1s, value["name"])
  688. list2s = append(list2s, value["rate"])
  689. }
  690. queryData["data1"] = list1s
  691. queryData["data2"] = list2s
  692. queryData["data3"] = list3s
  693. queryData["code"] = 0
  694. queryData["message"] = "ok"
  695. return queryData, nil
  696. }
  697. func homepageEqstatus(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  698. queryData := make(map[string]interface{})
  699. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  700. pastureName := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  701. pSumNow, _ := restful.MapStrList(sqlNow, pastureName, pastureName)
  702. list1s := make([]string, 0)
  703. list2s := make([]string, 0)
  704. for _, value := range pSumNow {
  705. list1s = append(list1s, value["status"])
  706. list2s = append(list2s, value["cou"])
  707. }
  708. queryData["data1"] = list1s
  709. queryData["data2"] = list2s
  710. queryData["code"] = 0
  711. queryData["message"] = "ok"
  712. return queryData, nil
  713. }
  714. func homepagePastureInfor(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  715. queryData := make(map[string]interface{})
  716. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  717. pSumNow, _ := restful.MapStrList(sqlNow)
  718. lolalists := make([]map[string]interface{}, 0)
  719. pasturelists := make([]map[string]interface{}, 0)
  720. lolalist := make(map[string]interface{}, 1)
  721. for _, value := range pSumNow {
  722. lola := make([]string, 0)
  723. pasturelist := make(map[string]interface{}, 1)
  724. lola = append(lola, value["longitude"])
  725. lola = append(lola, value["latitude"])
  726. lolalist[value["name"]] = lola
  727. pasturelist["name"] = value["name"]
  728. pasturelist["value"] = value["name"]
  729. pasturelists = append(pasturelists, pasturelist)
  730. }
  731. lolalists = append(lolalists, lolalist)
  732. queryData["data1"] = lolalists
  733. queryData["data2"] = pasturelists
  734. queryData["code"] = 0
  735. queryData["message"] = "ok"
  736. return queryData, nil
  737. }
  738. func geteqCostMonth(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  739. queryData := make(map[string]interface{})
  740. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  741. eqId := parammaps.Get(strings.Trim("eqId", " ")).ValueStr()
  742. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  743. pSumNow, _ := restful.MapStrList(sqlNow, receiveTime, eqId, receiveTime, receiveTime, eqId, receiveTime, receiveTime)
  744. chartData := make(map[string]interface{})
  745. months := []string{}
  746. upkeepCost := []string{}
  747. repireCost := []string{}
  748. //lastTotal,nowTotal := 0,0
  749. a := true
  750. month := ""
  751. for i := 1; i <= 12; i++ {
  752. if i < 10 {
  753. month = receiveTime + "-0" + strconv.Itoa(i)
  754. } else {
  755. month = receiveTime + "-" + strconv.Itoa(i)
  756. }
  757. months = append(months, month)
  758. for _, va := range pSumNow {
  759. if va["yearR"][5:] == month[5:] {
  760. upkeepCost = append(upkeepCost, va["Ucost"])
  761. repireCost = append(repireCost, va["Rcost"])
  762. a = false
  763. }
  764. }
  765. if a {
  766. upkeepCost = append(upkeepCost, "0")
  767. repireCost = append(repireCost, "0")
  768. }
  769. a = true
  770. }
  771. chartData["months"] = months
  772. chartData["upkeepCost"] = upkeepCost
  773. chartData["repireCost"] = repireCost
  774. queryData["chart_data"] = chartData
  775. queryData["code"] = 0
  776. queryData["message"] = "ok"
  777. return queryData, nil
  778. }
  779. func geteqCostYear(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  780. queryData := make(map[string]interface{})
  781. //receiveTime :=parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  782. eqId := parammaps.Get(strings.Trim("eqId", " ")).ValueStr()
  783. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  784. pSumNow, _ := restful.MapStrList(sqlNow, eqId, eqId, eqId)
  785. chartData := make(map[string]interface{})
  786. year := []string{}
  787. upkeepCost := []string{}
  788. repireCost := []string{}
  789. for _, va := range pSumNow {
  790. year = append(year, va["yearR"])
  791. upkeepCost = append(upkeepCost, va["Ucost"])
  792. repireCost = append(repireCost, va["Rcost"])
  793. }
  794. chartData["year"] = year
  795. chartData["upkeepCost"] = upkeepCost
  796. chartData["repireCost"] = repireCost
  797. queryData["chart_data"] = chartData
  798. queryData["code"] = 0
  799. queryData["message"] = "ok"
  800. return queryData, nil
  801. }
  802. func geteCowMonth(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  803. queryData := make(map[string]interface{})
  804. receiveTime := parammaps.Get(strings.Trim("year", " ")).ValueStr()
  805. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  806. var pSumNow, pSumLast []map[string]string
  807. if sqlnamestr == "geteCowAllMonthList" {
  808. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  809. pSumNow, _ = restful.MapStrList(sqlNow, receiveTime, receiveTime, receiveTime, receiveTime, receiveTime, receiveTime, pastureNames)
  810. tempTime, _ := strconv.Atoi(receiveTime)
  811. receiveTimetemp := strconv.Itoa(tempTime - 1)
  812. pSumLast, _ = restful.MapStrList(sqlNow, receiveTimetemp, receiveTimetemp, receiveTimetemp, receiveTimetemp, receiveTimetemp, receiveTimetemp, pastureNames)
  813. } else {
  814. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  815. pSumNow, _ = restful.MapStrList(sqlNow, receiveTime, receiveTime, receiveTime, pastureNames)
  816. tempTime, _ := strconv.Atoi(receiveTime)
  817. receiveTimetemp := strconv.Itoa(tempTime - 1)
  818. pSumLast, _ = restful.MapStrList(sqlNow, receiveTimetemp, receiveTimetemp, receiveTimetemp, pastureNames)
  819. }
  820. chartData := make(map[string]interface{})
  821. months := []string{}
  822. lastYear := []string{}
  823. nowYear := []string{}
  824. budgetYear := []string{}
  825. //lastTotal,nowTotal := 0,0
  826. a, b := true, true
  827. month := ""
  828. for i := 1; i <= 12; i++ {
  829. if i < 10 {
  830. month = receiveTime + "-0" + strconv.Itoa(i)
  831. } else {
  832. month = receiveTime + "-" + strconv.Itoa(i)
  833. }
  834. months = append(months, month)
  835. for _, va := range pSumNow {
  836. if va["month"][5:] == month[5:] {
  837. nowYear = append(nowYear, va["psum"])
  838. budgetYear = append(budgetYear, va["lsum"])
  839. a = false
  840. }
  841. }
  842. for _, va := range pSumLast {
  843. if va["month"][5:] == month[5:] {
  844. lastYear = append(lastYear, va["psum"])
  845. b = false
  846. }
  847. }
  848. if a {
  849. nowYear = append(nowYear, "0")
  850. budgetYear = append(budgetYear, "0")
  851. }
  852. if b {
  853. lastYear = append(lastYear, "0")
  854. }
  855. a = true
  856. b = true
  857. }
  858. chartData["months"] = months
  859. chartData["lastYear"] = lastYear
  860. chartData["nowYear"] = nowYear
  861. chartData["budgetYear"] = budgetYear
  862. queryData["chart_data"] = chartData
  863. queryData["code"] = 0
  864. queryData["message"] = "ok"
  865. return queryData, nil
  866. }
  867. func getEqRunSurveyListDay(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  868. queryData := make(map[string]interface{})
  869. receiveTime := parammaps.Get(strings.Trim("praseTime", " ")).ValueStr()
  870. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  871. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  872. pSumNow, _ := restful.MapStrList(sqlNow, pastureNames, receiveTime)
  873. if pSumNow == nil {
  874. return nil, nil
  875. }
  876. typeName := make([]map[string]interface{}, 4)
  877. ma := make(map[string]interface{})
  878. ma1 := make(map[string]interface{})
  879. ma2 := make(map[string]interface{})
  880. ma3 := make(map[string]interface{})
  881. ma["name"] = "在用"
  882. ma["value"] = pSumNow[0]["eqUse"]
  883. typeName[0] = ma
  884. ma1["name"] = "闲置"
  885. ma1["value"] = pSumNow[0]["eqXZ"]
  886. typeName[1] = ma1
  887. ma2["name"] = "报废"
  888. ma2["value"] = pSumNow[0]["eqBF"]
  889. typeName[2] = ma2
  890. ma3["name"] = "封存"
  891. ma3["value"] = pSumNow[0]["eqFC"]
  892. typeName[3] = ma3
  893. queryData["psm"] = typeName
  894. queryData["code"] = 0
  895. queryData["total"] = pSumNow[0]["eqSum"]
  896. queryData["message"] = "ok"
  897. return queryData, nil
  898. }
  899. func getEqRunSurveyListPasture(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  900. queryData := make(map[string]interface{})
  901. receiveTime := parammaps.Get(strings.Trim("praseTime", " ")).ValueStr()
  902. pastureNames, _ := restful.MapStrList("SELECT `name` FROM pasture order by sort")
  903. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  904. pSumNow, _ := restful.MapStrList(sqlNow, receiveTime)
  905. chartData := make(map[string]interface{})
  906. pasture := []string{}
  907. eqUse := []string{}
  908. eqXZ := []string{}
  909. eqBF := []string{}
  910. eqFC := []string{}
  911. //lastTotal,nowTotal := 0,0
  912. a := true
  913. for _, value := range pastureNames {
  914. pasture = append(pasture, value["name"])
  915. for _, va := range pSumNow {
  916. if va["pastureName"] == value["name"] {
  917. eqUset, _ := strconv.Atoi(va["eqUse"])
  918. eqSumt, _ := strconv.Atoi(va["eqSum"])
  919. eqXZt, _ := strconv.Atoi(va["eqXZ"])
  920. eqBFt, _ := strconv.Atoi(va["eqBF"])
  921. eqFCt, _ := strconv.Atoi(va["eqFC"])
  922. eqUsetem := float64(eqUset) / float64(eqSumt) * 100
  923. eqXZtem := float64(eqXZt) / float64(eqSumt) * 100
  924. eqBFtem := float64(eqBFt) / float64(eqSumt) * 100
  925. eqFCtem := float64(eqFCt) / float64(eqSumt) * 100
  926. eqUse = append(eqUse, strconv.FormatFloat(eqUsetem, 'f', 2, 64))
  927. //eqSum = append(eqSum, va["eqSum"])
  928. eqXZ = append(eqXZ, strconv.FormatFloat(eqXZtem, 'f', 2, 64))
  929. eqBF = append(eqBF, strconv.FormatFloat(eqBFtem, 'f', 2, 64))
  930. eqFC = append(eqFC, strconv.FormatFloat(eqFCtem, 'f', 2, 64))
  931. a = false
  932. }
  933. }
  934. if a {
  935. eqUse = append(eqUse, "0")
  936. //eqSum = append(eqSum, "0")
  937. eqXZ = append(eqXZ, "0")
  938. eqBF = append(eqBF, "0")
  939. eqFC = append(eqFC, "0")
  940. }
  941. a = true
  942. }
  943. chartData["pastureName"] = pasture
  944. chartData["eqUseRate"] = eqUse
  945. //chartData["eqSum"] = eqSum
  946. chartData["eqXZRate"] = eqXZ
  947. chartData["eqBFRate"] = eqBF
  948. chartData["eqFCRate"] = eqFC
  949. queryData["chart_data"] = chartData
  950. queryData["code"] = 0
  951. queryData["message"] = "ok"
  952. return queryData, nil
  953. }
  954. func geteqclassFeeSum(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  955. queryData := make(map[string]interface{})
  956. year := parammaps.Get(strings.Trim("year", " ")).ValueStr()
  957. month := parammaps.Get(strings.Trim("month", " ")).ValueStr()
  958. eqClassName := parammaps.Get(strings.Trim("eqClassName", " ")).ValueStr()
  959. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  960. pSumNow, _ := restful.MapStrList(sqlNow, year, month, eqClassName, eqClassName)
  961. if len(pSumNow) == 0 {
  962. log.Default().Printf("geteqclassFeeSum result is empty")
  963. }
  964. chartData := make(map[string]interface{})
  965. eqUse := []string{}
  966. eqXZ := []string{}
  967. for _, va := range pSumNow {
  968. eqUse = append(eqUse, va["psum"])
  969. eqXZ = append(eqXZ, va["typeName"])
  970. }
  971. chartData["data1"] = eqUse
  972. chartData["data2"] = eqXZ
  973. queryData["chart_data"] = chartData
  974. queryData["code"] = 0
  975. queryData["message"] = "ok"
  976. return queryData, nil
  977. }
  978. func geteqclassSumMonth(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  979. queryData := make(map[string]interface{})
  980. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  981. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  982. deptName := parammaps.Get(strings.Trim("deptName", " ")).ValueStr()
  983. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  984. pSumNow, _ := restful.MapStrList(sqlNow, pastureNames, receiveTime, deptName)
  985. if len(pSumNow) == 0 {
  986. log.Default().Printf("geteqclassSumMonth result is empty")
  987. }
  988. chartData := make(map[string]interface{})
  989. typeName := []string{}
  990. eqCost := []string{}
  991. for _, va := range pSumNow {
  992. typeName = append(typeName, va["typeName"])
  993. eqCost = append(eqCost, va["psum"])
  994. }
  995. chartData["typeName"] = typeName
  996. chartData["eqCost"] = eqCost
  997. queryData["chart_data"] = chartData
  998. queryData["code"] = 0
  999. queryData["message"] = "ok"
  1000. return queryData, nil
  1001. }
  1002. func getPastureSumDay(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  1003. queryData := make(map[string]interface{})
  1004. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  1005. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  1006. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1007. pSumNow, _ := restful.MapStrList(sqlNow, pastureNames, receiveTime)
  1008. if len(pSumNow) == 0 {
  1009. log.Default().Printf("getPastureSumDay result is empty")
  1010. }
  1011. tempTime, _ := strconv.Atoi(receiveTime[:4])
  1012. receiveTimetemp := strconv.Itoa(tempTime-1) + receiveTime[4:]
  1013. pSumLast, _ := restful.MapStrList(sqlNow, pastureNames, receiveTimetemp)
  1014. chartData := make(map[string]interface{})
  1015. months := []string{}
  1016. lastYear := []string{}
  1017. nowYear := []string{}
  1018. //lastTotal,nowTotal := 0,0
  1019. a, b := true, true
  1020. month := ""
  1021. dayCount := GetMonthCount(receiveTime)
  1022. for i := 1; i <= dayCount; i++ {
  1023. if i < 10 {
  1024. month = receiveTime + "-0" + strconv.Itoa(i)
  1025. } else {
  1026. month = receiveTime + "-" + strconv.Itoa(i)
  1027. }
  1028. months = append(months, strconv.Itoa(i))
  1029. for _, va := range pSumNow {
  1030. if va["month"][8:] == month[8:] {
  1031. nowYear = append(nowYear, va["psum"])
  1032. a = false
  1033. }
  1034. }
  1035. for _, va := range pSumLast {
  1036. if va["month"][8:] == month[8:] {
  1037. lastYear = append(lastYear, va["psum"])
  1038. b = false
  1039. }
  1040. }
  1041. if a {
  1042. if month[:] > time.Now().Format("2006-01-02") {
  1043. nowYear = append(nowYear, "")
  1044. } else {
  1045. nowYear = append(nowYear, "0")
  1046. }
  1047. }
  1048. if b {
  1049. lastYear = append(lastYear, "0")
  1050. }
  1051. a = true
  1052. b = true
  1053. }
  1054. chartData["day"] = months
  1055. chartData["lastYear"] = lastYear
  1056. chartData["nowYear"] = nowYear
  1057. queryData["chart_data"] = chartData
  1058. queryData["code"] = 0
  1059. queryData["message"] = "ok"
  1060. return queryData, nil
  1061. }
  1062. func getPastureSumDay1(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  1063. queryData := make(map[string]interface{})
  1064. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  1065. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  1066. departmentName := parammaps.Get(strings.Trim("departmentName", " ")).ValueStr()
  1067. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1068. pSumNow, _ := restful.MapStrList(sqlNow, pastureNames, receiveTime, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1069. if len(pSumNow) == 0 {
  1070. log.Default().Printf("getPastureSumDay1query is empty")
  1071. }
  1072. tempTime, _ := strconv.Atoi(receiveTime[:4])
  1073. receiveTimetemp := strconv.Itoa(tempTime-1) + receiveTime[4:]
  1074. pSumLast, _ := restful.MapStrList(sqlNow, pastureNames, receiveTimetemp, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1075. if len(pSumLast) == 0 {
  1076. log.Default().Printf("getPastureSumDay1 query is empty")
  1077. }
  1078. chartData := make(map[string]interface{})
  1079. months := []string{}
  1080. lastYear := []string{}
  1081. nowYear := []string{}
  1082. a, b := true, true
  1083. month := ""
  1084. for i := 1; i <= 12; i++ {
  1085. if i < 10 {
  1086. month = receiveTime[:4] + "-0" + strconv.Itoa(i)
  1087. } else {
  1088. month = receiveTime[:4] + "-" + strconv.Itoa(i)
  1089. }
  1090. months = append(months, month)
  1091. for _, va := range pSumNow {
  1092. if va["month"][5:7] == month[5:] {
  1093. nowYear = append(nowYear, va["psum"])
  1094. a = false
  1095. }
  1096. }
  1097. for _, va := range pSumLast {
  1098. if va["month"][5:7] == month[5:] {
  1099. lastYear = append(lastYear, va["psum"])
  1100. b = false
  1101. }
  1102. }
  1103. if a {
  1104. if month[:] > time.Now().Format("2006-01") {
  1105. nowYear = append(nowYear, "")
  1106. } else {
  1107. nowYear = append(nowYear, "0")
  1108. }
  1109. }
  1110. if b {
  1111. lastYear = append(lastYear, "0")
  1112. }
  1113. a = true
  1114. b = true
  1115. }
  1116. chartData["months"] = months
  1117. chartData["lastYear"] = lastYear
  1118. chartData["nowYear"] = nowYear
  1119. queryData["chart_data"] = chartData
  1120. queryData["code"] = 0
  1121. queryData["message"] = "ok"
  1122. return queryData, nil
  1123. }
  1124. func GetMonthCount(receiveTime string) (days int) {
  1125. year, _ := strconv.Atoi(receiveTime[:4])
  1126. month, _ := strconv.Atoi(receiveTime[5:])
  1127. if month != 2 {
  1128. if month == 4 || month == 6 || month == 9 || month == 11 {
  1129. days = 30
  1130. } else {
  1131. days = 31
  1132. }
  1133. } else {
  1134. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  1135. days = 29
  1136. } else {
  1137. days = 28
  1138. }
  1139. }
  1140. return
  1141. }
  1142. func getPastureSumMonth(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  1143. queryData := make(map[string]interface{})
  1144. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  1145. pastureNames := parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  1146. departmentName := parammaps.Get(strings.Trim("departmentName", " ")).ValueStr()
  1147. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1148. var pSumNow []map[string]string
  1149. if sqlnamestr == "getPastureSumMonth" {
  1150. pSumNow, _ = restful.MapStrList(sqlNow, pastureNames, receiveTime, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1151. } else {
  1152. pSumNow, _ = restful.MapStrList(sqlNow, pastureNames, receiveTime)
  1153. }
  1154. tempTime, _ := strconv.Atoi(receiveTime)
  1155. receiveTimetemp := strconv.Itoa(tempTime - 1)
  1156. var pSumLast []map[string]string
  1157. if sqlnamestr == "getPastureSumMonth" {
  1158. pSumLast, _ = restful.MapStrList(sqlNow, pastureNames, receiveTimetemp, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1159. } else {
  1160. pSumLast, _ = restful.MapStrList(sqlNow, pastureNames, receiveTimetemp)
  1161. }
  1162. chartData := make(map[string]interface{})
  1163. months := []string{}
  1164. lastYear := []string{}
  1165. nowYear := []string{}
  1166. //lastTotal,nowTotal := 0,0
  1167. a, b := true, true
  1168. month := ""
  1169. for i := 1; i <= 12; i++ {
  1170. if i < 10 {
  1171. month = receiveTime + "-0" + strconv.Itoa(i)
  1172. } else {
  1173. month = receiveTime + "-" + strconv.Itoa(i)
  1174. }
  1175. months = append(months, month)
  1176. for _, va := range pSumNow {
  1177. if va["month"][5:] == month[5:] {
  1178. nowYear = append(nowYear, va["psum"])
  1179. a = false
  1180. }
  1181. }
  1182. for _, va := range pSumLast {
  1183. if va["month"][5:] == month[5:] {
  1184. lastYear = append(lastYear, va["psum"])
  1185. b = false
  1186. }
  1187. }
  1188. if a {
  1189. if month[:] > time.Now().Format("2006-01") {
  1190. nowYear = append(nowYear, "")
  1191. } else {
  1192. nowYear = append(nowYear, "0")
  1193. }
  1194. }
  1195. if b {
  1196. lastYear = append(lastYear, "0")
  1197. }
  1198. a = true
  1199. b = true
  1200. }
  1201. chartData["months"] = months
  1202. chartData["lastYear"] = lastYear
  1203. chartData["nowYear"] = nowYear
  1204. queryData["chart_data"] = chartData
  1205. queryData["code"] = 0
  1206. queryData["message"] = "ok"
  1207. return queryData, nil
  1208. }
  1209. func geteCow(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  1210. queryData := make(map[string]interface{})
  1211. receiveTime := parammaps.Get(strings.Trim("year", " ")).ValueStr()
  1212. pastureNames, _ := restful.MapStrList("SELECT `name` FROM pasture where `name` <> '现代牧业' order by sort")
  1213. var pSumNow, pSumLast []map[string]string
  1214. rTime := ""
  1215. if sqlnamestr == "geteCowAllList" {
  1216. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1217. if receiveTime == time.Now().Format("2006") {
  1218. rTime = time.Now().Format("2006-01-02")
  1219. } else {
  1220. rTime = receiveTime + "-12-31"
  1221. }
  1222. pSumNow, _ = restful.MapStrList(sqlNow, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime)
  1223. if receiveTime == time.Now().Format("2006") {
  1224. rTime = time.Now().AddDate(-1, 0, 0).Format("2006-01-02")
  1225. } else {
  1226. rTime = receiveTime + "-12-31"
  1227. }
  1228. receiveTimei, _ := strconv.Atoi(receiveTime)
  1229. receiveTime = strconv.Itoa(receiveTimei - 1)
  1230. pSumLast, _ = restful.MapStrList(sqlNow, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime)
  1231. } else {
  1232. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1233. if receiveTime == time.Now().Format("2006") {
  1234. rTime = time.Now().Format("2006-01-02")
  1235. } else {
  1236. rTime = receiveTime + "-12-31"
  1237. }
  1238. pSumNow, _ = restful.MapStrList(sqlNow, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime)
  1239. if receiveTime == time.Now().Format("2006") {
  1240. rTime = time.Now().AddDate(-1, 0, 0).Format("2006-01-02")
  1241. } else {
  1242. rTime = receiveTime + "-12-31"
  1243. }
  1244. receiveTimei, _ := strconv.Atoi(receiveTime)
  1245. receiveTime = strconv.Itoa(receiveTimei - 1)
  1246. pSumLast, _ = restful.MapStrList(sqlNow, receiveTime, rTime, receiveTime, rTime, receiveTime, rTime)
  1247. }
  1248. chartData := make(map[string]interface{})
  1249. pasture := []string{}
  1250. lastYear := []string{}
  1251. nowYear := []string{}
  1252. budgetYear := []string{}
  1253. //lastTotal,nowTotal := 0,0
  1254. a, b := true, true
  1255. for _, value := range pastureNames {
  1256. pasture = append(pasture, value["name"])
  1257. for _, va := range pSumNow {
  1258. if va["name"] == value["name"] {
  1259. nowYear = append(nowYear, va["psum"])
  1260. budgetYear = append(budgetYear, va["lsum"])
  1261. a = false
  1262. }
  1263. }
  1264. for _, va := range pSumLast {
  1265. if va["name"] == value["name"] {
  1266. lastYear = append(lastYear, va["psum"])
  1267. b = false
  1268. }
  1269. }
  1270. if a {
  1271. nowYear = append(nowYear, "0.0000")
  1272. budgetYear = append(budgetYear, "0.0000")
  1273. }
  1274. if b {
  1275. lastYear = append(lastYear, "0.0000")
  1276. }
  1277. a = true
  1278. b = true
  1279. }
  1280. chartData["pasture"] = pasture
  1281. chartData["lastYear"] = lastYear
  1282. chartData["nowYear"] = nowYear
  1283. chartData["budgetYear"] = budgetYear
  1284. queryData["chart_data"] = chartData
  1285. queryData["code"] = 0
  1286. queryData["message"] = "ok"
  1287. return queryData, nil
  1288. }
  1289. func getpSumNow(sqlnamestr string, parammaps *gofasion.Fasion) (map[string]interface{}, error) {
  1290. queryData := make(map[string]interface{})
  1291. receiveTime := parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  1292. departmentName := parammaps.Get(strings.Trim("departmentName", " ")).ValueStr()
  1293. pastureNames, _ := restful.MapStrList("SELECT `name` FROM pasture where `name` <> '现代牧业' order by sort")
  1294. rTime := ""
  1295. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  1296. var pSumNow []map[string]string
  1297. if sqlnamestr == "getpSumNow" {
  1298. pSumNow, _ = restful.MapStrList(sqlNow, rTime, rTime, receiveTime, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1299. } else {
  1300. pSumNow, _ = restful.MapStrList(sqlNow, rTime, rTime, receiveTime)
  1301. }
  1302. //pSumNow, _ := restful.MapStrList(sqlNow, rTime, rTime, receiveTime,departmentName,departmentName,departmentName,departmentName,departmentName,departmentName)
  1303. receiveTimei, _ := strconv.Atoi(receiveTime)
  1304. if receiveTime == time.Now().Format("2006") {
  1305. rTime = time.Now().AddDate(-1, 0, 0).Format("2006-01-02")
  1306. } else {
  1307. rTime = receiveTime + "-12-31"
  1308. }
  1309. receiveTime = strconv.Itoa(receiveTimei - 1)
  1310. var pSumLast []map[string]string
  1311. if sqlnamestr == "getpSumNow" {
  1312. pSumLast, _ = restful.MapStrList(sqlNow, rTime, rTime, receiveTime, departmentName, departmentName, departmentName, departmentName, departmentName, departmentName)
  1313. } else {
  1314. pSumLast, _ = restful.MapStrList(sqlNow, rTime, rTime, receiveTime)
  1315. }
  1316. //pSumLast, _ = restful.MapStrList(sqlNow, rTime, rTime, receiveTime,departmentName,departmentName,departmentName,departmentName,departmentName,departmentName)
  1317. chartData := make(map[string]interface{})
  1318. pasture := []string{}
  1319. lastYear := []string{}
  1320. nowYear := []string{}
  1321. //lastTotal,nowTotal := 0,0
  1322. a, b := true, true
  1323. for _, value := range pastureNames {
  1324. pasture = append(pasture, value["name"])
  1325. for _, va := range pSumNow {
  1326. if va["name"] == value["name"] {
  1327. nowYear = append(nowYear, va["psum"])
  1328. a = false
  1329. }
  1330. }
  1331. for _, va := range pSumLast {
  1332. if va["name"] == value["name"] {
  1333. lastYear = append(lastYear, va["psum"])
  1334. b = false
  1335. }
  1336. }
  1337. if a {
  1338. nowYear = append(nowYear, "0")
  1339. }
  1340. if b {
  1341. lastYear = append(lastYear, "0")
  1342. }
  1343. a = true
  1344. b = true
  1345. }
  1346. chartData["pasture"] = pasture
  1347. chartData["lastYear"] = lastYear
  1348. chartData["nowYear"] = nowYear
  1349. queryData["chart_data"] = chartData
  1350. queryData["code"] = 0
  1351. queryData["message"] = "ok"
  1352. return queryData, nil
  1353. }
  1354. func GetUpkeepPlan(c *gin.Context) {
  1355. appG := app.Gin{C: c}
  1356. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1357. fsion := gofasion.NewFasion(string(dataByte))
  1358. sqlnamestr := fsion.Get("name").ValueStr()
  1359. offset := fsion.Get("offset").ValueDefaultInt(0)
  1360. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1361. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1362. month := fsion.Get("month").ValueDefaultStr("map")
  1363. logging.Info("GetUpkeepPlan ", c.Keys, sqlnamestr, month)
  1364. sql, p := restful.GetSqlByNameDBT(sqlnamestr, nil)
  1365. if sql != "" {
  1366. s_params := make([]interface{}, 0)
  1367. if fsion.HasKey("parammaps") {
  1368. parammaps := fsion.Get("parammaps")
  1369. paramslist := strings.Split(p, ",")
  1370. if len(paramslist) > 0 && p != "" {
  1371. for _, value := range paramslist {
  1372. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1373. if tempv, exists := c.Get("jwt_username"); exists {
  1374. s_params = append(s_params, tempv.(string))
  1375. } else {
  1376. s_params = append(s_params, "")
  1377. }
  1378. } else {
  1379. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1380. }
  1381. }
  1382. }
  1383. } else if fsion.HasKey("params") {
  1384. params := fsion.Get("params").Array()
  1385. for _, v_params := range params {
  1386. s_params = append(s_params, v_params.ValueStr())
  1387. }
  1388. }
  1389. if returntype == "xlsx" {
  1390. var xlsxfile *xlsx.File
  1391. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  1392. //c.File("c:/dd.xlsx")
  1393. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1394. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  1395. c.Header("Content-Transfer-Encoding", "binary")
  1396. _ = xlsxfile.Write(c.Writer)
  1397. } else {
  1398. // queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1399. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
  1400. if err != nil {
  1401. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1402. } else {
  1403. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]string) {
  1404. if v, ok := value["id"]; ok {
  1405. //month = "2020-03"
  1406. seviceplans, _ := restful.MapStrList("SELECT seviceplan.* FROM seviceplan WHERE seviceplan.eqId = ? AND (seviceplan.month=? OR (SELECT COUNT(*) FROM bigupkeep WHERE seviceplanId = seviceplan.`id` AND plantime BETWEEN CONCAT(?,'-01') AND CONCAT(?,'-31'))>0) ", v, month, month, month) //获取保养计划
  1407. if seviceplans != nil {
  1408. for _, va := range seviceplans { //根据保养计划获取保养单
  1409. //day,_ :=strconv.Atoi(va["day"])
  1410. bigupkeeps, _ := restful.MapStrList("SELECT * FROM bigupkeep where seviceplanId = ? and plantime between concat(?,'-01') and concat(?,'-31') ", va["id"], month, month) //获取保养计划中的保养单
  1411. //buk := make(map[string]map[string]string)
  1412. for _, bu := range bigupkeeps { //日期为key ,保养单为value 存到map中
  1413. jstr := ""
  1414. if bu["plantime"][8:9] == "0" {
  1415. jstr = "m" + bu["plantime"][9:]
  1416. } else {
  1417. jstr = "m" + bu["plantime"][8:]
  1418. }
  1419. if bu["flowCompeleted"] < "2" && bu["plantime"] < time.Now().Add(-24*time.Hour).Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  1420. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#2" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1421. } else {
  1422. if bu["flowCompeleted"] >= "2" {
  1423. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#1" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1424. } else {
  1425. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#0" + "#" + bu["id"] + "#" + bu["pushbackId"]
  1426. }
  1427. }
  1428. }
  1429. }
  1430. }
  1431. }
  1432. }
  1433. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1434. }
  1435. }
  1436. } else {
  1437. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1438. }
  1439. }
  1440. func GetAccount(c *gin.Context) {
  1441. appG := app.Gin{C: c}
  1442. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1443. fsion := gofasion.NewFasion(string(dataByte))
  1444. sqlnamestr := fsion.Get("name").ValueStr()
  1445. offset := fsion.Get("offset").ValueDefaultInt(0)
  1446. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  1447. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  1448. sql, p := restful.GetSqlByNameDBT(sqlnamestr, nil)
  1449. //fmt.Println("sql========\n",sqlnamestr,sql)
  1450. if sql != "" {
  1451. s_params := make([]interface{}, 0)
  1452. if fsion.HasKey("parammaps") {
  1453. parammaps := fsion.Get("parammaps")
  1454. if sqlnamestr != "getNoMesList" && sqlnamestr != "getNoMesCount" {
  1455. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  1456. }
  1457. paramslist := strings.Split(p, ",")
  1458. if len(paramslist) > 0 && p != "" {
  1459. for _, value := range paramslist {
  1460. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1461. if tempv, exists := c.Get("jwt_username"); exists {
  1462. s_params = append(s_params, tempv.(string))
  1463. } else {
  1464. s_params = append(s_params, "")
  1465. }
  1466. } else {
  1467. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1468. }
  1469. }
  1470. fmt.Println("params parse end")
  1471. }
  1472. } else if fsion.HasKey("params") {
  1473. params := fsion.Get("params").Array()
  1474. logging.Info("GetDataByName", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  1475. for _, v_params := range params {
  1476. s_params = append(s_params, v_params.ValueStr())
  1477. }
  1478. }
  1479. if returntype == "xlsx" {
  1480. var xlsxfile *xlsx.File
  1481. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  1482. //c.File("c:/dd.xlsx")
  1483. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1484. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  1485. c.Header("Content-Transfer-Encoding", "binary")
  1486. _ = xlsxfile.Write(c.Writer)
  1487. } else {
  1488. // queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params, tx)
  1489. queryData, err := getDataBySqlT(sql, offset, pagecount, returntype, s_params)
  1490. if err != nil {
  1491. logging.Error("GetDataByName err: ", err)
  1492. msg := geterrmsg(err.Error())
  1493. appG.Response(http.StatusOK, e.ERROR, msg)
  1494. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1495. } else {
  1496. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1497. }
  1498. }
  1499. } else {
  1500. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1501. }
  1502. }
  1503. // @Summary 根据APISQL表中SQL执行得到数据集
  1504. // @Tags PostDataByName
  1505. // @Accept json
  1506. // @Produce json
  1507. // @Param sqlname query string true "sqlname"
  1508. // @Param offset query int true "offset"
  1509. // @Param pagecount query int true "pagecount"
  1510. // @Param returntype query string true "returntype"
  1511. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1512. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1513. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1514. // @Router /data/PostDataByName [POST]
  1515. func PostDataByName(c *gin.Context) {
  1516. appG := app.Gin{C: c}
  1517. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1518. fsion := gofasion.NewFasion(string(dataByte))
  1519. sqlnamestr := fsion.Get("name").ValueStr()
  1520. parammaps := fsion.Get("parammaps")
  1521. if sqlnamestr == "partsCharge" && parammaps.Get("statue").ValueInt() == 1 {
  1522. //fmt.Println("jinlaile")
  1523. defer func() {
  1524. go util.EASBillSync(5, fsion.Get("parammaps").Get("id").ValueStr(), fsion.Get("parammaps").Get("statue").ValueStr())
  1525. }()
  1526. //go CheckEASWebServiceParts(fsion.Get("parammaps").Get("statue").ValueStr(),fsion.Get("parammaps").Get("id").ValueStr())
  1527. }
  1528. s_params := make([]interface{}, 0)
  1529. tx, err := restful.Dbs.Beginx()
  1530. defer func() {
  1531. switch {
  1532. case err != nil:
  1533. fmt.Println("__error:", err)
  1534. if tx != nil {
  1535. tx.Rollback()
  1536. }
  1537. default:
  1538. if tx != nil {
  1539. err = tx.Commit()
  1540. }
  1541. }
  1542. }()
  1543. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1544. if sql != "" {
  1545. if fsion.HasKey("parammaps") {
  1546. parammaps := fsion.Get("parammaps")
  1547. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  1548. paramslist := strings.Split(p, ",")
  1549. if len(paramslist) > 0 && p != "" {
  1550. for _, value := range paramslist {
  1551. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1552. if tempv, exists := c.Get("jwt_username"); exists {
  1553. s_params = append(s_params, tempv.(string))
  1554. } else {
  1555. s_params = append(s_params, "")
  1556. }
  1557. } else {
  1558. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1559. }
  1560. }
  1561. }
  1562. } else if fsion.HasKey("params") {
  1563. params := fsion.Get("params").Array()
  1564. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  1565. for _, v_params := range params {
  1566. s_params = append(s_params, v_params.ValueStr())
  1567. }
  1568. }
  1569. queryData, err := execDataBySqlT(sql, s_params, tx)
  1570. if err != nil {
  1571. logging.Error("PostDataByName err: ", err)
  1572. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1573. msg := geterrmsg(err.Error())
  1574. appG.Response(http.StatusOK, e.ERROR, msg)
  1575. } else {
  1576. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1577. }
  1578. } else {
  1579. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1580. }
  1581. }
  1582. func PostThirdPartyData(c *gin.Context) {
  1583. appG := app.Gin{C: c}
  1584. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1585. tempval_ := make(map[string]interface{})
  1586. tempval := make([]map[string]interface{}, 0)
  1587. err := json.Unmarshal(dataByte, &tempval_)
  1588. logging.Info("PostThirdPartyData ", c.Keys, c.Request.RemoteAddr, tempval_["name"])
  1589. //logging.Info("PostThirdPartyData ", c.Keys, c.Request.RemoteAddr, tempval_["name"],tempval_)
  1590. if err != nil {
  1591. logging.Error("PostThirdPartyData Unmarshal error:", err)
  1592. } else {
  1593. if v, ok := tempval_["list"].([]interface{}); ok {
  1594. for _, Tvalue := range v {
  1595. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1596. tempval = append(tempval, v1)
  1597. }
  1598. }
  1599. }
  1600. tx, err := restful.Dbs.Beginx()
  1601. if err != nil {
  1602. logging.Error("tx.Begin 事务启动失败__error:", err)
  1603. }
  1604. defer func() {
  1605. switch {
  1606. case err != nil:
  1607. fmt.Println("__error:", err)
  1608. if tx != nil {
  1609. tx.Rollback()
  1610. }
  1611. default:
  1612. if tx != nil {
  1613. err = tx.Commit()
  1614. }
  1615. }
  1616. if tx != nil {
  1617. }
  1618. }()
  1619. if err == nil {
  1620. sqlname, ok := tempval_["name"]
  1621. if ok {
  1622. }
  1623. sql, p := restful.GetSqlByNameDBT(sqlname.(string), tx)
  1624. if sql == "" {
  1625. appG.Response(http.StatusOK, e.ERROR, "调用接口不存在")
  1626. }
  1627. //s_params := make([]interface{}, 0)
  1628. paramslist := strings.Split(p, ",")
  1629. resname := ""
  1630. for _, paramvalue := range tempval {
  1631. s_params := make([]interface{}, 0)
  1632. if len(paramslist) > 0 && p != "" {
  1633. for _, value := range paramslist {
  1634. s_params = append(s_params, paramvalue[value])
  1635. //fmt.Println(value,paramvalue[value])
  1636. }
  1637. }
  1638. var res []map[string]string
  1639. res, err = restful.MapStrList("select id from measure where meterNumber = ? and meterType = '电表' and isdel = 0", paramvalue["meterNumber"])
  1640. if len(res) == 0 || err != nil {
  1641. switch paramvalue["meterNumber"].(type) {
  1642. case string:
  1643. resname = paramvalue["meterNumber"].(string)
  1644. }
  1645. err = errors.New("电表不存在")
  1646. break
  1647. }
  1648. _, err = execDataBySqlT(sql, s_params, tx)
  1649. if err != nil {
  1650. switch paramvalue["meterNumber"].(type) {
  1651. case string:
  1652. resname = paramvalue["meterNumber"].(string)
  1653. }
  1654. break
  1655. }
  1656. }
  1657. if err == nil {
  1658. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  1659. } else {
  1660. msg := geterrmsg(err.Error())
  1661. appG.Response(http.StatusOK, e.ERROR, msg+" "+resname)
  1662. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1663. }
  1664. }
  1665. }
  1666. if err != nil {
  1667. msg := geterrmsg(err.Error())
  1668. appG.Response(http.StatusOK, e.ERROR, msg)
  1669. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1670. }
  1671. }
  1672. func PostDataByNameOPEN(c *gin.Context) {
  1673. appG := app.Gin{C: c}
  1674. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1675. fsion := gofasion.NewFasion(string(dataByte))
  1676. sqlnamestr := fsion.Get("name").ValueStr()
  1677. sqlnamestr = sqlnamestr + "OPEN"
  1678. s_params := make([]interface{}, 0)
  1679. tx, err := restful.Dbs.Beginx()
  1680. defer func() {
  1681. switch {
  1682. case err != nil:
  1683. fmt.Println("__error:", err)
  1684. if tx != nil {
  1685. tx.Rollback()
  1686. }
  1687. default:
  1688. if tx != nil {
  1689. err = tx.Commit()
  1690. }
  1691. }
  1692. }()
  1693. sql, p := restful.GetSqlByNameDBT(sqlnamestr, tx)
  1694. if sql != "" {
  1695. if fsion.HasKey("parammaps") {
  1696. parammaps := fsion.Get("parammaps")
  1697. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, parammaps.ValueStr())
  1698. paramslist := strings.Split(p, ",")
  1699. if len(paramslist) > 0 && p != "" {
  1700. for _, value := range paramslist {
  1701. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1702. if tempv, exists := c.Get("jwt_username"); exists {
  1703. s_params = append(s_params, tempv.(string))
  1704. } else {
  1705. s_params = append(s_params, "")
  1706. }
  1707. } else {
  1708. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1709. }
  1710. }
  1711. }
  1712. } else if fsion.HasKey("params") {
  1713. params := fsion.Get("params").Array()
  1714. logging.Info("PostDataByName ", c.Keys, c.Request.RemoteAddr, sqlnamestr, params)
  1715. for _, v_params := range params {
  1716. s_params = append(s_params, v_params.ValueStr())
  1717. }
  1718. }
  1719. queryData, err := execDataBySqlT(sql, s_params, tx)
  1720. if err != nil {
  1721. logging.Error("PostDataByName err: ", err)
  1722. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1723. msg := geterrmsg(err.Error())
  1724. appG.Response(http.StatusOK, e.ERROR, msg)
  1725. } else {
  1726. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1727. }
  1728. } else {
  1729. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1730. }
  1731. }
  1732. // @Summary 根据APISQL表中SQL执行得到数据集
  1733. // @Tags ExecDataByConfig
  1734. // @Accept json
  1735. // @Produce json
  1736. // @Param sqlname query string true "sqlname"
  1737. // @Param offset query int true "offset"
  1738. // @Param pagecount query int true "pagecount"
  1739. // @Param returntype query string true "returntype"
  1740. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1741. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1742. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1743. // @Router /data/PostDataByName [POST]
  1744. //
  1745. func ExecDataByConfig(c *gin.Context) {
  1746. appG := app.Gin{C: c}
  1747. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1748. tempval_ := make(map[string]interface{})
  1749. tempCommon := make(map[string]interface{})
  1750. tempval := make([]map[string]interface{}, 0)
  1751. err := json.Unmarshal(dataByte, &tempval_)
  1752. logging.Info("ExecDataByConfig ", c.Keys, c.Request.RemoteAddr, tempval_["common"], tempval_["data"])
  1753. if err != nil {
  1754. fmt.Println("error:", err)
  1755. } else {
  1756. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  1757. tempCommon = v
  1758. }
  1759. if v, ok := tempval_["data"].([]interface{}); ok {
  1760. for _, Tvalue := range v {
  1761. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1762. tempval = append(tempval, v1)
  1763. }
  1764. }
  1765. }
  1766. tx, err := restful.Dbs.Beginx()
  1767. if err != nil {
  1768. logging.Error("tx.Begin 事务启动失败__error:", err)
  1769. }
  1770. defer func() {
  1771. switch {
  1772. case err != nil:
  1773. fmt.Println("__error:", err)
  1774. if tx != nil {
  1775. tx.Rollback()
  1776. }
  1777. default:
  1778. if tx != nil {
  1779. err = tx.Commit()
  1780. }
  1781. }
  1782. if tx != nil {
  1783. }
  1784. }()
  1785. if err == nil {
  1786. if tempv, exists := c.Get("jwt_username"); exists {
  1787. tempCommon["jwt_username"] = tempv.(string)
  1788. }
  1789. for _, paramvalue := range tempval {
  1790. if _, ok := paramvalue["resultname"]; !ok {
  1791. paramvalue["resultname"] = paramvalue["name"]
  1792. }
  1793. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  1794. if err != nil {
  1795. logging.Error("ExecDataParam error:", err)
  1796. break
  1797. }
  1798. }
  1799. if err == nil {
  1800. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  1801. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  1802. } else {
  1803. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  1804. }
  1805. go NextService(tempval)
  1806. } else {
  1807. msg := geterrmsg(err.Error())
  1808. appG.Response(http.StatusOK, e.ERROR, msg)
  1809. }
  1810. }
  1811. }
  1812. if err != nil {
  1813. msg := geterrmsg(err.Error())
  1814. appG.Response(http.StatusOK, e.ERROR, msg)
  1815. //appG.Response(http.StatusOK, e.ERROR, err.Error())
  1816. }
  1817. // 开协程
  1818. defer func() {
  1819. go BillType(tempval_)
  1820. }()
  1821. }
  1822. func BillType(tempval_ map[string]interface{}) {
  1823. var typein int
  1824. switch tempval_["data"].(type) {
  1825. case []interface{}:
  1826. if len(tempval_["data"].([]interface{})) > 0 {
  1827. name := tempval_["data"].([]interface{})[0].(map[string]interface{})["name"]
  1828. switch name {
  1829. case "insertBigPartlaid":
  1830. billcode := tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["laidCode"]
  1831. laidType := interfacetostringv2(tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["laidType"])
  1832. if laidType != "2" {
  1833. typein = 1
  1834. } else {
  1835. typein = 6
  1836. }
  1837. _ = util.EASBillSync(typein, billcode, "")
  1838. case "insertBigPartUse":
  1839. billcode := tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["useForm"]
  1840. usetypev := interfacetostringv2(tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["useType"])
  1841. if usetypev != "4" {
  1842. typein = 2
  1843. } else {
  1844. typein = 7
  1845. }
  1846. _ = util.EASBillSync(typein, billcode, "")
  1847. case "insertBigPartquit":
  1848. billcode := tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["useForm"]
  1849. _ = util.EASBillSync(3, billcode, "")
  1850. case "PartRefundCharge1":
  1851. billcode := tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["id"]
  1852. statue := interfacetostringv2(tempval_["data"].([]interface{})[0].(map[string]interface{})["parammaps"].(map[string]interface{})["statue"])
  1853. if statue == "5" {
  1854. _ = util.EASBillSync(4, billcode, "")
  1855. }
  1856. }
  1857. }
  1858. }
  1859. }
  1860. func interfacetostring(val interface{}) string {
  1861. switch val.(type) {
  1862. case string:
  1863. ss := val.(string)
  1864. return ss
  1865. case int:
  1866. ss := val.(int)
  1867. return strconv.Itoa(ss)
  1868. case int64:
  1869. ss := val.(int64)
  1870. return strconv.FormatInt(ss, 10)
  1871. case float64:
  1872. ss := val.(float64)
  1873. return strconv.FormatFloat(ss, 'E', -1, 32)
  1874. }
  1875. return ""
  1876. }
  1877. func interfacetostringv2(val interface{}) string {
  1878. switch val.(type) {
  1879. case string:
  1880. ss := val.(string)
  1881. return ss
  1882. case int:
  1883. ss := val.(int)
  1884. return strconv.Itoa(ss)
  1885. case int64:
  1886. ss := val.(int64)
  1887. return strconv.FormatInt(ss, 10)
  1888. case float64:
  1889. ss := int(val.(float64))
  1890. return strconv.Itoa(ss)
  1891. }
  1892. return ""
  1893. }
  1894. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  1895. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1896. tempstrb := strings.IndexAny(valuestring, "@")
  1897. tempstre := strings.IndexAny(valuestring, ".")
  1898. if tempstrb > -1 && tempstre > -1 {
  1899. for _, ParamMapList := range ParamMap {
  1900. if v, ok := ParamMapList["resultname"]; ok {
  1901. if v == valuestring[tempstrb+1:tempstre] {
  1902. if v1, ok := ParamMapList["resultmaps"]; ok {
  1903. if newA, ok := v1.([]map[string]interface{}); ok {
  1904. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  1905. } else if newA, ok := v1.(map[string]interface{}); ok {
  1906. return interfacetostring(newA[valuestring[tempstre+1:]])
  1907. }
  1908. }
  1909. }
  1910. }
  1911. }
  1912. } else {
  1913. return valuestring
  1914. }
  1915. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  1916. return strconv.FormatInt(int64(valuestring), 10)
  1917. }
  1918. return ""
  1919. }
  1920. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  1921. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1922. tempstrb := strings.IndexAny(valuestring, "@")
  1923. tempstre := strings.IndexAny(valuestring, ".")
  1924. if tempstrb > -1 && tempstre > -1 {
  1925. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  1926. if s, ok := v.(string); ok {
  1927. return s
  1928. } else if s, ok := v.(float64); ok {
  1929. return strconv.FormatInt(int64(s), 10)
  1930. }
  1931. }
  1932. }
  1933. }
  1934. return ""
  1935. }
  1936. func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}, tx *sqlx.Tx) ([]map[string]interface{}, error) {
  1937. sqlname := fasion["name"].(string)
  1938. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  1939. defer func() {
  1940. if sqlname == "batchPartsSH" {
  1941. id := ""
  1942. switch ParentParamMap["id"].(type) {
  1943. case float64:
  1944. id = strconv.Itoa(int(ParentParamMap["id"].(float64)))
  1945. case string:
  1946. id = ParentParamMap["id"].(string)
  1947. case int:
  1948. id = strconv.Itoa(ParentParamMap["id"].(int))
  1949. }
  1950. go util.EASBillSync(5, id, "1")
  1951. //go CheckEASWebServiceParts("1",id)
  1952. }
  1953. }()
  1954. if v, ok := fasion["type"]; ok {
  1955. sqltype = v.(string)
  1956. }
  1957. sql := ""
  1958. p := ""
  1959. s_params := make([]interface{}, 0)
  1960. if sqltype != "" {
  1961. sql, p = restful.GetSqlByName(sqlname, nil)
  1962. //sqlApi, _ := model.GetApisqlByName(sqlname)
  1963. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  1964. paramslist := strings.Split(p, ",")
  1965. if len(paramslist) > 0 && p != "" {
  1966. for _, value := range paramslist {
  1967. ss := ""
  1968. if ss == "" && CommonParamMap != nil {
  1969. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  1970. ss = valuestring
  1971. tempstrb := strings.IndexAny(valuestring, "@")
  1972. tempstre := strings.IndexAny(valuestring, ".")
  1973. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  1974. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  1975. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  1976. }
  1977. } else {
  1978. ss = ""
  1979. }
  1980. }
  1981. }
  1982. if ss == "" && ParentParamMap != nil {
  1983. ss = getParamValue_(v, value, ParentParamMap)
  1984. }
  1985. if ss == "" && sameParamMap != nil {
  1986. ss = getParamValue(v, value, sameParamMap)
  1987. }
  1988. if ss == "" && ParamMap != nil {
  1989. ss = getParamValue(v, value, ParamMap)
  1990. }
  1991. s_params = append(s_params, ss)
  1992. }
  1993. }
  1994. }
  1995. }
  1996. if sql == "" {
  1997. sqltype = ""
  1998. }
  1999. if sqltype == "s1" {
  2000. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2001. if v, ok := tempValParam.(map[string]interface{}); ok {
  2002. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2003. }
  2004. if err != nil {
  2005. return nil, err
  2006. }
  2007. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2008. if err != nil {
  2009. return nil, err
  2010. }
  2011. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2012. } else if sqltype == "sm" {
  2013. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  2014. if v, ok := tempValParam.(map[string]interface{}); ok {
  2015. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2016. } else {
  2017. fmt.Println("sm", tempValParam)
  2018. }
  2019. if err != nil {
  2020. return nil, err
  2021. }
  2022. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2023. if err != nil {
  2024. return nil, err
  2025. }
  2026. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2027. } else if sqltype == "v" {
  2028. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2029. if v, ok := tempValParam.(map[string]interface{}); ok {
  2030. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2031. } else {
  2032. fmt.Println("v", tempValParam)
  2033. }
  2034. if err != nil {
  2035. return nil, err
  2036. }
  2037. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  2038. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  2039. }
  2040. } else if sqltype == "e" {
  2041. tempVal_arrary := make([]map[string]interface{}, 0)
  2042. tempVal_map := make(map[string]interface{})
  2043. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  2044. if err == nil {
  2045. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  2046. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  2047. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  2048. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  2049. return tempVal_arrary, err
  2050. }
  2051. return nil, err
  2052. } else {
  2053. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  2054. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2055. if err != nil {
  2056. return nil, err
  2057. }
  2058. return nil, err
  2059. }
  2060. }
  2061. return nil, nil
  2062. }
  2063. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *sqlx.Tx) error {
  2064. var err error
  2065. subarray, ok := fasion["children"]
  2066. if ok {
  2067. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  2068. tempVal_arrary := make([]map[string]interface{}, 0)
  2069. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  2070. tempVal_ := make(map[string]interface{})
  2071. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  2072. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  2073. } else {
  2074. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  2075. }
  2076. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  2077. if err != nil {
  2078. return err
  2079. }
  2080. tempVal_arrary = append(tempVal_arrary, tempVal_)
  2081. }
  2082. v.(map[string]interface{})["children"] = tempVal_arrary
  2083. }
  2084. }
  2085. return nil
  2086. }
  2087. // @Summary 删除图片
  2088. // @Tags RemovePicByName
  2089. // @Accept json
  2090. // @Produce json
  2091. // @Param sqlname query string true "sqlname"
  2092. // @Param offset query int true "offset"
  2093. // @Param pagecount query int true "pagecount"
  2094. // @Param returntype query string true "returntype"
  2095. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2096. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2097. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2098. // @Router /data/PostDataByName [POST]
  2099. func RemovePicByName(c *gin.Context) {
  2100. logging.Info("RemovePicByName ", c.Keys)
  2101. appG := app.Gin{C: c}
  2102. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2103. fsion := gofasion.NewFasion(string(dataByte))
  2104. sqlnamestr := fsion.Get("name").ValueStr()
  2105. picname := fsion.Get("picname").ValueStr()
  2106. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2107. params := fsion.Get("params").Array()
  2108. s_params := make([]interface{}, 0)
  2109. for _, v_params := range params {
  2110. s_params = append(s_params, v_params.ValueStr())
  2111. }
  2112. if sql != "" {
  2113. queryData, err := execDataBySql(sql, s_params)
  2114. if picname != "" {
  2115. // Destination
  2116. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  2117. err := os.Remove(distPath) //删除文件test.txt
  2118. if err != nil {
  2119. //如果删除失败则输出 file remove Error!
  2120. fmt.Println("file remove Error!")
  2121. //输出错误详细信息
  2122. fmt.Printf("%s", err)
  2123. } else {
  2124. //如果删除成功则输出 file remove OK!
  2125. fmt.Print("file remove OK!")
  2126. }
  2127. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  2128. err = os.Remove(distPath) //删除文件test.txt
  2129. if err != nil {
  2130. //如果删除失败则输出 file remove Error!
  2131. fmt.Println("file remove Error!")
  2132. //输出错误详细信息
  2133. fmt.Printf("%s", err)
  2134. } else {
  2135. //如果删除成功则输出 file remove OK!
  2136. fmt.Print("file remove OK!")
  2137. }
  2138. }
  2139. if err != nil {
  2140. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2141. } else {
  2142. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2143. }
  2144. } else {
  2145. appG.Response(http.StatusOK, e.SUCCESS, nil)
  2146. }
  2147. }
  2148. // @Summary 删除文件
  2149. // @Tags RemoveFileByName
  2150. // @Accept json
  2151. // @Produce json
  2152. // @Param sqlname query string true "sqlname"
  2153. // @Param offset query int true "offset"
  2154. // @Param pagecount query int true "pagecount"
  2155. // @Param returntype query string true "returntype"
  2156. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2157. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2158. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2159. // @Router /data/PostDataByName [POST]
  2160. func RemoveFileByName(c *gin.Context) {
  2161. appG := app.Gin{C: c}
  2162. id := c.Param("id")
  2163. logging.Info("DownloadFile ", c.Keys, id)
  2164. eqdoc, err := restful.MapStrList("SELECT * FROM eq_doc where id = ? ", id)
  2165. if eqdoc == nil {
  2166. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2167. return
  2168. }
  2169. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  2170. err = os.Remove(distPath) //删除文件test.txt
  2171. if err != nil {
  2172. //如果删除失败则输出 file remove Error!
  2173. fmt.Println("file remove Error!")
  2174. //输出错误详细信息
  2175. fmt.Printf("%s", err)
  2176. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2177. return
  2178. }
  2179. fmt.Print("file remove OK!")
  2180. _, err = restful.MapStrList("delete FROM eq_doc where id = ? ", id)
  2181. if err != nil {
  2182. log.Default().Printf("RemoveFileByName:%v", err)
  2183. }
  2184. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2185. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2186. //fsion := gofasion.NewFasion(string(dataByte))
  2187. //sqlnamestr := fsion.Get("name").ValueStr()
  2188. //filename := fsion.Get("filename").ValueStr()
  2189. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2190. //params := fsion.Get("params").Array()
  2191. //s_params := make([]interface{}, 0)
  2192. //for _, v_params := range params {
  2193. // s_params = append(s_params, v_params.ValueStr())
  2194. //}
  2195. //if sql != "" {
  2196. // queryData, err := execDataBySql(sql, s_params)
  2197. //
  2198. // if filename != "" {
  2199. // // Destination
  2200. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  2201. //
  2202. // err := os.Remove(distPath) //删除文件test.txt
  2203. // if err != nil {
  2204. // //如果删除失败则输出 file remove Error!
  2205. // fmt.Println("file remove Error!")
  2206. // //输出错误详细信息
  2207. // fmt.Printf("%s", err)
  2208. // } else {
  2209. // //如果删除成功则输出 file remove OK!
  2210. // fmt.Print("file remove OK!")
  2211. // }
  2212. // }
  2213. // if err != nil {
  2214. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  2215. // } else {
  2216. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2217. // }
  2218. //} else {
  2219. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  2220. //}
  2221. }
  2222. // @Summary 根据APISQL表中SQL执行得到数据集
  2223. // @Tags PostDataByName
  2224. // @Accept json
  2225. // @Produce json
  2226. // @Param sqlname query string true "sqlname"
  2227. // @Param offset query int true "offset"
  2228. // @Param pagecount query int true "pagecount"
  2229. // @Param returntype query string true "returntype"
  2230. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2231. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2232. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2233. // @Router /data/PostDataByName [POST]
  2234. func UpdateDataRelation(c *gin.Context) {
  2235. appG := app.Gin{C: c}
  2236. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2237. fsion := gofasion.NewFasion(string(dataByte))
  2238. tablename := fsion.Get("name").ValueStr()
  2239. dataname := fsion.Get("dataname").ValueStr()
  2240. datavalue := fsion.Get("datavalue").ValueStr()
  2241. valuename := fsion.Get("valuename").ValueStr()
  2242. logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  2243. values := fsion.Get("values").Array()
  2244. tx, err := restful.Dbs.Beginx()
  2245. defer func() {
  2246. switch {
  2247. case err != nil:
  2248. fmt.Println("__error:", err)
  2249. if tx != nil {
  2250. tx.Rollback()
  2251. }
  2252. default:
  2253. if tx != nil {
  2254. err = tx.Commit()
  2255. }
  2256. }
  2257. }()
  2258. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  2259. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  2260. d_params := make([]interface{}, 4)
  2261. d_params[0] = datavalue
  2262. s_params := ""
  2263. for _, v_values := range values {
  2264. d_params[1] = v_values.ValueStr()
  2265. if s_params == "" {
  2266. s_params = v_values.ValueStr()
  2267. } else {
  2268. s_params = s_params + "," + v_values.ValueStr()
  2269. }
  2270. d_params[2] = d_params[1]
  2271. d_params[3] = d_params[0]
  2272. _, err := execDataBySqlT(insertSqlstr, d_params, tx)
  2273. if err != nil {
  2274. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2275. break
  2276. }
  2277. }
  2278. if s_params != "" {
  2279. deleteSqlstr := "delete from " + tablename + " " +
  2280. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  2281. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2282. if err != nil {
  2283. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2284. } else if err == nil {
  2285. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2286. }
  2287. } else {
  2288. deleteSqlstr := "delete from " + tablename + " " +
  2289. " where " + dataname + " = " + datavalue
  2290. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2291. if err != nil {
  2292. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2293. } else if err == nil {
  2294. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2295. }
  2296. }
  2297. }
  2298. // @Summary 根据APISQL表中SQL执行得到数据集
  2299. // @Tags PostDatasByName
  2300. // @Accept json
  2301. // @Produce json
  2302. // @Param sqlname query string true "sqlname"
  2303. // @Param offset query int true "offset"
  2304. // @Param pagecount query int true "pagecount"
  2305. // @Param returntype query string true "returntype"
  2306. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2307. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2308. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2309. // @Router /data/PostDatasByName [POST]
  2310. func PostDataByNames(c *gin.Context) {
  2311. appG := app.Gin{C: c}
  2312. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2313. fsion := gofasion.NewFasion(string(dataByte))
  2314. sqlParams := fsion.Get("array").Array()
  2315. queryData := make(map[string]interface{})
  2316. for _, v := range sqlParams {
  2317. fsion2 := gofasion.NewFasion(v.Json())
  2318. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  2319. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  2320. if sql == "" {
  2321. errorm := make(map[string]interface{})
  2322. errorm["code"] = e.ERROR
  2323. errorm["message"] = "sql没有找到"
  2324. errorm["param"] = ""
  2325. queryData[fsion2.Get("name").ValueStr()] = errorm
  2326. continue
  2327. } else if sql != "" {
  2328. s_params := make([]interface{}, 0)
  2329. if fsion2.HasKey("parammaps") {
  2330. parammaps := fsion2.Get("parammaps")
  2331. paramslist := strings.Split(p, ",")
  2332. if len(paramslist) > 0 && p != "" {
  2333. for _, value := range paramslist {
  2334. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2335. if tempv, exists := c.Get("jwt_username"); exists {
  2336. s_params = append(s_params, tempv.(string))
  2337. } else {
  2338. s_params = append(s_params, "")
  2339. }
  2340. } else {
  2341. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2342. }
  2343. }
  2344. }
  2345. } else if fsion2.HasKey("params") {
  2346. params := fsion2.Get("params").Array()
  2347. for _, v_params := range params {
  2348. s_params = append(s_params, v_params.ValueStr())
  2349. }
  2350. }
  2351. _, err := execDataBySql(sql, s_params)
  2352. if err != nil {
  2353. logging.Error("PostDataByName err: ", err)
  2354. errorm := make(map[string]interface{})
  2355. errorm["code"] = e.ERROR
  2356. errorm["message"] = err.Error()
  2357. errorm["msg"] = "fail"
  2358. errorm["param"] = s_params
  2359. queryData[fsion2.Get("name").ValueStr()] = errorm
  2360. continue
  2361. } else {
  2362. errorm := make(map[string]interface{})
  2363. errorm["code"] = e.SUCCESS
  2364. errorm["message"] = "success"
  2365. errorm["msg"] = "ok"
  2366. queryData[fsion2.Get("name").ValueStr()] = errorm
  2367. continue
  2368. }
  2369. } else {
  2370. errorm := make(map[string]interface{})
  2371. errorm["code"] = e.ERROR
  2372. errorm["message"] = "NO SQL"
  2373. errorm["param"] = ""
  2374. queryData[fsion2.Get("name").ValueStr()] = errorm
  2375. }
  2376. }
  2377. //appG.Response(http.StatusOK, e.SUCCESS, msg)
  2378. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2379. }
  2380. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2381. if offset > 0 {
  2382. offset = offset - 1
  2383. }
  2384. if returntype == "list" {
  2385. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  2386. return queryData, err
  2387. } else if returntype == "listlist" {
  2388. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  2389. return queryData, err
  2390. } else {
  2391. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  2392. return queryData, err
  2393. }
  2394. }
  2395. // func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *sqlx.Tx) (interface{}, error) {
  2396. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2397. if offset > 0 {
  2398. offset = offset - 1
  2399. }
  2400. if returntype == "list" {
  2401. // queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  2402. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params)
  2403. return queryData, err
  2404. } else if returntype == "listlist" {
  2405. // queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  2406. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params)
  2407. return queryData, err
  2408. } else {
  2409. // queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  2410. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params)
  2411. return queryData, err
  2412. }
  2413. }
  2414. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  2415. paramslist := strings.Split(Params, ",")
  2416. s_params := make([]interface{}, 0)
  2417. if isList {
  2418. for _, v_params := range thisParams["params"].([]interface{}) {
  2419. s_params = append(s_params, v_params)
  2420. }
  2421. } else {
  2422. paramslist := strings.Split(Params, ",")
  2423. if len(paramslist) > 0 && Params != "" {
  2424. for _, value := range paramslist {
  2425. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2426. }
  2427. }
  2428. }
  2429. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  2430. for _, queryData_ := range queryData {
  2431. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  2432. if isList {
  2433. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  2434. if queryData_[idname] != nil {
  2435. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  2436. }
  2437. } else {
  2438. if queryData_[idname] != nil {
  2439. thisParams[idname] = queryData_[idname].(interface{})
  2440. }
  2441. if len(paramslist) > 0 && Params != "" {
  2442. s_params = make([]interface{}, 0)
  2443. for _, value := range paramslist {
  2444. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2445. }
  2446. }
  2447. }
  2448. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  2449. } else {
  2450. }
  2451. }
  2452. return queryData, err
  2453. }
  2454. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2455. queryData, err := restful.ExecQuery(sqlstr, params)
  2456. return queryData, err
  2457. }
  2458. func execDataBySqlT(sqlstr string, params []interface{}, tx *sqlx.Tx) (interface{}, error) {
  2459. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  2460. return queryData, err
  2461. }
  2462. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2463. queryData, err := restful.ExecQuery(sqlstr, params)
  2464. return queryData, err
  2465. }
  2466. // @Summary 导入excel文件到数据库,excel第一行作为参数
  2467. // @Tags ImportExcel
  2468. // @Accept json
  2469. // @Produce json
  2470. // @Param file query string true "文件名"
  2471. // @Param name header string true "执行sql的名字"
  2472. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2473. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2474. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2475. // @Router /data/ImportExcel [POST]
  2476. // @param 备件编号,备件名称,规格型号,计量单位,品牌,计划量,单价,备注,jwt_username
  2477. // func ImportExcel(c *gin.Context) {
  2478. // log.Default().Println("Importing Excel================================")
  2479. // appG := app.Gin{C: c}
  2480. // file, _, err := c.Request.FormFile("file")
  2481. // if err != nil {
  2482. // logrus.Errorf("request.FormFile: %v", err)
  2483. // appG.Response(http.StatusOK, e.ERROR, err)
  2484. // return
  2485. // }
  2486. // xlsx1, err := excelize.OpenReader(file)
  2487. // if err != nil {
  2488. // logrus.Errorf("excelize.OpenReader: %v", err)
  2489. // appG.Response(http.StatusOK, e.ERROR, err)
  2490. // return
  2491. // }
  2492. // c.Request.ParseForm()
  2493. // params := c.Request.Form
  2494. // sheetname := params.Get("sheetname")
  2495. // rows := xlsx1.GetRows(sheetname)
  2496. // importParams := params.Get("importParams")
  2497. // paramslist := strings.Split(importParams, ",")
  2498. // sqlnamestr := params.Get("name")
  2499. // sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2500. // inputdata := ""
  2501. // ok := 0
  2502. // error := 0
  2503. // queryData := make([]interface{}, 0)
  2504. // data := make(map[string]interface{})
  2505. // if len(rows) > 10000 {
  2506. // rows = rows[:10000]
  2507. // }
  2508. // if len(rows) <= 1 {
  2509. // logrus.Errorln("表格为空")
  2510. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格为空"))
  2511. // return
  2512. // }
  2513. // if len(rows[0]) < len(paramslist) {
  2514. // logrus.Errorln("表格缺少数据列")
  2515. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格缺少数据列"))
  2516. // return
  2517. // }
  2518. // for i, v := range paramslist {
  2519. // if strings.Trim(rows[0][i], " ") != v {
  2520. // logrus.Errorln("表格数据列顺序不符合标准,或标题名称错误")
  2521. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格数据列顺序不符合标准,或标题名称错误"))
  2522. // return
  2523. // }
  2524. // }
  2525. // for _, row := range rows[1:] {
  2526. // s_params := make([]interface{}, 0)
  2527. // outentry := make(map[string]interface{})
  2528. // for i := range row {
  2529. // row[i] = strings.TrimSpace(row[i])
  2530. // s_params = append(s_params, row[i])
  2531. // }
  2532. // uname := ""
  2533. // if tempv, b := c.Get("jwt_username"); b {
  2534. // uname = tempv.(string)
  2535. // }
  2536. // s_params = append(s_params, uname)
  2537. // outentry["input"] = inputdata
  2538. // log.Default().Printf("params=%v", s_params...)
  2539. // returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  2540. // returnmsgmap, _ := returnmsg.(map[string]interface{})
  2541. // returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  2542. // if err != nil {
  2543. // outentry["error_msg"] = err.Error()
  2544. // error++
  2545. // queryData = append(queryData, outentry)
  2546. // } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  2547. // ok++
  2548. // } else {
  2549. // msg := geterrmsg(returnmsgmap1[0]["msg"].(string))
  2550. // outentry["error_msg"] = msg
  2551. // error++
  2552. // queryData = append(queryData, outentry)
  2553. // }
  2554. // }
  2555. // data["result"] = queryData
  2556. // data["success"] = ok
  2557. // data["err_count"] = error
  2558. // appG.Response(http.StatusOK, e.SUCCESS, data)
  2559. // }
  2560. func ImportExcel(c *gin.Context) {
  2561. appG := app.Gin{C: c}
  2562. inputdata := ""
  2563. if err := c.Request.ParseForm(); err != nil {
  2564. log.Default().Printf("request.parseform:%v", err)
  2565. appG.Response(http.StatusOK, e.ERROR, err)
  2566. return
  2567. }
  2568. params := c.Request.Form
  2569. // file, _, err := c.Request.FormFile("file")
  2570. fileHeader, err := c.FormFile("file")
  2571. file, err := fileHeader.Open()
  2572. if err != nil {
  2573. log.Default().Printf("multipart.FileHeader.open:%v", err)
  2574. appG.Response(http.StatusOK, e.ERROR, err)
  2575. return
  2576. }
  2577. if err != nil {
  2578. log.Default().Printf("request.FormFile: %v", err)
  2579. appG.Response(http.StatusOK, e.ERROR, err)
  2580. return
  2581. }
  2582. sqlnamestr := params.Get("name")
  2583. sheetname := params.Get("sheetname")
  2584. importParams := params.Get("importParams")
  2585. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2586. // 备件编号,备件名称,规格型号,计量单位,品牌,计划量,单价,备注,jwt_username
  2587. paramslist := strings.Split(importParams, ",")
  2588. paramslist = append(paramslist, "jwt_username")
  2589. queryData := make([]interface{}, 0)
  2590. xlsx1, err := excelize.OpenReader(file)
  2591. if err != nil {
  2592. log.Default().Printf("excelize.OpenReader:%v", err)
  2593. return
  2594. }
  2595. ok := 0
  2596. error := 0
  2597. rows := xlsx1.GetRows(sheetname)
  2598. if len(rows) > 10000 {
  2599. rows = rows[:10000]
  2600. }
  2601. for i, row := range rows {
  2602. if i > 0 {
  2603. if len(paramslist) > 0 {
  2604. s_params := make([]interface{}, 0)
  2605. entry := make(map[string]interface{})
  2606. outentry := make(map[string]interface{})
  2607. inputdata = ""
  2608. for _, value := range paramslist {
  2609. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2610. if tempv, exists := c.Get("jwt_username"); exists {
  2611. s_params = append(s_params, tempv.(string))
  2612. } else {
  2613. s_params = append(s_params, "")
  2614. }
  2615. } else {
  2616. for j, colCell := range rows[0] {
  2617. if strings.Trim(value, " ") == colCell {
  2618. for k, colCell1 := range row {
  2619. if j == k {
  2620. outentry[colCell] = colCell1
  2621. entry[colCell] = colCell1
  2622. //inputdata = inputdata + colCell + ":" + colCell1 + ";"
  2623. }
  2624. }
  2625. }
  2626. }
  2627. s_params = append(s_params, entry[strings.Trim(value, " ")])
  2628. }
  2629. }
  2630. outentry["input"] = inputdata
  2631. returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  2632. returnmsgmap, _ := returnmsg.(map[string]interface{})
  2633. returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  2634. log.Default().Printf("return:%v", returnmsgmap)
  2635. if err != nil {
  2636. outentry["error_msg"] = err.Error()
  2637. error++
  2638. queryData = append(queryData, outentry)
  2639. } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  2640. ok++
  2641. } else {
  2642. error++
  2643. msg := ""
  2644. if len(returnmsgmap1) > 0 && returnmsgmap1[0]["msg"] != nil {
  2645. msg = geterrmsg(returnmsgmap1[0]["msg"].(string))
  2646. }
  2647. outentry["error_msg"] = msg
  2648. queryData = append(queryData, outentry)
  2649. }
  2650. }
  2651. }
  2652. }
  2653. data := make(map[string]interface{})
  2654. data["result"] = queryData
  2655. data["success"] = ok
  2656. data["err_count"] = error
  2657. appG.Response(http.StatusOK, e.SUCCESS, data)
  2658. }
  2659. func DownloadMailReport(context *gin.Context) {
  2660. dataByte, _ := ioutil.ReadAll(context.Request.Body)
  2661. fsion := gofasion.NewFasion(string(dataByte))
  2662. //filename := context.Param("filename")
  2663. sqlnamestr := fsion.Get("name").ValueStr()
  2664. pastureId := fsion.Get("pastureId").ValueStr()
  2665. pastureName := fsion.Get("pastureName").ValueStr()
  2666. date := fsion.Get("date").ValueStr()
  2667. logging.Info("DownloadMailReport ", context.Keys, sqlnamestr, pastureName)
  2668. filepath := ""
  2669. if sqlnamestr == "DownloadMailReportJY" {
  2670. filepath, _ = util.DownloadDataJT(date)
  2671. } else if sqlnamestr == "DownloadMailReportMC" {
  2672. filepath, _ = util.DownloadDataMC(pastureName, date, pastureId)
  2673. } else if sqlnamestr == "sssss" { // 平时用来拉数据的
  2674. util.Sssss()
  2675. } else if sqlnamestr == "DownloadEquipment" { // 设备指标导出方法
  2676. parammaps := fsion.Get("parammaps").ValueStr()
  2677. filepath, _ = util.DownloadEquipment(parammaps)
  2678. }
  2679. if filepath == "" {
  2680. http.NotFound(context.Writer, context.Request)
  2681. return
  2682. }
  2683. originFilePath := filepath
  2684. //println("originFilePath==========",originFilePath)
  2685. if fs.PathExists(originFilePath) == false {
  2686. // if the path not found
  2687. http.NotFound(context.Writer, context.Request)
  2688. return
  2689. }
  2690. http.ServeFile(context.Writer, context.Request, originFilePath)
  2691. }
  2692. // 资产标准管理判断牧场是否有选择的该部门
  2693. func PastureDepartment(c *gin.Context) {
  2694. appG := app.Gin{C: c}
  2695. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2696. fsion := gofasion.NewFasion(string(dataByte))
  2697. paramMap := fsion.Get("param")
  2698. ids := paramMap.Get("ids").ValueStr()
  2699. deptName := paramMap.Get("deptName").ValueStr()
  2700. idList := strings.Split(ids, ",")
  2701. checkDeptList := make([]map[string]interface{}, 0)
  2702. for _, v := range idList {
  2703. aa, _ := strconv.Atoi(v)
  2704. data, err := restful.MapStrList("select * from department where pastureId=? and name=? and enable=1 and isDel=0 and isPasture=0", aa, deptName)
  2705. if err != nil {
  2706. fmt.Println(err)
  2707. } else {
  2708. list := make(map[string]interface{}, 1)
  2709. if len(data) == 0 {
  2710. list["id"] = v
  2711. list["enable"] = 0
  2712. } else {
  2713. list["id"] = v
  2714. list["enable"] = 1
  2715. }
  2716. checkDeptList = append(checkDeptList, list)
  2717. }
  2718. }
  2719. appG.Response(http.StatusOK, e.SUCCESS, checkDeptList)
  2720. }
  2721. //{ 'label': '汶上', 'property': 'm1' }
  2722. // 资产标准管理
  2723. func Equipment(c *gin.Context) {
  2724. appG := app.Gin{C: c}
  2725. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2726. fsion := gofasion.NewFasion(string(dataByte))
  2727. deptName := fsion.Get("deptName").ValueStr()
  2728. eqClassId := fsion.Get("eqClassId").ValueStr()
  2729. offset := fsion.Get("offset").ValueDefaultInt(0)
  2730. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2731. dataList := make([]interface{}, 0)
  2732. pastureData, err := restful.MapStrList("select p.`name`,p.id from pasture p group by p.`name`")
  2733. str := "select eqS.id,eqS.deapartmentName deptName, eqS.`use`,eqclass.`typeName`,p.`name`,p.id pastureId ,eqS.eqClassId " +
  2734. " from equipment_standard eqS" +
  2735. " left join pasture p on p.id= eqS.pastureId" +
  2736. " left join eqclass on eqclass.id=eqS.eqClassId " +
  2737. " where (eqS.deapartmentName = :deptName or :deptName ='') and " +
  2738. " (eqS.eqClassId = :eqClassId or :eqClassId ='') " +
  2739. " group by deptName,eqClassId " +
  2740. " ORDER BY eqS.id DESC"
  2741. offset = offset - 1
  2742. if pagecount > 0 && offset > 0 {
  2743. offset = offset * pagecount
  2744. }
  2745. paramMap_3_6 := map[string]interface{}{"deptName": deptName, "eqClassId": eqClassId}
  2746. str += fmt.Sprintf("limit %d offset %d", pagecount, offset)
  2747. equipmentStandardData1, err := restful.MapStrList(str, paramMap_3_6)
  2748. //数据
  2749. equipmentStandardData2, err := restful.MapStrList("select eqS.deapartmentName deptName, eqS.id eqSId,eqS.equipmentNumber,p.`name`,p.id pastureId,eqS.eqClassId"+
  2750. " from equipment_standard eqS"+
  2751. " left join pasture p on p.id= eqS.pastureId "+
  2752. " left join eqclass eqc on eqc.id=eqS.eqClassId "+
  2753. "where (eqS.deapartmentName =? or ?='') and"+
  2754. " (eqS.eqClassId = ? or ?='') ", deptName, deptName, eqClassId, eqClassId)
  2755. sqlstr := str
  2756. params := make([]interface{}, 0)
  2757. for i := 0; i < 2; i++ {
  2758. param := make(map[string]interface{}, 0)
  2759. param["deptName"] = deptName
  2760. param["eqClassId"] = eqClassId
  2761. params = append(params, param)
  2762. }
  2763. label := make([]map[string]interface{}, 0)
  2764. if err != nil {
  2765. fmt.Println(err)
  2766. } else {
  2767. for i, v := range pastureData {
  2768. pasturelist := make(map[string]interface{}, 1)
  2769. pasturelist["label"] = v["name"]
  2770. pasturelist["id"] = v["id"]
  2771. pasturelist["property"] = "m" + strconv.Itoa(i+1)
  2772. label = append(label, pasturelist)
  2773. }
  2774. label2 := make([]map[string]interface{}, 0)
  2775. for _, s := range equipmentStandardData1 {
  2776. equipmentStandardList := make(map[string]interface{}, 1)
  2777. for i := 1; i <= len(label); i++ {
  2778. equipmentStandardList["m"+strconv.Itoa(i)] = ""
  2779. }
  2780. equipmentStandardList["deptName"] = s["deptName"]
  2781. equipmentStandardList["use"] = s["use"]
  2782. equipmentStandardList["typeName"] = s["typeName"]
  2783. equipmentStandardList["eqClassId"] = s["eqClassId"]
  2784. for _, z := range equipmentStandardData2 {
  2785. for e, x := range label {
  2786. if s["deptName"] == z["deptName"] && x["label"] == z["name"] && s["eqClassId"] == z["eqClassId"] {
  2787. equipmentStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  2788. equipmentStandardList[x["property"].(string)] = z["equipmentNumber"]
  2789. } else {
  2790. equipmentStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  2791. }
  2792. }
  2793. }
  2794. label2 = append(label2, equipmentStandardList)
  2795. }
  2796. if err != nil {
  2797. msg := geterrmsg(err.Error())
  2798. appG.Response(http.StatusOK, e.ERROR, msg)
  2799. } else {
  2800. dataList = append(dataList, label)
  2801. dataList = append(dataList, label2)
  2802. data := make(map[string]interface{})
  2803. recordcount := int64(0)
  2804. if pagecount > 1 {
  2805. sqlstr := fmt.Sprintf("SELECT COUNT(*) as count FROM (%s) a", sqlstr)
  2806. params := []interface{}{deptName, deptName, eqClassId, eqClassId}
  2807. err := restful.BasicList(sqlstr, params, &recordcount)
  2808. if err != nil {
  2809. log.Default().Printf("Equipment query error: %v", err)
  2810. }
  2811. }
  2812. if err != nil {
  2813. fmt.Println(err)
  2814. }
  2815. if recordcount == 0 {
  2816. recordcount = int64(len(equipmentStandardData1))
  2817. }
  2818. data["list"] = dataList
  2819. //data := make(map[string]interface{})
  2820. data["total"] = recordcount
  2821. data["code"] = 0
  2822. data["message"] = "ok"
  2823. if pagecount > 0 {
  2824. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  2825. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  2826. }
  2827. data["pageSize"] = pagecount
  2828. appG.Response(http.StatusOK, e.SUCCESS, data)
  2829. }
  2830. }
  2831. }
  2832. // 资产标准管理插入修改写在一起
  2833. func UpdateAndInsert(c *gin.Context) {
  2834. appG := app.Gin{C: c}
  2835. data := make(map[string]interface{}, 0)
  2836. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2837. fsion := gofasion.NewFasion(string(dataByte))
  2838. param := fsion.Get("param")
  2839. check := fsion.Get("check").ValueStr()
  2840. tx, err := restful.Dbs.Beginx()
  2841. deptName := param.Get("deptName").ValueStr()
  2842. eqCode := param.Get("eqCode").ValueStr()
  2843. eqClassId, err := strconv.Atoi(param.Get("eqClassId").ValueStr())
  2844. use := param.Get("use").ValueStr()
  2845. paramStr := fsion.Get("param").ValueStr()
  2846. paramMap := make(map[string]interface{}, 0)
  2847. err = json.Unmarshal([]byte(paramStr), &paramMap)
  2848. fmt.Println(len(paramMap))
  2849. fmt.Println(paramMap)
  2850. // 标准库存
  2851. name := fsion.Get("name").ValueStr()
  2852. partClassId, err := strconv.Atoi(param.Get("partClassId").ValueStr())
  2853. ff := 0
  2854. if check == "insert" {
  2855. querydata := make([]map[string]string, 0)
  2856. if name == "partStandard" {
  2857. ff = 2
  2858. querydata, err = restful.MapStrList(" select * from part_standard where partClassId2 =? ", partClassId)
  2859. if querydata != nil {
  2860. //data["msg"]="请不输入重复数据"
  2861. msg := geterrmsg("请不输入重复数据")
  2862. appG.Response(http.StatusOK, e.ERROR, msg)
  2863. return
  2864. }
  2865. } else {
  2866. ff = 4
  2867. querydata, err = restful.MapStrList(" select * from equipment_standard where eqClassId =? and deapartmentName = ?", eqClassId, deptName)
  2868. if querydata != nil {
  2869. //data["msg"]="请不输入重复数据"
  2870. msg := geterrmsg("请不输入重复数据")
  2871. appG.Response(http.StatusOK, e.ERROR, msg)
  2872. return
  2873. }
  2874. }
  2875. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2876. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2877. if err != nil {
  2878. }
  2879. if name == "partStandard" {
  2880. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2881. tx.Exec("call add_part_standard(?,?,?)", partClassId, pastureId, param.Get("m"+strconv.Itoa(i)))
  2882. }
  2883. } else {
  2884. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2885. tx.Exec("call add_equipment_standard(?,?,?,?,?,?)", deptName, use, eqClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr(), eqCode)
  2886. }
  2887. }
  2888. }
  2889. if err != nil {
  2890. }
  2891. }
  2892. if check == "update" {
  2893. if name == "partStandard" {
  2894. ff = 2
  2895. tx.Exec("delete from part_standard where partClassId2 =?", partClassId)
  2896. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2897. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2898. if err != nil {
  2899. }
  2900. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2901. tx.Exec("call add_part_standard(?,?,?)", partClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr())
  2902. }
  2903. }
  2904. } else {
  2905. ff = 4
  2906. tx.Exec("delete from equipment_standard where deapartmentName=? and eqClassId=?", deptName, eqClassId)
  2907. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2908. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2909. if err != nil {
  2910. }
  2911. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2912. tx.Exec("call add_equipment_standard(?,?,?,?,?,?)", deptName, use, eqClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr(), eqCode)
  2913. // " WHERE deapartmentName=? and pastureId = ? and eqClassId=?", use, param.Get("m"+strconv.Itoa(i)).ValueStr(), deptName, pastureId,eqClassId).Execute()
  2914. }
  2915. }
  2916. }
  2917. if err != nil {
  2918. data["msg"] = "error"
  2919. data["error"] = err.Error()
  2920. appG.Response(http.StatusOK, e.SUCCESS, data)
  2921. }
  2922. }
  2923. if check == "delete" {
  2924. if name == "partStandard" {
  2925. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2926. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2927. if err != nil {
  2928. }
  2929. tx.Exec("delete from part_standard where pastureId=? and partClassId2=?", pastureId, partClassId)
  2930. }
  2931. } else {
  2932. ff = 4
  2933. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2934. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2935. if err != nil {
  2936. }
  2937. tx.Exec("delete from equipment_standard where deapartmentName=? and pastureId=? and eqClassId=?", deptName, pastureId, eqClassId)
  2938. }
  2939. }
  2940. }
  2941. data["msg"] = "ok"
  2942. appG.Response(http.StatusOK, e.SUCCESS, data)
  2943. defer func() {
  2944. switch {
  2945. case err != nil:
  2946. logging.Error("tx.Begin 事务启动失败__error:", err)
  2947. if tx != nil {
  2948. tx.Rollback()
  2949. }
  2950. default:
  2951. if tx != nil {
  2952. err = tx.Commit()
  2953. }
  2954. }
  2955. }()
  2956. }
  2957. func StandardPart(c *gin.Context) {
  2958. appG := app.Gin{C: c}
  2959. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2960. fsion := gofasion.NewFasion(string(dataByte))
  2961. partClassId := fsion.Get("partClassId").ValueStr()
  2962. offset := fsion.Get("offset").ValueDefaultInt(0)
  2963. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2964. pastureData, err := restful.MapStrList("select p.`name`,p.id from pasture p group by p.`name`")
  2965. //s_params := make([]interface{}, 0)
  2966. offset = offset - 1
  2967. if pagecount > 0 && offset > 0 {
  2968. offset = offset * pagecount
  2969. }
  2970. dataList := make([]interface{}, 0)
  2971. label := make([]map[string]interface{}, 0)
  2972. str := "select p_c.partName,p.`name`,p_s.pastureId,p_s.partClassId2 from part_standard p_s left join part_class p_c on p_c.id= p_s.partClassId2 " +
  2973. " left join pasture p on p.id = p_s.pastureId where p_s.partClassId2 = ? or ?='' " +
  2974. " GROUP BY p_s.partClassId2 ORDER BY p_s.id DESC"
  2975. str1 := "select p_s.standardMoney,p_s.partClassId2,p_s.pastureId,p.`name` from part_standard p_s left join pasture p on p.id=p_s.pastureId where p_s.partClassId2 = ? or ?=''"
  2976. str += fmt.Sprintf(" limit %d offset %d", pagecount, offset)
  2977. partStandardData1, err := restful.MapStrList(str, partClassId, partClassId)
  2978. //数据
  2979. partStandardData2, err := restful.MapStrList(str1, partClassId, partClassId)
  2980. if err != nil {
  2981. fmt.Println(err)
  2982. } else {
  2983. for i, v := range pastureData {
  2984. pasturelist := make(map[string]interface{}, 1)
  2985. pasturelist["label"] = v["name"]
  2986. pasturelist["id"] = v["id"]
  2987. pasturelist["property"] = "m" + strconv.Itoa(i+1)
  2988. label = append(label, pasturelist)
  2989. }
  2990. label2 := make([]map[string]interface{}, 0)
  2991. //equipmentStandardList := make(map[string]interface{}, 1)
  2992. for _, s := range partStandardData1 {
  2993. partStandardList := make(map[string]interface{}, 1)
  2994. for i := 1; i <= len(label); i++ {
  2995. partStandardList["m"+strconv.Itoa(i)] = ""
  2996. }
  2997. partStandardList["partClassId"] = s["partClassId2"]
  2998. partStandardList["partName"] = s["partName"]
  2999. for _, z := range partStandardData2 {
  3000. for e, x := range label {
  3001. if x["label"] == z["name"] && s["partClassId2"] == z["partClassId2"] {
  3002. partStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  3003. //equipmentStandardList["eqSId"+strconv.Itoa(e+1)] = z["eqSId"]
  3004. partStandardList[x["property"].(string)] = z["standardMoney"]
  3005. } else {
  3006. partStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  3007. }
  3008. }
  3009. }
  3010. label2 = append(label2, partStandardList)
  3011. }
  3012. if err != nil {
  3013. //logging.Error("GetDataByName err: ", err)
  3014. msg := geterrmsg(err.Error())
  3015. appG.Response(http.StatusOK, e.ERROR, msg)
  3016. } else {
  3017. dataList = append(dataList, label)
  3018. dataList = append(dataList, label2)
  3019. fmt.Println(label2)
  3020. data := make(map[string]interface{})
  3021. recordcount := int64(0)
  3022. if pagecount > 1 {
  3023. sqlstr := fmt.Sprintf("SELECT COUNT(*) as count FROM (%s) a", str)
  3024. params := []interface{}{partClassId, partClassId}
  3025. restful.BasicList(sqlstr, params, &recordcount)
  3026. if err != nil {
  3027. fmt.Println("s1", err)
  3028. }
  3029. }
  3030. if err != nil {
  3031. fmt.Println(err)
  3032. }
  3033. if recordcount == 0 {
  3034. recordcount = int64(len(partStandardData1))
  3035. }
  3036. data["list"] = dataList
  3037. //data := make(map[string]interface{})
  3038. data["total"] = recordcount
  3039. data["code"] = 0
  3040. data["message"] = "ok"
  3041. if pagecount > 0 {
  3042. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  3043. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  3044. }
  3045. data["pageSize"] = pagecount
  3046. //pageNum: 1 pageSize: 10 pages: 202 total: 2019
  3047. //data["pageNum"]=queryData
  3048. //data["pageNum"]=queryData["pageNum"]
  3049. appG.Response(http.StatusOK, e.SUCCESS, data)
  3050. }
  3051. }
  3052. }