package controllers import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hongze_mobile_admin/models/response/message" "hongze/hongze_mobile_admin/models/tables/business_trip" "hongze/hongze_mobile_admin/models/tables/company_approval" "hongze/hongze_mobile_admin/models/tables/company_approval_message" "hongze/hongze_mobile_admin/models/tables/contract_approval" "hongze/hongze_mobile_admin/models/tables/contract_approval_record" "hongze/hongze_mobile_admin/models/tables/seal" "hongze/hongze_mobile_admin/models/tables/yb_community_question" "hongze/hongze_mobile_admin/models/tables/yb_community_question_comment" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/utils" "sort" "strconv" "strings" "time" ) // 消息模块 type MessageCommon struct { BaseAuth } // @Title 消息列表汇总页 // @Description 消息列表汇总页接口 // @Success 200 {object} []message.SummaryMessage // @router /summaryMessageList [get] func (c *MessageCommon) SummaryMessageList() { sysUser := c.AdminWx sysUserId := sysUser.AdminId //SummaryMessageListResp := message.SummaryMessageListResp{} //sourceTypeList := [...]int{1, 2, 3, 5, 6} sourceTypeList := [...]int{1, 2, 3, 6, 10} // 问答的暂时不要了 messageMap := make(map[int]message.SummaryMessage) timeList := make([]int, 0) var err error commentTypes := []int{services.CompanyApprovalMessageSourceTypeByQuestionComment, services.CompanyApprovalMessageSourceTypeByVideoComment, services.CompanyApprovalMessageSourceTypeByRoadVideoComment} for _, sourceType := range sourceTypeList { //最近一条消息 tmpMessage := `` messageInfo := new(company_approval_message.CompanyApprovalMessageList) // 来源类型为6-评论时, 查询6,7,8最近的一条消息 if sourceType == 6 { messageInfo, err = company_approval_message.GetLastMessageBySourceTypes(sysUserId, commentTypes) } else { messageInfo, err = company_approval_message.GetLastMessage(sysUserId, sourceType) } if err != nil { if err.Error() == utils.ErrNoRow() { continue } c.FailWithMessage("获取失败", "获取消息失败,Err:"+err.Error()) return } else { switch sourceType { case 5: content := messageInfo.Content var tmpMessageInfo services.MessageInfo if messageInfo.MessageInfo != `` { tmpErr := json.Unmarshal([]byte(messageInfo.MessageInfo), &tmpMessageInfo) if tmpErr == nil { content = tmpMessageInfo.Title } } tmpMessage = fmt.Sprint("待分配提问:", content) case 6, 7, 8: content := messageInfo.Content var tmpMessageInfo services.MessageInfo if messageInfo.MessageInfo != `` { tmpErr := json.Unmarshal([]byte(messageInfo.MessageInfo), &tmpMessageInfo) if tmpErr == nil { content = tmpMessageInfo.Title } } tmpMessage = fmt.Sprint("待查看评论:", content) default: tmpMessage = fmt.Sprint(messageInfo.RealName, " ", messageInfo.CreateTime.Format("2006.01.02"), " ", messageInfo.Content) } } notReadTotal := 0 if sourceType == 6 { cnt, e := company_approval_message.GetCommentNotReadMessageTotal(sysUserId, commentTypes) if e != nil { c.FailWithMessage("获取失败", "获取评论未读消息数失败,Err:"+e.Error()) return } notReadTotal = int(cnt) } else { nt, e := company_approval_message.GetNotReadMessageTotal(sysUserId, sourceType) if e != nil { c.FailWithMessage("获取失败", "获取未读消息数失败,Err:"+e.Error()) return } notReadTotal = int(nt) } companyMessage := message.SummaryMessage{ Message: tmpMessage, Total: notReadTotal, SourceType: sourceType, } timeInt := int(messageInfo.CreateTime.Unix()) //这么做避免时间重复了 timeStr := fmt.Sprintf("%d%d", timeInt, messageInfo.Id) timeInt, _ = strconv.Atoi(timeStr) messageMap[timeInt] = companyMessage timeList = append(timeList, timeInt) //switch sourceType { //case 1: // SummaryMessageListResp.CompanyMessage = CompanyMessage //case 2: // SummaryMessageListResp.ContractMessage = CompanyMessage //case 3: // SummaryMessageListResp.SealMessage = CompanyMessage //} } ///1,根据参数名称的ASCII码表的顺序排序 sort.Ints(timeList) messageList := make([]message.SummaryMessage, 0) for _, currTime := range timeList { messageList = append(messageList, messageMap[currTime]) } rotate(messageList) c.OkDetailed(messageList, "获取成功") } // 切片数据反转 func rotate(s []message.SummaryMessage) { left, rigth := 0, 0 if len(s)%2 == 0 { //判断长度是奇数还是偶数,根据这个来确定旋转的中心 left = len(s)/2 - 1 rigth = len(s) / 2 } else { left = len(s)/2 - 1 rigth = len(s)/2 + 1 } for ; left >= 0 || rigth <= len(s)-1; left, rigth = left-1, rigth+1 { s[left], s[rigth] = s[rigth], s[left] } } // @Title 消息列表 // @Description 消息列表接口 // @Param SourceType query int true "消息类型" // @Param CurrentIndex query int true "当前页码" // @Param pageSize query int true "每页数量" // @Success 200 {object} message.CompanyApprovalMessageListResp // @router /list [get] func (c *MessageCommon) MessageList() { sysUser := c.AdminWx sysUserId := sysUser.AdminId sourceType, err := c.GetInt("SourceType", 1) if err != nil { c.FailWithMessage("获取失败", "获取消息类型失败,Err:"+err.Error()) return } pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) total, err := company_approval_message.GetCompanyApprovalMessageCount(sysUserId, sourceType) if err != nil { c.FailWithMessage("获取失败", "获取消息总数失败,Err:"+err.Error()) return } // 消息类型为6-评论时, 查询6-问答评论,7-视频评论,8-路演视频 typeArr := make([]int, 0) if sourceType == services.CompanyApprovalMessageSourceTypeByQuestionComment { typeArr = append(typeArr, services.CompanyApprovalMessageSourceTypeByQuestionComment, services.CompanyApprovalMessageSourceTypeByVideoComment, services.CompanyApprovalMessageSourceTypeByRoadVideoComment) } else { typeArr = append(typeArr, sourceType) } list, err := company_approval_message.GetCompanyApprovalMessageList(sysUserId, startSize, pageSize, typeArr) if err != nil { c.FailWithMessage("获取失败", "获取消息列表失败,Err:"+err.Error()) return } //将所有未读消息标记为已读 _ = company_approval_message.ModifyAllCompanyApprovalMessageStatus(sysUserId, typeArr) switch sourceType { case 5, 6: err = getMessageInfo(list) resp := message.CompanyApprovalMessageListResp{ List: list, Total: total, } if err != nil { c.FailWithMessage("获取失败", "获取问答失败,Err:"+err.Error()) return } c.OkDetailed(resp, "获取成功") return } companyApprovalIds := make([]string, 0) newApprovalRecordIds := make([]string, 0) companyApprovalMap := make(map[int]*company_approval.CompanyApproval) newApprovalMap := make(map[int]*contract_approval.ContractApproval) for _, messageInfo := range list { if messageInfo.SourceType == 1 { companyApprovalIds = append(companyApprovalIds, strconv.Itoa(messageInfo.CompanyApprovalId)) } else { //如果不是文字消息 newApprovalRecordIds = append(newApprovalRecordIds, strconv.Itoa(messageInfo.CompanyApprovalId)) } } //客户信息 if len(companyApprovalIds) > 0 { companyApprovalList, err := company_approval.GetApprovalListByApprovalIds(strings.Join(companyApprovalIds, ",")) if err != nil { c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error()) } for _, companyApproval := range companyApprovalList { companyApprovalMap[companyApproval.CompanyApprovalId] = companyApproval } } //新的审批单信息 if len(newApprovalRecordIds) > 0 { //审批流程单信息 contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordByIds(strings.Join(newApprovalRecordIds, ",")) if err != nil { c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error()) } newApprovalIds := make([]string, 0) approvalRecordMap := make(map[int][]int) for _, contractApprovalRecord := range contractApprovalRecordList { newApprovalIds = append(newApprovalIds, strconv.Itoa(contractApprovalRecord.ContractApprovalId)) if _, ok := approvalRecordMap[contractApprovalRecord.ContractApprovalId]; ok { approvalRecordMap[contractApprovalRecord.ContractApprovalId] = append(approvalRecordMap[contractApprovalRecord.ContractApprovalId], contractApprovalRecord.ContractApprovalRecordId) } else { tmpContractApprovalRecordIdList := make([]int, 0) tmpContractApprovalRecordIdList = append(tmpContractApprovalRecordIdList, contractApprovalRecord.ContractApprovalRecordId) approvalRecordMap[contractApprovalRecord.ContractApprovalId] = tmpContractApprovalRecordIdList } } //审批单信息 if len(newApprovalIds) > 0 { contractApprovalList, err := contract_approval.GetContractApprovalByIds(strings.Join(newApprovalIds, ",")) if err != nil { c.FailWithMessage("获取失败", "获取申请列表信息失败,Err:"+err.Error()) } for _, contractApproval := range contractApprovalList { if _, ok := approvalRecordMap[contractApproval.ContractApprovalId]; ok { for _, contractApprovalRecordId := range approvalRecordMap[contractApproval.ContractApprovalId] { newApprovalMap[contractApprovalRecordId] = contractApproval } } } } } for i, messageInfo := range list { approvalInfo := company_approval_message.ApprovalInfo{} if messageInfo.SourceType == 1 { if companyApproval, ok := companyApprovalMap[messageInfo.CompanyApprovalId]; ok { //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议 switch companyApproval.ApplyMethod { case 1: approvalInfo.Type = "申请转正" case 2: approvalInfo.Type = "冻结转试用" case 3: approvalInfo.Type = "试用延期" case 4: approvalInfo.Type = "原销售申请领取流失客户" case 5: approvalInfo.Type = "续约申请" case 6: approvalInfo.Type = "补充协议" } approvalInfo.ApplyName = companyApproval.ApplyRealName approvalInfo.ApplyTime = companyApproval.CreateTime approvalInfo.ApprovalTime = companyApproval.ApproveTime } } else if messageInfo.SourceType == 10 { applyId := messageInfo.CompanyApprovalId if applyId > 0 { bt, err := business_trip.GetBusinessApplyById(applyId) if err != nil && err.Error() != utils.ErrNoRow() { c.FailWithMessage("获取失败", "获取信息失败,GetBusinessApplyById,Err:"+err.Error()) return } messageInfo.BusinessTripInfo = bt } } else if messageInfo.SourceType == 3 { if newApproval, ok := newApprovalMap[messageInfo.CompanyApprovalId]; ok { approvalInfo.Type = newApproval.ApplyContent approvalInfo.ApplyName = newApproval.ApplyUserName approvalInfo.ApplyTime = newApproval.CreateTime approvalInfo.ApprovalTime = newApproval.ModifyTime var sealItem *seal.Seal err = json.Unmarshal([]byte(newApproval.ContractDetail), &sealItem) if err != nil { return } list[i].AffiliatedCompany = sealItem.AffiliatedCompany } } else { if newApproval, ok := newApprovalMap[messageInfo.CompanyApprovalId]; ok { approvalInfo.Type = newApproval.ApplyContent approvalInfo.ApplyName = newApproval.ApplyUserName approvalInfo.ApplyTime = newApproval.CreateTime approvalInfo.ApprovalTime = newApproval.ModifyTime } } messageInfo.ApprovalInfo = approvalInfo } // resp := message.CompanyApprovalMessageListResp{ List: list, Total: total, } c.OkDetailed(resp, "获取成功") } // getQuestionMessage 获取问答的消息 func getQuestionMessage(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) { questionId := make([]string, 0) for _, v := range companyApprovalMessageList { questionId = append(questionId, strconv.Itoa(v.CompanyApprovalId)) } questionList, err := yb_community_question.GetCommunityQuestionListByIds(strings.Join(questionId, ",")) if err != nil { return } approvalInfoMap := make(map[int]company_approval_message.ApprovalInfo) for _, v := range questionList { approvalInfo := company_approval_message.ApprovalInfo{ ApplyName: v.RealName, Title: v.QuestionContent, ApplyTime: v.CreateTime, //ApprovalTime: time.Time{}, } approvalInfoMap[v.CommunityQuestionId] = approvalInfo } for _, v := range companyApprovalMessageList { if tmpApprovalInfo, ok := approvalInfoMap[v.CompanyApprovalId]; ok { v.ApprovalInfo = tmpApprovalInfo } } return } // getQuestionCommentMessage 获取问答评论的消息 func getQuestionCommentMessage(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) { questionCommentId := make([]string, 0) for _, v := range companyApprovalMessageList { questionCommentId = append(questionCommentId, strconv.Itoa(v.CompanyApprovalId)) } // 获取问答评论 questionCommentList, err := yb_community_question_comment.GetCommunityQuestionCommentListByIds(strings.Join(questionCommentId, ",")) if err != nil { return } approvalInfoMap := make(map[int]company_approval_message.ApprovalInfo) for _, v := range questionCommentList { approvalInfo := company_approval_message.ApprovalInfo{ ApplyName: v.RealName, Title: v.Content, ApplyTime: v.CreateTime, Content: `善斌lj`, //ApprovalTime: time.Time{}, } approvalInfoMap[int(v.CommunityQuestionCommentId)] = approvalInfo } for _, v := range companyApprovalMessageList { if tmpApprovalInfo, ok := approvalInfoMap[v.CompanyApprovalId]; ok { tmpApprovalInfo.CompanyName = v.CompanyName v.ApprovalInfo = tmpApprovalInfo } } return } // getMessageInfo 从消息主体中获取的消息 func getMessageInfo(companyApprovalMessageList []*company_approval_message.CompanyApprovalMessageList) (err error) { errMsgList := make([]string, 0) defer func() { if len(errMsgList) > 0 { fmt.Println("errMsg:", strings.Join(errMsgList, "\n")) } }() for _, v := range companyApprovalMessageList { var tmpMessageInfo services.MessageInfo if v.MessageInfo != `` { tmpErr := json.Unmarshal([]byte(v.MessageInfo), &tmpMessageInfo) if tmpErr != nil { errMsgList = append(errMsgList, fmt.Sprintf("%d格式化消息失败,err:%s", v.CompanyApprovalId, v.MessageInfo)) continue } } createTime, _ := time.ParseInLocation(utils.FormatDateTime, tmpMessageInfo.CreateTime.Format(utils.FormatDateTime), time.Local) approvalInfo := company_approval_message.ApprovalInfo{ ApplyName: tmpMessageInfo.UserName, Title: tmpMessageInfo.Title, ApplyTime: createTime, Content: tmpMessageInfo.Content, CompanyName: tmpMessageInfo.CompanyName, Extra: tmpMessageInfo.Extra, MessageSource: tmpMessageInfo.MessageSource, } v.ApprovalInfo = approvalInfo } return } // @Title 消息已读 // @Description 消息已读接口 // @Param request body company.CompanyApprovalMessageReadReq true "type json string" // @Success Ret=200 申请成功 // @router /read [post] func (c *MessageCommon) MessageRead() { //sysUser := c.AdminWx var req company_approval_message.CompanyApprovalMessageReadReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } if req.Id <= 0 { c.FailWithMessage("参数错误", "参数错误,客户id小于等于0") return } err = company_approval_message.ModifyCompanyApprovalMessageStatus(req.Id) if err != nil { c.FailWithMessage("修改失败!", "修改已读失败,Err:"+err.Error()) return } c.OkWithMessage("已读成功") }