attachmentController.go 21 KB

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