company_approval_message.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_mobile_admin/models"
  6. "hongze/hongze_mobile_admin/models/tables/admin"
  7. "hongze/hongze_mobile_admin/models/tables/company_approval_message"
  8. "hongze/hongze_mobile_admin/models/tables/company_contract"
  9. "hongze/hongze_mobile_admin/models/tables/company_product"
  10. "hongze/hongze_mobile_admin/models/tables/cygx_config"
  11. "hongze/hongze_mobile_admin/services/alarm_msg"
  12. "hongze/hongze_mobile_admin/utils"
  13. "strings"
  14. "time"
  15. )
  16. // 消息来源类型,1:客户,2:合同,3:用印,4:指标替换,5:问答社区,6:问答评论,7:视频评论,8:线上路演评论
  17. const (
  18. CompanyApprovalMessageSourceTypeByCompany = 1
  19. CompanyApprovalMessageSourceTypeByContract = 2
  20. CompanyApprovalMessageSourceTypeBySeal = 3
  21. CompanyApprovalMessageSourceTypeByEdbInfo = 4
  22. CompanyApprovalMessageSourceTypeByQuestion = 5
  23. CompanyApprovalMessageSourceTypeByQuestionComment = 6
  24. CompanyApprovalMessageSourceTypeByVideoComment = 7
  25. CompanyApprovalMessageSourceTypeByRoadVideoComment = 8
  26. )
  27. // 1:申请消息,2:审批结果,3:消息通知
  28. const (
  29. CompanyApprovalMessageMessageTypeByApply = 1
  30. CompanyApprovalMessageMessageTypeByApprovalResult = 2
  31. CompanyApprovalMessageMessageTypeByMessage = 3
  32. )
  33. // 审批状态,1:待审批,2:已审批,3:已驳回
  34. const (
  35. CompanyApprovalMessageApprovalStatusByPending = 1
  36. CompanyApprovalMessageApprovalStatusByApproved = 2
  37. CompanyApprovalMessageApprovalStatusByRejected = 3
  38. )
  39. // MessageInfo 消息主要内容
  40. type MessageInfo struct {
  41. CompanyName string `json:"company_name"`
  42. ProductId int `json:"product_id"`
  43. CompanyProductStatus string `json:"company_product_status"`
  44. Title string `json:"title"`
  45. Content string `json:"content"`
  46. UserId uint64 `json:"user_id"`
  47. UserName string `json:"user_name"`
  48. CreateTime time.Time `json:"create_time"`
  49. Extra string `json:"extra" description:"附加字段"`
  50. MessageSource string `json:"message_source" description:"消息来源:问答社区/视频社区/线上路演"`
  51. }
  52. // AddCompanyApprovalMessage 添加待办消息
  53. func AddCompanyApprovalMessage(createUserId, receiveUserId, companyId, companyApprovalId, msgType, sourceType, approvalStatus int, companyName, remark, content, mobile, messageInfo string) (err error) {
  54. defer func() {
  55. if err != nil {
  56. fmt.Println("异常提醒:"+utils.RunMode, "给用户发送消息失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  57. //go utils.SendEmail("异常提醒:"+utils.RunMode, "给用户发送消息失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  58. go alarm_msg.SendAlarmMsg("给用户发送消息失败,ERR:"+err.Error(), 3)
  59. }
  60. }()
  61. if msgType == 2 {
  62. company_approval_message.ModifyCompanyApprovalMessage(companyApprovalId, sourceType)
  63. }
  64. msgItem := new(company_approval_message.CompanyApprovalMessage)
  65. msgItem.CreateUserId = createUserId
  66. msgItem.ReceiveUserId = receiveUserId
  67. msgItem.MessageStatus = 0 //消息状态:0未读,1:已读,2:作废
  68. msgItem.Remark = remark
  69. msgItem.Content = content
  70. msgItem.CompanyId = companyId
  71. msgItem.CompanyName = companyName
  72. msgItem.CreateTime = time.Now()
  73. msgItem.ModifyTime = time.Now()
  74. msgItem.CompanyApprovalId = companyApprovalId
  75. msgItem.OperationStatus = 1 //消息状态:1:待审批,2:已审批
  76. msgItem.MessageType = msgType //1:申请消息,2:审批结果,3:消息通知
  77. msgItem.SourceType = sourceType //消息来源,1:客户,2:合同,3:用印
  78. msgItem.ApprovalStatus = approvalStatus //审批状态,1:待审批,2:已审批,3:已驳回
  79. msgItem.MessageInfo = messageInfo //消息主要内容,json数据
  80. err = company_approval_message.AddCompanyApprovalMessage(msgItem)
  81. if mobile != "" {
  82. //跳转地址
  83. redirectUrl := ""
  84. //微信小程序路径
  85. wxAppPath := ``
  86. //微信消息map数据
  87. wxMsgMap := make(map[int]string)
  88. switch msgType {
  89. case 1:
  90. wxMsgMap[0] = "Hi,您有新的审批单待处理"
  91. case 2:
  92. wxMsgMap[0] = "Hi,您有新的审批单已处理"
  93. case 3:
  94. wxMsgMap[0] = "Hi,有新的消息待查看"
  95. wxAppPath = "pages-todomessages/list/list?type=" + fmt.Sprint(sourceType)
  96. }
  97. //审批类型
  98. switch sourceType {
  99. case 2:
  100. wxMsgMap[1] = "合同审批"
  101. case 3:
  102. wxMsgMap[1] = "用印审批"
  103. default:
  104. wxMsgMap[1] = "客户审批"
  105. }
  106. //产生时间
  107. wxMsgMap[2] = time.Now().Format(utils.FormatDateTime)
  108. //任务描述
  109. wxMsgMap[3] = content
  110. //pages-todomessages/list/list?type=1(1:客户,2:合同,3:用印)
  111. if sourceType == 1 { //目前只有客户审批才有手机端审核页面
  112. redirectUrl = utils.TemplateCompanyApplyRedirectUrl
  113. }
  114. if wxAppPath == "" {
  115. //小程序跳转路径map;下标是:来源_消息类型
  116. wxAppPathMap := map[string]string{
  117. "1_1": "pages-approve/custome/list",
  118. "1_2": "pages-approve/custome/detail?id=" + fmt.Sprint(companyApprovalId),
  119. "2_1": "pages-approve/contract/list",
  120. "2_2": "pages-approve/contract/detail?ContractApprovalId=0&ContractId=0&ContractApprovalRecordId=" + fmt.Sprint(companyApprovalId),
  121. "3_1": "pages-approve/seal/list",
  122. "3_2": "pages-approve/seal/detail?ContractApprovalId=0&SealId=0&ContractApprovalRecordId=" + fmt.Sprint(companyApprovalId),
  123. }
  124. key := fmt.Sprint(sourceType, "_", msgType)
  125. if tmpWxAppPath, ok := wxAppPathMap[key]; ok {
  126. wxAppPath = tmpWxAppPath
  127. }
  128. }
  129. go SendCompanyApplyWxTemplateMsg(mobile, redirectUrl, wxAppPath, wxMsgMap)
  130. }
  131. return
  132. }
  133. // AddCompanyApprovalMessageRai 权益销售客户申请转正后,消息群发给所有销售
  134. func AddCompanyApprovalMessageRai(companyId, companyContractId int, applyRealName, companyName string) (err error) {
  135. defer func() {
  136. if err != nil {
  137. fmt.Println("err:", err)
  138. go alarm_msg.SendAlarmMsg("权益销售客户申请转正后,消息群发给所有销售,Err:"+err.Error()+fmt.Sprint("companyId:", companyId, "companyContractId:", companyContractId, "applyRealName:", applyRealName, "companyName:", companyName), 3)
  139. }
  140. }()
  141. companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(companyId, 2)
  142. if err != nil {
  143. return
  144. }
  145. if companyProduct == nil {
  146. return
  147. }
  148. contractDetail, err := company_contract.GetCompanyContractDetail(companyId, 2, companyContractId)
  149. if err != nil {
  150. return
  151. }
  152. var first, keyword1, keyword2, keyword3, keyword4 string
  153. keyword1 = applyRealName + "已签约" + companyName
  154. keyword2 = time.Now().Format(utils.FormatDate)
  155. keyword3 = contractDetail.StartDate
  156. adminList, err := admin.GetSysuserRaiList()
  157. if err != nil {
  158. return
  159. }
  160. var mobiles []string
  161. for _, v := range adminList {
  162. mobiles = append(mobiles, v.Mobile)
  163. }
  164. openIdList, err := models.GetUserRecordListByMobileArr(mobiles)
  165. if err != nil && err.Error() != utils.ErrNoRow() {
  166. return err
  167. }
  168. go SendWxMsgWithRaiSell(first, keyword1, keyword2, keyword3, keyword4, openIdList)
  169. list, e := company_contract.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  170. if e != nil && e.Error() != utils.ErrNoRow() {
  171. err = errors.New("GetCompanyContractPermissionByCompanyContractId, Err: " + e.Error())
  172. return
  173. }
  174. if len(list) == 0 {
  175. return
  176. }
  177. for _, v := range list {
  178. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN || v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  179. openIdList, err = models.GetMfyxUserRecordListByMobileArr(mobiles)
  180. if err != nil && err.Error() != utils.ErrNoRow() {
  181. return err
  182. }
  183. go SendWxCategoryMsgWithRaiSell(companyName, "销售:"+applyRealName, "已签约研选", time.Now().Format(utils.FormatDateTimeMinute2), openIdList)
  184. }
  185. }
  186. return
  187. }
  188. // AddCompanyApprovalMessageWangYang 客户研选行业转正时(王芳审批通过),模板消息提醒汪洋
  189. func AddCompanyApprovalMessageWangYang(companyId, companyContractId int, applyRealName, companyName string) (err error) {
  190. defer func() {
  191. if err != nil {
  192. go alarm_msg.SendAlarmMsg("客户研选行业转正时(王芳审批通过),模板消息提醒汪洋,Err:"+err.Error()+fmt.Sprint("companyId:", companyId, "companyContractId:", companyContractId, "applyRealName:", applyRealName, "companyName:", companyName), 3)
  193. }
  194. }()
  195. var mobiles []string
  196. cnf, e := cygx_config.GetConfigByCode(utils.TPL_MSG_YANXAUN_APPROVAL)
  197. if e != nil {
  198. err = errors.New("GetConfigByCode, Err: " + e.Error())
  199. return
  200. }
  201. mobiles = strings.Split(cnf.ConfigValue, ",")
  202. openIdList, e := models.GetUserRecordListByMobileArr(mobiles)
  203. if e != nil && e.Error() != utils.ErrNoRow() {
  204. err = errors.New("GetUserRecordListByMobileArr, Err: " + e.Error())
  205. return
  206. }
  207. if len(openIdList) == 0 {
  208. return
  209. }
  210. mfyxopenIdList, e := models.GetMfyxUserRecordListByMobileArr(mobiles)
  211. if e != nil && e.Error() != utils.ErrNoRow() {
  212. err = errors.New("GetMfyxUserRecordListByMobileArr, Err: " + e.Error())
  213. return
  214. }
  215. if len(mfyxopenIdList) == 0 {
  216. return
  217. }
  218. list, e := company_contract.GetCompanyContractPermissionByCompanyContractId(companyContractId)
  219. if e != nil && e.Error() != utils.ErrNoRow() {
  220. err = errors.New("GetCompanyContractPermissionByCompanyContractId, Err: " + e.Error())
  221. return
  222. }
  223. if len(list) == 0 {
  224. return
  225. }
  226. var haveDingYue bool // 是否签约了研选订阅
  227. var points float64 // 是否签约了研选扣点包,以及对应的点数
  228. for _, v := range list {
  229. if v.ChartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  230. haveDingYue = true
  231. }
  232. if v.ChartPermissionId == utils.YAN_XUAN_KOU_DIAN_BAO_ID {
  233. points = v.Points
  234. }
  235. }
  236. var keyword1, keyword2, keyword3, keyword4 string
  237. if haveDingYue {
  238. keyword1 = companyName + "(" + applyRealName + ")"
  239. keyword2 = time.Now().Format(utils.FormatDateTime)
  240. keyword3 = "签约【研选订阅】"
  241. go SendWxMsgWithRaiWangYang(keyword1, keyword2, keyword3, keyword4, openIdList)
  242. go SendWxCategoryMsgWithRaiWangYang(companyName, "销售:"+applyRealName, keyword3, time.Now().Format(utils.FormatDateTimeMinute2), mfyxopenIdList)
  243. }
  244. if points > 0 {
  245. keyword1 = companyName + "(" + applyRealName + ")"
  246. keyword2 = time.Now().Format(utils.FormatDateTime)
  247. keyword3 = "签约【研选扣点包(" + fmt.Sprint(points) + "个点)】"
  248. go SendWxMsgWithRaiWangYang(keyword1, keyword2, keyword3, keyword4, openIdList)
  249. go SendWxCategoryMsgWithRaiWangYang(companyName, "销售:"+applyRealName, keyword3, time.Now().Format(utils.FormatDateTimeMinute2), mfyxopenIdList)
  250. }
  251. return
  252. }