message.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_mobile_admin/models/response/message"
  7. "hongze/hongze_mobile_admin/models/tables/company_approval"
  8. "hongze/hongze_mobile_admin/models/tables/company_approval_message"
  9. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  10. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  11. "hongze/hongze_mobile_admin/models/tables/yb_community_question"
  12. "hongze/hongze_mobile_admin/models/tables/yb_community_question_comment"
  13. "hongze/hongze_mobile_admin/services"
  14. "hongze/hongze_mobile_admin/utils"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. //消息模块
  21. type MessageCommon struct {
  22. BaseAuth
  23. }
  24. // @Title 消息列表汇总页
  25. // @Description 消息列表汇总页接口
  26. // @Success 200 {object} []message.SummaryMessage
  27. // @router /summaryMessageList [get]
  28. func (c *MessageCommon) SummaryMessageList() {
  29. sysUser := c.AdminWx
  30. sysUserId := sysUser.AdminId
  31. //SummaryMessageListResp := message.SummaryMessageListResp{}
  32. sourceTypeList := [...]int{1, 2, 3, 5, 6}
  33. messageMap := make(map[int]message.SummaryMessage)
  34. timeList := make([]int, 0)
  35. var err error
  36. commentTypes := []int{services.CompanyApprovalMessageSourceTypeByQuestionComment, services.CompanyApprovalMessageSourceTypeByVideoComment, services.CompanyApprovalMessageSourceTypeByRoadVideoComment}
  37. for _, sourceType := range sourceTypeList {
  38. //最近一条消息
  39. tmpMessage := ``
  40. messageInfo := new(company_approval_message.CompanyApprovalMessageList)
  41. // 来源类型为6-评论时, 查询6,7,8最近的一条消息
  42. if sourceType == 6 {
  43. messageInfo, err = company_approval_message.GetLastMessageBySourceTypes(sysUserId, commentTypes)
  44. } else {
  45. messageInfo, err = company_approval_message.GetLastMessage(sysUserId, sourceType)
  46. }
  47. if err != nil {
  48. if err.Error() == utils.ErrNoRow() {
  49. continue
  50. }
  51. c.FailWithMessage("获取失败", "获取消息失败,Err:"+err.Error())
  52. return
  53. } else {
  54. switch sourceType {
  55. case 5:
  56. content := messageInfo.Content
  57. var tmpMessageInfo services.MessageInfo
  58. if messageInfo.MessageInfo != `` {
  59. tmpErr := json.Unmarshal([]byte(messageInfo.MessageInfo), &tmpMessageInfo)
  60. if tmpErr == nil {
  61. content = tmpMessageInfo.Title
  62. }
  63. }
  64. tmpMessage = fmt.Sprint("待分配提问:", content)
  65. case 6, 7, 8:
  66. content := messageInfo.Content
  67. var tmpMessageInfo services.MessageInfo
  68. if messageInfo.MessageInfo != `` {
  69. tmpErr := json.Unmarshal([]byte(messageInfo.MessageInfo), &tmpMessageInfo)
  70. if tmpErr == nil {
  71. content = tmpMessageInfo.Title
  72. }
  73. }
  74. tmpMessage = fmt.Sprint("待查看评论:", content)
  75. default:
  76. tmpMessage = fmt.Sprint(messageInfo.RealName, " ", messageInfo.CreateTime.Format("2006.01.02"), " ", messageInfo.Content)
  77. }
  78. }
  79. notReadTotal := 0
  80. if sourceType == 6 {
  81. cnt, e := company_approval_message.GetCommentNotReadMessageTotal(sysUserId, commentTypes)
  82. if e != nil {
  83. c.FailWithMessage("获取失败", "获取评论未读消息数失败,Err:"+e.Error())
  84. return
  85. }
  86. notReadTotal = int(cnt)
  87. } else {
  88. nt, e := company_approval_message.GetNotReadMessageTotal(sysUserId, sourceType)
  89. if e != nil {
  90. c.FailWithMessage("获取失败", "获取未读消息数失败,Err:"+e.Error())
  91. return
  92. }
  93. notReadTotal = int(nt)
  94. }
  95. companyMessage := message.SummaryMessage{
  96. Message: tmpMessage,
  97. Total: notReadTotal,
  98. SourceType: sourceType,
  99. }
  100. timeInt := int(messageInfo.CreateTime.Unix())
  101. //这么做避免时间重复了
  102. timeStr := fmt.Sprintf("%d%d", timeInt, messageInfo.Id)
  103. timeInt, _ = strconv.Atoi(timeStr)
  104. messageMap[timeInt] = companyMessage
  105. timeList = append(timeList, timeInt)
  106. //switch sourceType {
  107. //case 1:
  108. // SummaryMessageListResp.CompanyMessage = CompanyMessage
  109. //case 2:
  110. // SummaryMessageListResp.ContractMessage = CompanyMessage
  111. //case 3:
  112. // SummaryMessageListResp.SealMessage = CompanyMessage
  113. //}
  114. }
  115. ///1,根据参数名称的ASCII码表的顺序排序
  116. sort.Ints(timeList)
  117. messageList := make([]message.SummaryMessage, 0)
  118. for _, currTime := range timeList {
  119. messageList = append(messageList, messageMap[currTime])
  120. }
  121. rotate(messageList)
  122. c.OkDetailed(messageList, "获取成功")
  123. }
  124. //切片数据反转
  125. func rotate(s []message.SummaryMessage) {
  126. left, rigth := 0, 0
  127. if len(s)%2 == 0 { //判断长度是奇数还是偶数,根据这个来确定旋转的中心
  128. left = len(s)/2 - 1
  129. rigth = len(s) / 2
  130. } else {
  131. left = len(s)/2 - 1
  132. rigth = len(s)/2 + 1
  133. }
  134. for ; left >= 0 || rigth <= len(s)-1; left, rigth = left-1, rigth+1 {
  135. s[left], s[rigth] = s[rigth], s[left]
  136. }
  137. }
  138. // @Title 消息列表
  139. // @Description 消息列表接口
  140. // @Param SourceType query int true "消息类型"
  141. // @Param CurrentIndex query int true "当前页码"
  142. // @Param pageSize query int true "每页数量"
  143. // @Success 200 {object} message.CompanyApprovalMessageListResp
  144. // @router /list [get]
  145. func (c *MessageCommon) MessageList() {
  146. sysUser := c.AdminWx
  147. sysUserId := sysUser.AdminId
  148. sourceType, err := c.GetInt("SourceType", 1)
  149. if err != nil {
  150. c.FailWithMessage("获取失败", "获取消息类型失败,Err:"+err.Error())
  151. return
  152. }
  153. pageSize, _ := c.GetInt("PageSize")
  154. currentIndex, _ := c.GetInt("CurrentIndex")
  155. var startSize int
  156. if pageSize <= 0 {
  157. pageSize = utils.PageSize20
  158. }
  159. if currentIndex <= 0 {
  160. currentIndex = 1
  161. }
  162. startSize = paging.StartIndex(currentIndex, pageSize)
  163. total, err := company_approval_message.GetCompanyApprovalMessageCount(sysUserId, sourceType)
  164. if err != nil {
  165. c.FailWithMessage("获取失败", "获取消息总数失败,Err:"+err.Error())
  166. return
  167. }
  168. // 消息类型为6-评论时, 查询6-问答评论,7-视频评论,8-路演视频
  169. typeArr := make([]int, 0)
  170. if sourceType == services.CompanyApprovalMessageSourceTypeByQuestionComment {
  171. typeArr = append(typeArr, services.CompanyApprovalMessageSourceTypeByQuestionComment, services.CompanyApprovalMessageSourceTypeByVideoComment, services.CompanyApprovalMessageSourceTypeByRoadVideoComment)
  172. } else {
  173. typeArr = append(typeArr, sourceType)
  174. }
  175. list, err := company_approval_message.GetCompanyApprovalMessageList(sysUserId, startSize, pageSize, typeArr)
  176. if err != nil {
  177. c.FailWithMessage("获取失败", "获取消息列表失败,Err:"+err.Error())
  178. return
  179. }
  180. //将所有未读消息标记为已读
  181. _ = company_approval_message.ModifyAllCompanyApprovalMessageStatus(sysUserId, typeArr)
  182. switch sourceType {
  183. case 5, 6:
  184. err = getMessageInfo(list)
  185. resp := message.CompanyApprovalMessageListResp{
  186. List: list,
  187. Total: total,
  188. }
  189. if err != nil {
  190. c.FailWithMessage("获取失败", "获取问答失败,Err:"+err.Error())
  191. return
  192. }
  193. c.OkDetailed(resp, "获取成功")
  194. return
  195. }
  196. companyApprovalIds := make([]string, 0)
  197. newApprovalRecordIds := make([]string, 0)
  198. companyApprovalMap := make(map[int]*company_approval.CompanyApproval)
  199. newApprovalMap := make(map[int]*contract_approval.ContractApproval)
  200. for _, messageInfo := range list {
  201. if messageInfo.SourceType == 1 {
  202. companyApprovalIds = append(companyApprovalIds, strconv.Itoa(messageInfo.CompanyApprovalId))
  203. } else {
  204. //如果不是文字消息
  205. newApprovalRecordIds = append(newApprovalRecordIds, strconv.Itoa(messageInfo.CompanyApprovalId))
  206. }
  207. }
  208. //客户信息
  209. if len(companyApprovalIds) > 0 {
  210. companyApprovalList, err := company_approval.GetApprovalListByApprovalIds(strings.Join(companyApprovalIds, ","))
  211. if err != nil {
  212. c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error())
  213. }
  214. for _, companyApproval := range companyApprovalList {
  215. companyApprovalMap[companyApproval.CompanyApprovalId] = companyApproval
  216. }
  217. }
  218. //新的审批单信息
  219. if len(newApprovalRecordIds) > 0 {
  220. //审批流程单信息
  221. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordByIds(strings.Join(newApprovalRecordIds, ","))
  222. if err != nil {
  223. c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error())
  224. }
  225. newApprovalIds := make([]string, 0)
  226. approvalRecordMap := make(map[int][]int)
  227. for _, contractApprovalRecord := range contractApprovalRecordList {
  228. newApprovalIds = append(newApprovalIds, strconv.Itoa(contractApprovalRecord.ContractApprovalId))
  229. if _, ok := approvalRecordMap[contractApprovalRecord.ContractApprovalId]; ok {
  230. approvalRecordMap[contractApprovalRecord.ContractApprovalId] = append(approvalRecordMap[contractApprovalRecord.ContractApprovalId], contractApprovalRecord.ContractApprovalRecordId)
  231. } else {
  232. tmpContractApprovalRecordIdList := make([]int, 0)
  233. tmpContractApprovalRecordIdList = append(tmpContractApprovalRecordIdList, contractApprovalRecord.ContractApprovalRecordId)
  234. approvalRecordMap[contractApprovalRecord.ContractApprovalId] = tmpContractApprovalRecordIdList
  235. }
  236. }
  237. //审批单信息
  238. if len(newApprovalIds) > 0 {
  239. contractApprovalList, err := contract_approval.GetContractApprovalByIds(strings.Join(newApprovalIds, ","))
  240. if err != nil {
  241. c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error())
  242. }
  243. for _, contractApproval := range contractApprovalList {
  244. if _, ok := approvalRecordMap[contractApproval.ContractApprovalId]; ok {
  245. for _, contractApprovalRecordId := range approvalRecordMap[contractApproval.ContractApprovalId] {
  246. newApprovalMap[contractApprovalRecordId] = contractApproval
  247. }
  248. }
  249. }
  250. }
  251. }
  252. for _, messageInfo := range list {
  253. approvalInfo := company_approval_message.ApprovalInfo{}
  254. if messageInfo.SourceType == 1 {
  255. if companyApproval, ok := companyApprovalMap[messageInfo.CompanyApprovalId]; ok {
  256. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议
  257. switch companyApproval.ApplyMethod {
  258. case 1:
  259. approvalInfo.Type = "申请转正"
  260. case 2:
  261. approvalInfo.Type = "冻结转试用"
  262. case 3:
  263. approvalInfo.Type = "试用延期"
  264. case 4:
  265. approvalInfo.Type = "原销售申请领取流失客户"
  266. case 5:
  267. approvalInfo.Type = "续约申请"
  268. case 6:
  269. approvalInfo.Type = "补充协议"
  270. }
  271. approvalInfo.ApplyName = companyApproval.ApplyRealName
  272. approvalInfo.ApplyTime = companyApproval.CreateTime
  273. approvalInfo.ApprovalTime = companyApproval.ApproveTime
  274. }
  275. } else {
  276. if newApproval, ok := newApprovalMap[messageInfo.CompanyApprovalId]; ok {
  277. approvalInfo.Type = newApproval.ApplyContent
  278. approvalInfo.ApplyName = newApproval.ApplyUserName
  279. approvalInfo.ApplyTime = newApproval.CreateTime
  280. approvalInfo.ApprovalTime = newApproval.ModifyTime
  281. }
  282. }
  283. messageInfo.ApprovalInfo = approvalInfo
  284. }
  285. //
  286. resp := message.CompanyApprovalMessageListResp{
  287. List: list,
  288. Total: total,
  289. }
  290. c.OkDetailed(resp, "获取成功")
  291. }
  292. // getQuestionMessage 获取问答的消息
  293. func getQuestionMessage(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) {
  294. questionId := make([]string, 0)
  295. for _, v := range companyApprovalMessageList {
  296. questionId = append(questionId, strconv.Itoa(v.CompanyApprovalId))
  297. }
  298. questionList, err := yb_community_question.GetCommunityQuestionListByIds(strings.Join(questionId, ","))
  299. if err != nil {
  300. return
  301. }
  302. approvalInfoMap := make(map[int]company_approval_message.ApprovalInfo)
  303. for _, v := range questionList {
  304. approvalInfo := company_approval_message.ApprovalInfo{
  305. ApplyName: v.RealName,
  306. Title: v.QuestionContent,
  307. ApplyTime: v.CreateTime,
  308. //ApprovalTime: time.Time{},
  309. }
  310. approvalInfoMap[v.CommunityQuestionId] = approvalInfo
  311. }
  312. for _, v := range companyApprovalMessageList {
  313. if tmpApprovalInfo, ok := approvalInfoMap[v.CompanyApprovalId]; ok {
  314. v.ApprovalInfo = tmpApprovalInfo
  315. }
  316. }
  317. return
  318. }
  319. // getQuestionCommentMessage 获取问答评论的消息
  320. func getQuestionCommentMessage(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) {
  321. questionCommentId := make([]string, 0)
  322. for _, v := range companyApprovalMessageList {
  323. questionCommentId = append(questionCommentId, strconv.Itoa(v.CompanyApprovalId))
  324. }
  325. // 获取问答评论
  326. questionCommentList, err := yb_community_question_comment.GetCommunityQuestionCommentListByIds(strings.Join(questionCommentId, ","))
  327. if err != nil {
  328. return
  329. }
  330. approvalInfoMap := make(map[int]company_approval_message.ApprovalInfo)
  331. for _, v := range questionCommentList {
  332. approvalInfo := company_approval_message.ApprovalInfo{
  333. ApplyName: v.RealName,
  334. Title: v.Content,
  335. ApplyTime: v.CreateTime,
  336. Content: `善斌lj`,
  337. //ApprovalTime: time.Time{},
  338. }
  339. approvalInfoMap[int(v.CommunityQuestionCommentId)] = approvalInfo
  340. }
  341. for _, v := range companyApprovalMessageList {
  342. if tmpApprovalInfo, ok := approvalInfoMap[v.CompanyApprovalId]; ok {
  343. tmpApprovalInfo.CompanyName = v.CompanyName
  344. v.ApprovalInfo = tmpApprovalInfo
  345. }
  346. }
  347. return
  348. }
  349. // getMessageInfo 从消息主体中获取的消息
  350. func getMessageInfo(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) {
  351. errMsgList := make([]string, 0)
  352. defer func() {
  353. if len(errMsgList) > 0 {
  354. fmt.Println("errMsg:", strings.Join(errMsgList, "\n"))
  355. }
  356. }()
  357. for _, v := range companyApprovalMessageList {
  358. var tmpMessageInfo services.MessageInfo
  359. if v.MessageInfo != `` {
  360. tmpErr := json.Unmarshal([]byte(v.MessageInfo), &tmpMessageInfo)
  361. if tmpErr != nil {
  362. errMsgList = append(errMsgList, fmt.Sprintf("%d格式化消息失败,err:%s", v.CompanyApprovalId, v.MessageInfo))
  363. continue
  364. }
  365. }
  366. createTime, _ := time.ParseInLocation(utils.FormatDateTime, tmpMessageInfo.CreateTime.Format(utils.FormatDateTime), time.Local)
  367. approvalInfo := company_approval_message.ApprovalInfo{
  368. ApplyName: tmpMessageInfo.UserName,
  369. Title: tmpMessageInfo.Title,
  370. ApplyTime: createTime,
  371. Content: tmpMessageInfo.Content,
  372. CompanyName: tmpMessageInfo.CompanyName,
  373. Extra: tmpMessageInfo.Extra,
  374. MessageSource: tmpMessageInfo.MessageSource,
  375. }
  376. v.ApprovalInfo = approvalInfo
  377. }
  378. return
  379. }
  380. // @Title 消息已读
  381. // @Description 消息已读接口
  382. // @Param request body company.CompanyApprovalMessageReadReq true "type json string"
  383. // @Success Ret=200 申请成功
  384. // @router /read [post]
  385. func (c *MessageCommon) MessageRead() {
  386. //sysUser := c.AdminWx
  387. var req company_approval_message.CompanyApprovalMessageReadReq
  388. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  389. if err != nil {
  390. c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error())
  391. return
  392. }
  393. if req.Id <= 0 {
  394. c.FailWithMessage("参数错误", "参数错误,客户id小于等于0")
  395. return
  396. }
  397. err = company_approval_message.ModifyCompanyApprovalMessageStatus(req.Id)
  398. if err != nil {
  399. c.FailWithMessage("修改失败!", "修改已读失败,Err:"+err.Error())
  400. return
  401. }
  402. c.OkWithMessage("已读成功")
  403. }