db.go 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264
  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. fmt.Println("=========")
  1997. if sql == "" {
  1998. sqltype = ""
  1999. }
  2000. if sqltype == "s1" {
  2001. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2002. if v, ok := tempValParam.(map[string]interface{}); ok {
  2003. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2004. }
  2005. if err != nil {
  2006. return nil, err
  2007. }
  2008. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2009. if err != nil {
  2010. return nil, err
  2011. }
  2012. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2013. } else if sqltype == "sm" {
  2014. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  2015. if v, ok := tempValParam.(map[string]interface{}); ok {
  2016. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2017. } else {
  2018. fmt.Println("sm", tempValParam)
  2019. }
  2020. if err != nil {
  2021. return nil, err
  2022. }
  2023. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2024. if err != nil {
  2025. return nil, err
  2026. }
  2027. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  2028. } else if sqltype == "v" {
  2029. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  2030. if v, ok := tempValParam.(map[string]interface{}); ok {
  2031. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  2032. } else {
  2033. fmt.Println("v", tempValParam)
  2034. }
  2035. if err != nil {
  2036. return nil, err
  2037. }
  2038. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  2039. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  2040. }
  2041. } else if sqltype == "e" {
  2042. tempVal_arrary := make([]map[string]interface{}, 0)
  2043. tempVal_map := make(map[string]interface{})
  2044. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  2045. if err == nil {
  2046. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  2047. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  2048. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  2049. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  2050. return tempVal_arrary, err
  2051. }
  2052. return nil, err
  2053. } else {
  2054. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  2055. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  2056. if err != nil {
  2057. return nil, err
  2058. }
  2059. return nil, err
  2060. }
  2061. }
  2062. return nil, nil
  2063. }
  2064. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *sqlx.Tx) error {
  2065. var err error
  2066. subarray, ok := fasion["children"]
  2067. if ok {
  2068. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  2069. tempVal_arrary := make([]map[string]interface{}, 0)
  2070. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  2071. tempVal_ := make(map[string]interface{})
  2072. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  2073. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  2074. } else {
  2075. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  2076. }
  2077. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  2078. if err != nil {
  2079. return err
  2080. }
  2081. tempVal_arrary = append(tempVal_arrary, tempVal_)
  2082. }
  2083. v.(map[string]interface{})["children"] = tempVal_arrary
  2084. }
  2085. }
  2086. return nil
  2087. }
  2088. // @Summary 删除图片
  2089. // @Tags RemovePicByName
  2090. // @Accept json
  2091. // @Produce json
  2092. // @Param sqlname query string true "sqlname"
  2093. // @Param offset query int true "offset"
  2094. // @Param pagecount query int true "pagecount"
  2095. // @Param returntype query string true "returntype"
  2096. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2097. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2098. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2099. // @Router /data/PostDataByName [POST]
  2100. func RemovePicByName(c *gin.Context) {
  2101. logging.Info("RemovePicByName ", c.Keys)
  2102. appG := app.Gin{C: c}
  2103. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2104. fsion := gofasion.NewFasion(string(dataByte))
  2105. sqlnamestr := fsion.Get("name").ValueStr()
  2106. picname := fsion.Get("picname").ValueStr()
  2107. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2108. params := fsion.Get("params").Array()
  2109. s_params := make([]interface{}, 0)
  2110. for _, v_params := range params {
  2111. s_params = append(s_params, v_params.ValueStr())
  2112. }
  2113. if sql != "" {
  2114. queryData, err := execDataBySql(sql, s_params)
  2115. if picname != "" {
  2116. // Destination
  2117. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  2118. err := os.Remove(distPath) //删除文件test.txt
  2119. if err != nil {
  2120. //如果删除失败则输出 file remove Error!
  2121. fmt.Println("file remove Error!")
  2122. //输出错误详细信息
  2123. fmt.Printf("%s", err)
  2124. } else {
  2125. //如果删除成功则输出 file remove OK!
  2126. fmt.Print("file remove OK!")
  2127. }
  2128. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  2129. err = os.Remove(distPath) //删除文件test.txt
  2130. if err != nil {
  2131. //如果删除失败则输出 file remove Error!
  2132. fmt.Println("file remove Error!")
  2133. //输出错误详细信息
  2134. fmt.Printf("%s", err)
  2135. } else {
  2136. //如果删除成功则输出 file remove OK!
  2137. fmt.Print("file remove OK!")
  2138. }
  2139. }
  2140. if err != nil {
  2141. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2142. } else {
  2143. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2144. }
  2145. } else {
  2146. appG.Response(http.StatusOK, e.SUCCESS, nil)
  2147. }
  2148. }
  2149. // @Summary 删除文件
  2150. // @Tags RemoveFileByName
  2151. // @Accept json
  2152. // @Produce json
  2153. // @Param sqlname query string true "sqlname"
  2154. // @Param offset query int true "offset"
  2155. // @Param pagecount query int true "pagecount"
  2156. // @Param returntype query string true "returntype"
  2157. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2158. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2159. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2160. // @Router /data/PostDataByName [POST]
  2161. func RemoveFileByName(c *gin.Context) {
  2162. appG := app.Gin{C: c}
  2163. id := c.Param("id")
  2164. logging.Info("DownloadFile ", c.Keys, id)
  2165. eqdoc, err := restful.MapStrList("SELECT * FROM eq_doc where id = ? ", id)
  2166. if eqdoc == nil {
  2167. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2168. return
  2169. }
  2170. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, eqdoc[0]["docpath"], eqdoc[0]["newdocname"])
  2171. err = os.Remove(distPath) //删除文件test.txt
  2172. if err != nil {
  2173. //如果删除失败则输出 file remove Error!
  2174. fmt.Println("file remove Error!")
  2175. //输出错误详细信息
  2176. fmt.Printf("%s", err)
  2177. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2178. return
  2179. }
  2180. fmt.Print("file remove OK!")
  2181. _, err = restful.MapStrList("delete FROM eq_doc where id = ? ", id)
  2182. if err != nil {
  2183. log.Default().Printf("RemoveFileByName:%v", err)
  2184. }
  2185. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2186. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2187. //fsion := gofasion.NewFasion(string(dataByte))
  2188. //sqlnamestr := fsion.Get("name").ValueStr()
  2189. //filename := fsion.Get("filename").ValueStr()
  2190. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2191. //params := fsion.Get("params").Array()
  2192. //s_params := make([]interface{}, 0)
  2193. //for _, v_params := range params {
  2194. // s_params = append(s_params, v_params.ValueStr())
  2195. //}
  2196. //if sql != "" {
  2197. // queryData, err := execDataBySql(sql, s_params)
  2198. //
  2199. // if filename != "" {
  2200. // // Destination
  2201. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  2202. //
  2203. // err := os.Remove(distPath) //删除文件test.txt
  2204. // if err != nil {
  2205. // //如果删除失败则输出 file remove Error!
  2206. // fmt.Println("file remove Error!")
  2207. // //输出错误详细信息
  2208. // fmt.Printf("%s", err)
  2209. // } else {
  2210. // //如果删除成功则输出 file remove OK!
  2211. // fmt.Print("file remove OK!")
  2212. // }
  2213. // }
  2214. // if err != nil {
  2215. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  2216. // } else {
  2217. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  2218. // }
  2219. //} else {
  2220. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  2221. //}
  2222. }
  2223. // @Summary 根据APISQL表中SQL执行得到数据集
  2224. // @Tags PostDataByName
  2225. // @Accept json
  2226. // @Produce json
  2227. // @Param sqlname query string true "sqlname"
  2228. // @Param offset query int true "offset"
  2229. // @Param pagecount query int true "pagecount"
  2230. // @Param returntype query string true "returntype"
  2231. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2232. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2233. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2234. // @Router /data/PostDataByName [POST]
  2235. func UpdateDataRelation(c *gin.Context) {
  2236. appG := app.Gin{C: c}
  2237. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2238. fsion := gofasion.NewFasion(string(dataByte))
  2239. tablename := fsion.Get("name").ValueStr()
  2240. dataname := fsion.Get("dataname").ValueStr()
  2241. datavalue := fsion.Get("datavalue").ValueStr()
  2242. valuename := fsion.Get("valuename").ValueStr()
  2243. logging.Info("UpdateDataRelation ", c.Keys, c.Request.RemoteAddr, tablename, dataname, datavalue, valuename)
  2244. values := fsion.Get("values").Array()
  2245. tx, err := restful.Dbs.Beginx()
  2246. defer func() {
  2247. switch {
  2248. case err != nil:
  2249. fmt.Println("__error:", err)
  2250. if tx != nil {
  2251. tx.Rollback()
  2252. }
  2253. default:
  2254. if tx != nil {
  2255. err = tx.Commit()
  2256. }
  2257. }
  2258. }()
  2259. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  2260. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  2261. d_params := make([]interface{}, 4)
  2262. d_params[0] = datavalue
  2263. s_params := ""
  2264. for _, v_values := range values {
  2265. d_params[1] = v_values.ValueStr()
  2266. if s_params == "" {
  2267. s_params = v_values.ValueStr()
  2268. } else {
  2269. s_params = s_params + "," + v_values.ValueStr()
  2270. }
  2271. d_params[2] = d_params[1]
  2272. d_params[3] = d_params[0]
  2273. _, err := execDataBySqlT(insertSqlstr, d_params, tx)
  2274. if err != nil {
  2275. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2276. break
  2277. }
  2278. }
  2279. if s_params != "" {
  2280. deleteSqlstr := "delete from " + tablename + " " +
  2281. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  2282. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2283. if err != nil {
  2284. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2285. } else if err == nil {
  2286. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2287. }
  2288. } else {
  2289. deleteSqlstr := "delete from " + tablename + " " +
  2290. " where " + dataname + " = " + datavalue
  2291. queryData, err := execDataBySqlT(deleteSqlstr, nil, tx)
  2292. if err != nil {
  2293. appG.Response(http.StatusOK, e.ERROR, err.Error())
  2294. } else if err == nil {
  2295. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2296. }
  2297. }
  2298. }
  2299. // @Summary 根据APISQL表中SQL执行得到数据集
  2300. // @Tags PostDatasByName
  2301. // @Accept json
  2302. // @Produce json
  2303. // @Param sqlname query string true "sqlname"
  2304. // @Param offset query int true "offset"
  2305. // @Param pagecount query int true "pagecount"
  2306. // @Param returntype query string true "returntype"
  2307. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2308. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2309. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2310. // @Router /data/PostDatasByName [POST]
  2311. func PostDataByNames(c *gin.Context) {
  2312. appG := app.Gin{C: c}
  2313. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2314. fsion := gofasion.NewFasion(string(dataByte))
  2315. sqlParams := fsion.Get("array").Array()
  2316. queryData := make(map[string]interface{})
  2317. for _, v := range sqlParams {
  2318. fsion2 := gofasion.NewFasion(v.Json())
  2319. logging.Info("PostDataByNames ", c.Keys, fsion2.Get("name").ValueStr())
  2320. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  2321. if sql == "" {
  2322. errorm := make(map[string]interface{})
  2323. errorm["code"] = e.ERROR
  2324. errorm["message"] = "sql没有找到"
  2325. errorm["param"] = ""
  2326. queryData[fsion2.Get("name").ValueStr()] = errorm
  2327. continue
  2328. } else if sql != "" {
  2329. s_params := make([]interface{}, 0)
  2330. if fsion2.HasKey("parammaps") {
  2331. parammaps := fsion2.Get("parammaps")
  2332. paramslist := strings.Split(p, ",")
  2333. if len(paramslist) > 0 && p != "" {
  2334. for _, value := range paramslist {
  2335. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2336. if tempv, exists := c.Get("jwt_username"); exists {
  2337. s_params = append(s_params, tempv.(string))
  2338. } else {
  2339. s_params = append(s_params, "")
  2340. }
  2341. } else {
  2342. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  2343. }
  2344. }
  2345. }
  2346. } else if fsion2.HasKey("params") {
  2347. params := fsion2.Get("params").Array()
  2348. for _, v_params := range params {
  2349. s_params = append(s_params, v_params.ValueStr())
  2350. }
  2351. }
  2352. _, err := execDataBySql(sql, s_params)
  2353. if err != nil {
  2354. logging.Error("PostDataByName err: ", err)
  2355. errorm := make(map[string]interface{})
  2356. errorm["code"] = e.ERROR
  2357. errorm["message"] = err.Error()
  2358. errorm["msg"] = "fail"
  2359. errorm["param"] = s_params
  2360. queryData[fsion2.Get("name").ValueStr()] = errorm
  2361. continue
  2362. } else {
  2363. errorm := make(map[string]interface{})
  2364. errorm["code"] = e.SUCCESS
  2365. errorm["message"] = "success"
  2366. errorm["msg"] = "ok"
  2367. queryData[fsion2.Get("name").ValueStr()] = errorm
  2368. continue
  2369. }
  2370. } else {
  2371. errorm := make(map[string]interface{})
  2372. errorm["code"] = e.ERROR
  2373. errorm["message"] = "NO SQL"
  2374. errorm["param"] = ""
  2375. queryData[fsion2.Get("name").ValueStr()] = errorm
  2376. }
  2377. }
  2378. //appG.Response(http.StatusOK, e.SUCCESS, msg)
  2379. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  2380. }
  2381. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2382. if offset > 0 {
  2383. offset = offset - 1
  2384. }
  2385. if returntype == "list" {
  2386. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  2387. return queryData, err
  2388. } else if returntype == "listlist" {
  2389. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  2390. return queryData, err
  2391. } else {
  2392. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  2393. return queryData, err
  2394. }
  2395. }
  2396. // func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}, tx *sqlx.Tx) (interface{}, error) {
  2397. func getDataBySqlT(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  2398. if offset > 0 {
  2399. offset = offset - 1
  2400. }
  2401. if returntype == "list" {
  2402. // queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params, tx)
  2403. queryData, err := restful.QueryByListT(sqlstr, offset, pagecount, params)
  2404. return queryData, err
  2405. } else if returntype == "listlist" {
  2406. // queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params, tx)
  2407. queryData, err := restful.QueryByListListT(sqlstr, offset, pagecount, params)
  2408. return queryData, err
  2409. } else {
  2410. // queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params, tx)
  2411. queryData, err := restful.QueryByMapT(sqlstr, offset, pagecount, params)
  2412. return queryData, err
  2413. }
  2414. }
  2415. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  2416. paramslist := strings.Split(Params, ",")
  2417. s_params := make([]interface{}, 0)
  2418. if isList {
  2419. for _, v_params := range thisParams["params"].([]interface{}) {
  2420. s_params = append(s_params, v_params)
  2421. }
  2422. } else {
  2423. paramslist := strings.Split(Params, ",")
  2424. if len(paramslist) > 0 && Params != "" {
  2425. for _, value := range paramslist {
  2426. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2427. }
  2428. }
  2429. }
  2430. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  2431. for _, queryData_ := range queryData {
  2432. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  2433. if isList {
  2434. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  2435. if queryData_[idname] != nil {
  2436. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  2437. }
  2438. } else {
  2439. if queryData_[idname] != nil {
  2440. thisParams[idname] = queryData_[idname].(interface{})
  2441. }
  2442. if len(paramslist) > 0 && Params != "" {
  2443. s_params = make([]interface{}, 0)
  2444. for _, value := range paramslist {
  2445. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  2446. }
  2447. }
  2448. }
  2449. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  2450. } else {
  2451. }
  2452. }
  2453. return queryData, err
  2454. }
  2455. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2456. queryData, err := restful.ExecQuery(sqlstr, params)
  2457. return queryData, err
  2458. }
  2459. func execDataBySqlT(sqlstr string, params []interface{}, tx *sqlx.Tx) (interface{}, error) {
  2460. queryData, err := restful.ExecQueryT(sqlstr, params, tx)
  2461. return queryData, err
  2462. }
  2463. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  2464. queryData, err := restful.ExecQuery(sqlstr, params)
  2465. return queryData, err
  2466. }
  2467. // @Summary 导入excel文件到数据库,excel第一行作为参数
  2468. // @Tags ImportExcel
  2469. // @Accept json
  2470. // @Produce json
  2471. // @Param file query string true "文件名"
  2472. // @Param name header string true "执行sql的名字"
  2473. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  2474. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  2475. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  2476. // @Router /data/ImportExcel [POST]
  2477. // @param 备件编号,备件名称,规格型号,计量单位,品牌,计划量,单价,备注,jwt_username
  2478. // func ImportExcel(c *gin.Context) {
  2479. // log.Default().Println("Importing Excel================================")
  2480. // appG := app.Gin{C: c}
  2481. // file, _, err := c.Request.FormFile("file")
  2482. // if err != nil {
  2483. // logrus.Errorf("request.FormFile: %v", err)
  2484. // appG.Response(http.StatusOK, e.ERROR, err)
  2485. // return
  2486. // }
  2487. // xlsx1, err := excelize.OpenReader(file)
  2488. // if err != nil {
  2489. // logrus.Errorf("excelize.OpenReader: %v", err)
  2490. // appG.Response(http.StatusOK, e.ERROR, err)
  2491. // return
  2492. // }
  2493. // c.Request.ParseForm()
  2494. // params := c.Request.Form
  2495. // sheetname := params.Get("sheetname")
  2496. // rows := xlsx1.GetRows(sheetname)
  2497. // importParams := params.Get("importParams")
  2498. // paramslist := strings.Split(importParams, ",")
  2499. // sqlnamestr := params.Get("name")
  2500. // sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2501. // inputdata := ""
  2502. // ok := 0
  2503. // error := 0
  2504. // queryData := make([]interface{}, 0)
  2505. // data := make(map[string]interface{})
  2506. // if len(rows) > 10000 {
  2507. // rows = rows[:10000]
  2508. // }
  2509. // if len(rows) <= 1 {
  2510. // logrus.Errorln("表格为空")
  2511. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格为空"))
  2512. // return
  2513. // }
  2514. // if len(rows[0]) < len(paramslist) {
  2515. // logrus.Errorln("表格缺少数据列")
  2516. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格缺少数据列"))
  2517. // return
  2518. // }
  2519. // for i, v := range paramslist {
  2520. // if strings.Trim(rows[0][i], " ") != v {
  2521. // logrus.Errorln("表格数据列顺序不符合标准,或标题名称错误")
  2522. // appG.Response(http.StatusOK, e.ERROR, errors.New("表格数据列顺序不符合标准,或标题名称错误"))
  2523. // return
  2524. // }
  2525. // }
  2526. // for _, row := range rows[1:] {
  2527. // s_params := make([]interface{}, 0)
  2528. // outentry := make(map[string]interface{})
  2529. // for i := range row {
  2530. // row[i] = strings.TrimSpace(row[i])
  2531. // s_params = append(s_params, row[i])
  2532. // }
  2533. // uname := ""
  2534. // if tempv, b := c.Get("jwt_username"); b {
  2535. // uname = tempv.(string)
  2536. // }
  2537. // s_params = append(s_params, uname)
  2538. // outentry["input"] = inputdata
  2539. // log.Default().Printf("params=%v", s_params...)
  2540. // returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  2541. // returnmsgmap, _ := returnmsg.(map[string]interface{})
  2542. // returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  2543. // if err != nil {
  2544. // outentry["error_msg"] = err.Error()
  2545. // error++
  2546. // queryData = append(queryData, outentry)
  2547. // } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  2548. // ok++
  2549. // } else {
  2550. // msg := geterrmsg(returnmsgmap1[0]["msg"].(string))
  2551. // outentry["error_msg"] = msg
  2552. // error++
  2553. // queryData = append(queryData, outentry)
  2554. // }
  2555. // }
  2556. // data["result"] = queryData
  2557. // data["success"] = ok
  2558. // data["err_count"] = error
  2559. // appG.Response(http.StatusOK, e.SUCCESS, data)
  2560. // }
  2561. func ImportExcel(c *gin.Context) {
  2562. appG := app.Gin{C: c}
  2563. inputdata := ""
  2564. if err := c.Request.ParseForm(); err != nil {
  2565. log.Default().Printf("request.parseform:%v", err)
  2566. appG.Response(http.StatusOK, e.ERROR, err)
  2567. return
  2568. }
  2569. params := c.Request.Form
  2570. // file, _, err := c.Request.FormFile("file")
  2571. fileHeader, err := c.FormFile("file")
  2572. file, err := fileHeader.Open()
  2573. if err != nil {
  2574. log.Default().Printf("multipart.FileHeader.open:%v", err)
  2575. appG.Response(http.StatusOK, e.ERROR, err)
  2576. return
  2577. }
  2578. if err != nil {
  2579. log.Default().Printf("request.FormFile: %v", err)
  2580. appG.Response(http.StatusOK, e.ERROR, err)
  2581. return
  2582. }
  2583. sqlnamestr := params.Get("name")
  2584. sheetname := params.Get("sheetname")
  2585. importParams := params.Get("importParams")
  2586. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  2587. // 备件编号,备件名称,规格型号,计量单位,品牌,计划量,单价,备注,jwt_username
  2588. paramslist := strings.Split(importParams, ",")
  2589. paramslist = append(paramslist, "jwt_username")
  2590. queryData := make([]interface{}, 0)
  2591. xlsx1, err := excelize.OpenReader(file)
  2592. if err != nil {
  2593. log.Default().Printf("excelize.OpenReader:%v", err)
  2594. return
  2595. }
  2596. ok := 0
  2597. error := 0
  2598. rows := xlsx1.GetRows(sheetname)
  2599. if len(rows) > 10000 {
  2600. rows = rows[:10000]
  2601. }
  2602. for i, row := range rows {
  2603. if i > 0 {
  2604. if len(paramslist) > 0 {
  2605. s_params := make([]interface{}, 0)
  2606. entry := make(map[string]interface{})
  2607. outentry := make(map[string]interface{})
  2608. inputdata = ""
  2609. for _, value := range paramslist {
  2610. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  2611. if tempv, exists := c.Get("jwt_username"); exists {
  2612. s_params = append(s_params, tempv.(string))
  2613. } else {
  2614. s_params = append(s_params, "")
  2615. }
  2616. } else {
  2617. for j, colCell := range rows[0] {
  2618. if strings.Trim(value, " ") == colCell {
  2619. for k, colCell1 := range row {
  2620. if j == k {
  2621. outentry[colCell] = colCell1
  2622. entry[colCell] = colCell1
  2623. //inputdata = inputdata + colCell + ":" + colCell1 + ";"
  2624. }
  2625. }
  2626. }
  2627. }
  2628. s_params = append(s_params, entry[strings.Trim(value, " ")])
  2629. }
  2630. }
  2631. outentry["input"] = inputdata
  2632. returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  2633. returnmsgmap, _ := returnmsg.(map[string]interface{})
  2634. returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  2635. log.Default().Printf("return:%v", returnmsgmap)
  2636. if err != nil {
  2637. outentry["error_msg"] = err.Error()
  2638. error++
  2639. queryData = append(queryData, outentry)
  2640. } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  2641. ok++
  2642. } else {
  2643. error++
  2644. msg := ""
  2645. if len(returnmsgmap1) > 0 && returnmsgmap1[0]["msg"] != nil {
  2646. msg = geterrmsg(returnmsgmap1[0]["msg"].(string))
  2647. }
  2648. outentry["error_msg"] = msg
  2649. queryData = append(queryData, outentry)
  2650. }
  2651. }
  2652. }
  2653. }
  2654. data := make(map[string]interface{})
  2655. data["result"] = queryData
  2656. data["success"] = ok
  2657. data["err_count"] = error
  2658. appG.Response(http.StatusOK, e.SUCCESS, data)
  2659. }
  2660. func DownloadMailReport(context *gin.Context) {
  2661. dataByte, _ := ioutil.ReadAll(context.Request.Body)
  2662. fsion := gofasion.NewFasion(string(dataByte))
  2663. //filename := context.Param("filename")
  2664. sqlnamestr := fsion.Get("name").ValueStr()
  2665. pastureId := fsion.Get("pastureId").ValueStr()
  2666. pastureName := fsion.Get("pastureName").ValueStr()
  2667. date := fsion.Get("date").ValueStr()
  2668. logging.Info("DownloadMailReport ", context.Keys, sqlnamestr, pastureName)
  2669. filepath := ""
  2670. if sqlnamestr == "DownloadMailReportJY" {
  2671. filepath, _ = util.DownloadDataJT(date)
  2672. } else if sqlnamestr == "DownloadMailReportMC" {
  2673. filepath, _ = util.DownloadDataMC(pastureName, date, pastureId)
  2674. } else if sqlnamestr == "sssss" { // 平时用来拉数据的
  2675. util.Sssss()
  2676. } else if sqlnamestr == "DownloadEquipment" { // 设备指标导出方法
  2677. parammaps := fsion.Get("parammaps").ValueStr()
  2678. filepath, _ = util.DownloadEquipment(parammaps)
  2679. }
  2680. if filepath == "" {
  2681. http.NotFound(context.Writer, context.Request)
  2682. return
  2683. }
  2684. originFilePath := filepath
  2685. //println("originFilePath==========",originFilePath)
  2686. if fs.PathExists(originFilePath) == false {
  2687. // if the path not found
  2688. http.NotFound(context.Writer, context.Request)
  2689. return
  2690. }
  2691. http.ServeFile(context.Writer, context.Request, originFilePath)
  2692. }
  2693. // 资产标准管理判断牧场是否有选择的该部门
  2694. func PastureDepartment(c *gin.Context) {
  2695. appG := app.Gin{C: c}
  2696. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2697. fsion := gofasion.NewFasion(string(dataByte))
  2698. paramMap := fsion.Get("param")
  2699. ids := paramMap.Get("ids").ValueStr()
  2700. deptName := paramMap.Get("deptName").ValueStr()
  2701. idList := strings.Split(ids, ",")
  2702. checkDeptList := make([]map[string]interface{}, 0)
  2703. for _, v := range idList {
  2704. aa, _ := strconv.Atoi(v)
  2705. data, err := restful.MapStrList("select * from department where pastureId=? and name=? and enable=1 and isDel=0 and isPasture=0", aa, deptName)
  2706. if err != nil {
  2707. fmt.Println(err)
  2708. } else {
  2709. list := make(map[string]interface{}, 1)
  2710. if len(data) == 0 {
  2711. list["id"] = v
  2712. list["enable"] = 0
  2713. } else {
  2714. list["id"] = v
  2715. list["enable"] = 1
  2716. }
  2717. checkDeptList = append(checkDeptList, list)
  2718. }
  2719. }
  2720. appG.Response(http.StatusOK, e.SUCCESS, checkDeptList)
  2721. }
  2722. //{ 'label': '汶上', 'property': 'm1' }
  2723. // 资产标准管理
  2724. func Equipment(c *gin.Context) {
  2725. appG := app.Gin{C: c}
  2726. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2727. fsion := gofasion.NewFasion(string(dataByte))
  2728. deptName := fsion.Get("deptName").ValueStr()
  2729. eqClassId := fsion.Get("eqClassId").ValueStr()
  2730. offset := fsion.Get("offset").ValueDefaultInt(0)
  2731. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2732. dataList := make([]interface{}, 0)
  2733. pastureData, err := restful.MapStrList("select p.`name`,p.id from pasture p group by p.`name`")
  2734. str := "select eqS.id,eqS.deapartmentName deptName, eqS.`use`,eqclass.`typeName`,p.`name`,p.id pastureId ,eqS.eqClassId " +
  2735. " from equipment_standard eqS" +
  2736. " left join pasture p on p.id= eqS.pastureId" +
  2737. " left join eqclass on eqclass.id=eqS.eqClassId " +
  2738. " where (eqS.deapartmentName = :deptName or :deptName ='') and " +
  2739. " (eqS.eqClassId = :eqClassId or :eqClassId ='') " +
  2740. " group by deptName,eqClassId " +
  2741. " ORDER BY eqS.id DESC"
  2742. offset = offset - 1
  2743. if pagecount > 0 && offset > 0 {
  2744. offset = offset * pagecount
  2745. }
  2746. paramMap_3_6 := map[string]interface{}{"deptName": deptName, "eqClassId": eqClassId}
  2747. str += fmt.Sprintf("limit %d offset %d", pagecount, offset)
  2748. equipmentStandardData1, err := restful.MapStrList(str, paramMap_3_6)
  2749. //数据
  2750. equipmentStandardData2, err := restful.MapStrList("select eqS.deapartmentName deptName, eqS.id eqSId,eqS.equipmentNumber,p.`name`,p.id pastureId,eqS.eqClassId"+
  2751. " from equipment_standard eqS"+
  2752. " left join pasture p on p.id= eqS.pastureId "+
  2753. " left join eqclass eqc on eqc.id=eqS.eqClassId "+
  2754. "where (eqS.deapartmentName =? or ?='') and"+
  2755. " (eqS.eqClassId = ? or ?='') ", deptName, deptName, eqClassId, eqClassId)
  2756. sqlstr := str
  2757. params := make([]interface{}, 0)
  2758. for i := 0; i < 2; i++ {
  2759. param := make(map[string]interface{}, 0)
  2760. param["deptName"] = deptName
  2761. param["eqClassId"] = eqClassId
  2762. params = append(params, param)
  2763. }
  2764. label := make([]map[string]interface{}, 0)
  2765. if err != nil {
  2766. fmt.Println(err)
  2767. } else {
  2768. for i, v := range pastureData {
  2769. pasturelist := make(map[string]interface{}, 1)
  2770. pasturelist["label"] = v["name"]
  2771. pasturelist["id"] = v["id"]
  2772. pasturelist["property"] = "m" + strconv.Itoa(i+1)
  2773. label = append(label, pasturelist)
  2774. }
  2775. label2 := make([]map[string]interface{}, 0)
  2776. for _, s := range equipmentStandardData1 {
  2777. equipmentStandardList := make(map[string]interface{}, 1)
  2778. for i := 1; i <= len(label); i++ {
  2779. equipmentStandardList["m"+strconv.Itoa(i)] = ""
  2780. }
  2781. equipmentStandardList["deptName"] = s["deptName"]
  2782. equipmentStandardList["use"] = s["use"]
  2783. equipmentStandardList["typeName"] = s["typeName"]
  2784. equipmentStandardList["eqClassId"] = s["eqClassId"]
  2785. for _, z := range equipmentStandardData2 {
  2786. for e, x := range label {
  2787. if s["deptName"] == z["deptName"] && x["label"] == z["name"] && s["eqClassId"] == z["eqClassId"] {
  2788. equipmentStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  2789. equipmentStandardList[x["property"].(string)] = z["equipmentNumber"]
  2790. } else {
  2791. equipmentStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  2792. }
  2793. }
  2794. }
  2795. label2 = append(label2, equipmentStandardList)
  2796. }
  2797. if err != nil {
  2798. msg := geterrmsg(err.Error())
  2799. appG.Response(http.StatusOK, e.ERROR, msg)
  2800. } else {
  2801. dataList = append(dataList, label)
  2802. dataList = append(dataList, label2)
  2803. data := make(map[string]interface{})
  2804. recordcount := int64(0)
  2805. if pagecount > 1 {
  2806. sqlstr := fmt.Sprintf("SELECT COUNT(*) as count FROM (%s) a", sqlstr)
  2807. params := []interface{}{deptName, deptName, eqClassId, eqClassId}
  2808. err := restful.BasicList(sqlstr, params, &recordcount)
  2809. if err != nil {
  2810. log.Default().Printf("Equipment query error: %v", err)
  2811. }
  2812. }
  2813. if err != nil {
  2814. fmt.Println(err)
  2815. }
  2816. if recordcount == 0 {
  2817. recordcount = int64(len(equipmentStandardData1))
  2818. }
  2819. data["list"] = dataList
  2820. //data := make(map[string]interface{})
  2821. data["total"] = recordcount
  2822. data["code"] = 0
  2823. data["message"] = "ok"
  2824. if pagecount > 0 {
  2825. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  2826. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  2827. }
  2828. data["pageSize"] = pagecount
  2829. appG.Response(http.StatusOK, e.SUCCESS, data)
  2830. }
  2831. }
  2832. }
  2833. // 资产标准管理插入修改写在一起
  2834. func UpdateAndInsert(c *gin.Context) {
  2835. appG := app.Gin{C: c}
  2836. data := make(map[string]interface{}, 0)
  2837. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2838. fsion := gofasion.NewFasion(string(dataByte))
  2839. param := fsion.Get("param")
  2840. check := fsion.Get("check").ValueStr()
  2841. tx, err := restful.Dbs.Beginx()
  2842. deptName := param.Get("deptName").ValueStr()
  2843. eqCode := param.Get("eqCode").ValueStr()
  2844. eqClassId, err := strconv.Atoi(param.Get("eqClassId").ValueStr())
  2845. use := param.Get("use").ValueStr()
  2846. paramStr := fsion.Get("param").ValueStr()
  2847. paramMap := make(map[string]interface{}, 0)
  2848. err = json.Unmarshal([]byte(paramStr), &paramMap)
  2849. fmt.Println(len(paramMap))
  2850. fmt.Println(paramMap)
  2851. // 标准库存
  2852. name := fsion.Get("name").ValueStr()
  2853. partClassId, err := strconv.Atoi(param.Get("partClassId").ValueStr())
  2854. ff := 0
  2855. if check == "insert" {
  2856. querydata := make([]map[string]string, 0)
  2857. if name == "partStandard" {
  2858. ff = 2
  2859. querydata, err = restful.MapStrList(" select * from part_standard where partClassId2 =? ", partClassId)
  2860. if querydata != nil {
  2861. //data["msg"]="请不输入重复数据"
  2862. msg := geterrmsg("请不输入重复数据")
  2863. appG.Response(http.StatusOK, e.ERROR, msg)
  2864. return
  2865. }
  2866. } else {
  2867. ff = 4
  2868. querydata, err = restful.MapStrList(" select * from equipment_standard where eqClassId =? and deapartmentName = ?", eqClassId, deptName)
  2869. if querydata != nil {
  2870. //data["msg"]="请不输入重复数据"
  2871. msg := geterrmsg("请不输入重复数据")
  2872. appG.Response(http.StatusOK, e.ERROR, msg)
  2873. return
  2874. }
  2875. }
  2876. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2877. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2878. if err != nil {
  2879. }
  2880. if name == "partStandard" {
  2881. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2882. tx.Exec("call add_part_standard(?,?,?)", partClassId, pastureId, param.Get("m"+strconv.Itoa(i)))
  2883. }
  2884. } else {
  2885. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2886. tx.Exec("call add_equipment_standard(?,?,?,?,?,?)", deptName, use, eqClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr(), eqCode)
  2887. }
  2888. }
  2889. }
  2890. if err != nil {
  2891. }
  2892. }
  2893. if check == "update" {
  2894. if name == "partStandard" {
  2895. ff = 2
  2896. tx.Exec("delete from part_standard where partClassId2 =?", partClassId)
  2897. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2898. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2899. if err != nil {
  2900. }
  2901. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2902. tx.Exec("call add_part_standard(?,?,?)", partClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr())
  2903. }
  2904. }
  2905. } else {
  2906. ff = 4
  2907. tx.Exec("delete from equipment_standard where deapartmentName=? and eqClassId=?", deptName, eqClassId)
  2908. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2909. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2910. if err != nil {
  2911. }
  2912. if param.Get("m"+strconv.Itoa(i)).ValueStr() != "" {
  2913. tx.Exec("call add_equipment_standard(?,?,?,?,?,?)", deptName, use, eqClassId, pastureId, param.Get("m"+strconv.Itoa(i)).ValueStr(), eqCode)
  2914. // " WHERE deapartmentName=? and pastureId = ? and eqClassId=?", use, param.Get("m"+strconv.Itoa(i)).ValueStr(), deptName, pastureId,eqClassId).Execute()
  2915. }
  2916. }
  2917. }
  2918. if err != nil {
  2919. data["msg"] = "error"
  2920. data["error"] = err.Error()
  2921. appG.Response(http.StatusOK, e.SUCCESS, data)
  2922. }
  2923. }
  2924. if check == "delete" {
  2925. if name == "partStandard" {
  2926. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2927. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2928. if err != nil {
  2929. }
  2930. tx.Exec("delete from part_standard where pastureId=? and partClassId2=?", pastureId, partClassId)
  2931. }
  2932. } else {
  2933. ff = 4
  2934. for i := 1; i <= (len(paramMap)-ff)/2; i++ {
  2935. pastureId, err := strconv.Atoi(param.Get("pastureId" + strconv.Itoa(i)).ValueStr())
  2936. if err != nil {
  2937. }
  2938. tx.Exec("delete from equipment_standard where deapartmentName=? and pastureId=? and eqClassId=?", deptName, pastureId, eqClassId)
  2939. }
  2940. }
  2941. }
  2942. data["msg"] = "ok"
  2943. appG.Response(http.StatusOK, e.SUCCESS, data)
  2944. defer func() {
  2945. switch {
  2946. case err != nil:
  2947. logging.Error("tx.Begin 事务启动失败__error:", err)
  2948. if tx != nil {
  2949. tx.Rollback()
  2950. }
  2951. default:
  2952. if tx != nil {
  2953. err = tx.Commit()
  2954. }
  2955. }
  2956. }()
  2957. }
  2958. func StandardPart(c *gin.Context) {
  2959. appG := app.Gin{C: c}
  2960. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  2961. fsion := gofasion.NewFasion(string(dataByte))
  2962. partClassId := fsion.Get("partClassId").ValueStr()
  2963. offset := fsion.Get("offset").ValueDefaultInt(0)
  2964. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  2965. pastureData, err := restful.MapStrList("select p.`name`,p.id from pasture p group by p.`name`")
  2966. //s_params := make([]interface{}, 0)
  2967. offset = offset - 1
  2968. if pagecount > 0 && offset > 0 {
  2969. offset = offset * pagecount
  2970. }
  2971. dataList := make([]interface{}, 0)
  2972. label := make([]map[string]interface{}, 0)
  2973. 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 " +
  2974. " left join pasture p on p.id = p_s.pastureId where p_s.partClassId2 = ? or ?='' " +
  2975. " GROUP BY p_s.partClassId2 ORDER BY p_s.id DESC"
  2976. 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 ?=''"
  2977. str += fmt.Sprintf(" limit %d offset %d", pagecount, offset)
  2978. partStandardData1, err := restful.MapStrList(str, partClassId, partClassId)
  2979. //数据
  2980. partStandardData2, err := restful.MapStrList(str1, partClassId, partClassId)
  2981. if err != nil {
  2982. fmt.Println(err)
  2983. } else {
  2984. for i, v := range pastureData {
  2985. pasturelist := make(map[string]interface{}, 1)
  2986. pasturelist["label"] = v["name"]
  2987. pasturelist["id"] = v["id"]
  2988. pasturelist["property"] = "m" + strconv.Itoa(i+1)
  2989. label = append(label, pasturelist)
  2990. }
  2991. label2 := make([]map[string]interface{}, 0)
  2992. //equipmentStandardList := make(map[string]interface{}, 1)
  2993. for _, s := range partStandardData1 {
  2994. partStandardList := make(map[string]interface{}, 1)
  2995. for i := 1; i <= len(label); i++ {
  2996. partStandardList["m"+strconv.Itoa(i)] = ""
  2997. }
  2998. partStandardList["partClassId"] = s["partClassId2"]
  2999. partStandardList["partName"] = s["partName"]
  3000. for _, z := range partStandardData2 {
  3001. for e, x := range label {
  3002. if x["label"] == z["name"] && s["partClassId2"] == z["partClassId2"] {
  3003. partStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  3004. //equipmentStandardList["eqSId"+strconv.Itoa(e+1)] = z["eqSId"]
  3005. partStandardList[x["property"].(string)] = z["standardMoney"]
  3006. } else {
  3007. partStandardList["pastureId"+strconv.Itoa(e+1)] = x["id"]
  3008. }
  3009. }
  3010. }
  3011. label2 = append(label2, partStandardList)
  3012. }
  3013. if err != nil {
  3014. //logging.Error("GetDataByName err: ", err)
  3015. msg := geterrmsg(err.Error())
  3016. appG.Response(http.StatusOK, e.ERROR, msg)
  3017. } else {
  3018. dataList = append(dataList, label)
  3019. dataList = append(dataList, label2)
  3020. fmt.Println(label2)
  3021. data := make(map[string]interface{})
  3022. recordcount := int64(0)
  3023. if pagecount > 1 {
  3024. sqlstr := fmt.Sprintf("SELECT COUNT(*) as count FROM (%s) a", str)
  3025. params := []interface{}{partClassId, partClassId}
  3026. restful.BasicList(sqlstr, params, &recordcount)
  3027. if err != nil {
  3028. fmt.Println("s1", err)
  3029. }
  3030. }
  3031. if err != nil {
  3032. fmt.Println(err)
  3033. }
  3034. if recordcount == 0 {
  3035. recordcount = int64(len(partStandardData1))
  3036. }
  3037. data["list"] = dataList
  3038. //data := make(map[string]interface{})
  3039. data["total"] = recordcount
  3040. data["code"] = 0
  3041. data["message"] = "ok"
  3042. if pagecount > 0 {
  3043. data["pages"] = math.Ceil(float64(recordcount) / float64(pagecount))
  3044. data["pageNum"] = math.Ceil(float64(offset+1) / float64(pagecount))
  3045. }
  3046. data["pageSize"] = pagecount
  3047. //pageNum: 1 pageSize: 10 pages: 202 total: 2019
  3048. //data["pageNum"]=queryData
  3049. //data["pageNum"]=queryData["pageNum"]
  3050. appG.Response(http.StatusOK, e.SUCCESS, data)
  3051. }
  3052. }
  3053. }