message.go 14 KB

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