doc.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. package help_doc
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/help_doc"
  8. "hongze/hz_crm_api/utils"
  9. "html"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/rdlucklib/rdluck_tools/paging"
  14. )
  15. // HelpDocController 帮助文档
  16. type HelpDocController struct {
  17. controllers.BaseAuthController
  18. }
  19. // @Title 新增报告接口
  20. // @Description 新增报告
  21. // @Param request body help_doc.AddHelpDocReq true "type json string"
  22. // @Success 200 {object} models.AddEnglishReportResp
  23. // @router /add [post]
  24. func (this *HelpDocController) Add() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req help_doc.AddHelpDocReq
  38. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  39. if err != nil {
  40. br.Msg = "参数解析异常!"
  41. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  42. return
  43. }
  44. fmt.Println("Id:", req.Id)
  45. if req.Content == "" {
  46. br.Msg = "请输入内容"
  47. return
  48. }
  49. var anchor []byte
  50. if req.AnchorData != nil {
  51. anchor, err = json.Marshal(req.AnchorData)
  52. if err != nil {
  53. br.Msg = "参数解析异常!"
  54. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  55. return
  56. }
  57. }
  58. var recommend []byte
  59. if req.RecommendData != nil {
  60. recommend, err = json.Marshal(req.RecommendData)
  61. if err != nil {
  62. br.Msg = "参数解析异常!"
  63. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  64. return
  65. }
  66. }
  67. var resp help_doc.HelpDocSaveResp
  68. resp.HelpDocId = req.Id
  69. resp.ModifyTime = time.Now().Format(utils.FormatDateTime)
  70. item := new(help_doc.HelpDoc)
  71. item.ClassifyId = req.ClassifyId
  72. item.Title = req.Title
  73. item.Author = req.Author
  74. item.Status = req.Status
  75. item.Content = html.EscapeString(req.Content)
  76. item.CreateTime = time.Now()
  77. item.ModifyTime = time.Now()
  78. item.AdminId = sysUser.AdminId
  79. item.AdminRealName = sysUser.RealName
  80. item.Anchor = string(anchor)
  81. item.Recommend = string(recommend)
  82. if req.Status == 2 {
  83. item.PublishTime = time.Now()
  84. }
  85. if req.Id > 0 {
  86. tmpHelpDoc, err := help_doc.GetHelpDocById(int(req.Id))
  87. if err != nil {
  88. br.Msg = "保存失败"
  89. br.ErrMsg = "查询帮助文档失败,Err:" + err.Error()
  90. return
  91. }
  92. if tmpHelpDoc.Status != req.Status {
  93. req.IsChange = true
  94. }
  95. }
  96. if req.Id == 0 {
  97. id, e := help_doc.AddHelpDoc(item)
  98. if e != nil {
  99. err = e
  100. br.Msg = "新增失败"
  101. br.ErrMsg = "保存失败,Err:" + err.Error()
  102. return
  103. }
  104. resp.HelpDocId = id
  105. resp.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  106. } else if req.IsChange {
  107. err = help_doc.EditHelpDoc(item, req.Id)
  108. if err != nil {
  109. br.Msg = "保存失败"
  110. br.ErrMsg = "保存失败,Err:" + err.Error()
  111. return
  112. }
  113. }
  114. if req.Status == 1 {
  115. br.Msg = "保存成功"
  116. } else {
  117. br.Msg = "发布成功"
  118. }
  119. br.Ret = 200
  120. br.Success = true
  121. br.Data = resp
  122. }
  123. // @Title 获取报告详情接口
  124. // @Description 获取报告详情
  125. // @Param request body models.ReportDetailReq true "type json string"
  126. // @Success 200 {object} models.EnglishReportDetailView
  127. // @router /detail [get]
  128. func (this *HelpDocController) Detail() {
  129. br := new(models.BaseResponse).Init()
  130. defer func() {
  131. this.Data["json"] = br
  132. this.ServeJSON()
  133. }()
  134. docId, err := this.GetInt("DocId")
  135. if err != nil {
  136. br.Msg = "获取参数失败!"
  137. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  138. return
  139. }
  140. if docId <= 0 {
  141. br.Msg = "参数错误"
  142. return
  143. }
  144. item, err := help_doc.GetHelpDocById(docId)
  145. if err != nil {
  146. br.Msg = "获取失败"
  147. br.ErrMsg = "获取失败,Err:" + err.Error()
  148. return
  149. }
  150. item.Content = html.UnescapeString(item.Content)
  151. var anchor []help_doc.AnchorList
  152. if item.Anchor != "" {
  153. err = json.Unmarshal([]byte(item.Anchor), &anchor)
  154. if err != nil {
  155. br.Msg = "解析失败"
  156. br.ErrMsg = "解析失败,Err:" + err.Error()
  157. return
  158. }
  159. }
  160. var recommend []help_doc.RecommendList
  161. if item.Recommend != "" {
  162. err = json.Unmarshal([]byte(item.Recommend), &recommend)
  163. if err != nil {
  164. br.Msg = "解析失败"
  165. br.ErrMsg = "解析失败,Err:" + err.Error()
  166. return
  167. }
  168. }
  169. resp := help_doc.HelpDocResp{
  170. Id: item.Id,
  171. ClassifyId: item.ClassifyId,
  172. Title: item.Title,
  173. Author: item.Author,
  174. CreateTime: item.CreateTime,
  175. ModifyTime: item.ModifyTime,
  176. Status: item.Status,
  177. PublishTime: item.PublishTime,
  178. Content: item.Content,
  179. AdminId: item.AdminId,
  180. AdminRealName: item.AdminRealName,
  181. Anchor: anchor,
  182. Recommend: recommend,
  183. }
  184. br.Ret = 200
  185. br.Success = true
  186. br.Msg = "获取成功"
  187. br.Data = resp
  188. }
  189. // @Title 获取报告列表接口
  190. // @Description 获取报告列表
  191. // @Param PageSize query int true "每页数据条数"
  192. // @Param CurrentIndex query int true "当前页页码,从1开始"
  193. // @Param KeyWord query string true "搜索关键词"
  194. // @Success 200 {object} models.ReportListResp
  195. // @router /list [get]
  196. func (this *HelpDocController) ListReport() {
  197. br := new(models.BaseResponse).Init()
  198. defer func() {
  199. this.Data["json"] = br
  200. this.ServeJSON()
  201. }()
  202. sysUser := this.SysUser
  203. if sysUser == nil {
  204. br.Msg = "请登录"
  205. br.ErrMsg = "请登录,SysUser Is Empty"
  206. br.Ret = 408
  207. return
  208. }
  209. pageSize, _ := this.GetInt("PageSize")
  210. currentIndex, _ := this.GetInt("CurrentIndex")
  211. classifyIds := this.GetString("ClassifyIds")
  212. keyWord := this.GetString("KeyWord")
  213. var startSize int
  214. if pageSize <= 0 {
  215. pageSize = utils.PageSize20
  216. }
  217. if currentIndex <= 0 {
  218. currentIndex = 1
  219. }
  220. startSize = utils.StartIndex(currentIndex, pageSize)
  221. var condition string
  222. var pars []interface{}
  223. if keyWord != "" {
  224. condition += ` AND title LIKE '%` + keyWord + `%' `
  225. }
  226. if len(classifyIds) > 0 {
  227. classifyIdSlice := strings.Split(classifyIds, ",")
  228. condition += ` AND classify_id IN ( ` + utils.GetOrmInReplace(len(classifyIdSlice)) + ` ) `
  229. pars = append(pars, classifyIdSlice)
  230. }
  231. //拿到所有分类信息,显示路径用
  232. classifyAll, err := help_doc.GetAllHelpDocClassify()
  233. if err != nil {
  234. br.Msg = "获取失败"
  235. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  236. return
  237. }
  238. parentMap := make(map[int]int)
  239. classifyNameMap := make(map[int]string)
  240. for _, v := range classifyAll {
  241. classifyNameMap[v.ClassifyId] = v.ClassifyName
  242. if v.ParentId != 0 {
  243. parentMap[v.ClassifyId] = v.ParentId
  244. }
  245. }
  246. total, err := help_doc.GetHelpDocListCount(condition, pars)
  247. if err != nil {
  248. br.Msg = "获取失败"
  249. br.ErrMsg = "获取失败,Err:" + err.Error()
  250. return
  251. }
  252. list, err := help_doc.GetHelpDocList(condition, pars, startSize, pageSize)
  253. if err != nil {
  254. br.Msg = "获取失败"
  255. br.ErrMsg = "获取失败,Err:" + err.Error()
  256. return
  257. }
  258. resp := new(help_doc.HelpDocListResp)
  259. for _, item := range list {
  260. var anchor []help_doc.AnchorList
  261. if item.Anchor != "" {
  262. err = json.Unmarshal([]byte(item.Anchor), &anchor)
  263. if err != nil {
  264. br.Msg = "解析失败"
  265. br.ErrMsg = "解析失败,Err:" + err.Error()
  266. return
  267. }
  268. }
  269. var recommend []help_doc.RecommendList
  270. if item.Recommend != "" {
  271. err = json.Unmarshal([]byte(item.Recommend), &recommend)
  272. if err != nil {
  273. br.Msg = "解析失败"
  274. br.ErrMsg = "解析失败,Err:" + err.Error()
  275. return
  276. }
  277. }
  278. respItem := help_doc.HelpDocResp{
  279. Id: item.Id,
  280. ClassifyId: item.ClassifyId,
  281. Title: item.Title,
  282. Author: item.Author,
  283. CreateTime: item.CreateTime,
  284. ModifyTime: item.ModifyTime,
  285. Status: item.Status,
  286. PublishTime: item.PublishTime,
  287. Content: item.Content,
  288. AdminId: item.AdminId,
  289. AdminRealName: item.AdminRealName,
  290. Anchor: anchor,
  291. Recommend: recommend,
  292. }
  293. if pid, ok := parentMap[respItem.ClassifyId]; ok {
  294. pName := classifyNameMap[pid]
  295. if ppid, ok := parentMap[pid]; ok {
  296. ppName := classifyNameMap[ppid]
  297. respItem.ClassifyName = ppName + "/" + pName + "/" + classifyNameMap[respItem.ClassifyId]
  298. } else {
  299. respItem.ClassifyName = pName + "/" + classifyNameMap[respItem.ClassifyId]
  300. }
  301. } else {
  302. respItem.ClassifyName = classifyNameMap[respItem.ClassifyId]
  303. }
  304. resp.List = append(resp.List, &respItem)
  305. }
  306. page := paging.GetPaging(currentIndex, pageSize, total)
  307. resp.Paging = page
  308. br.Ret = 200
  309. br.Success = true
  310. br.Msg = "获取成功"
  311. br.Data = resp
  312. }
  313. // @Title 发布报告接口
  314. // @Description 发布报告
  315. // @Param request body models.PublishReq true "type json string"
  316. // @Success 200 Ret=200 发布成功
  317. // @router /publish [post]
  318. func (this *HelpDocController) PublishReport() {
  319. br := new(models.BaseResponse).Init()
  320. defer func() {
  321. this.Data["json"] = br
  322. this.ServeJSON()
  323. }()
  324. var req help_doc.PublishReq
  325. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  326. if err != nil {
  327. br.Msg = "参数解析异常!"
  328. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  329. return
  330. }
  331. docId := req.DocId
  332. if docId <= 0 {
  333. br.Msg = "参数错误"
  334. br.ErrMsg = "参数错误,报告id不可为空"
  335. return
  336. }
  337. doc, err := help_doc.GetHelpDocById(docId)
  338. if err != nil {
  339. br.Msg = "获取报告信息失败"
  340. br.ErrMsg = "获取报告信息失败,Err:" + err.Error()
  341. return
  342. }
  343. if doc == nil {
  344. br.Msg = "报告不存在"
  345. return
  346. }
  347. var tmpErr error
  348. if doc.Content == "" {
  349. br.Msg = "报告内容为空,不可发布"
  350. br.ErrMsg = "报告内容为空,不需要生成,report_id:" + strconv.Itoa(doc.Id)
  351. return
  352. }
  353. if tmpErr = help_doc.PublishHelpDocById(docId, req.Status); tmpErr != nil {
  354. br.Msg = "报告发布失败"
  355. br.ErrMsg = "报告发布失败, Err:" + tmpErr.Error() + ", doc_id:" + strconv.Itoa(docId)
  356. return
  357. }
  358. if req.Status == 1 {
  359. br.Msg = "取消发布成功"
  360. } else {
  361. br.Msg = "发布成功"
  362. }
  363. br.Ret = 200
  364. br.Success = true
  365. }
  366. // @Title 删除报告接口
  367. // @Description 删除报告
  368. // @Param request body models.DeleteReq true "type json string"
  369. // @Success 200 Ret=200 删除成功
  370. // @router /delete [post]
  371. func (this *HelpDocController) Delete() {
  372. br := new(models.BaseResponse).Init()
  373. defer func() {
  374. this.Data["json"] = br
  375. this.ServeJSON()
  376. }()
  377. var req help_doc.DeleteReq
  378. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  379. if err != nil {
  380. br.Msg = "参数解析异常!"
  381. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  382. return
  383. }
  384. if req.DocId <= 0 {
  385. br.Msg = "参数错误"
  386. br.ErrMsg = "参数错误,报告id不可为空"
  387. return
  388. }
  389. _, err = help_doc.GetHelpDocById(req.DocId)
  390. if err != nil {
  391. if err.Error() == utils.ErrNoRow() {
  392. br.Msg = "报告不存在"
  393. return
  394. }
  395. br.Msg = "查询报告信息失败"
  396. br.ErrMsg = "查询报告信息失败, Err" + err.Error()
  397. return
  398. }
  399. err = help_doc.DeleteHelpDoc(req.DocId)
  400. if err != nil {
  401. br.Msg = "删除报告失败"
  402. br.ErrMsg = "删除报告失败, Err" + err.Error()
  403. return
  404. }
  405. br.Ret = 200
  406. br.Success = true
  407. br.Msg = "删除成功"
  408. }