pdf.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package yb
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hz_crm_api/controllers"
  7. "hongze/hz_crm_api/models"
  8. "hongze/hz_crm_api/models/yb"
  9. "hongze/hz_crm_api/services"
  10. "hongze/hz_crm_api/services/alarm_msg"
  11. "hongze/hz_crm_api/utils"
  12. "os"
  13. "path"
  14. "strings"
  15. "time"
  16. )
  17. // PdfController 报告PDF
  18. type PdfController struct {
  19. controllers.BaseAuthController
  20. }
  21. // Add
  22. // @Title 新增PDF
  23. // @Description 新增PDF
  24. // @Param request body yb.PdfAddReq true "type json string"
  25. // @Success 200 Ret=200 操作成功
  26. // @router /pdf/add [post]
  27. func (this *PdfController) Add() {
  28. br := new(models.BaseResponse).Init()
  29. defer func() {
  30. if br.ErrMsg == "" {
  31. br.IsSendEmail = false
  32. }
  33. this.Data["json"] = br
  34. this.ServeJSON()
  35. }()
  36. sysUser := this.SysUser
  37. if sysUser == nil {
  38. br.Msg = "请登录"
  39. br.ErrMsg = "请登录,SysUser Is Empty"
  40. br.Ret = 408
  41. return
  42. }
  43. var req yb.PdfAddReq
  44. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  45. br.Msg = "参数解析异常!"
  46. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  47. return
  48. }
  49. req.PdfName = strings.TrimSpace(req.PdfName)
  50. if req.PdfName == "" {
  51. br.Msg = "请输入PDF名称"
  52. return
  53. }
  54. nameRune := []rune(req.PdfName)
  55. if len(nameRune) > 30 {
  56. br.Msg = "名称不得超过30个字符"
  57. return
  58. }
  59. req.OriginName = strings.TrimSpace(req.OriginName)
  60. if req.OriginName == "" {
  61. br.Msg = "文件名称有误"
  62. return
  63. }
  64. // 重名校验
  65. existItem := new(yb.Pdf)
  66. existCond := fmt.Sprintf(` AND %s = ? `, yb.PdfColumns.PdfName)
  67. existPars := make([]interface{}, 0)
  68. existPars = append(existPars, req.PdfName)
  69. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  70. br.Msg = "操作失败"
  71. br.ErrMsg = "获取重名PDF失败, Err: " + e.Error()
  72. return
  73. }
  74. if existItem != nil && existItem.PdfId > 0 {
  75. br.Msg = "该PDF已存在"
  76. return
  77. }
  78. nowTime := time.Now().Local()
  79. newItem := &yb.Pdf{
  80. PdfName: req.PdfName,
  81. PdfUrl: req.PdfUrl,
  82. OriginName: req.OriginName,
  83. SysAdminId: sysUser.AdminId,
  84. SysAdminName: sysUser.RealName,
  85. CreateTime: nowTime,
  86. ModifyTime: nowTime,
  87. }
  88. if e := newItem.Create(); e != nil {
  89. br.Msg = "操作失败"
  90. br.ErrMsg = "新增PDF失败, Err: " + e.Error()
  91. return
  92. }
  93. go func() {
  94. var err error
  95. defer func() {
  96. if err != nil {
  97. fmt.Println("报告PDF-生成二维码失败: ", err.Error())
  98. alarm_msg.SendAlarmMsg("报告PDF-生成二维码失败: "+err.Error(), 3)
  99. }
  100. }()
  101. var qrcode string
  102. page := "pages-report/previewPDF"
  103. scene := fmt.Sprintf(`{"pdf_id":"%d"}`, newItem.PdfId)
  104. origin, e := models.GetYbPcSunCode(scene, page)
  105. if e != nil && e.Error() != utils.ErrNoRow() {
  106. err = fmt.Errorf("GetYbPcSunCode err: %s", e.Error())
  107. return
  108. }
  109. if origin != nil {
  110. qrcode = origin.SuncodeUrl
  111. } else {
  112. qrcode, e = services.PcCreateAndUploadSunCode(scene, page)
  113. if e != nil {
  114. err = fmt.Errorf("PcCreateAndUploadSunCode err: %s", e.Error())
  115. return
  116. }
  117. }
  118. newItem.ShareUrl = qrcode
  119. if e = newItem.Update([]string{yb.PdfColumns.ShareUrl}); e != nil {
  120. err = fmt.Errorf("PDF Update err: %s", e.Error())
  121. return
  122. }
  123. }()
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "操作成功"
  127. }
  128. // Edit
  129. // @Title 编辑PDF
  130. // @Description 编辑PDF
  131. // @Param request body yb.PdfEditReq true "type json string"
  132. // @Success 200 Ret=200 操作成功
  133. // @router /pdf/edit [post]
  134. func (this *PdfController) Edit() {
  135. br := new(models.BaseResponse).Init()
  136. defer func() {
  137. if br.ErrMsg == "" {
  138. br.IsSendEmail = false
  139. }
  140. this.Data["json"] = br
  141. this.ServeJSON()
  142. }()
  143. sysUser := this.SysUser
  144. if sysUser == nil {
  145. br.Msg = "请登录"
  146. br.ErrMsg = "请登录,SysUser Is Empty"
  147. br.Ret = 408
  148. return
  149. }
  150. var req yb.PdfEditReq
  151. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  152. br.Msg = "参数解析异常!"
  153. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  154. return
  155. }
  156. req.PdfName = strings.TrimSpace(req.PdfName)
  157. if req.PdfName == "" {
  158. br.Msg = "请输入PDF名称"
  159. return
  160. }
  161. nameRune := []rune(req.PdfName)
  162. if len(nameRune) > 30 {
  163. br.Msg = "名称不得超过30个字符"
  164. return
  165. }
  166. req.OriginName = strings.TrimSpace(req.OriginName)
  167. if req.OriginName == "" {
  168. br.Msg = "文件名称有误"
  169. return
  170. }
  171. item := new(yb.Pdf)
  172. if e := item.GetItemById(req.PdfId); e != nil {
  173. br.Msg = "操作失败"
  174. br.ErrMsg = "获取PDF信息失败, Err: " + e.Error()
  175. return
  176. }
  177. if item.PdfId <= 0 {
  178. br.Msg = "PDF已被删除, 请刷新页面"
  179. return
  180. }
  181. // 重名校验
  182. existItem := new(yb.Pdf)
  183. existCond := fmt.Sprintf(` AND %s = ? AND %s <> ? `, yb.PdfColumns.PdfName, yb.PdfColumns.PdfId)
  184. existPars := make([]interface{}, 0)
  185. existPars = append(existPars, req.PdfName, req.PdfId)
  186. if e := existItem.GetItemByCondition(existCond, existPars); e != nil && e.Error() != utils.ErrNoRow() {
  187. br.Msg = "操作失败"
  188. br.ErrMsg = "获取重名PDF失败, Err: " + e.Error()
  189. return
  190. }
  191. if existItem != nil && existItem.PdfId > 0 {
  192. br.Msg = "该PDF已存在"
  193. return
  194. }
  195. item.PdfName = req.PdfName
  196. item.OriginName = req.OriginName
  197. item.PdfUrl = req.PdfUrl
  198. item.ModifyTime = time.Now().Local()
  199. updateCols := []string{yb.PdfColumns.PdfName, yb.PdfColumns.OriginName, yb.PdfColumns.PdfUrl, yb.PdfColumns.ModifyTime}
  200. if item.ShareUrl == "" {
  201. var qrcode string
  202. page := "pages-report/previewPDF"
  203. scene := fmt.Sprintf(`{"pdf_id":"%d"}`, item.PdfId)
  204. origin, e := models.GetYbPcSunCode(scene, page)
  205. if e != nil && e.Error() != utils.ErrNoRow() {
  206. br.Msg = "操作失败"
  207. br.ErrMsg = "获取小程序二维码失败, GetYbPcSunCode: " + e.Error()
  208. return
  209. }
  210. if origin != nil {
  211. qrcode = origin.SuncodeUrl
  212. } else {
  213. qrcode, e = services.PcCreateAndUploadSunCode(scene, page)
  214. if e != nil {
  215. br.Msg = "操作失败"
  216. br.ErrMsg = "生成小程序二维码失败, PcCreateAndUploadSunCode: " + e.Error()
  217. return
  218. }
  219. }
  220. item.ShareUrl = qrcode
  221. updateCols = append(updateCols, yb.PdfColumns.ShareUrl)
  222. }
  223. if e := item.Update(updateCols); e != nil {
  224. br.Msg = "操作失败"
  225. br.ErrMsg = "PDF编辑失败, Err: " + e.Error()
  226. return
  227. }
  228. br.Ret = 200
  229. br.Success = true
  230. br.Msg = "操作成功"
  231. }
  232. // Del
  233. // @Title 删除PDF
  234. // @Description 删除PDF
  235. // @Param request body yb.PdfDelReq true "type json string"
  236. // @Success 200 Ret=200 操作成功
  237. // @router /pdf/del [post]
  238. func (this *PdfController) Del() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. if br.ErrMsg == "" {
  242. br.IsSendEmail = false
  243. }
  244. this.Data["json"] = br
  245. this.ServeJSON()
  246. }()
  247. sysUser := this.SysUser
  248. if sysUser == nil {
  249. br.Msg = "请登录"
  250. br.ErrMsg = "请登录,SysUser Is Empty"
  251. br.Ret = 408
  252. return
  253. }
  254. var req yb.PdfDelReq
  255. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  256. br.Msg = "参数解析异常!"
  257. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  258. return
  259. }
  260. if req.PdfId <= 0 {
  261. br.Msg = "参数有误"
  262. return
  263. }
  264. item := new(yb.Pdf)
  265. e := item.GetItemById(req.PdfId)
  266. if e != nil {
  267. if e.Error() == utils.ErrNoRow() {
  268. br.Msg = "PDF已被删除, 请刷新页面"
  269. return
  270. }
  271. br.Msg = "操作失败"
  272. br.ErrMsg = "获取PDF信息失败, Err: " + e.Error()
  273. return
  274. }
  275. if item.PdfId <= 0 {
  276. br.Msg = "PDF已被删除, 请刷新页面"
  277. return
  278. }
  279. if e = item.Del(); e != nil {
  280. br.Msg = "操作失败"
  281. br.ErrMsg = "删除PDF失败, Err: " + e.Error()
  282. return
  283. }
  284. br.Ret = 200
  285. br.Success = true
  286. br.Msg = "操作成功"
  287. }
  288. // PageList
  289. // @Title PDF列表
  290. // @Description PDF列表
  291. // @Param Keyword query string false "关键词"
  292. // @Success 200 Ret=200 获取成功
  293. // @router /pdf/page_list [get]
  294. func (this *PdfController) PageList() {
  295. br := new(models.BaseResponse).Init()
  296. defer func() {
  297. if br.ErrMsg == "" {
  298. br.IsSendEmail = false
  299. }
  300. this.Data["json"] = br
  301. this.ServeJSON()
  302. }()
  303. sysUser := this.SysUser
  304. if sysUser == nil {
  305. br.Msg = "请登录"
  306. br.ErrMsg = "请登录,SysUser Is Empty"
  307. br.Ret = 408
  308. return
  309. }
  310. keyword := this.GetString("Keyword", "")
  311. startTime := this.GetString("StartTime", "")
  312. endTime := this.GetString("EndTime", "")
  313. cond := ``
  314. pars := make([]interface{}, 0)
  315. if keyword != "" {
  316. kw := "%" + keyword + "%"
  317. cond += fmt.Sprintf(` AND %s LIKE ?`, yb.PdfColumns.PdfName)
  318. pars = append(pars, kw)
  319. }
  320. if startTime != "" && endTime != "" {
  321. _, e := time.ParseInLocation(utils.FormatDate, startTime, time.Local)
  322. if e != nil {
  323. br.Msg = "开始日期格式有误, 格式2006-01-02"
  324. return
  325. }
  326. _, e = time.ParseInLocation(utils.FormatDate, endTime, time.Local)
  327. if e != nil {
  328. br.Msg = "结束日期格式有误, 格式2006-01-02"
  329. return
  330. }
  331. st := fmt.Sprint(startTime, " 00:00:00")
  332. et := fmt.Sprint(endTime, " 23:59:59")
  333. cond += fmt.Sprintf(` AND (%s BETWEEN ? AND ?)`, yb.PdfColumns.ModifyTime)
  334. pars = append(pars, st, et)
  335. }
  336. var startSize int
  337. pageSize, _ := this.GetInt("PageSize")
  338. currentIndex, _ := this.GetInt("CurrentIndex")
  339. if pageSize <= 0 {
  340. pageSize = utils.PageSize20
  341. }
  342. if currentIndex <= 0 {
  343. currentIndex = 1
  344. }
  345. startSize = paging.StartIndex(currentIndex, pageSize)
  346. pdfOB := new(yb.Pdf)
  347. total, list, e := pdfOB.GetPageItemsByCondition(startSize, pageSize, cond, pars, []string{}, "modify_time DESC")
  348. if e != nil {
  349. br.Msg = "获取失败"
  350. br.ErrMsg = "获取PDF列表失败, Err: " + e.Error()
  351. return
  352. }
  353. respList := make([]*yb.PdfItem, 0)
  354. for i := range list {
  355. respList = append(respList, &yb.PdfItem{
  356. PdfId: list[i].PdfId,
  357. PdfName: list[i].PdfName,
  358. OriginName: list[i].OriginName,
  359. PdfUrl: list[i].PdfUrl,
  360. ShareUrl: list[i].ShareUrl,
  361. SysAdminId: list[i].SysAdminId,
  362. SysAdminName: list[i].SysAdminName,
  363. CreateTime: list[i].CreateTime.Format(utils.FormatDateTime),
  364. ModifyTime: list[i].ModifyTime.Format(utils.FormatDateTime),
  365. })
  366. }
  367. page := paging.GetPaging(currentIndex, pageSize, total)
  368. resp := &yb.PdfPageListResp{
  369. Paging: page,
  370. List: respList,
  371. }
  372. br.Ret = 200
  373. br.Success = true
  374. br.Msg = "获取成功"
  375. br.Data = resp
  376. }
  377. // Upload
  378. // @Title 上传PDF
  379. // @Description 上传PDF
  380. // @Param File query file true "PDF文件"
  381. // @Success 200 Ret=200 操作成功
  382. // @router /pdf/upload [post]
  383. func (this *PdfController) Upload() {
  384. br := new(models.BaseResponse).Init()
  385. defer func() {
  386. this.Data["json"] = br
  387. this.ServeJSON()
  388. }()
  389. sysUser := this.SysUser
  390. if sysUser == nil {
  391. br.Msg = "请登录"
  392. br.ErrMsg = "请登录,SysUser Is Empty"
  393. br.Ret = 408
  394. return
  395. }
  396. f, h, e := this.GetFile("file")
  397. if e != nil {
  398. br.Msg = "获取资源信息失败"
  399. br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
  400. return
  401. }
  402. defer func() {
  403. _ = f.Close()
  404. }()
  405. ext := path.Ext(h.Filename)
  406. if ext != ".pdf" {
  407. br.Msg = "请上传pdf文件"
  408. return
  409. }
  410. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  411. if e = os.MkdirAll(uploadDir, 766); e != nil {
  412. br.Msg = "存储目录创建失败"
  413. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  414. return
  415. }
  416. fileName := utils.GetRandStringNoSpecialChar(28) + ext
  417. filePath := uploadDir + "/" + fileName
  418. if e = this.SaveToFile("file", filePath); e != nil {
  419. br.Msg = "文件保存失败"
  420. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  421. return
  422. }
  423. defer func() {
  424. _ = os.Remove(filePath)
  425. }()
  426. // 上传到阿里云
  427. ossDir := "static/yb/pdf/"
  428. pdfUrl, e := services.UploadAliyunToDir(fileName, filePath, ossDir, fmt.Sprint(utils.RunMode, "/"))
  429. if e != nil {
  430. br.Msg = "文件上传失败"
  431. br.ErrMsg = "文件上传失败, Err:" + e.Error()
  432. return
  433. }
  434. br.Msg = "上传成功"
  435. br.Ret = 200
  436. br.Success = true
  437. br.Data = pdfUrl
  438. }