message.go 15 KB

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