day_new.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/day_new"
  7. "hongze/hz_crm_api/services"
  8. "hongze/hz_crm_api/services/alarm_msg"
  9. "hongze/hz_crm_api/utils"
  10. "html"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. type DayNewController struct {
  16. BaseAuthController
  17. }
  18. // List
  19. // @Title 每日资讯列表
  20. // @Description 每日资讯列表
  21. // @Success 200 {object} models.WeworkMsgListResp
  22. // @router /day_new/list [get]
  23. func (this *DayNewController) List() {
  24. br := new(models.BaseResponse).Init()
  25. br.IsSendEmail = false
  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 startSize int
  38. pageSize, _ := this.GetInt("PageSize")
  39. currentIndex, _ := this.GetInt("CurrentIndex")
  40. if pageSize <= 0 {
  41. pageSize = utils.PageSize20
  42. }
  43. if currentIndex <= 0 {
  44. currentIndex = 1
  45. }
  46. startSize = paging.StartIndex(currentIndex, pageSize)
  47. cond := " and is_add = 0 and is_delete = 0"
  48. var pars []interface{}
  49. total, list, e := day_new.GetWeworkMsgList(cond, pars, startSize, pageSize)
  50. if e != nil {
  51. br.Msg = "获取失败"
  52. br.ErrMsg = "获取消息列表失败, Err: " + e.Error()
  53. return
  54. }
  55. // 查询消息发送人
  56. userIds := ""
  57. extendUserIds := ""
  58. userNameMap := make(map[string]string, 0)
  59. ExtendUserNameMap := make(map[string]string, 0)
  60. for _, v := range list {
  61. if v.From != "" {
  62. if len(v.From) > 10 && strings.HasPrefix(v.From, "wm") {
  63. extendUserIds += `"` + v.From + `",`
  64. } else {
  65. userIds += `"` + v.From + `",`
  66. }
  67. }
  68. }
  69. if userIds != "" {
  70. userIds = strings.Trim(userIds, ",")
  71. users, err := day_new.GetWeworkUserByUserIds(userIds)
  72. if err != nil {
  73. br.Msg = "查询企业员工信息失败"
  74. br.ErrMsg = "查询企业员工信息失败, Err: " + err.Error()
  75. return
  76. }
  77. for _, v := range users {
  78. userNameMap[v.WwUserId] = v.WwNickName
  79. }
  80. }
  81. if extendUserIds != "" {
  82. extendUserIds = strings.Trim(extendUserIds, ",")
  83. users, err := day_new.GetWeworkUserByExtendUserIds(extendUserIds)
  84. if err != nil {
  85. br.Msg = "查询企业员工信息失败"
  86. br.ErrMsg = "查询企业员工信息失败, Err: " + err.Error()
  87. return
  88. }
  89. for _, v := range users {
  90. ExtendUserNameMap[v.WwExtendUserId] = v.WwNickName
  91. }
  92. }
  93. respList := make([]*day_new.WeworkMsgListItem, 0)
  94. for _, v := range list {
  95. tmp := &day_new.WeworkMsgListItem{
  96. MsgId: v.MsgId,
  97. Content: v.Content,
  98. ContentEn: v.ContentEn,
  99. CreateTime: v.CreateTime.Format(utils.FormatDateTime),
  100. IsAdd: v.IsAdd,
  101. IsDelete: v.IsDelete,
  102. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  103. MsgTime: time.Unix(int64(v.MsgTime/1e3), 0).Format(utils.FormatDateTime),
  104. }
  105. if len(v.From) > 10 && strings.HasPrefix(v.From, "wm") {
  106. if name, ok := ExtendUserNameMap[v.From]; ok {
  107. tmp.FromName = name
  108. }
  109. } else {
  110. if name, ok := userNameMap[v.From]; ok {
  111. tmp.FromName = name
  112. }
  113. }
  114. respList = append(respList, tmp)
  115. }
  116. //查询上次更新时间
  117. lastLog, err := day_new.GetLasReqLog()
  118. if err != nil && err.Error() != utils.ErrNoRow() {
  119. br.Msg = "查询上次刷新时间失败"
  120. br.ErrMsg = "查询上次刷新时间失败, Err: " + err.Error()
  121. return
  122. }
  123. var lastUpdateTime string
  124. if err == nil {
  125. lastUpdateTime = lastLog.CreateTime.Format(utils.FormatDateTime)
  126. }
  127. page := paging.GetPaging(currentIndex, pageSize, total)
  128. resp := &day_new.WeworkMsgListResp{
  129. Paging: page,
  130. List: respList,
  131. LastUpdateTime: lastUpdateTime,
  132. }
  133. br.Ret = 200
  134. br.Success = true
  135. br.Msg = "获取成功"
  136. br.Data = resp
  137. }
  138. // RefreshMsg
  139. // @Title 手动刷新企业微信群消息
  140. // @Description 手动刷新
  141. // @router /day_new/refresh [get]
  142. func (this *DayNewController) RefreshMsg() {
  143. br := new(models.BaseResponse).Init()
  144. br.IsSendEmail = false
  145. defer func() {
  146. this.Data["json"] = br
  147. this.ServeJSON()
  148. }()
  149. sysUser := this.SysUser
  150. if sysUser == nil {
  151. br.Msg = "请登录"
  152. br.ErrMsg = "请登录,SysUser Is Empty"
  153. br.Ret = 408
  154. return
  155. }
  156. err := services.DayNewWeworkMsgRefresh()
  157. if err != nil {
  158. br.Msg = "刷新失败, " + err.Error()
  159. return
  160. }
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "刷新成功"
  164. }
  165. // TranslateContent
  166. // @Title 手动翻译内容
  167. // @Description 手动翻译内容
  168. // @router /day_new/translate [get]
  169. func (this *DayNewController) TranslateContent() {
  170. br := new(models.BaseResponse).Init()
  171. br.IsSendEmail = false
  172. defer func() {
  173. this.Data["json"] = br
  174. this.ServeJSON()
  175. }()
  176. sysUser := this.SysUser
  177. if sysUser == nil {
  178. br.Msg = "请登录"
  179. br.ErrMsg = "请登录,SysUser Is Empty"
  180. br.Ret = 408
  181. return
  182. }
  183. err := services.DayNewTranslateContent()
  184. if err != nil {
  185. br.Msg = "翻译失败," + err.Error()
  186. return
  187. }
  188. br.Ret = 200
  189. br.Success = true
  190. br.Msg = "获取成功"
  191. }
  192. // Delete
  193. // @Title 删除企业微信消息
  194. // @Description 删除企业微信消息
  195. // @Param request body day_new.DeleteWeworkMsgReq true "type json string"
  196. // @Success 200 Ret=200 删除成功
  197. // @router /day_new/delete [post]
  198. func (this *DayNewController) Delete() {
  199. br := new(models.BaseResponse).Init()
  200. defer func() {
  201. this.Data["json"] = br
  202. this.ServeJSON()
  203. }()
  204. var req day_new.DeleteWeworkMsgReq
  205. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  206. if err != nil {
  207. br.Msg = "参数解析异常!"
  208. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  209. return
  210. }
  211. if req.MsgId == "" {
  212. br.Msg = "请选择要删除的消息"
  213. return
  214. }
  215. _, err = day_new.GetWeworkMsgByMsgId(req.MsgId)
  216. if err != nil {
  217. br.Msg = "消息不存在"
  218. return
  219. }
  220. err = day_new.DeleteWeworkMsgByMsgId(req.MsgId)
  221. if err != nil {
  222. br.Msg = "删除失败"
  223. br.ErrMsg = "删除失败,Err:" + err.Error()
  224. return
  225. }
  226. br.Ret = 200
  227. br.Success = true
  228. br.Msg = "删除成功"
  229. }
  230. // EditContentEn
  231. // @Title 编辑英文翻译内容
  232. // @Description 编辑英文翻译内容
  233. // @Param request body day_new.EditWeworkMsgContentReq true "type json string"
  234. // @Success 200 Ret=200 删除成功
  235. // @router /day_new/edit_content_en [post]
  236. func (this *DayNewController) EditContentEn() {
  237. br := new(models.BaseResponse).Init()
  238. defer func() {
  239. this.Data["json"] = br
  240. this.ServeJSON()
  241. }()
  242. var req day_new.EditWeworkMsgContentReq
  243. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  244. if err != nil {
  245. br.Msg = "参数解析异常!"
  246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  247. return
  248. }
  249. if req.MsgId == "" {
  250. br.Msg = "请选择要编辑的消息"
  251. return
  252. }
  253. if req.ContentEn == "" {
  254. br.Msg = "请输入翻译后的内容"
  255. return
  256. }
  257. _, err = day_new.GetWeworkMsgByMsgId(req.MsgId)
  258. if err != nil {
  259. br.Msg = "消息不存在"
  260. return
  261. }
  262. err = day_new.UpdateContentEnByMsgId(req.ContentEn, req.MsgId)
  263. if err != nil {
  264. br.Msg = "更新失败"
  265. br.ErrMsg = "更新失败,Err:" + err.Error()
  266. return
  267. }
  268. br.Ret = 200
  269. br.Success = true
  270. br.Msg = "更新成功"
  271. }
  272. // @Title 新增英文报告接口
  273. // @Description 新增英文报告接口
  274. // @Param request body day_new.AddEnglishReportReq true "type json string"
  275. // @Success 200 {object}
  276. // @router /day_new/add_report [post]
  277. func (this *DayNewController) AddReport() {
  278. br := new(models.BaseResponse).Init()
  279. defer func() {
  280. this.Data["json"] = br
  281. this.ServeJSON()
  282. }()
  283. /*var req day_new.AddEnglishReportReq
  284. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  285. if err != nil {
  286. br.Msg = "参数解析异常!"
  287. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  288. return
  289. }
  290. //校验标题和分类
  291. if req.ClassifyIdFirst == 0 {
  292. br.Msg = "报告分类不能为空"
  293. return
  294. }
  295. if req.ClassifyNameFirst == "" {
  296. br.Msg = "报告分类不能为空"
  297. return
  298. }
  299. if req.Title == "" {
  300. br.Msg = "报告标题不能为空"
  301. return
  302. }
  303. if req.Author == "" {
  304. br.Msg = "作者不能为空"
  305. return
  306. }
  307. if req.Frequency == "" {
  308. br.Msg = "频度不能为空"
  309. return
  310. }*/
  311. sysUser := this.SysUser
  312. if sysUser == nil {
  313. br.Msg = "请登录"
  314. br.ErrMsg = "请登录,SysUser Is Empty"
  315. br.Ret = 408
  316. return
  317. }
  318. dayNewDefault, err := day_new.GetDayNewDefault()
  319. if err != nil {
  320. br.Msg = "获取默认值失败"
  321. br.ErrMsg = "获取默认值失败, Err: " + err.Error()
  322. return
  323. }
  324. overview := dayNewDefault.Overview
  325. var contentSub string
  326. var content string
  327. var msgIds string
  328. // 整理报告内容
  329. {
  330. var cond string
  331. var pars []interface{}
  332. cond = " and is_delete = 0 and is_add = 0"
  333. msgList, e := day_new.GetWeworkMsgByCondition(cond, pars)
  334. if e != nil {
  335. br.Msg = "获取消息列表失败"
  336. br.ErrMsg = "获取消息列表失败, Err: " + e.Error()
  337. return
  338. }
  339. if len(msgList) == 0 {
  340. br.Msg = "生成失败,列表无内容"
  341. return
  342. }
  343. for _, v := range msgList {
  344. if v.ContentEn == "" {
  345. br.Msg = "生成失败,列表未全部完成英文翻译"
  346. return
  347. }
  348. content += `<p>` + time.Unix(v.MsgTime/1e3, 0).Format(utils.FormatDateTimeMinute) + ` ` + v.ContentEn + `</p></br>`
  349. msgIds += `"` + v.MsgId + `",`
  350. }
  351. if content != "" {
  352. contentSub, err = services.GetReportContentSub(content)
  353. if err != nil {
  354. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  355. }
  356. }
  357. }
  358. maxStage, err := models.GetEnglishReportStage(dayNewDefault.ClassifyIdSecond, dayNewDefault.ClassifyIdThird)
  359. if err != nil {
  360. br.Msg = "期数获取失败!"
  361. br.ErrMsg = "期数获取失败,Err:" + err.Error()
  362. return
  363. }
  364. item := new(models.EnglishReport)
  365. item.AddType = 1 // 新增方式:1:新增报告,2:继承报告
  366. item.ClassifyIdFirst = dayNewDefault.ClassifyIdSecond // 报告表中的id_first实际为二级ID
  367. item.ClassifyNameFirst = dayNewDefault.ClassifyNameSecond
  368. item.ClassifyIdSecond = dayNewDefault.ClassifyIdThird // 报告表中的id_second实际为三级ID
  369. item.ClassifyNameSecond = dayNewDefault.ClassifyNameThird
  370. item.Title = dayNewDefault.Title
  371. item.Abstract = dayNewDefault.Abstract
  372. item.Author = dayNewDefault.Author
  373. item.Frequency = dayNewDefault.Frequency
  374. item.State = 1 //状态:1:未发布,2:已发布
  375. item.Content = html.EscapeString(content)
  376. item.Stage = maxStage + 1
  377. item.ContentSub = html.EscapeString(contentSub)
  378. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  379. item.ModifyTime = time.Now()
  380. item.Overview = overview
  381. item.AdminId = sysUser.AdminId
  382. item.AdminRealName = sysUser.RealName
  383. newReportId, err := models.AddEnglishReport(item)
  384. if err != nil {
  385. br.Msg = "保存失败"
  386. br.ErrMsg = "保存失败,Err:" + err.Error()
  387. return
  388. }
  389. reportCode := utils.MD5(strconv.Itoa(int(newReportId)))
  390. //修改唯一编码
  391. {
  392. go models.ModifyEnglishReportCode(newReportId, reportCode)
  393. }
  394. //更新消息状态为已生成报告
  395. if msgIds != "" {
  396. msgIds = strings.Trim(msgIds, ",")
  397. err = day_new.UpdateWeworkMsgIsAdd(msgIds, newReportId)
  398. if err != nil {
  399. br.Msg = "更新消息状态失败"
  400. br.ErrMsg = "更新消息状态失败,Err:" + err.Error()
  401. return
  402. }
  403. }
  404. br.Ret = 200
  405. br.Success = true
  406. br.Msg = "生成成功"
  407. }
  408. // ReportDefault
  409. // @Title 转报告默认设置
  410. // @Description 转报告默认设置
  411. // @Success 200 {object} models.WeworkMsgListResp
  412. // @router /day_new/report_default [get]
  413. func (this *DayNewController) ReportDefault() {
  414. br := new(models.BaseResponse).Init()
  415. br.IsSendEmail = false
  416. defer func() {
  417. this.Data["json"] = br
  418. this.ServeJSON()
  419. }()
  420. sysUser := this.SysUser
  421. if sysUser == nil {
  422. br.Msg = "请登录"
  423. br.ErrMsg = "请登录,SysUser Is Empty"
  424. br.Ret = 408
  425. return
  426. }
  427. dayNewDefault, err := day_new.GetDayNewDefault()
  428. if err != nil {
  429. br.Msg = "获取默认值失败"
  430. br.ErrMsg = "获取默认值失败, Err: " + err.Error()
  431. return
  432. }
  433. defaultReport := &day_new.DayNewReportDefaultResp{
  434. ClassifyIdFirst: dayNewDefault.ClassifyIdFirst,
  435. ClassifyNameFirst: dayNewDefault.ClassifyNameFirst,
  436. ClassifyIdSecond: dayNewDefault.ClassifyIdSecond,
  437. ClassifyNameSecond: dayNewDefault.ClassifyNameSecond,
  438. ClassifyIdThird: dayNewDefault.ClassifyIdThird,
  439. ClassifyNameThird: dayNewDefault.ClassifyNameThird,
  440. Title: dayNewDefault.Title,
  441. Abstract: dayNewDefault.Abstract,
  442. Author: dayNewDefault.Author,
  443. Frequency: dayNewDefault.Frequency,
  444. Overview: dayNewDefault.Overview,
  445. }
  446. br.Ret = 200
  447. br.Success = true
  448. br.Msg = "获取成功"
  449. br.Data = defaultReport
  450. }
  451. // ReportDefaultSave
  452. // @Title 保存-转报告默认设置
  453. // @Description 保存-转报告默认设置
  454. // @Param request body day_new.EnglishReportDefaultSaveReq true "type json string"
  455. // @Success 200 {object}
  456. // @router /day_new/report_default/save [post]
  457. func (this *DayNewController) ReportDefaultSave() {
  458. br := new(models.BaseResponse).Init()
  459. defer func() {
  460. this.Data["json"] = br
  461. this.ServeJSON()
  462. }()
  463. var req day_new.EnglishReportDefaultSaveReq
  464. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  465. if err != nil {
  466. br.Msg = "参数解析异常!"
  467. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  468. return
  469. }
  470. //校验标题和分类
  471. if req.ClassifyIdFirst == 0 {
  472. br.Msg = "报告分类不能为空"
  473. return
  474. }
  475. if req.ClassifyNameFirst == "" {
  476. br.Msg = "报告分类不能为空"
  477. return
  478. }
  479. if req.Title == "" {
  480. br.Msg = "报告标题不能为空"
  481. return
  482. }
  483. if req.Author == "" {
  484. br.Msg = "作者不能为空"
  485. return
  486. }
  487. if req.Frequency == "" {
  488. br.Msg = "频度不能为空"
  489. return
  490. }
  491. if req.Abstract == "" {
  492. br.Msg = "摘要不能为空"
  493. return
  494. }
  495. if req.Overview == "" {
  496. br.Msg = "概述不能为空"
  497. return
  498. }
  499. item := new(day_new.DayNewDefault)
  500. item.ClassifyIdFirst = req.ClassifyIdFirst
  501. item.ClassifyNameFirst = req.ClassifyNameFirst
  502. item.ClassifyIdSecond = req.ClassifyIdSecond
  503. item.ClassifyNameSecond = req.ClassifyNameSecond
  504. item.ClassifyIdThird = req.ClassifyIdThird
  505. item.ClassifyNameThird = req.ClassifyNameThird
  506. item.Title = req.Title
  507. item.Abstract = req.Abstract
  508. item.Author = req.Author
  509. item.Frequency = req.Frequency
  510. item.ModifyTime = time.Now().Format(utils.FormatDateTime)
  511. item.Overview = req.Overview
  512. exist, err := day_new.GetDayNewDefault()
  513. if err != nil {
  514. if err.Error() == utils.ErrNoRow() {
  515. item.CreateTime = time.Now().Format(utils.FormatDateTime)
  516. err = day_new.AddDayNewDefault(item)
  517. if err != nil {
  518. br.Msg = "保存默认值失败"
  519. br.ErrMsg = "保存默认值失败, Err: " + err.Error()
  520. return
  521. }
  522. } else {
  523. br.Msg = "获取默认值失败"
  524. br.ErrMsg = "获取默认值失败, Err: " + err.Error()
  525. return
  526. }
  527. } else {
  528. item.Id = exist.Id
  529. ups := []string{"ClassifyIdFirst", "ClassifyNameFirst", "ClassifyIdSecond", "ClassifyNameSecond", "Title", "Abstract", "Author", "Frequency", "Overview", "ModifyTime", "ClassifyIdThird", "ClassifyNameThird"}
  530. err = item.Update(ups)
  531. if err != nil {
  532. br.Msg = "保存失败"
  533. br.ErrMsg = "保存失败,Err:" + err.Error()
  534. return
  535. }
  536. }
  537. br.Ret = 200
  538. br.Success = true
  539. br.Msg = "保存成功"
  540. }