attachmentController.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * @Author: liling
  3. * @Date: 2022-06-23
  4. * @LastEditors: liling
  5. * @LastEditTime: 2022-06-23
  6. * @FilePath: \SCD\controllers\AttachmentController.go
  7. * @Description:附件管理相关对外API
  8. *
  9. * Copyright (c) 2022 by liling/jujutong, All Rights Reserved.
  10. */
  11. package controllers
  12. import (
  13. "crypto/md5"
  14. "fmt"
  15. "io"
  16. "io/fs"
  17. "io/ioutil"
  18. "log"
  19. "os"
  20. "os/exec"
  21. "path"
  22. "path/filepath"
  23. "scd_check_tools/global"
  24. "scd_check_tools/logger"
  25. "scd_check_tools/models/bo"
  26. "scd_check_tools/tools"
  27. "strconv"
  28. "strings"
  29. "time"
  30. "github.com/astaxie/beego/orm"
  31. )
  32. //文件/附件服务
  33. type AttachmentController struct {
  34. BaseController
  35. }
  36. func init() {
  37. }
  38. // @Summary 上传文件
  39. // @Description 上传文件
  40. // @Tags 文件/附件服务接口
  41. // @Accept x-www-form-urlencoded
  42. // @Produce json
  43. // @Param file formData file true "文件流"
  44. // @Param station_id formData int true "变电站ID"
  45. // @Param is_checkin formData int false "是否是管控文档"
  46. // @Param data_type formData string false "业务类型。值为:a_scd|ccd|icd|cid|空。"
  47. // @Param attachment_type formData string false "文件类别。值关联系统字典file_types或者为空。"
  48. // @Success 200 {object} ResultOK 成功
  49. // @Failure 500 {object} ResultError 失败
  50. // @router /uploadfile [post]
  51. func (c *AttachmentController) UploadFile() {
  52. _, fh, err := c.GetFile("file")
  53. if err != nil {
  54. logger.Logger.Error(err)
  55. //c.Ctx.WriteString("上传失败:" + err.Error())
  56. c.Data["json"] = c.ResultError("上传失败:" + err.Error())
  57. c.ServeJSON()
  58. return
  59. }
  60. station_id, _ := c.GetInt32("station_id")
  61. if station_id == 0 {
  62. c.Data["json"] = c.ResultError("无效的变电站编号")
  63. c.ServeJSON()
  64. return
  65. }
  66. PthSep := string(os.PathSeparator)
  67. is_checkin, _ := c.GetInt("is_checkin") //是否是管控文档
  68. datatype := strings.ToLower(c.GetString("data_type")) //业务数据类型
  69. attachment_type := c.GetString("attachment_type") //附件文件类型
  70. fileext := path.Ext(fh.Filename)
  71. file_name := fh.Filename
  72. logger.Logger.Debug("上传文件,业务数据类型:" + datatype + " file:" + fh.Filename + " fileext:" + fileext)
  73. if tools.HasChineseChar(file_name) {
  74. isgbk := tools.IsGBK([]byte(file_name))
  75. isutf8 := tools.IsUTF8([]byte(file_name))
  76. logger.Logger.Debug(fmt.Sprintf("文件名称编码 ISGBK:%+v ISUTF8:%+v", isgbk, tools.IsUTF8([]byte(file_name))))
  77. if isgbk && !isutf8 {
  78. file_name = tools.ConvertByte2String([]byte(file_name), "UTF-8")
  79. logger.Logger.Debug("文件名称转换结果:" + file_name)
  80. }
  81. }
  82. newFilename := fmt.Sprintf("%d_%d%s", station_id, time.Now().Unix(), fileext)
  83. file := strings.Join([]string{".", "static", "upload", newFilename}, PthSep)
  84. os.MkdirAll(strings.Join([]string{".", "static", "upload"}, PthSep), fs.ModeDir)
  85. err = c.SaveToFile("file", file)
  86. doc_id := int32(0)
  87. if err != nil {
  88. logger.Logger.Error(err)
  89. c.Data["json"] = c.ResultError(err.Error())
  90. } else {
  91. if is_checkin == 1 {
  92. file_type_id := ""
  93. if datatype != "" {
  94. file_type_row, _ := global.GoCahce.Get("global_code_file_typesfile_" + strings.ToLower(datatype))
  95. file_type_id = tools.IsEmpty(file_type_row.(orm.Params)["id"])
  96. }
  97. //清理指定变电站已上传但不使用的指定类型文件
  98. new(bo.AttachmentMgr).ClearDityFile(station_id, file_type_id)
  99. filelist := map[string]string{} //文件列表。主要是zip时用于存放压缩包内的文件名
  100. if datatype == "a_scd" {
  101. datatype = "scd"
  102. }
  103. if fileext == ".zip" && datatype == "scd" {
  104. //解压包
  105. unzipDir := strings.Join([]string{".", "static", "upload", strings.Replace(fh.Filename, ".zip", "", 1)}, PthSep)
  106. saveTargetDir := strings.Join([]string{".", "static", "download", datatype}, PthSep)
  107. os.MkdirAll(saveTargetDir, fs.ModePerm)
  108. err = tools.Unzip(file, unzipDir)
  109. if err != nil {
  110. log.Println(err)
  111. c.Data["json"] = c.ResultError("压缩包解压失败:" + err.Error())
  112. c.ServeJSON()
  113. return
  114. }
  115. dir, err2 := ioutil.ReadDir(unzipDir)
  116. if err2 != nil {
  117. log.Println(err2)
  118. c.Data["json"] = c.ResultError("压缩包解压失败:" + err2.Error())
  119. c.ServeJSON()
  120. return
  121. }
  122. suffix := "." + datatype //忽略后缀匹配的大小写
  123. findscd := ""
  124. ind := int64(0)
  125. for _, fi := range dir {
  126. if fi.IsDir() { // 忽略目录
  127. continue
  128. }
  129. file_name = fi.Name()
  130. isgbk := tools.IsGBK([]byte(file_name))
  131. isutf8 := tools.IsUTF8([]byte(file_name))
  132. if isgbk && !isutf8 {
  133. logger.Logger.Debug(fmt.Sprintf("文件名称:%s 编码 ISGBK:%+v ISUTF8:%+v", file_name, isgbk, tools.IsUTF8([]byte(file_name))))
  134. file_name = tools.ConvertByte2String([]byte(file_name), "UTF-8")
  135. logger.Logger.Debug("文件名称转换结果:" + file_name)
  136. }
  137. if strings.HasSuffix(strings.ToLower(file_name), suffix) { //匹配文件
  138. findscd = unzipDir + PthSep + file_name
  139. scdFileName := saveTargetDir + PthSep + fmt.Sprintf("%d_%d%s", station_id, time.Now().Unix()+ind, suffix)
  140. os.Rename(findscd, scdFileName)
  141. filelist[file_name] = scdFileName
  142. ind = ind + 1
  143. }
  144. }
  145. if len(filelist) == 0 {
  146. c.Data["json"] = c.ResultError("压缩包内没有发现" + datatype + "文件!")
  147. c.ServeJSON()
  148. return
  149. }
  150. os.RemoveAll(unzipDir)
  151. os.RemoveAll(file)
  152. } else if fileext == ".zip" && (datatype == "icd" || datatype == "cid" || datatype == "ccd") {
  153. has := md5.Sum([]byte(fh.Filename))
  154. randmdirname := fmt.Sprintf("%x", has)
  155. //解压包
  156. unzipDir := strings.Join([]string{".", "static", "upload", strings.Replace(fh.Filename, ".zip", "", 1)}, PthSep)
  157. saveTargetDir := strings.Join([]string{".", "static", "download", datatype, randmdirname}, PthSep)
  158. os.MkdirAll(saveTargetDir, fs.ModePerm)
  159. err = tools.Unzip(file, unzipDir)
  160. if err != nil {
  161. log.Println(err)
  162. c.Data["json"] = c.ResultError("压缩包解压失败:" + err.Error())
  163. c.ServeJSON()
  164. return
  165. }
  166. dir, err2 := ioutil.ReadDir(unzipDir)
  167. if err2 != nil {
  168. log.Println(err2)
  169. c.Data["json"] = c.ResultError("压缩包解压失败:" + err2.Error())
  170. c.ServeJSON()
  171. return
  172. }
  173. suffix := "." + datatype //忽略后缀匹配的大小写
  174. findscd := ""
  175. for _, fi := range dir {
  176. if fi.IsDir() { // 忽略目录
  177. continue
  178. }
  179. tmp_file_name := fi.Name()
  180. if strings.HasSuffix(strings.ToLower(tmp_file_name), suffix) { //匹配文件
  181. findscd = unzipDir + PthSep + tmp_file_name
  182. scdFileName := saveTargetDir + PthSep + tmp_file_name
  183. os.Rename(findscd, scdFileName)
  184. filelist[tmp_file_name] = scdFileName
  185. }
  186. }
  187. if len(filelist) == 0 {
  188. c.Data["json"] = c.ResultError("压缩包内没有发现" + datatype + "文件!")
  189. c.ServeJSON()
  190. return
  191. }
  192. os.RemoveAll(unzipDir)
  193. filelist[file_name] = file //保存原始文件记录
  194. } else {
  195. filelist[file_name] = file
  196. }
  197. attachmentMgrObj := new(bo.AttachmentMgr)
  198. attachmentMgrObj.SetUserInfo(c.GetCurrentUserInfo())
  199. attachmentMgrObj.Model.StationId = station_id
  200. for fn, fpath := range filelist {
  201. f, _ := os.Stat(fpath)
  202. size := tools.FormatFileSize(f.Size())
  203. attachmentMgrObj.Model.ScdId = int64(-1) //临时设置为-1。scd解析完成后,需要更新附件表中该站scdid为-1的值为实际的scdid
  204. attachmentMgrObj.Model.FileName = fn
  205. attachmentMgrObj.Model.SavePath = strings.TrimLeft(fpath, ".")
  206. attachmentMgrObj.Model.FileSize = size
  207. attachmentMgrObj.Model.CheckFlag = 1
  208. attachmentMgrObj.Model.FileSuffix = strings.ToLower(path.Ext(fpath)[1:])
  209. if file_type_id != "" {
  210. attachmentMgrObj.Model.FileType = file_type_id
  211. } else {
  212. attachmentMgrObj.Model.FileType = ""
  213. }
  214. attachmentMgrObj.Model.IedName = ""
  215. if datatype != "scd" {
  216. attachmentMgrObj.Model.IedName = strings.Split(fn, ".")[0]
  217. }
  218. doc_id, _ = attachmentMgrObj.Save()
  219. }
  220. } else if attachment_type != "" {
  221. //写附件表
  222. //非需要解压的文件处理
  223. f, ferr := os.Stat(file)
  224. if ferr != nil {
  225. c.Data["json"] = c.ResultError(ferr.Error())
  226. c.ServeJSON()
  227. return
  228. }
  229. ied_name := c.GetString("ied_name")
  230. size := tools.FormatFileSize(f.Size())
  231. attachmentMgrObj := new(bo.AttachmentMgr)
  232. attachmentMgrObj.SetUserInfo(c.GetCurrentUserInfo())
  233. attachmentMgrObj.Model.StationId = station_id
  234. attachmentMgrObj.Model.FileName = file_name
  235. attachmentMgrObj.Model.SavePath = strings.TrimLeft(file, ".")
  236. attachmentMgrObj.Model.FileSize = size
  237. attachmentMgrObj.Model.FileSuffix = strings.ToLower(fileext[1:])
  238. attachmentMgrObj.Model.FileType = attachment_type
  239. attachmentMgrObj.Model.IedName = ied_name
  240. attachmentMgrObj.Model.CheckFlag = 0
  241. doc_id, _ = attachmentMgrObj.Save()
  242. }
  243. f, _ := exec.LookPath(os.Args[0])
  244. fp, _ := filepath.Abs(f)
  245. i := strings.LastIndex(fp, string(os.PathSeparator))
  246. res := string(fp[0:i])
  247. if res != "" {
  248. res = file[1:]
  249. }
  250. logger.Logger.Debug(fmt.Sprintf("fileid=====%d", doc_id))
  251. returninfo := map[string]string{"filename": file_name, "path": res, "fileid": tools.IsEmpty(doc_id)}
  252. c.Data["json"] = c.ResultOK(returninfo, 0)
  253. }
  254. c.ServeJSON()
  255. }
  256. // @Summary 根据id获取附件信息
  257. // @Description 根据id获取附件信息
  258. // @Tags 文件/附件服务接口
  259. // @Accept x-www-form-urlencoded
  260. // @Produce json
  261. // @Param id query int true "附件ID"
  262. // @Success 200 {object} ResultOK 成功
  263. // @Failure 500 {object} ResultError 失败
  264. // @router /attachment/one [get]
  265. func (c *AttachmentController) AttachmentRecord() {
  266. obj := new(bo.AttachmentMgr)
  267. obj.SetUserInfo(c.GetCurrentUserInfo())
  268. id, _ := c.GetInt32("Id", 0)
  269. t := new(bo.T_sys_attachment)
  270. t.Id = id
  271. obj.Model = *t
  272. tableData, err := obj.One()
  273. if err != nil {
  274. c.Data["json"] = c.ResultError(err.Error())
  275. } else {
  276. c.Data["json"] = c.ResultOK(tableData, 1)
  277. }
  278. c.ServeJSON()
  279. }
  280. // @Summary 查询附件列表
  281. // @Description 查询附件列表
  282. // @Tags 文件/附件服务接口
  283. // @Accept x-www-form-urlencoded
  284. // @Produce json
  285. // @Param pageindex query int true "当前查询的分页页码,默认为1"
  286. // @Param pagesize query int true "每页显示的数据条数,默认为20"
  287. // @Param station_id query int true "变电站ID"
  288. // @Param scd_id query int false "SCD文件ID"
  289. // @Param is_checkin query int false "是否管控文档。0 其他文件 1 管控文件 2 裁剪文件"
  290. // @Param name query string false "附件名称"
  291. // @Param ied_name query string false "装置名称"
  292. // @Param type query int false "文件类别ID"
  293. // @Success 200 {object} ResultOK 成功
  294. // @Failure 500 {object} ResultError 失败
  295. // @router /attachment/list [get]
  296. func (c *AttachmentController) AttachmentList() {
  297. obj := new(bo.AttachmentMgr)
  298. obj.SetUserInfo(c.GetCurrentUserInfo())
  299. station_id, _ := c.GetInt32("station_id")
  300. if station_id == 0 {
  301. c.Data["json"] = c.ResultError("变电站编号不能为空!")
  302. c.ServeJSON()
  303. return
  304. }
  305. scd_id := c.GetString("scd_id")
  306. is_checkin := c.GetString("is_checkin") //文件类型。0 其他文件 1 管控文件 2 裁剪文件(仅支持ccd\icd\cid)
  307. filename := c.GetString("name")
  308. ied_name := c.GetString("ied_name")
  309. filetype := c.GetString("type")
  310. pageno, _ := c.GetInt32("pageno", 1)
  311. pagesize, _ := c.GetInt32("pagesize", 20)
  312. tableData, n, err := obj.List(scd_id, station_id, ied_name, filename, filetype, is_checkin, pageno, pagesize)
  313. if err != nil {
  314. c.Data["json"] = c.ResultError(err.Error())
  315. } else {
  316. c.Data["json"] = c.ResultOK(tableData, n)
  317. }
  318. c.ServeJSON()
  319. }
  320. // @Summary 下载附件
  321. // @Description 查询附件列表
  322. // @Tags 文件/附件服务接口
  323. // @Accept x-www-form-urlencoded
  324. // @Produce json
  325. // @Param ids formData string true "需要下载的附件id。多个附件id使用逗号分隔。"
  326. // @Success 200 {object} ResultOK 成功
  327. // @Failure 500 {object} ResultError 失败
  328. // @router /attachment/download [post]
  329. func (c *AttachmentController) AttachmentMutlDownload() {
  330. ids := c.GetString("ids")
  331. if ids == "" {
  332. c.Data["json"] = c.ResultError("下载文件不能为空!")
  333. c.ServeJSON()
  334. return
  335. }
  336. dirChar := string(os.PathSeparator)
  337. f, _ := exec.LookPath(os.Args[0])
  338. fp, _ := filepath.Abs(f)
  339. i := strings.LastIndex(fp, string(os.PathSeparator))
  340. fp = string(fp[0:i])
  341. var err error
  342. fpathlist := []string{}
  343. obj := new(bo.AttachmentMgr)
  344. zipFileName := dirChar + "static" + dirChar + "download" + dirChar + tools.IsEmpty(time.Now().Unix())
  345. //fmt.Println("zipFileName:" + zipFileName)
  346. err = os.MkdirAll(fp+zipFileName, fs.ModePerm)
  347. if err != nil {
  348. log.Println(err)
  349. c.Data["json"] = c.ResultError(err.Error())
  350. c.ServeJSON()
  351. return
  352. }
  353. defer func() {
  354. _, ferr := os.Stat(fp + zipFileName)
  355. if ferr != nil && os.IsNotExist(ferr) {
  356. return
  357. }
  358. os.RemoveAll(fp + zipFileName)
  359. }()
  360. for _, idstr := range strings.Split(ids, ",") {
  361. id, _ := strconv.Atoi(idstr)
  362. obj.Model = bo.T_sys_attachment{Id: int32(id)}
  363. m, err2 := obj.One()
  364. if err2 != nil {
  365. c.Data["json"] = c.ResultError(err2.Error())
  366. c.ServeJSON()
  367. return
  368. }
  369. savep := strings.ReplaceAll(m.SavePath, "/", dirChar)
  370. if savep[0:1] == "." {
  371. savep = savep[1:]
  372. }
  373. //fmt.Println("sourceFile:" + fp + savep)
  374. source, err1 := os.Open(fp + savep)
  375. if err1 != nil {
  376. c.Data["json"] = c.ResultError(err1.Error())
  377. c.ServeJSON()
  378. return
  379. }
  380. newFile := fp + zipFileName + dirChar + m.FileName
  381. //fmt.Println("newFile:" + newFile)
  382. destination, err2 := os.Create(newFile)
  383. if err2 != nil {
  384. c.Data["json"] = c.ResultError(err2.Error())
  385. c.ServeJSON()
  386. return
  387. }
  388. io.Copy(destination, source)
  389. source.Close()
  390. destination.Close()
  391. fpathlist = append(fpathlist, newFile)
  392. }
  393. zipFileNameA := zipFileName + ".zip"
  394. err = tools.ZipMutile(fpathlist, fp+zipFileNameA)
  395. if err != nil {
  396. c.Data["json"] = c.ResultError(err.Error())
  397. c.ServeJSON()
  398. return
  399. }
  400. //fmt.Println("zipfile:" + zipFileNameA)
  401. c.Data["json"] = c.ResultOK(zipFileNameA, 0)
  402. c.ServeJSON()
  403. }
  404. // @Summary 删除附件
  405. // @Description 删除指定的附件
  406. // @Tags 文件/附件服务接口
  407. // @Accept x-www-form-urlencoded
  408. // @Produce json
  409. // @Param id formData int true "需要删除的附件id。"
  410. // @Param ids formData string true "需要删除的附件id。多个附件id使用逗号分隔。"
  411. // @Param scd_id formData int false "SCD文件ID。不为空时表示删除该SCD关联的文件"
  412. // @Param check_flag formData int false "是否是管控文件。1表示是,否则表示为否"
  413. // @Success 200 {object} ResultOK 成功
  414. // @Failure 500 {object} ResultError 失败
  415. // @router /attachment/delete [post]
  416. func (c *AttachmentController) AttachmentDel() {
  417. obj := new(bo.AttachmentMgr)
  418. obj.SetUserInfo(c.GetCurrentUserInfo())
  419. id, _ := c.GetInt("id", 0)
  420. checkFlag := c.GetString("check_flag")
  421. scdId := c.GetString("scd_id")
  422. checkFlagInt := 0
  423. if checkFlag != "" {
  424. checkFlagInt, _ = strconv.Atoi(checkFlag)
  425. }
  426. var err error
  427. if id == 0 {
  428. //判断是否批量删除
  429. ids := c.GetString("ids")
  430. if ids == "" {
  431. c.Data["json"] = c.ResultError("删除文件的编号不能为空!")
  432. c.ServeJSON()
  433. return
  434. }
  435. for _, idstr := range strings.Split(ids, ",") {
  436. id, _ = strconv.Atoi(idstr)
  437. obj.Model = bo.T_sys_attachment{Id: int32(id)}
  438. obj.Model.CheckFlag = checkFlagInt
  439. err = obj.Delete(true)
  440. if err != nil {
  441. break
  442. }
  443. }
  444. } else {
  445. obj.Model = bo.T_sys_attachment{Id: int32(id)}
  446. obj.Model.CheckFlag = checkFlagInt
  447. if scdId != "" {
  448. obj.Model.ScdId, _ = strconv.ParseInt(scdId, 10, 64)
  449. obj.Model.FileSuffix = "scd"
  450. }
  451. err = obj.Delete(true)
  452. }
  453. if err != nil {
  454. c.Data["json"] = c.ResultError(err.Error())
  455. } else {
  456. c.Data["json"] = c.ResultOK(nil, 0)
  457. }
  458. c.ServeJSON()
  459. }
  460. // @Summary 查询指定附件下载历史记录
  461. // @Description 查询指定附件下载历史记录。(预留)
  462. // @Tags 文件/附件服务接口
  463. // @Accept x-www-form-urlencoded
  464. // @Produce json
  465. // @Param id query int true "附件id。"
  466. // @Success 200 {object} ResultOK 成功
  467. // @Failure 500 {object} ResultError 失败
  468. // @router /attachment/download/rec [post]
  469. func (c *AttachmentController) AttachmentDownloadRec() {
  470. obj := new(bo.AttachmentMgr)
  471. obj.SetUserInfo(c.GetCurrentUserInfo())
  472. id, _ := c.GetInt("id", 0)
  473. err := obj.DownloadRec(id, c.GetString("desc"))
  474. if err != nil {
  475. c.Data["json"] = c.ResultError(err.Error())
  476. } else {
  477. c.Data["json"] = c.ResultOK("", 0)
  478. }
  479. c.ServeJSON()
  480. }
  481. // @Summary 获取指定的数据及附件自动清理配置
  482. // @Description 获取指定的数据及附件自动清理配置
  483. // @Tags 文件/附件服务接口
  484. // @Accept x-www-form-urlencoded
  485. // @Produce json
  486. // @Param id query int true "配置记录id。"
  487. // @Success 200 {object} ResultOK 成功
  488. // @Failure 500 {object} ResultError 失败
  489. // @router /attachment/cfg/one [get]
  490. func (c *AttachmentController) AttachmentCfgRecord() {
  491. obj := new(bo.AttachmentClearMgr)
  492. obj.SetUserInfo(c.GetCurrentUserInfo())
  493. id, _ := c.GetInt("Id", 0)
  494. t := new(bo.T_data_attachment)
  495. t.Id = id
  496. obj.Model = *t
  497. tableData, err := obj.One()
  498. if err != nil {
  499. c.Data["json"] = c.ResultError(err.Error())
  500. } else {
  501. c.Data["json"] = c.ResultOK(tableData, 1)
  502. }
  503. c.ServeJSON()
  504. }
  505. // @Summary 获取数据及附件自动清理配置列表
  506. // @Description 获取数据及附件自动清理配置列表
  507. // @Tags 文件/附件服务接口
  508. // @Accept x-www-form-urlencoded
  509. // @Produce json
  510. // @Success 200 {object} ResultOK 成功
  511. // @Failure 500 {object} ResultError 失败
  512. // @router /attachment/cfg/list [get]
  513. func (c *AttachmentController) AttachmentCfgList() {
  514. obj := new(bo.AttachmentClearMgr)
  515. obj.SetUserInfo(c.GetCurrentUserInfo())
  516. tableData, n, err := obj.List()
  517. if err != nil {
  518. c.Data["json"] = c.ResultError(err.Error())
  519. } else {
  520. c.Data["json"] = c.ResultOK(tableData, n)
  521. }
  522. c.ServeJSON()
  523. }
  524. // @Summary 保存数据及附件自动清理配置
  525. // @Description 保存数据及附件自动清理配置
  526. // @Tags 文件/附件服务接口
  527. // @Accept x-www-form-urlencoded
  528. // @Produce json
  529. // @Param id formData int true "配置记录id。不为0时表示编辑保存"
  530. // @Param name formData string true "配置名称"
  531. // @Param skeepday formData int true "数据及附件保留的天数"
  532. // @Param tablename formData string true "数据模型名称"
  533. // @Param colname formData string true "数据模型中的记录创建日期列名"
  534. // @Param filterwhere formData string false "数据过滤条件。支持SQL语法"
  535. // @Param memo formData string false "配置说明"
  536. // @Success 200 {object} ResultOK 成功
  537. // @Failure 500 {object} ResultError 失败
  538. // @router /attachment/cfg/save [post]
  539. func (c *AttachmentController) SaveAttachmentCfg() {
  540. param_name := tools.FilterSpecialChar(c.GetString("name"))
  541. skeepday, _ := c.GetInt("skeepday", 0)
  542. tablename := tools.FilterSpecialChar(c.GetString("tablename"))
  543. colname := tools.FilterSpecialChar(c.GetString("colname"))
  544. filterwhere := tools.FilterSpecialChar(c.GetString("filterwhere"), "'")
  545. memo := c.GetString("memo")
  546. id, _ := c.GetInt("id", 0)
  547. t := bo.T_data_attachment{Id: id, Name: param_name, Skeepday: skeepday, Tablename: tablename, Colname: colname, Filterwhere: filterwhere, Memo: memo}
  548. obj := new(bo.AttachmentClearMgr)
  549. obj.SetUserInfo(c.GetCurrentUserInfo())
  550. obj.Model = t
  551. err := obj.Save()
  552. if err != nil {
  553. c.Data["json"] = c.ResultError(err.Error())
  554. } else {
  555. c.Data["json"] = c.ResultOK("", 0)
  556. }
  557. c.ServeJSON()
  558. }
  559. // @Summary 删除指定的数据及附件自动清理配置
  560. // @Description 删除指定的数据及附件自动清理配置
  561. // @Tags 文件/附件服务接口
  562. // @Accept x-www-form-urlencoded
  563. // @Produce json
  564. // @Param id formData int true "配置记录id"
  565. // @Success 200 {object} ResultOK 成功
  566. // @Failure 500 {object} ResultError 失败
  567. // @router /attachment/cfg/delete [post]
  568. func (c *AttachmentController) AttachmentCfgDel() {
  569. obj := new(bo.AttachmentClearMgr)
  570. obj.SetUserInfo(c.GetCurrentUserInfo())
  571. id, _ := c.GetInt("id", 0)
  572. obj.Model = bo.T_data_attachment{Id: id}
  573. err := obj.Delete()
  574. if err != nil {
  575. c.Data["json"] = c.ResultError(err.Error())
  576. } else {
  577. c.Data["json"] = c.ResultOK(nil, 0)
  578. }
  579. c.ServeJSON()
  580. }
  581. // @Summary 重新解析指定的SCD文件
  582. // @Description 重新解析指定的SCD文件
  583. // @Tags 文件/附件服务接口
  584. // @Accept x-www-form-urlencoded
  585. // @Produce json
  586. // @Param station_id formData int true "变电站ID"
  587. // @Param scd_name formData string true "SCD文件名称"
  588. // @Param scd_path formData string true "SCD附件路径"
  589. // @Success 200 {object} ResultOK 成功
  590. // @Failure 500 {object} ResultError 失败
  591. // @router /attachment/scdparse/start [post]
  592. func (c *AttachmentController) TmpParseScd() {
  593. //统计当前正在解析中的scd数量
  594. //需要对同时解析的scd数量进行限制,否则可能会造成大量内存占用使服务器崩溃
  595. scdMgr := new(bo.ScdMgr)
  596. if h, msg := scdMgr.CheckParseMaxLimit(); h {
  597. c.Data["json"] = c.ResultError("系统繁忙:" + msg + ",请稍候(约5分钟)再试")
  598. c.ServeJSON()
  599. return
  600. }
  601. obj := new(bo.AttachmentMgr)
  602. obj.SetUserInfo(c.GetCurrentUserInfo())
  603. stationid, _ := c.GetInt("station_id", 0)
  604. err := obj.StartScdTempParse(stationid, c.GetString("scd_name"), c.GetString("scd_path"))
  605. if err != nil {
  606. c.Data["json"] = c.ResultError(err.Error())
  607. } else {
  608. c.Data["json"] = c.ResultOK(nil, 0)
  609. }
  610. c.ServeJSON()
  611. }