attachmentController.go 21 KB

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