message.go 16 KB

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