package yb import ( "errors" "fmt" ybResponse "hongze/hongze_mobile_admin/models/response/yb" "hongze/hongze_mobile_admin/models/tables/admin" "hongze/hongze_mobile_admin/models/tables/company" "hongze/hongze_mobile_admin/models/tables/company_approval_message" "hongze/hongze_mobile_admin/models/tables/sys_role" "hongze/hongze_mobile_admin/models/tables/sys_role_admin" "hongze/hongze_mobile_admin/models/tables/variety_classify" "hongze/hongze_mobile_admin/models/tables/variety_tag" "hongze/hongze_mobile_admin/models/tables/wx_user" "hongze/hongze_mobile_admin/models/tables/yb_community_question" "hongze/hongze_mobile_admin/services" "hongze/hongze_mobile_admin/services/alarm_msg" "hongze/hongze_mobile_admin/utils" "strconv" "strings" "time" ) // GetQuestionList 问题列表 func GetQuestionList(condition string, pars []interface{}, startSize, pageSize int) (total int, resp *ybResponse.CommunityQuestionListResp, err error) { resp = new(ybResponse.CommunityQuestionListResp) total, list, e := yb_community_question.GetCommunityQuestionList(condition, pars, startSize, pageSize) if e != nil { err = errors.New("获取问题列表失败 Err:" + e.Error()) return } if len(list) == 0 { return } // 判断是否存在已禁用的回复人, 需要重新分配回复人 adminIdArr := make([]string, 0) questionLen := len(list) for i := 0; i < questionLen; i++ { if list[i].ReplierAdminId > 0 { adminIdArr = append(adminIdArr, strconv.Itoa(list[i].ReplierAdminId)) } } //管理员信息 adminEnableMap := make(map[int]int, 0) if len(adminIdArr) > 0 { adminIds := strings.Join(adminIdArr, ",") adminList, tmpErr := admin.GetAdminListByIds(adminIds) if tmpErr != nil { err = tmpErr return } adminLen := len(adminList) if adminLen > 0 { for i := 0; i < adminLen; i++ { adminEnableMap[adminList[i].AdminId] = adminList[i].Enabled } } } respList := make([]*ybResponse.CommunityQuestionItem, 0) for _, v := range list { // 根据回复人被禁用/待回答/已推送消息判断是否需要重新分配 needReset := false adminEnable := 1 if v.ReplierAdminId > 0 { adminEnable = adminEnableMap[v.ReplierAdminId] } if adminEnable == 0 && v.ReplyStatus == 2 && v.MsgSendStatus >= 1 { needReset = true } item := &ybResponse.CommunityQuestionItem{ CommunityQuestionId: v.CommunityQuestionId, UserId: v.UserId, Mobile: v.Mobile, RealName: v.RealName, QuestionContent: v.QuestionContent, ReplierUserId: v.ReplierUserId, ReplierAdminId: v.ReplierAdminId, ReplierRealName: v.ReplierRealName, ReplierAvatar: v.ReplierAvatar, ResearchGroupFirstId: v.ResearchGroupFirstId, ResearchGroupSecondId: v.ResearchGroupSecondId, ResearchGroupFirstName: v.ResearchGroupFirstName, ResearchGroupSecondName: v.ResearchGroupSecondName, ReplyStatus: v.ReplyStatus, MsgSendStatus: v.MsgSendStatus, NeedRedistribute: needReset, CreateTime: v.CreateTime.Format(utils.FormatDateTime), ReplyTime: v.ReplyTime.Format(utils.FormatDateTime), ClickNum: v.ClickNum, StopReason: v.StopReason, } respList = append(respList, item) } // 数量统计 //countList, e := yb_community_question.GetCommunityQuestionCount() //if e != nil { // err = errors.New("获取问题数量统计失败 Err:" + e.Error()) // return //} //respCount := new(ybResponse.CommunityQuestionCount) //for _, v := range countList { // if v.ReplyStatus == 1 { // respCount.Free = v.Total // continue // } // if v.ReplyStatus == 2 { // respCount.Wait = v.Total // continue // } // if v.ReplyStatus == 3 { // respCount.Replied = v.Total // continue // } //} //respCount.Total = respCount.Free + respCount.Wait + respCount.Replied resp.List = respList //resp.Count = respCount return } // GetQuestionCompanyUser 获取提问者详情 func GetQuestionCompanyUser(questionId int) (ret *ybResponse.QuestionCompanyUser, err error) { questionInfo, e := yb_community_question.GetQuestionById(questionId) if e != nil { err = errors.New("提问信息有误 Err:" + e.Error()) return } companyUserList, err := company.GetFiccCompanyUserByUserIds(strconv.Itoa(questionInfo.UserId)) if err != nil { err = errors.New("获取客户信息失败 Err:" + err.Error()) return } if len(companyUserList) > 0 { item := companyUserList[0] ret = &ybResponse.QuestionCompanyUser{ UserId: int(item.UserId), CompanyStatus: item.Status, CompanyName: item.CompanyName, RealName: item.RealName, CompanyId: item.CompanyId, QuestionContent: questionInfo.QuestionContent, CommunityQuestionId: questionInfo.CommunityQuestionId, } } else { err = errors.New("用户不存在") return } return } // DistributeQuestion 分配回复人 func DistributeQuestion(questionId, adminId, researchGroupFirstId, researchGroupSecondId, distributeId int) (errMsg string, err error) { errMsg = "操作成功" // 校验提问信息 item, e := yb_community_question.GetQuestionById(questionId) if e != nil { errMsg = "分配失败, 提问信息有误" err = errors.New("提问信息有误 Err:" + e.Error()) return } contentRune := []rune(item.QuestionContent) if len(contentRune) > 28 { errMsg = "问题字数不可超过28个字符" err = errors.New("问题字数不可超过28个字符") return } if item.ReplyStatus == 3 { errMsg = "分配失败, 回复状态有误" err = errors.New(fmt.Sprintf("分配失败, 回复状态有误, ReplyStatus:%d", item.ReplyStatus)) return } // 获取研究员信息 adminInfo, e := admin.GetAdminById(adminId) if e != nil { errMsg = "分配失败, 研究员信息有误" err = errors.New("研究员信息有误 Err:" + e.Error()) return } if adminInfo.Mobile == "" { errMsg = "分配失败, 研究员手机号有误" err = errors.New("研究员手机号为空") return } // 获取研究员关联的联系人信息 companyId := 16 userInfo, e := wx_user.GetWxUserByCompanyIdAndMobile(companyId, adminInfo.Mobile) if e != nil { if e.Error() == utils.ErrNoRow() { errMsg = "分配失败, 未找到研究员手机号对应的联系人信息" err = errors.New("未获取到相关联系人信息") return } errMsg = "分配失败" err = errors.New("获取手机号所属联系人失败, Err:" + e.Error()) return } // 回复人openid, 此处可能会出现取不到openid的情况, 可直接忽略掉 replierUserId := int(userInfo.UserId) replierOpenid := "" // 分组、品种权限信息 //firstGroup, e := admin.GetResearchGroupById(researchGroupFirstId) firstGroup, e := variety_classify.GetVarietyClassifyById(researchGroupFirstId) if e != nil { errMsg = "分配失败, 一级分组信息有误" err = errors.New("获取一级分组信息失败, Err:" + e.Error()) return } //secondGroup, e := admin.GetResearchGroupById(researchGroupSecondId) secondGroup, e := variety_tag.GetVarietyTagById(researchGroupSecondId) if e != nil { errMsg = "分配失败, 二级分组信息有误" err = errors.New("获取二级分组信息失败, Err:" + e.Error()) return } // 0627-不绑定品种权限了 //permissionInfo, e := models.GetChartPermissionById(secondGroup.ChartPermissionId) //if e != nil { // errMsg = "分配失败, 品种权限信息有误" // err = errors.New("获取品种权限信息失败, Err:" + e.Error()) // return //} // 更新提问信息 updateCols := make([]string, 0) updateCols = append(updateCols, "ReplierUserId", "ReplierOpenid", "ReplierAdminId", "ReplierRealName", "ReplierAvatar", "ResearchGroupFirstId", "ResearchGroupSecondId", "ResearchGroupFirstName", "ResearchGroupSecondName", "DistributeAdminId", "DistributeTime", "VarietyTagId", "VarietyTagName", "ReplyStatus", "ReplierIsRead", "MsgSendStatus", "ModifyTime") item.ReplierUserId = replierUserId item.ReplierOpenid = replierOpenid item.ReplierAdminId = adminId item.ReplierRealName = adminInfo.RealName item.ReplierAvatar = adminInfo.AdminAvatar item.ResearchGroupFirstId = researchGroupFirstId item.ResearchGroupSecondId = researchGroupSecondId item.ResearchGroupFirstName = firstGroup.ClassifyName item.ResearchGroupSecondName = secondGroup.TagName item.DistributeAdminId = distributeId item.DistributeTime = time.Now().Local() item.VarietyTagId = secondGroup.VarietyTagId item.VarietyTagName = secondGroup.TagName item.ReplyStatus = 2 item.ReplierIsRead = 0 item.MsgSendStatus = 0 item.ModifyTime = time.Now().Local() if e := item.Update(updateCols); e != nil { errMsg = "分配失败,更新提问信息失败" err = errors.New("更新提问信息失败, Err:" + e.Error()) } return } // SoftDeleteQuestion 删除问题 func SoftDeleteQuestion(questionId int) (err error) { if _, e := yb_community_question.GetQuestionById(questionId); e != nil { err = errors.New("提问信息有误 Err:" + e.Error()) return } if e := yb_community_question.DeleteQuestion(questionId); e != nil { err = errors.New("删除提问失败 Err:" + e.Error()) } // 删除问答后的逻辑处理 go afterDeleteQuestion(questionId) return } // SendMsgToReplier 推送消息给回复人 func SendMsgToReplier(questionId int) (errMsg string, err error) { defer func() { if err != nil { go alarm_msg.SendAlarmMsg(fmt.Sprintf("问答分配给研究员后,推送消息失败,问答id:%d;errMsg:%s;ERR:%s", questionId, errMsg, err.Error()), 3) } }() errMsg = "推送成功" item, e := yb_community_question.GetQuestionById(questionId) if e != nil { errMsg = "问答信息有误" err = errors.New("提问信息有误, Err:" + e.Error()) return } if item.MsgSendStatus >= 1 { return } if item.ReplierUserId == 0 { errMsg = "请先分配回复人" err = errors.New("未分配回复人, 不可推送") return } // 回复人openid为空时查询当前是否已有openid updateCols := make([]string, 0) // 找到研究员的手机号,并查到管理员绑定的账号,找到对应的openid adminInfo, e := admin.GetAdminById(item.ReplierAdminId) if e != nil { errMsg = "发送失败, 研究员账号查询失败" err = errors.New("发送失败, 研究员账号查询失败, Err:" + e.Error()) return } if adminInfo.OpenId == "" { errMsg = "该研究员未关注公众号,无法发送消息通知" err = errors.New("回复人openid为空, 不可推送") return } if e := services.SendYbQuestionDistributeWxMsg(item.CommunityQuestionId, item.ReplierUserId, adminInfo.OpenId, item.QuestionContent); e != nil { err = errors.New("推送模板消息失败, Err:" + e.Error()) return } // 更新问答信息 updateCols = append(updateCols, "ModifyTime", "MsgSendStatus") item.ModifyTime = time.Now().Local() item.MsgSendStatus = 1 if e := item.Update(updateCols); e != nil { err = errors.New("更新问答信息失败, Err:" + e.Error()) } return } // EditQuestion 编辑问题 func EditQuestion(questionId int, content string) (errMsg string, err error) { item, e := yb_community_question.GetQuestionById(questionId) if e != nil { errMsg = "提问信息有误" err = errors.New("提问信息有误 Err:" + e.Error()) return } if item.MsgSendStatus >= 1 { errMsg = "问题状态有误,不可编辑" err = errors.New(fmt.Sprintf("问题状态不可编辑, MsgSendStatus:%d", item.MsgSendStatus)) return } updateCols := make([]string, 0) updateCols = append(updateCols, "QuestionContent", "ModifyTime") item.QuestionContent = content item.ModifyTime = time.Now().Local() if e := item.Update(updateCols); e != nil { errMsg = "更新提问信息失败" err = errors.New("更新提问信息失败, Err:" + e.Error()) } return } // CheckCommunityQuestionPermission 查询当前操作用户是否运营管理员权限 func CheckCommunityQuestionPermission(adminId int) (errMsg string, err error) { roleInfo, e := sys_role.GetSysRoleByRoleTypeCode("yb_question_admin") if e != nil { errMsg = "角色信息有误" if e.Error() == utils.ErrNoRow() { err = errors.New("角色不存在, Err: " + e.Error()) return } err = errors.New("角色信息有误, Err: " + e.Error()) return } _, e = sys_role_admin.GetRoleIdsByAdminIdRoleId(adminId, roleInfo.RoleId) if e != nil { errMsg = "无权操作" err = errors.New("无权操作, Err: " + e.Error()) return } return } // afterDeleteQuestion 删除问答后的逻辑处理 func afterDeleteQuestion(questionId int) { var err error defer func() { if err != nil { go alarm_msg.SendAlarmMsg("问答信息删除后,标记站内消息失败"+time.Now().Format(utils.FormatDateTime)+";Err:"+err.Error(), 3) } }() err = company_approval_message.CancelCompanyApprovalMessage(questionId, services.CompanyApprovalMessageSourceTypeByQuestion) return }