db.go 108 KB

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