company_approval_message.go 10 KB

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