doc.go 9.4 KB

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