81befe9a26c137fd6ec275ee2081733053e1246b.svn-base 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. package api
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/360EntSecGroup-Skylar/excelize"
  7. "github.com/Anderson-Lu/gofasion/gofasion"
  8. "github.com/gin-gonic/gin"
  9. "github.com/kptyun/KPTCOMM/pkg/app"
  10. "github.com/kptyun/KPTCOMM/pkg/e"
  11. "github.com/kptyun/KPTCOMM/pkg/logging"
  12. "github.com/kptyun/KPTCOMM/pkg/setting"
  13. "github.com/kptyun/KPTCOMM/routers/restful"
  14. "github.com/tealeg/xlsx"
  15. "github.com/xormplus/xorm"
  16. "io/ioutil"
  17. "net/http"
  18. "os"
  19. "path"
  20. "strconv"
  21. "strings"
  22. "time"
  23. )
  24. // @Summary 根据SQL执行得到数据集
  25. // @Tags getData
  26. // @Accept json
  27. // @Produce json
  28. // @Param sql query string true "sql"
  29. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  30. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  31. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  32. // @Router /data/getdata [POST]
  33. func GetData(c *gin.Context) {
  34. appG := app.Gin{C: c}
  35. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  36. fsion := gofasion.NewFasion(string(dataByte))
  37. sqlstr := fsion.Get("sql").ValueStr()
  38. offset := fsion.Get("offset").ValueDefaultInt(0)
  39. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  40. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  41. queryData, err := getDataBySql(sqlstr, offset, pagecount, returntype, nil)
  42. if err != nil {
  43. appG.Response(http.StatusOK, e.ERROR, err.Error())
  44. } else {
  45. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  46. }
  47. }
  48. func TestData(c *gin.Context) {
  49. appG := app.Gin{C: c}
  50. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  51. fsion := gofasion.NewFasion(string(dataByte))
  52. sqlstr := fsion.Get("sql").ValueStr()
  53. token := ""
  54. if tempv, exists := c.Get("jwt_username"); exists {
  55. token = tempv.(string)
  56. }
  57. appG.Response(http.StatusOK, e.SUCCESS, "你录入的名字是:"+sqlstr+";token:"+token)
  58. }
  59. // @Summary 根据APISQL表中SQL执行得到数据集
  60. // @Tags GetDataByName
  61. // @Accept json
  62. // @Produce json
  63. // @Param sqlname body string true "sqlname"
  64. // @Param offset body int true "offset"
  65. // @Param pagecount body int true "pagecount"
  66. // @Param returntype body string true "returntype"
  67. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  68. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  69. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  70. // @Router /data/GetDataByName [POST]
  71. func GetDataByName(c *gin.Context) {
  72. appG := app.Gin{C: c}
  73. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  74. fsion := gofasion.NewFasion(string(dataByte))
  75. sqlnamestr := fsion.Get("name").ValueStr()
  76. offset := fsion.Get("offset").ValueDefaultInt(0)
  77. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  78. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  79. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  80. fmt.Println("restful.GetSqlByNameDB",sql)
  81. if sql != "" {
  82. s_params := make([]interface{}, 0)
  83. if fsion.HasKey("parammaps") {
  84. parammaps := fsion.Get("parammaps")
  85. logging.Info("GetDataByName",c.Keys,c.Request.RemoteAddr,sqlnamestr,parammaps.ValueStr())
  86. paramslist := strings.Split(p, ",")
  87. if len(paramslist) > 0 && p != "" {
  88. for _, value := range paramslist {
  89. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  90. if tempv, exists := c.Get("jwt_username"); exists {
  91. s_params = append(s_params, tempv.(string))
  92. } else {
  93. s_params = append(s_params, "")
  94. }
  95. } else {
  96. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  97. }
  98. }
  99. }
  100. } else if fsion.HasKey("params") {
  101. params := fsion.Get("params").Array()
  102. logging.Info("GetDataByName",c.Keys,c.Request.RemoteAddr,sqlnamestr,params)
  103. for _, v_params := range params {
  104. s_params = append(s_params, v_params.ValueStr())
  105. }
  106. }
  107. if returntype == "xlsx" {
  108. var xlsxfile *xlsx.File
  109. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  110. //c.File("c:/dd.xlsx")
  111. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  112. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  113. c.Header("Content-Transfer-Encoding", "binary")
  114. _ = xlsxfile.Write(c.Writer)
  115. } else {
  116. queryData, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  117. if err != nil {
  118. logging.Error("GetDataByName err: ",err)
  119. appG.Response(http.StatusOK, e.ERROR, err.Error())
  120. } else {
  121. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  122. }
  123. }
  124. } else {
  125. appG.Response(http.StatusOK, e.SUCCESS, nil)
  126. }
  127. }
  128. // @Summary 根据APISQL表中SQL执行得到数据集
  129. // @Tags GetDataByNameForm
  130. // @Accept json
  131. // @Produce json
  132. // @Param sqlname body json true "sqlname"
  133. // @Param offset query int true "offset"
  134. // @Param pagecount query int true "pagecount"
  135. // @Param returntype query string true "returntype"
  136. // @Param s formData json true "ss"
  137. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  138. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  139. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  140. // @Router /data/GetDataByNameForm [POST]
  141. func GetDataByNameForm(c *gin.Context) {
  142. appG := app.Gin{C: c}
  143. //sqlnamestr := c.PostForm("measureNumber")
  144. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  145. //fsion := gofasion.NewFasion(string(dataByte))
  146. //sqlnamestr := fsion.Get("name").ValueStr()
  147. //offset := fsion.Get("offset").ValueInt()
  148. //pagecount := fsion.Get("pagecount").ValueInt()
  149. //returntype := fsion.Get("returntype").ValueStr()
  150. //params := fsion.Get("params").Array()
  151. c.Request.ParseForm()
  152. params := c.Request.Form
  153. offset := 0
  154. pageSize_ := 0
  155. returntype := params.Get("page")
  156. if returntype == "" {
  157. returntype = "0"
  158. }
  159. if tempv, exists := c.Get("jwt_username"); exists {
  160. params.Set("JwtUserName", tempv.(string))
  161. } else {
  162. params.Set("JwtUserName", "")
  163. }
  164. sqlnamestr := params.Get("name")
  165. logging.Info("GetDataByNameForm ",c.Keys,sqlnamestr)
  166. s_params := make([]interface{}, 0)
  167. if returntype == "1" {
  168. pageNum, error := strconv.Atoi(params.Get("pageNum"))
  169. if error != nil {
  170. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "页码不是整数")
  171. }
  172. pageSize, error := strconv.Atoi(params.Get("pageSize"))
  173. if error != nil {
  174. appG.ResponseEq(http.StatusOK, e.ERROR, 0, "每页行数不是整数")
  175. }
  176. pageSize_ = pageSize
  177. offset = pageNum
  178. }
  179. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  180. paramslist := strings.Split(p, ",")
  181. if len(paramslist) > 0 && p != "" {
  182. for _, value := range paramslist {
  183. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  184. }
  185. }
  186. if sql != "" {
  187. if returntype == "xlsx" {
  188. var xlsxfile *xlsx.File
  189. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  190. c.Header("Content-Type", "application/octet-stream")
  191. c.Header("Content-Disposition", "attachment; filename="+sqlnamestr+".xlsx")
  192. c.Header("Content-Transfer-Encoding", "binary")
  193. _ = xlsxfile.Write(c.Writer)
  194. } else {
  195. queryData, err := getDataBySql(sql, offset, pageSize_, returntype, s_params)
  196. if err != nil {
  197. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  198. } else {
  199. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  200. }
  201. }
  202. } else {
  203. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  204. }
  205. }
  206. // @Summary 递归获取所有
  207. // @Tags menu
  208. // @Accept json
  209. // @Produce json
  210. // @Param Ton query string true "Ton"
  211. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  212. // @Router /authdata/GetRoleMenus [GET]
  213. func GetRecuDataByName(c *gin.Context) {
  214. appG := app.Gin{C: c}
  215. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  216. fsion := gofasion.NewFasion(string(dataByte))
  217. sqlnamestr := fsion.Get("name").ValueStr()
  218. parentidname := fsion.Get("idname").ValueStr()
  219. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  220. params := make(map[string]interface{})
  221. s_params := make([]interface{}, 0)
  222. if fsion.HasKey("params") {
  223. for _, v_params := range fsion.Get("params").Array() {
  224. s_params = append(s_params, v_params.ValueStr())
  225. }
  226. params["params"] = s_params
  227. } else {
  228. jparams := fsion.Get("parammaps")
  229. paramslist := strings.Split(p, ",")
  230. if len(paramslist) > 0 && p != "" {
  231. for _, value := range paramslist {
  232. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  233. if tempv, exists := c.Get("jwt_username"); exists {
  234. params[value] = tempv.(string)
  235. } else {
  236. params[value] = ""
  237. }
  238. } else {
  239. params[value] = jparams.Get(strings.Trim(value, " ")).ValueStr()
  240. }
  241. }
  242. }
  243. }
  244. queryData, err := GetRecuData_BySql(sql, p, parentidname, params, fsion.HasKey("params"))
  245. logging.Info("sqlname parammaps",c.Keys,c.Request.RemoteAddr,sqlnamestr,params)
  246. if err != nil {
  247. appG.Response(http.StatusOK, e.ERROR, err.Error())
  248. }
  249. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  250. }
  251. // @Summary 根据APISQL表中SQL执行得到数据集
  252. // @Tags GetDataByNames
  253. // @Accept json
  254. // @Produce json
  255. // @RequestBody sqlname query string true "sqlname"
  256. // @Param offset query int true "offset"
  257. // @Param pagecount query int true "pagecount"
  258. // @Param returntype query string true "returntype"
  259. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  260. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  261. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  262. // @Router /data/GetDataByNames [POST]
  263. func GetDataByNames(c *gin.Context) {
  264. appG := app.Gin{C: c}
  265. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  266. fsion := gofasion.NewFasion(string(dataByte))
  267. sqlParams := fsion.Array()
  268. queryData := make(map[string]interface{})
  269. for _, v := range sqlParams {
  270. fsion2 := gofasion.NewFasion(v.Json())
  271. logging.Info("GetDataByNames",c.Keys,fsion2.Get("name").ValueStr(),c.Request.RemoteAddr)
  272. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  273. if sql == "" {
  274. errorm := make(map[string]interface{})
  275. errorm["code"] = e.ERROR
  276. errorm["message"] = "sql没找到"
  277. queryData[fsion2.Get("name").ValueStr()] = errorm
  278. continue
  279. } else if sql != "" {
  280. offset := fsion2.Get("offset").ValueInt()
  281. pagecount := fsion2.Get("pagecount").ValueInt()
  282. returntype := fsion2.Get("returntype").ValueStr()
  283. s_params := make([]interface{}, 0)
  284. if fsion2.HasKey("parammaps") {
  285. parammaps := fsion2.Get("parammaps")
  286. paramslist := strings.Split(p, ",")
  287. if len(paramslist) > 0 && p != "" {
  288. for _, value := range paramslist {
  289. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  290. if tempv, exists := c.Get("jwt_username"); exists {
  291. s_params = append(s_params, tempv.(string))
  292. } else {
  293. s_params = append(s_params, "")
  294. }
  295. } else {
  296. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  297. }
  298. }
  299. }
  300. } else if fsion2.HasKey("params") {
  301. params := fsion2.Get("params").Array()
  302. for _, v_params := range params {
  303. s_params = append(s_params, v_params.ValueStr())
  304. }
  305. }
  306. queryData1, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  307. if err != nil {
  308. errorm := make(map[string]interface{})
  309. errorm["code"] = e.ERROR
  310. errorm["message"] = err.Error()
  311. queryData[fsion2.Get("name").ValueStr()] = errorm
  312. logging.Error("GetDataByNames err: ",err)
  313. continue
  314. } else {
  315. queryData[fsion2.Get("name").ValueStr()] = queryData1
  316. }
  317. }
  318. }
  319. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  320. }
  321. // @Summary 根据APISQL表中SQL执行得到数据集
  322. // @Tags PostDataByNameForm
  323. // @Accept json
  324. // @Produce json
  325. // @Param sqlname query string true "sqlname"
  326. // @Param offset query int true "offset"
  327. // @Param pagecount query int true "pagecount"
  328. // @Param returntype query string true "returntype"
  329. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  330. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误朱红彬彬"}"
  331. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  332. // @Router /data/PostDataByNameForm [POST]
  333. func PostDataByNameForm(c *gin.Context) {
  334. appG := app.Gin{C: c}
  335. c.Request.ParseForm()
  336. params := c.Request.Form
  337. sqlnamestr := params.Get("name")
  338. logging.Info("PostDataByNameForm ",c.Keys,sqlnamestr)
  339. s_params := make([]interface{}, 0)
  340. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  341. paramslist := strings.Split(p, ",")
  342. if len(paramslist) > 0 {
  343. for _, value := range paramslist {
  344. s_params = append(s_params, params.Get(strings.Trim(value, " ")))
  345. }
  346. }
  347. if sql != "" {
  348. queryData, err := execDataBySql(sql, s_params)
  349. if err != nil {
  350. appG.ResponseEq(http.StatusOK, e.ERROR, 1, err.Error())
  351. } else {
  352. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, queryData)
  353. }
  354. } else {
  355. appG.ResponseEq(http.StatusOK, e.SUCCESS, 0, "后台没有数据接口")
  356. }
  357. }
  358. func GetReportform(c *gin.Context) {
  359. appG := app.Gin{C: c}
  360. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  361. fsion := gofasion.NewFasion(string(dataByte))
  362. sqlnamestr := fsion.Get("name").ValueStr()
  363. parammaps := fsion.Get("parammaps")
  364. logging.Info("GetReportform ",c.Keys,sqlnamestr,parammaps)
  365. queryData,_ :=praseReportform(sqlnamestr,parammaps)
  366. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  367. }
  368. func praseReportform(sqlnamestr string,parammaps *gofasion.Fasion)(queryData map[string]interface{},err error){
  369. switch sqlnamestr {
  370. case "getpSumNow","getpSumNowWater","getpSumNowElec","getpSumNowDiesel" :
  371. queryData,err =getpSumNow(sqlnamestr,parammaps)
  372. case "getPastureSumMonth","getPastureSumMonthWater","getPastureSumMonthElec" ,"getPastureSumMonthDiesel":
  373. queryData,err =getPastureSumMonth(sqlnamestr,parammaps)
  374. case "getPastureSumDay","getPastureSumDayWater","getPastureSumDayElec","getPastureSumDayDiesel" :
  375. queryData,err =getPastureSumDay(sqlnamestr,parammaps)
  376. case "geteqclassSumMonth","geteqclassSumMonthWater","geteqclassSumMonthElec","geteqclassSumMonthDiesel" :
  377. queryData,err =geteqclassSumMonth(sqlnamestr,parammaps)
  378. case "geteCowWaterList","geteCowElecList","geteCowDieselList","geteCowMaintainList","geteCowAllList" :
  379. queryData,err =geteCow(sqlnamestr,parammaps)
  380. case "geteCowWaterMonthList","geteCowElecMonthList","geteCowDieselMonthList","geteCowMaintainMonthList","geteCowAllMonthList" :
  381. queryData,err =geteCowMonth(sqlnamestr,parammaps)
  382. case "getEqRunSurveyListPasture" :
  383. queryData,err =getEqRunSurveyListPasture(sqlnamestr,parammaps)
  384. case "getEqRunSurveyListDay" :
  385. queryData,err =getEqRunSurveyListDay(sqlnamestr,parammaps)
  386. default :
  387. queryData,err =nil,nil
  388. }
  389. return
  390. }
  391. func geteCowMonth(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  392. queryData := make(map[string]interface{})
  393. receiveTime :=parammaps.Get(strings.Trim("year", " ")).ValueStr()
  394. pastureNames:= parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  395. var pSumNow, pSumLast []map[string]string
  396. if sqlnamestr == "geteCowAllMonthList" {
  397. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  398. pSumNow, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime,pastureNames).QueryString()
  399. tempTime,_ := strconv.Atoi(receiveTime)
  400. receiveTimetemp :=strconv.Itoa(tempTime-1)
  401. pSumLast, _ = restful.Engine.SQL(sqlNow,receiveTimetemp,receiveTimetemp,receiveTimetemp,receiveTimetemp,receiveTimetemp,receiveTimetemp,pastureNames).QueryString()
  402. }else {
  403. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  404. pSumNow, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime,pastureNames).QueryString()
  405. tempTime,_ := strconv.Atoi(receiveTime)
  406. receiveTimetemp :=strconv.Itoa(tempTime-1)
  407. pSumLast, _ = restful.Engine.SQL(sqlNow,receiveTimetemp,receiveTimetemp,receiveTimetemp,pastureNames).QueryString()
  408. }
  409. //sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  410. //pSumNow, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTime).QueryString()
  411. //tempTime,_ := strconv.Atoi(receiveTime)
  412. //receiveTimetemp :=strconv.Itoa(tempTime-1)
  413. //pSumLast, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTimetemp).QueryString()
  414. chartData :=make(map[string]interface{})
  415. months := []string {}
  416. lastYear := []string{}
  417. nowYear := []string{}
  418. budgetYear := []string{}
  419. //lastTotal,nowTotal := 0,0
  420. a,b :=true,true
  421. month :=""
  422. for i:= 1; i<=12; i++{
  423. if i<10{
  424. month = receiveTime+"-0"+strconv.Itoa(i)
  425. }else {
  426. month = receiveTime+"-"+strconv.Itoa(i)
  427. }
  428. months = append(months,month)
  429. for _, va := range pSumNow {
  430. if va["month"][5:] == month[5:] {
  431. nowYear = append(nowYear, va["psum"])
  432. budgetYear = append(budgetYear, va["lsum"])
  433. a = false
  434. }
  435. }
  436. for _, va := range pSumLast {
  437. if va["month"][5:] == month[5:] {
  438. lastYear = append(lastYear, va["psum"])
  439. b = false
  440. }
  441. }
  442. if a {
  443. nowYear = append(nowYear, "0")
  444. budgetYear = append(budgetYear, "0")
  445. }
  446. if b {
  447. lastYear = append(lastYear, "0")
  448. }
  449. a = true
  450. b = true
  451. }
  452. chartData["months"] = months
  453. chartData["lastYear"] = lastYear
  454. chartData["nowYear"] = nowYear
  455. chartData["budgetYear"] = budgetYear
  456. queryData["chart_data"] = chartData
  457. queryData["code"] = 0
  458. queryData["message"] = "ok"
  459. return queryData,nil
  460. }
  461. func getEqRunSurveyListDay(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  462. queryData := make(map[string]interface{})
  463. receiveTime :=parammaps.Get(strings.Trim("praseTime", " ")).ValueStr()
  464. pastureNames:= parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  465. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  466. pSumNow, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTime).QueryString()
  467. if pSumNow == nil {
  468. return nil,nil
  469. }
  470. typeName := make([]map[string]interface{},4)
  471. ma := make(map[string]interface{})
  472. ma1 := make(map[string]interface{})
  473. ma2 := make(map[string]interface{})
  474. ma3 := make(map[string]interface{})
  475. ma["name"] = "在用"
  476. ma["value"] =pSumNow[0]["eqUse"]
  477. typeName[0] = ma
  478. ma1["name"] = "闲置"
  479. ma1["value"] =pSumNow[0]["eqXZ"]
  480. typeName[1] = ma1
  481. ma2["name"] = "报废"
  482. ma2["value"] =pSumNow[0]["eqBF"]
  483. typeName[2] = ma2
  484. ma3["name"] = "封存"
  485. ma3["value"] =pSumNow[0]["eqFC"]
  486. typeName[3] = ma3
  487. queryData["psm"] = typeName
  488. queryData["code"] = 0
  489. queryData["total"] = pSumNow[0]["eqSum"]
  490. queryData["message"] = "ok"
  491. return queryData,nil
  492. }
  493. func getEqRunSurveyListPasture(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  494. queryData := make(map[string]interface{})
  495. receiveTime :=parammaps.Get(strings.Trim("praseTime", " ")).ValueStr()
  496. pastureNames, _ := restful.Engine.SQL("SELECT `name` FROM pasture").QueryString()
  497. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  498. pSumNow, _ := restful.Engine.SQL(sqlNow,receiveTime).QueryString()
  499. chartData :=make(map[string]interface{})
  500. pasture := []string {}
  501. eqUse := []string{}
  502. eqXZ := []string{}
  503. eqBF := []string{}
  504. eqFC := []string{}
  505. //lastTotal,nowTotal := 0,0
  506. a :=true
  507. for _, value := range pastureNames {
  508. pasture = append(pasture, value["name"])
  509. for _, va := range pSumNow {
  510. if va["pastureName"] == value["name"] {
  511. eqUset,_ := strconv.Atoi( va["eqUse"])
  512. eqSumt,_ := strconv.Atoi( va["eqSum"])
  513. eqXZt,_ := strconv.Atoi( va["eqXZ"])
  514. eqBFt,_ := strconv.Atoi( va["eqBF"])
  515. eqFCt,_ := strconv.Atoi( va["eqFC"])
  516. eqUsetem := float64(eqUset)/float64(eqSumt)*100
  517. eqXZtem := float64(eqXZt)/float64(eqSumt)*100
  518. eqBFtem := float64(eqBFt)/float64(eqSumt)*100
  519. eqFCtem := float64(eqFCt)/float64(eqSumt)*100
  520. eqUse = append(eqUse, strconv.FormatFloat(eqUsetem,'f',2,64))
  521. //eqSum = append(eqSum, va["eqSum"])
  522. eqXZ = append(eqXZ, strconv.FormatFloat(eqXZtem,'f',2,64))
  523. eqBF = append(eqBF, strconv.FormatFloat(eqBFtem,'f',2,64))
  524. eqFC = append(eqFC, strconv.FormatFloat(eqFCtem,'f',2,64))
  525. a = false
  526. }
  527. }
  528. if a {
  529. eqUse = append(eqUse, "0")
  530. //eqSum = append(eqSum, "0")
  531. eqXZ = append(eqXZ, "0")
  532. eqBF = append(eqBF, "0")
  533. eqFC = append(eqFC, "0")
  534. }
  535. a = true
  536. }
  537. chartData["pastureName"] = pasture
  538. chartData["eqUseRate"] = eqUse
  539. //chartData["eqSum"] = eqSum
  540. chartData["eqXZRate"] = eqXZ
  541. chartData["eqBFRate"] = eqBF
  542. chartData["eqFCRate"] = eqFC
  543. queryData["chart_data"] = chartData
  544. queryData["code"] = 0
  545. queryData["message"] = "ok"
  546. return queryData,nil
  547. }
  548. func geteqclassSumMonth(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  549. queryData := make(map[string]interface{})
  550. receiveTime :=parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  551. pastureNames:= parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  552. deptName:= parammaps.Get(strings.Trim("deptName", " ")).ValueStr()
  553. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  554. pSumNow, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTime,deptName).QueryString()
  555. chartData :=make(map[string]interface{})
  556. typeName := []string {}
  557. eqCost := []string{}
  558. for _, va := range pSumNow {
  559. typeName = append(typeName, va["typeName"])
  560. eqCost = append(eqCost, va["psum"])
  561. }
  562. chartData["typeName"] = typeName
  563. chartData["eqCost"] = eqCost
  564. queryData["chart_data"] = chartData
  565. queryData["code"] = 0
  566. queryData["message"] = "ok"
  567. return queryData,nil
  568. }
  569. func getPastureSumDay(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  570. queryData := make(map[string]interface{})
  571. receiveTime :=parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  572. pastureNames:= parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  573. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  574. pSumNow, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTime).QueryString()
  575. tempTime,_ := strconv.Atoi(receiveTime[:4])
  576. receiveTimetemp :=strconv.Itoa(tempTime-1)+receiveTime[4:]
  577. pSumLast, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTimetemp).QueryString()
  578. chartData :=make(map[string]interface{})
  579. months := []string {}
  580. lastYear := []string{}
  581. nowYear := []string{}
  582. //lastTotal,nowTotal := 0,0
  583. a,b :=true,true
  584. month :=""
  585. dayCount :=GetMonthCount(receiveTime)
  586. for i:= 1; i<=dayCount; i++{
  587. if i<10{
  588. month = receiveTime+"-0"+strconv.Itoa(i)
  589. }else {
  590. month = receiveTime+"-"+strconv.Itoa(i)
  591. }
  592. months = append(months,strconv.Itoa(i))
  593. for _, va := range pSumNow {
  594. if va["month"][8:] == month[8:] {
  595. nowYear = append(nowYear, va["psum"])
  596. a = false
  597. }
  598. }
  599. for _, va := range pSumLast {
  600. if va["month"][8:] == month[8:] {
  601. lastYear = append(lastYear, va["psum"])
  602. b = false
  603. }
  604. }
  605. if a {
  606. nowYear = append(nowYear, "0")
  607. }
  608. if b {
  609. lastYear = append(lastYear, "0")
  610. }
  611. a = true
  612. b = true
  613. }
  614. chartData["day"] = months
  615. chartData["lastYear"] = lastYear
  616. chartData["nowYear"] = nowYear
  617. queryData["chart_data"] = chartData
  618. queryData["code"] = 0
  619. queryData["message"] = "ok"
  620. return queryData,nil
  621. }
  622. func GetMonthCount(receiveTime string) (days int) {
  623. year,_ := strconv.Atoi(receiveTime[:4])
  624. month,_ := strconv.Atoi(receiveTime[5:])
  625. if month != 2 {
  626. if month == 4 || month == 6 || month == 9 || month == 11 {
  627. days = 30
  628. } else {
  629. days = 31
  630. }
  631. } else {
  632. if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) {
  633. days = 29
  634. } else {
  635. days = 28
  636. }
  637. }
  638. return
  639. }
  640. func getPastureSumMonth(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  641. queryData := make(map[string]interface{})
  642. receiveTime :=parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  643. pastureNames:= parammaps.Get(strings.Trim("pastureName", " ")).ValueStr()
  644. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  645. pSumNow, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTime).QueryString()
  646. tempTime,_ := strconv.Atoi(receiveTime)
  647. receiveTimetemp :=strconv.Itoa(tempTime-1)
  648. pSumLast, _ := restful.Engine.SQL(sqlNow,pastureNames,receiveTimetemp).QueryString()
  649. chartData :=make(map[string]interface{})
  650. months := []string {}
  651. lastYear := []string{}
  652. nowYear := []string{}
  653. //lastTotal,nowTotal := 0,0
  654. a,b :=true,true
  655. month :=""
  656. for i:= 1; i<=12; i++{
  657. if i<10{
  658. month = receiveTime+"-0"+strconv.Itoa(i)
  659. }else {
  660. month = receiveTime+"-"+strconv.Itoa(i)
  661. }
  662. months = append(months,month)
  663. for _, va := range pSumNow {
  664. if va["month"][5:] == month[5:] {
  665. nowYear = append(nowYear, va["psum"])
  666. a = false
  667. }
  668. }
  669. for _, va := range pSumLast {
  670. if va["month"][5:] == month[5:] {
  671. lastYear = append(lastYear, va["psum"])
  672. b = false
  673. }
  674. }
  675. if a {
  676. nowYear = append(nowYear, "0")
  677. }
  678. if b {
  679. lastYear = append(lastYear, "0")
  680. }
  681. a = true
  682. b = true
  683. }
  684. chartData["months"] = months
  685. chartData["lastYear"] = lastYear
  686. chartData["nowYear"] = nowYear
  687. queryData["chart_data"] = chartData
  688. queryData["code"] = 0
  689. queryData["message"] = "ok"
  690. return queryData,nil
  691. }
  692. func geteCow(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  693. queryData := make(map[string]interface{})
  694. receiveTime :=parammaps.Get(strings.Trim("year", " ")).ValueStr()
  695. pastureNames, _ := restful.Engine.SQL("SELECT `name` FROM pasture").QueryString()
  696. var pSumNow, pSumLast []map[string]string
  697. if sqlnamestr == "geteCowAllList" {
  698. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  699. pSumNow, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime).QueryString()
  700. receiveTimei,_ := strconv.Atoi(receiveTime)
  701. receiveTime =strconv.Itoa(receiveTimei-1)
  702. pSumLast, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime,receiveTime).QueryString()
  703. }else {
  704. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  705. pSumNow, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime).QueryString()
  706. receiveTimei,_ := strconv.Atoi(receiveTime)
  707. receiveTime =strconv.Itoa(receiveTimei-1)
  708. pSumLast, _ = restful.Engine.SQL(sqlNow,receiveTime,receiveTime,receiveTime).QueryString()
  709. }
  710. chartData :=make(map[string]interface{})
  711. pasture := []string {}
  712. lastYear := []string{}
  713. nowYear := []string{}
  714. budgetYear := []string{}
  715. //lastTotal,nowTotal := 0,0
  716. a,b :=true,true
  717. for _, value := range pastureNames {
  718. pasture = append(pasture, value["name"])
  719. for _, va := range pSumNow {
  720. if va["name"] == value["name"] {
  721. nowYear = append(nowYear, va["psum"])
  722. budgetYear = append(budgetYear, va["lsum"])
  723. a = false
  724. }
  725. }
  726. for _, va := range pSumLast {
  727. if va["name"] == value["name"] {
  728. lastYear = append(lastYear, va["psum"])
  729. b = false
  730. }
  731. }
  732. if a {
  733. nowYear = append(nowYear, "0")
  734. budgetYear = append(budgetYear, "0")
  735. }
  736. if b {
  737. lastYear = append(lastYear, "0")
  738. }
  739. a = true
  740. b = true
  741. }
  742. chartData["pasture"] = pasture
  743. chartData["lastYear"] = lastYear
  744. chartData["nowYear"] = nowYear
  745. chartData["budgetYear"] = budgetYear
  746. queryData["chart_data"] = chartData
  747. queryData["code"] = 0
  748. queryData["message"] = "ok"
  749. return queryData,nil
  750. }
  751. func getpSumNow(sqlnamestr string,parammaps *gofasion.Fasion)(map[string]interface{},error){
  752. queryData := make(map[string]interface{})
  753. receiveTime :=parammaps.Get(strings.Trim("receiveTime", " ")).ValueStr()
  754. pastureNames, _ := restful.Engine.SQL("SELECT `name` FROM pasture").QueryString()
  755. sqlNow, _ := restful.GetSqlByNameDB(sqlnamestr)
  756. pSumNow, _ := restful.Engine.SQL(sqlNow,receiveTime).QueryString()
  757. receiveTimei,_ := strconv.Atoi(receiveTime)
  758. receiveTime =strconv.Itoa(receiveTimei-1)
  759. pSumLast, _ := restful.Engine.SQL(sqlNow,receiveTime).QueryString()
  760. chartData :=make(map[string]interface{})
  761. pasture := []string {}
  762. lastYear := []string{}
  763. nowYear := []string{}
  764. //lastTotal,nowTotal := 0,0
  765. a,b :=true,true
  766. for _, value := range pastureNames {
  767. pasture = append(pasture, value["name"])
  768. for _, va := range pSumNow {
  769. if va["name"] == value["name"] {
  770. nowYear = append(nowYear, va["psum"])
  771. a = false
  772. }
  773. }
  774. for _, va := range pSumLast {
  775. if va["name"] == value["name"] {
  776. lastYear = append(lastYear, va["psum"])
  777. b = false
  778. }
  779. }
  780. if a {
  781. nowYear = append(nowYear, "0")
  782. }
  783. if b {
  784. lastYear = append(lastYear, "0")
  785. }
  786. a = true
  787. b = true
  788. }
  789. chartData["pasture"] = pasture
  790. chartData["lastYear"] = lastYear
  791. chartData["nowYear"] = nowYear
  792. queryData["chart_data"] = chartData
  793. queryData["code"] = 0
  794. queryData["message"] = "ok"
  795. return queryData,nil
  796. }
  797. func GetUpkeepPlan(c *gin.Context) {
  798. appG := app.Gin{C: c}
  799. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  800. fsion := gofasion.NewFasion(string(dataByte))
  801. sqlnamestr := fsion.Get("name").ValueStr()
  802. offset := fsion.Get("offset").ValueDefaultInt(0)
  803. pagecount := fsion.Get("pagecount").ValueDefaultInt(0)
  804. returntype := fsion.Get("returntype").ValueDefaultStr("map")
  805. month := fsion.Get("month").ValueDefaultStr("map")
  806. logging.Info("GetUpkeepPlan ",c.Keys,sqlnamestr,month)
  807. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  808. if sql != "" {
  809. s_params := make([]interface{}, 0)
  810. if fsion.HasKey("parammaps") {
  811. parammaps := fsion.Get("parammaps")
  812. paramslist := strings.Split(p, ",")
  813. if len(paramslist) > 0 && p != "" {
  814. for _, value := range paramslist {
  815. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  816. if tempv, exists := c.Get("jwt_username"); exists {
  817. s_params = append(s_params, tempv.(string))
  818. } else {
  819. s_params = append(s_params, "")
  820. }
  821. } else {
  822. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  823. }
  824. }
  825. }
  826. } else if fsion.HasKey("params") {
  827. params := fsion.Get("params").Array()
  828. for _, v_params := range params {
  829. s_params = append(s_params, v_params.ValueStr())
  830. }
  831. }
  832. if returntype == "xlsx" {
  833. var xlsxfile *xlsx.File
  834. xlsxfile, _ = restful.QueryByXlsx(sql, s_params)
  835. //c.File("c:/dd.xlsx")
  836. c.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  837. c.Header("Content-Disposition", "attachment; filename="+"Workbook.xlsx")
  838. c.Header("Content-Transfer-Encoding", "binary")
  839. _ = xlsxfile.Write(c.Writer)
  840. } else {
  841. queryData, err := getDataBySql(sql, offset, pagecount, returntype, s_params)
  842. if err != nil {
  843. appG.Response(http.StatusOK, e.ERROR, err.Error())
  844. } else {
  845. for index, value := range queryData.(map[string]interface{})["list"].([]map[string]interface{}) {
  846. if v, ok := value["id"]; ok {
  847. //month = "2020-03"
  848. seviceplans, _ := restful.Engine.SQL("SELECT * FROM seviceplan where eqId = ? and month=? ", v, month).QueryString() //获取保养计划
  849. if seviceplans != nil {
  850. for _, va := range seviceplans { //根据保养计划获取保养单
  851. //day,_ :=strconv.Atoi(va["day"])
  852. bigupkeeps, _ := restful.Engine.SQL("SELECT * FROM bigupkeep where seviceplanId = ?", va["id"]).QueryString() //获取保养计划中的保养单
  853. //buk := make(map[string]map[string]string)
  854. for _, bu := range bigupkeeps { //日期为key ,保养单为value 存到map中
  855. jstr := ""
  856. if bu["plantime"][8:9] == "0" {
  857. jstr = "m" + bu["plantime"][9:]
  858. } else {
  859. jstr = "m" + bu["plantime"][8:]
  860. }
  861. if bu["statue"] == "0" && bu["plantime"] < time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  862. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#2" + "#" + bu["id"]
  863. } else {
  864. if bu["statue"] == "2" {
  865. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#1" + "#" + bu["id"]
  866. }else {
  867. queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"] + "#" + va["id"] + "#0" + "#" + bu["id"]
  868. }
  869. }
  870. //buk[bu["plantime"]] = bu
  871. }
  872. //upkeepCycel,_ :=strconv.Atoi(va["upkeepCycel"])
  873. //if upkeepCycel<=0 {
  874. // upkeepCycel = 1
  875. //}
  876. //for j:=day;j<=31 ;j+= upkeepCycel {
  877. // var daytem string
  878. // if j<10 {
  879. // daytem = "0"+strconv.Itoa(j)
  880. // }else {
  881. // daytem =strconv.Itoa(j)
  882. // }
  883. // //date := month + "-" + daytem
  884. // ////bigupkeep, _ := restful.Engine.SQL("SELECT * FROM bigupkeep where seviceplanId = ? and plantime=?",va["id"],date).QueryString()
  885. // //jstr := "m"+strconv.Itoa(int(j))
  886. // //if bigupkeep != nil {
  887. // // if bigupkeep[0]["statue"] == "0" && date<time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  888. // // queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"]+"#"+va["id"]+"#2"
  889. // // }else {
  890. // // queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"]+"#"+va["id"]+"#" +bigupkeep[0]["statue"]
  891. // // }
  892. // //
  893. // //}
  894. // //if val,k :=buk[date]; k {
  895. // // if val["statue"] == "0" && date<time.Now().Format("2006-01-02") { //如果保养状态是0并且小于当前日期 则状态已延期2
  896. // // queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"]+"#"+va["id"]+"#2"
  897. // // }else {
  898. // // queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"]+"#"+va["id"]+"#" +val["statue"]
  899. // // }
  900. // //}
  901. // //else {
  902. // //
  903. // // queryData.(map[string]interface{})["list"].([]map[string]interface{})[index][jstr] = va["upkeepName"]+"#"+va["id"]
  904. // //}
  905. //
  906. //}
  907. }
  908. }
  909. //fmt.Println("==============",seviceplans,err)
  910. //queryData.(map[string]interface{})["list"].([]map[string]interface{})[index] =
  911. }
  912. }
  913. //in := -1
  914. //tem := int64(0)
  915. //elementtem :=make(map[string]interface{})
  916. //
  917. //fmt.Println(queryData.(map[string]interface {})["list"])
  918. //element := queryData.(map[string]interface{})["list"].([]map[string]interface{})
  919. //temp := make([]map[string]interface{},0)
  920. //for i:=0; i<len(element); i++ {
  921. // if _,ok := element[i]["upkeepTemplateId"];ok {
  922. // if element[i]["id"].(int64) == tem {
  923. // //upkeepCycel,_ := strconv.Atoi(element[i]["upkeepCycel"].(string))
  924. // upkeepCycel := element[i]["upkeepCycel"].(int64)
  925. // day,_ :=strconv.Atoi(element[i]["day"].(string))
  926. // for j:=int64(day);j<=31 ;j+= upkeepCycel {
  927. // jstr := strconv.Itoa(int(j))
  928. // elementtem[jstr] = element[i]["upkeepName"]
  929. // }
  930. // }else{
  931. // if in != -1 {
  932. // temp = append(temp,elementtem)
  933. // }
  934. // elementtem = element[i]
  935. // upkeepCycel := element[i]["upkeepCycel"].(int64)
  936. // day,_ :=strconv.Atoi(element[i]["day"].(string))
  937. // for j:=int64(day);j<=31 ;j+= upkeepCycel{
  938. // jstr := strconv.Itoa(int(j))
  939. // elementtem[jstr] = element[i]["upkeepName"]
  940. // }
  941. // in++
  942. // temp = append(temp,elementtem)
  943. // }
  944. //
  945. // }else {
  946. // temp = append(temp,element[i])
  947. // in++
  948. // }
  949. //
  950. // tem = element[i]["id"].(int64)
  951. // fmt.Println(element[i])
  952. //}
  953. //queryData.(map[string]interface {})["list"] = temp
  954. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  955. }
  956. }
  957. } else {
  958. appG.Response(http.StatusOK, e.SUCCESS, nil)
  959. }
  960. }
  961. // @Summary 根据APISQL表中SQL执行得到数据集
  962. // @Tags PostDataByName
  963. // @Accept json
  964. // @Produce json
  965. // @Param sqlname query string true "sqlname"
  966. // @Param offset query int true "offset"
  967. // @Param pagecount query int true "pagecount"
  968. // @Param returntype query string true "returntype"
  969. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  970. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  971. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  972. // @Router /data/PostDataByName [POST]
  973. func PostDataByName(c *gin.Context) {
  974. appG := app.Gin{C: c}
  975. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  976. fsion := gofasion.NewFasion(string(dataByte))
  977. sqlnamestr := fsion.Get("name").ValueStr()
  978. sql, p := restful.GetSqlByNameDB(sqlnamestr)
  979. s_params := make([]interface{}, 0)
  980. if sql != "" {
  981. if fsion.HasKey("parammaps") {
  982. parammaps := fsion.Get("parammaps")
  983. logging.Info("PostDataByName ",c.Keys,c.Request.RemoteAddr,sqlnamestr,parammaps)
  984. paramslist := strings.Split(p, ",")
  985. if len(paramslist) > 0 && p != "" {
  986. for _, value := range paramslist {
  987. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  988. if tempv, exists := c.Get("jwt_username"); exists {
  989. s_params = append(s_params, tempv.(string))
  990. } else {
  991. s_params = append(s_params, "")
  992. }
  993. } else {
  994. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  995. }
  996. }
  997. }
  998. } else if fsion.HasKey("params") {
  999. params := fsion.Get("params").Array()
  1000. logging.Info("PostDataByName ",c.Keys,c.Request.RemoteAddr,sqlnamestr,params)
  1001. for _, v_params := range params {
  1002. s_params = append(s_params, v_params.ValueStr())
  1003. }
  1004. }
  1005. queryData, err := execDataBySql(sql, s_params)
  1006. if err != nil {
  1007. logging.Error("PostDataByName err: ",err)
  1008. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1009. } else {
  1010. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1011. }
  1012. } else {
  1013. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1014. }
  1015. }
  1016. // @Summary 根据APISQL表中SQL执行得到数据集
  1017. // @Tags ExecDataByConfig
  1018. // @Accept json
  1019. // @Produce json
  1020. // @Param sqlname query string true "sqlname"
  1021. // @Param offset query int true "offset"
  1022. // @Param pagecount query int true "pagecount"
  1023. // @Param returntype query string true "returntype"
  1024. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1025. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1026. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1027. // @Router /data/PostDataByName [POST]
  1028. func ExecDataByConfig(c *gin.Context) {
  1029. appG := app.Gin{C: c}
  1030. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1031. tempval_ := make(map[string]interface{})
  1032. tempCommon := make(map[string]interface{})
  1033. tempval := make([]map[string]interface{}, 0)
  1034. err := json.Unmarshal(dataByte, &tempval_)
  1035. logging.Info("ExecDataByConfig ",c.Keys,c.Request.RemoteAddr,tempval_["common"],tempval_["data"])
  1036. if err != nil {
  1037. fmt.Println("error:", err)
  1038. } else {
  1039. if v, ok := tempval_["common"].(map[string]interface{}); ok {
  1040. tempCommon = v
  1041. }
  1042. if v, ok := tempval_["data"].([]interface{}); ok {
  1043. for _, Tvalue := range v {
  1044. if v1, ok := Tvalue.(map[string]interface{}); ok {
  1045. tempval = append(tempval, v1)
  1046. }
  1047. }
  1048. }
  1049. tx := restful.Engine.NewSession()
  1050. err := tx.Begin()
  1051. if err != nil {
  1052. fmt.Println("GetT error:", err)
  1053. }
  1054. defer func() {
  1055. switch {
  1056. case err != nil:
  1057. fmt.Println("__error:", err)
  1058. if tx != nil {
  1059. tx.Rollback()
  1060. }
  1061. default:
  1062. if tx != nil {
  1063. err = tx.Commit()
  1064. }
  1065. }
  1066. if tx != nil {
  1067. tx.Close()
  1068. }
  1069. }()
  1070. if err == nil {
  1071. if tempv, exists := c.Get("jwt_username"); exists {
  1072. tempCommon["jwt_username"] = tempv.(string)
  1073. }
  1074. for _, paramvalue := range tempval {
  1075. if _, ok := paramvalue["resultname"]; !ok {
  1076. paramvalue["resultname"] = paramvalue["name"]
  1077. }
  1078. _, err = ExecDataParam(paramvalue, tempCommon, tempval, nil, nil, tx)
  1079. if err != nil {
  1080. fmt.Println("ExecDataParam error:", err)
  1081. break
  1082. }
  1083. }
  1084. if err == nil {
  1085. if tempCommon["returnmap"] != nil && tempCommon["returnmap"] != "" {
  1086. appG.Response(http.StatusOK, e.SUCCESS, tempval)
  1087. } else {
  1088. appG.Response(http.StatusOK, e.SUCCESS, "成功执行!")
  1089. }
  1090. } else {
  1091. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1092. }
  1093. }
  1094. }
  1095. if err != nil {
  1096. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1097. }
  1098. }
  1099. func interfacetostring(val interface{}) string {
  1100. switch val.(type) {
  1101. case string:
  1102. ss := val.(string)
  1103. return ss
  1104. break
  1105. case int:
  1106. ss := val.(int)
  1107. return strconv.Itoa(ss)
  1108. break
  1109. case int64:
  1110. ss := val.(int64)
  1111. return strconv.FormatInt(ss, 10)
  1112. break
  1113. case float64:
  1114. ss := val.(float64)
  1115. return strconv.FormatFloat(ss, 'E', -1, 32)
  1116. break
  1117. }
  1118. return ""
  1119. }
  1120. func getParamValue(parammaps map[string]interface{}, value string, ParamMap []map[string]interface{}) string {
  1121. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1122. tempstrb := strings.IndexAny(valuestring, "@")
  1123. tempstre := strings.IndexAny(valuestring, ".")
  1124. if tempstrb > -1 && tempstre > -1 {
  1125. for _, ParamMapList := range ParamMap {
  1126. if v, ok := ParamMapList["resultname"]; ok {
  1127. if v == valuestring[tempstrb+1:tempstre] {
  1128. if v1, ok := ParamMapList["resultmaps"]; ok {
  1129. if newA, ok := v1.([]map[string]interface{}); ok {
  1130. return interfacetostring(newA[0][valuestring[tempstre+1:]])
  1131. } else if newA, ok := v1.(map[string]interface{}); ok {
  1132. return interfacetostring(newA[valuestring[tempstre+1:]])
  1133. }
  1134. }
  1135. }
  1136. }
  1137. }
  1138. } else {
  1139. return valuestring
  1140. }
  1141. } else if valuestring, ok := parammaps[strings.Trim(value, " ")].(float64); ok {
  1142. return strconv.FormatInt(int64(valuestring), 10)
  1143. }
  1144. return ""
  1145. }
  1146. func getParamValue_(parammaps map[string]interface{}, value string, ParamMap map[string]interface{}) string {
  1147. if valuestring, ok := parammaps[strings.Trim(value, " ")].(string); ok {
  1148. tempstrb := strings.IndexAny(valuestring, "@")
  1149. tempstre := strings.IndexAny(valuestring, ".")
  1150. if tempstrb > -1 && tempstre > -1 {
  1151. if v, ok := ParamMap[valuestring[tempstre+1:]]; ok {
  1152. if s, ok := v.(string); ok {
  1153. return s
  1154. } else if s, ok := v.(float64); ok {
  1155. return strconv.FormatInt(int64(s), 10)
  1156. }
  1157. }
  1158. }
  1159. }
  1160. return ""
  1161. }
  1162. func ExecDataParam(fasion map[string]interface{}, CommonParamMap map[string]interface{}, ParamMap []map[string]interface{}, ParentParamMap map[string]interface{}, sameParamMap []map[string]interface{}, tx *xorm.Session) ([]map[string]interface{}, error) {
  1163. sqlname := fasion["name"].(string)
  1164. sqltype := "" //v 校验/s1 查询/sm 查询/e 执行
  1165. if v, ok := fasion["type"]; ok {
  1166. sqltype = v.(string)
  1167. }
  1168. sql := ""
  1169. p := ""
  1170. s_params := make([]interface{}, 0)
  1171. if sqltype != "" {
  1172. sql, p = restful.GetSqlByName(sqlname, tx)
  1173. //sqlApi, _ := models.GetApisqlByName(sqlname)
  1174. if v, ok := fasion["parammaps"].(map[string]interface{}); ok {
  1175. paramslist := strings.Split(p, ",")
  1176. if len(paramslist) > 0 && p != "" {
  1177. for _, value := range paramslist {
  1178. ss := ""
  1179. if ss == "" && CommonParamMap != nil {
  1180. if valuestring, ok := v[strings.Trim(value, " ")].(string); ok {
  1181. ss = valuestring
  1182. tempstrb := strings.IndexAny(valuestring, "@")
  1183. tempstre := strings.IndexAny(valuestring, ".")
  1184. if (tempstre > tempstrb) && (strings.ToLower(valuestring[tempstrb+1:tempstre]) == "common") {
  1185. if CommonParamMap[valuestring[tempstre+1:]] != nil {
  1186. ss = CommonParamMap[valuestring[tempstre+1:]].(string)
  1187. }
  1188. } else {
  1189. ss = ""
  1190. }
  1191. }
  1192. }
  1193. if ss == "" && ParentParamMap != nil {
  1194. ss = getParamValue_(v, value, ParentParamMap)
  1195. }
  1196. if ss == "" && sameParamMap != nil {
  1197. ss = getParamValue(v, value, sameParamMap)
  1198. }
  1199. if ss == "" && ParamMap != nil {
  1200. ss = getParamValue(v, value, ParamMap)
  1201. }
  1202. s_params = append(s_params, ss)
  1203. }
  1204. }
  1205. }
  1206. }
  1207. if sql == "" {
  1208. sqltype = ""
  1209. }
  1210. if sqltype == "s1" {
  1211. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  1212. if v, ok := tempValParam.(map[string]interface{}); ok {
  1213. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1214. }
  1215. if err != nil {
  1216. return nil, err
  1217. }
  1218. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1219. if err != nil {
  1220. return nil, err
  1221. }
  1222. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  1223. } else if sqltype == "sm" {
  1224. tempValParam, err := restful.QueryByMap(sql, 0, 0, s_params)
  1225. if v, ok := tempValParam.(map[string]interface{}); ok {
  1226. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1227. } else {
  1228. fmt.Println("sm", tempValParam)
  1229. }
  1230. if err != nil {
  1231. return nil, err
  1232. }
  1233. err = ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1234. if err != nil {
  1235. return nil, err
  1236. }
  1237. return tempValParam.(map[string]interface{})["list"].([]map[string]interface{}), err
  1238. } else if sqltype == "v" {
  1239. tempValParam, err := restful.QueryByMap(sql, 0, 1, s_params)
  1240. if v, ok := tempValParam.(map[string]interface{}); ok {
  1241. fasion["resultmaps"] = v["list"].([]map[string]interface{})
  1242. } else {
  1243. fmt.Println("v", tempValParam)
  1244. }
  1245. if err != nil {
  1246. return nil, err
  1247. }
  1248. if tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"] != "" {
  1249. return nil, errors.New(tempValParam.(map[string]interface{})["list"].([]map[string]interface{})[0]["vmsg"].(string))
  1250. }
  1251. } else if sqltype == "e" {
  1252. tempVal_arrary := make([]map[string]interface{}, 0)
  1253. tempVal_map := make(map[string]interface{})
  1254. tempValParam, err := restful.ExecQueryT(sql, s_params, tx)
  1255. if err == nil {
  1256. fasion["resultmaps"] = tempValParam.(map[string]interface{})
  1257. tempVal_map["result"] = tempValParam.(map[string]interface{})["result"]
  1258. tempVal_map["LastInsertId"] = tempValParam.(map[string]interface{})["LastInsertId"]
  1259. tempVal_arrary = append(tempVal_arrary, tempVal_map)
  1260. return tempVal_arrary, err
  1261. }
  1262. return nil, err
  1263. } else {
  1264. if tempValParam, ok := fasion["resultmaps"].(map[string]interface{}); ok {
  1265. err := ExitsChildrenParam(fasion, tempValParam, ParamMap, CommonParamMap, tx)
  1266. if err != nil {
  1267. return nil, err
  1268. }
  1269. return nil, err
  1270. }
  1271. }
  1272. return nil, nil
  1273. }
  1274. func ExitsChildrenParam(fasion map[string]interface{}, tempValParam interface{}, ParamMap []map[string]interface{}, commonParam map[string]interface{}, tx *xorm.Session) error {
  1275. var err error
  1276. subarray, ok := fasion["children"]
  1277. if ok {
  1278. for _, v := range tempValParam.(map[string]interface{})["list"].([]interface{}) { //遍历父集有多少行数据
  1279. tempVal_arrary := make([]map[string]interface{}, 0)
  1280. for _, subarrayvalue := range subarray.([]interface{}) { //遍历所有执行命令
  1281. tempVal_ := make(map[string]interface{})
  1282. if _, ok := subarrayvalue.(map[string]interface{})["resultname"]; !ok {
  1283. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["name"]
  1284. } else {
  1285. tempVal_["resultname"] = subarrayvalue.(map[string]interface{})["resultname"]
  1286. }
  1287. tempVal_["resultmaps"], err = ExecDataParam(subarrayvalue.(map[string]interface{}), commonParam, ParamMap, v.(map[string]interface{}), tempVal_arrary, tx) //递归调用执行
  1288. if err != nil {
  1289. return err
  1290. }
  1291. tempVal_arrary = append(tempVal_arrary, tempVal_)
  1292. }
  1293. v.(map[string]interface{})["children"] = tempVal_arrary
  1294. }
  1295. }
  1296. return nil
  1297. }
  1298. // @Summary 删除图片
  1299. // @Tags RemovePicByName
  1300. // @Accept json
  1301. // @Produce json
  1302. // @Param sqlname query string true "sqlname"
  1303. // @Param offset query int true "offset"
  1304. // @Param pagecount query int true "pagecount"
  1305. // @Param returntype query string true "returntype"
  1306. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1307. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1308. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1309. // @Router /data/PostDataByName [POST]
  1310. func RemovePicByName(c *gin.Context) {
  1311. logging.Info("RemovePicByName ",c.Keys)
  1312. appG := app.Gin{C: c}
  1313. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1314. fsion := gofasion.NewFasion(string(dataByte))
  1315. sqlnamestr := fsion.Get("name").ValueStr()
  1316. picname := fsion.Get("picname").ValueStr()
  1317. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1318. params := fsion.Get("params").Array()
  1319. s_params := make([]interface{}, 0)
  1320. for _, v_params := range params {
  1321. s_params = append(s_params, v_params.ValueStr())
  1322. }
  1323. if sql != "" {
  1324. queryData, err := execDataBySql(sql, s_params)
  1325. if picname != "" {
  1326. // Destination
  1327. distPath := path.Join(setting.CurrentPath, setting.AppSetting.ImageSavePath, picname)
  1328. err := os.Remove(distPath) //删除文件test.txt
  1329. if err != nil {
  1330. //如果删除失败则输出 file remove Error!
  1331. fmt.Println("file remove Error!")
  1332. //输出错误详细信息
  1333. fmt.Printf("%s", err)
  1334. } else {
  1335. //如果删除成功则输出 file remove OK!
  1336. fmt.Print("file remove OK!")
  1337. }
  1338. distPath = path.Join(setting.CurrentPath, setting.AppSetting.ThumbnailSavePath, picname)
  1339. err = os.Remove(distPath) //删除文件test.txt
  1340. if err != nil {
  1341. //如果删除失败则输出 file remove Error!
  1342. fmt.Println("file remove Error!")
  1343. //输出错误详细信息
  1344. fmt.Printf("%s", err)
  1345. } else {
  1346. //如果删除成功则输出 file remove OK!
  1347. fmt.Print("file remove OK!")
  1348. }
  1349. }
  1350. if err != nil {
  1351. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1352. } else {
  1353. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1354. }
  1355. } else {
  1356. appG.Response(http.StatusOK, e.SUCCESS, nil)
  1357. }
  1358. }
  1359. // @Summary 删除文件
  1360. // @Tags RemoveFileByName
  1361. // @Accept json
  1362. // @Produce json
  1363. // @Param sqlname query string true "sqlname"
  1364. // @Param offset query int true "offset"
  1365. // @Param pagecount query int true "pagecount"
  1366. // @Param returntype query string true "returntype"
  1367. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1368. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1369. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1370. // @Router /data/PostDataByName [POST]
  1371. func RemoveFileByName(c *gin.Context) {
  1372. appG := app.Gin{C: c}
  1373. id := c.Param("id")
  1374. logging.Info("DownloadFile ",c.Keys,id)
  1375. eqdoc, err := restful.Engine.SQL("SELECT * FROM eq_doc where id = ? ", id).QueryString()
  1376. if eqdoc == nil {
  1377. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1378. return
  1379. }
  1380. distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath,eqdoc[0]["docpath"],eqdoc[0]["newdocname"])
  1381. err = os.Remove(distPath) //删除文件test.txt
  1382. if err != nil {
  1383. //如果删除失败则输出 file remove Error!
  1384. fmt.Println("file remove Error!")
  1385. //输出错误详细信息
  1386. fmt.Printf("%s", err)
  1387. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1388. return
  1389. }
  1390. fmt.Print("file remove OK!")
  1391. _, err = restful.Engine.SQL("delete FROM eq_doc where id = ? ", id).QueryString()
  1392. appG.Response(http.StatusOK, e.SUCCESS, "OK")
  1393. //dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1394. //fsion := gofasion.NewFasion(string(dataByte))
  1395. //sqlnamestr := fsion.Get("name").ValueStr()
  1396. //filename := fsion.Get("filename").ValueStr()
  1397. //sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1398. //params := fsion.Get("params").Array()
  1399. //s_params := make([]interface{}, 0)
  1400. //for _, v_params := range params {
  1401. // s_params = append(s_params, v_params.ValueStr())
  1402. //}
  1403. //if sql != "" {
  1404. // queryData, err := execDataBySql(sql, s_params)
  1405. //
  1406. // if filename != "" {
  1407. // // Destination
  1408. // distPath := path.Join(setting.CurrentPath, setting.AppSetting.FileSavePath, filename)
  1409. //
  1410. // err := os.Remove(distPath) //删除文件test.txt
  1411. // if err != nil {
  1412. // //如果删除失败则输出 file remove Error!
  1413. // fmt.Println("file remove Error!")
  1414. // //输出错误详细信息
  1415. // fmt.Printf("%s", err)
  1416. // } else {
  1417. // //如果删除成功则输出 file remove OK!
  1418. // fmt.Print("file remove OK!")
  1419. // }
  1420. // }
  1421. // if err != nil {
  1422. // appG.Response(http.StatusOK, e.ERROR, err.Error())
  1423. // } else {
  1424. // appG.Response(http.StatusOK, e.SUCCESS, "OK")
  1425. // }
  1426. //} else {
  1427. // appG.Response(http.StatusOK, e.SUCCESS, nil)
  1428. //}
  1429. }
  1430. // @Summary 根据APISQL表中SQL执行得到数据集
  1431. // @Tags PostDataByName
  1432. // @Accept json
  1433. // @Produce json
  1434. // @Param sqlname query string true "sqlname"
  1435. // @Param offset query int true "offset"
  1436. // @Param pagecount query int true "pagecount"
  1437. // @Param returntype query string true "returntype"
  1438. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1439. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1440. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1441. // @Router /data/PostDataByName [POST]
  1442. func UpdateDataRelation(c *gin.Context) {
  1443. appG := app.Gin{C: c}
  1444. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1445. fsion := gofasion.NewFasion(string(dataByte))
  1446. tablename := fsion.Get("name").ValueStr()
  1447. dataname := fsion.Get("dataname").ValueStr()
  1448. datavalue := fsion.Get("datavalue").ValueStr()
  1449. valuename := fsion.Get("valuename").ValueStr()
  1450. logging.Info("UpdateDataRelation ",c.Keys,c.Request.RemoteAddr,tablename,dataname,datavalue,valuename)
  1451. values := fsion.Get("values").Array()
  1452. insertSqlstr := "insert into " + tablename + "( " + dataname + "," + valuename + ") select DISTINCT ?, ? from DUAL " +
  1453. " where NOT EXISTS (select " + valuename + " from " + tablename + " where " + valuename + " = ? and " + dataname + " = ?)"
  1454. d_params := make([]interface{}, 4)
  1455. d_params[0] = datavalue
  1456. s_params := ""
  1457. for _, v_values := range values {
  1458. d_params[1] = v_values.ValueStr()
  1459. if s_params == "" {
  1460. s_params = v_values.ValueStr()
  1461. } else {
  1462. s_params = s_params + "," + v_values.ValueStr()
  1463. }
  1464. d_params[2] = d_params[1]
  1465. d_params[3] = d_params[0]
  1466. _, err := execDataBySql(insertSqlstr, d_params)
  1467. if err != nil {
  1468. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1469. break
  1470. }
  1471. }
  1472. if s_params != "" {
  1473. deleteSqlstr := "delete from " + tablename + " " +
  1474. " where " + dataname + " = " + datavalue + " and " + valuename + " not in (" + s_params + ")"
  1475. queryData, err := execDataBySql(deleteSqlstr, nil)
  1476. if err != nil {
  1477. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1478. } else if err == nil {
  1479. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1480. }
  1481. } else {
  1482. deleteSqlstr := "delete from " + tablename + " " +
  1483. " where " + dataname + " = " + datavalue
  1484. queryData, err := execDataBySql(deleteSqlstr, nil)
  1485. if err != nil {
  1486. appG.Response(http.StatusOK, e.ERROR, err.Error())
  1487. } else if err == nil {
  1488. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1489. }
  1490. }
  1491. }
  1492. // @Summary 根据APISQL表中SQL执行得到数据集
  1493. // @Tags PostDatasByName
  1494. // @Accept json
  1495. // @Produce json
  1496. // @Param sqlname query string true "sqlname"
  1497. // @Param offset query int true "offset"
  1498. // @Param pagecount query int true "pagecount"
  1499. // @Param returntype query string true "returntype"
  1500. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1501. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1502. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1503. // @Router /data/PostDatasByName [POST]
  1504. func PostDataByNames(c *gin.Context) {
  1505. appG := app.Gin{C: c}
  1506. dataByte, _ := ioutil.ReadAll(c.Request.Body)
  1507. fsion := gofasion.NewFasion(string(dataByte))
  1508. sqlParams := fsion.Array()
  1509. queryData := make(map[string]interface{})
  1510. for _, v := range sqlParams {
  1511. fsion2 := gofasion.NewFasion(v.Json())
  1512. logging.Info("PostDataByNames ",c.Keys,fsion2.Get("name").ValueStr())
  1513. sql, p := restful.GetSqlByNameDB(fsion2.Get("name").ValueStr())
  1514. if sql == "" {
  1515. errorm := make(map[string]interface{})
  1516. errorm["code"] = e.ERROR
  1517. errorm["message"] = "sql没有找到"
  1518. errorm["param"] = ""
  1519. queryData[fsion2.Get("name").ValueStr()] = errorm
  1520. continue
  1521. } else if sql != "" {
  1522. s_params := make([]interface{}, 0)
  1523. if fsion2.HasKey("parammaps") {
  1524. parammaps := fsion2.Get("parammaps")
  1525. paramslist := strings.Split(p, ",")
  1526. if len(paramslist) > 0 && p != "" {
  1527. for _, value := range paramslist {
  1528. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1529. if tempv, exists := c.Get("jwt_username"); exists {
  1530. s_params = append(s_params, tempv.(string))
  1531. } else {
  1532. s_params = append(s_params, "")
  1533. }
  1534. } else {
  1535. s_params = append(s_params, parammaps.Get(strings.Trim(value, " ")).ValueStr())
  1536. }
  1537. }
  1538. }
  1539. } else if fsion2.HasKey("params") {
  1540. params := fsion2.Get("params").Array()
  1541. for _, v_params := range params {
  1542. s_params = append(s_params, v_params.ValueStr())
  1543. }
  1544. }
  1545. _, err := execDataBySql(sql, s_params)
  1546. if err != nil {
  1547. logging.Error("PostDataByName err: ",err)
  1548. errorm := make(map[string]interface{})
  1549. errorm["code"] = e.ERROR
  1550. errorm["message"] = err.Error()
  1551. errorm["param"] = s_params
  1552. queryData[fsion2.Get("name").ValueStr()] = errorm
  1553. continue
  1554. } else {
  1555. errorm := make(map[string]interface{})
  1556. errorm["code"] = e.SUCCESS
  1557. errorm["message"] = "success"
  1558. queryData[fsion2.Get("name").ValueStr()] = errorm
  1559. continue
  1560. }
  1561. } else {
  1562. errorm := make(map[string]interface{})
  1563. errorm["code"] = e.ERROR
  1564. errorm["message"] = "NO SQL"
  1565. errorm["param"] = ""
  1566. queryData[fsion2.Get("name").ValueStr()] = errorm
  1567. }
  1568. }
  1569. appG.Response(http.StatusOK, e.SUCCESS, queryData)
  1570. }
  1571. func getDataBySql(sqlstr string, offset, pagecount int, returntype string, params []interface{}) (interface{}, error) {
  1572. if offset > 0 {
  1573. offset = offset - 1
  1574. }
  1575. //if pagecount == 0 {
  1576. // pagecount = setting.AppSetting.PageSize
  1577. //}else if pagecount>setting.AppSetting.PageSize{
  1578. // pagecount = setting.AppSetting.PageSize
  1579. //}
  1580. if returntype == "list" {
  1581. queryData, err := restful.QueryByList(sqlstr, offset, pagecount, params)
  1582. return queryData, err
  1583. } else if returntype == "listlist" {
  1584. queryData, err := restful.QueryByListList(sqlstr, offset, pagecount, params)
  1585. return queryData, err
  1586. } else {
  1587. queryData, err := restful.QueryByMap(sqlstr, offset, pagecount, params)
  1588. return queryData, err
  1589. }
  1590. }
  1591. func GetRecuData_BySql(sql string, Params string, idname string, thisParams map[string]interface{}, isList bool) (interface{}, error) {
  1592. paramslist := strings.Split(Params, ",")
  1593. s_params := make([]interface{}, 0)
  1594. if isList {
  1595. for _, v_params := range thisParams["params"].([]interface{}) {
  1596. s_params = append(s_params, v_params)
  1597. }
  1598. } else {
  1599. paramslist := strings.Split(Params, ",")
  1600. if len(paramslist) > 0 && Params != "" {
  1601. for _, value := range paramslist {
  1602. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1603. }
  1604. }
  1605. }
  1606. queryData, err := restful.QueryByMapMenu(sql, 0, 0, s_params)
  1607. for _, queryData_ := range queryData {
  1608. if v, ok := queryData_["haschildren"]; (ok && v.(int64) > 0) || !ok {
  1609. if isList {
  1610. //fmt.Println(idname, "-------------- -queryData_[idname]", queryData_)
  1611. if queryData_[idname] != nil {
  1612. thisParams["params"].([]interface{})[len(thisParams["params"].([]interface{}))-1] = queryData_[idname].(interface{})
  1613. }
  1614. } else {
  1615. if queryData_[idname] != nil {
  1616. thisParams[idname] = queryData_[idname].(interface{})
  1617. }
  1618. if len(paramslist) > 0 && Params != "" {
  1619. s_params = make([]interface{}, 0)
  1620. for _, value := range paramslist {
  1621. s_params = append(s_params, thisParams[strings.Trim(value, " ")])
  1622. }
  1623. }
  1624. }
  1625. queryData_["children"], _ = GetRecuData_BySql(sql, Params, idname, thisParams, isList)
  1626. } else {
  1627. }
  1628. }
  1629. return queryData, err
  1630. }
  1631. func execDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1632. queryData, err := restful.ExecQuery(sqlstr, params)
  1633. return queryData, err
  1634. }
  1635. func ExecDataBySql(sqlstr string, params []interface{}) (interface{}, error) {
  1636. queryData, err := restful.ExecQuery(sqlstr, params)
  1637. return queryData, err
  1638. }
  1639. // @Summary 导入excel文件到数据库,excel第一行作为参数
  1640. // @Tags ImportExcel
  1641. // @Accept json
  1642. // @Produce json
  1643. // @Param file query string true "文件名"
  1644. // @Param name header string true "执行sql的名字"
  1645. // @Success 200 {string} json "{ "code": 200, "data": {}, "msg": "ok" }"
  1646. // @Failure 400 {string} json "{"code":400, "data":null,"msg":"请求参数错误"}"
  1647. // @Failure 404 {string} json "{ "code": 404, "data":null,"msg":"请求参数错误"}"
  1648. // @Router /data/ImportExcel [POST]
  1649. func ImportExcel(c *gin.Context) {
  1650. appG := app.Gin{C: c}
  1651. inputdata := ""
  1652. c.Request.ParseForm()
  1653. //tempv1, _ := c.Get("jwt_username")
  1654. //fmt.Println("=======================",tempv1)
  1655. params := c.Request.Form
  1656. file, _, err := c.Request.FormFile("file")
  1657. if err != nil {
  1658. appG.Response(http.StatusOK, e.ERROR, err)
  1659. return
  1660. }
  1661. sqlnamestr := params.Get("name")
  1662. sheetname := params.Get("sheetname")
  1663. importParams := params.Get("importParams")
  1664. logging.Info("ImportExcel ",c.Keys,c.Request.RemoteAddr,sqlnamestr)
  1665. sql, _ := restful.GetSqlByNameDB(sqlnamestr)
  1666. paramslist := strings.Split(importParams, ",")
  1667. paramslist = append(paramslist, "jwt_username")
  1668. queryData := make([]interface{}, 0)
  1669. xlsx1, err := excelize.OpenReader(file)
  1670. if err != nil {
  1671. fmt.Println(err)
  1672. return
  1673. }
  1674. ok := 0
  1675. error := 0
  1676. rows, _ := xlsx1.GetRows(sheetname)
  1677. //fmt.Println("============================",rows)
  1678. for i, row := range rows {
  1679. fmt.Print(i, "\t")
  1680. if i > 0 {
  1681. if len(paramslist) > 0 {
  1682. s_params := make([]interface{}, 0)
  1683. entry := make(map[string]interface{})
  1684. outentry := make(map[string]interface{})
  1685. inputdata = ""
  1686. for _, value := range paramslist {
  1687. if strings.ToLower(strings.Trim(value, " ")) == "jwt_username" {
  1688. if tempv, exists := c.Get("jwt_username"); exists {
  1689. s_params = append(s_params, tempv.(string))
  1690. } else {
  1691. s_params = append(s_params, "")
  1692. }
  1693. } else {
  1694. for j, colCell := range rows[0] {
  1695. if strings.Trim(value, " ") == colCell {
  1696. for k, colCell1 := range row {
  1697. if j == k {
  1698. outentry[colCell] = colCell1
  1699. entry[colCell] = colCell1
  1700. //inputdata = inputdata + colCell + ":" + colCell1 + ";"
  1701. }
  1702. }
  1703. }
  1704. }
  1705. s_params = append(s_params, entry[strings.Trim(value, " ")])
  1706. }
  1707. }
  1708. outentry["input"] = inputdata
  1709. // returnmsg, err := execDataBySql(sqlApi.Sqlstr, s_params)
  1710. returnmsg, err := getDataBySql(sql, 0, 0, "4", s_params)
  1711. returnmsgmap, _ := returnmsg.(map[string]interface{})
  1712. returnmsgmap1, _ := returnmsgmap["list"].([]map[string]interface{})
  1713. if err != nil {
  1714. outentry["error_msg"] = err.Error()
  1715. error++
  1716. queryData = append(queryData, outentry)
  1717. } else if (len(returnmsgmap1) > 0) && (returnmsgmap1[0]["msg"] == "success") {
  1718. ok++
  1719. } else {
  1720. outentry["error_msg"] = returnmsgmap1[0]["msg"]
  1721. error++
  1722. queryData = append(queryData, outentry)
  1723. }
  1724. }
  1725. }
  1726. }
  1727. data := make(map[string]interface{})
  1728. data["result"] = queryData
  1729. data["success"] = ok
  1730. data["err_count"] = error
  1731. appG.Response(http.StatusOK, e.SUCCESS, data)
  1732. }