123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- 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("已读成功")
- }
|