eta_business.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/models/eta_business"
  7. "hongze/hongze_task/services/alarm_msg"
  8. "hongze/hongze_task/utils"
  9. "time"
  10. )
  11. // EtaBusinessExpiredRemind ETA商家到期提醒
  12. func EtaBusinessExpiredRemind(cont context.Context) (err error) {
  13. // 频次: 1-1天; 2-7天; 3-15天; 4-30天; 5-60天
  14. frequencyArr := []int{1, 2, 3, 4, 5}
  15. for _, f := range frequencyArr {
  16. _ = HandleEtaBusinessExpiredRemind(f)
  17. time.Sleep(15 * time.Second)
  18. }
  19. return
  20. }
  21. // HandleEtaBusinessExpiredRemind ETA商家到期提醒
  22. func HandleEtaBusinessExpiredRemind(frequency int) (err error) {
  23. defer func() {
  24. if err != nil {
  25. tips := fmt.Sprintf("ETA商家到期提醒失败, frequency: %d, Err: %s", frequency, err.Error())
  26. utils.FileLog.Info("%s", tips)
  27. go alarm_msg.SendAlarmMsg(tips, 3)
  28. }
  29. }()
  30. // 频次对应天数
  31. dayMap := map[int]int{1: 1, 2: 7, 3: 15, 4: 30, 5: 60}
  32. days := dayMap[frequency]
  33. if days <= 0 {
  34. err = fmt.Errorf("提醒频次有误")
  35. return
  36. }
  37. // 获取当前合约N后过期的ETA商家
  38. monthLater := time.Now().Local().AddDate(0, 0, days)
  39. expiredTime := monthLater.Format(utils.FormatDate)
  40. businesses := make([]*eta_business.EtaBusiness, 0)
  41. {
  42. businessOb := new(eta_business.EtaBusiness)
  43. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.ExpiredTime)
  44. pars := make([]interface{}, 0)
  45. pars = append(pars, expiredTime)
  46. list, e := businessOb.GetItemsByCondition(cond, pars, []string{}, "")
  47. if e != nil {
  48. err = fmt.Errorf("获取商家列表失败, Err: " + e.Error())
  49. return
  50. }
  51. businesses = list
  52. }
  53. if len(businesses) == 0 {
  54. return
  55. }
  56. // 获取销售
  57. sellers, e := models.GetSellersOpenId()
  58. if e != nil {
  59. err = fmt.Errorf("获取销售信息失败, Err: " + e.Error())
  60. return
  61. }
  62. // 以销售为单位
  63. sellerBusinessMap := make(map[int][]*eta_business.EtaBusiness)
  64. for _, v := range businesses {
  65. if sellerBusinessMap[v.SellerId] == nil {
  66. sellerBusinessMap[v.SellerId] = make([]*eta_business.EtaBusiness, 0)
  67. }
  68. sellerBusinessMap[v.SellerId] = append(sellerBusinessMap[v.SellerId], v)
  69. }
  70. sellerMap := make(map[int]*models.Sellers)
  71. for _, s := range sellers {
  72. sellerMap[s.AdminId] = s
  73. }
  74. // 推送邮件和公众号
  75. remindRecords := make([]*eta_business.EtaBusinessRemindRecord, 0)
  76. for k, v := range sellerBusinessMap {
  77. seller := sellerMap[k]
  78. if seller == nil {
  79. continue
  80. }
  81. if seller.Email == "" && seller.OpenId == "" {
  82. continue
  83. }
  84. if len(v) == 0 {
  85. continue
  86. }
  87. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), frequency, utils.GetRandDigit(5))
  88. contentRemark := ""
  89. contentsEmail := fmt.Sprintf(`<div><p>您有【%d】ETA客户将于%d天后到期,请注意查看</p>`, len(v), days)
  90. contentsEmail += fmt.Sprintf(`<table border='1'><tr><td width='200'>%d天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>`, days)
  91. for _, bz := range v {
  92. row := fmt.Sprintf(`<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>`, bz.BusinessName, bz.ExpiredTime.Format(utils.FormatDate), bz.SellerName, "ETA")
  93. contentsEmail += row
  94. contentRemark += fmt.Sprintf("【%s】", bz.BusinessName)
  95. // 数据入库
  96. remindRecords = append(remindRecords, &eta_business.EtaBusinessRemindRecord{
  97. Type: frequency,
  98. SellerId: seller.AdminId,
  99. SellerName: seller.RealName,
  100. EtaBusinessId: bz.EtaBusinessId,
  101. BusinessName: bz.BusinessName,
  102. EndDate: bz.ExpiredTime.Format(utils.FormatDate),
  103. UniqueCode: uniqueCode,
  104. CreateTime: time.Now(),
  105. })
  106. }
  107. contentsEmail += "</table></div>"
  108. // 邮件
  109. if seller.Email != "" {
  110. msg := fmt.Sprintf("到期前%d天提醒", days)
  111. utils.SendEmailByHongze(msg, contentsEmail, seller.Email, "", "")
  112. }
  113. // 公众号, first和remark已经无效了
  114. if seller.OpenId != "" {
  115. first := fmt.Sprintf(`您有【%d】 客户将于%d天后到期,请注意查看`, len(v), days)
  116. keyword1 := fmt.Sprintf(`【%d】ETA到期前%d天提醒,点击查看`, len(v), days)
  117. keyword2 := expiredTime
  118. remark := contentRemark
  119. openIdList := make([]*models.OpenIdList, 0)
  120. openIdItem := new(models.OpenIdList)
  121. openIdItem.OpenId = seller.OpenId
  122. openIdList = append(openIdList, openIdItem)
  123. _ = SendWxMsgWithEtaBusinessRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  124. }
  125. // 数据入库
  126. if len(remindRecords) > 0 {
  127. recordOb := new(eta_business.EtaBusinessRemindRecord)
  128. if e = recordOb.CreateMulti(remindRecords); e != nil {
  129. err = fmt.Errorf("批量新增ETA商家提醒记录失败, Err: %s", e.Error())
  130. return
  131. }
  132. }
  133. }
  134. return
  135. }
  136. // EtaBusinessUpdateStatus 每日更新ETA商家签约状态
  137. func EtaBusinessUpdateStatus(cont context.Context) (err error) {
  138. defer func() {
  139. if err != nil {
  140. tips := "ETA商家签约状态更新: EtaBusinessUpdateStatus, Err: " + err.Error()
  141. utils.FileLog.Info("%s", tips)
  142. go alarm_msg.SendAlarmMsg(tips, 3)
  143. }
  144. }()
  145. // 获取所有商家
  146. businesses := make([]*eta_business.EtaBusiness, 0)
  147. {
  148. ob := new(eta_business.EtaBusiness)
  149. cond := ``
  150. pars := make([]interface{}, 0)
  151. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  152. if e != nil {
  153. err = fmt.Errorf("获取商家列表失败, Err: " + e.Error())
  154. return
  155. }
  156. businesses = list
  157. }
  158. if len(businesses) == 0 {
  159. return
  160. }
  161. // 获取所有商家签约
  162. contracts := make([]*eta_business.EtaBusinessContract, 0)
  163. {
  164. ob := new(eta_business.EtaBusinessContract)
  165. cond := ``
  166. pars := make([]interface{}, 0)
  167. list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
  168. if e != nil {
  169. err = fmt.Errorf("获取商家合同列表失败, Err: " + e.Error())
  170. return
  171. }
  172. contracts = list
  173. }
  174. if len(contracts) == 0 {
  175. return
  176. }
  177. businessContracts := make(map[int][]*eta_business.EtaBusinessContract) // 商家对应的所有签约
  178. for _, c := range contracts {
  179. if businessContracts[c.EtaBusinessId] == nil {
  180. businessContracts[c.EtaBusinessId] = make([]*eta_business.EtaBusinessContract, 0)
  181. }
  182. businessContracts[c.EtaBusinessId] = append(businessContracts[c.EtaBusinessId], c)
  183. }
  184. // 遍历判断商家当前合约的时间, 更新签约状态
  185. updateBusiness := make([]*eta_business.EtaBusiness, 0)
  186. strToday := time.Now().Format(utils.FormatDate)
  187. today, _ := time.ParseInLocation(utils.FormatDate, strToday, time.Local)
  188. for _, b := range businesses {
  189. cs := businessContracts[b.EtaBusinessId]
  190. // 待签约
  191. if cs == nil || (cs != nil && len(cs) == 0) {
  192. //if b.SigningStatus != eta_business.EtaBusinessSigningStatusTerminate {
  193. // b.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate
  194. // b.ModifyTime = time.Now().Local()
  195. // updateBusiness = append(updateBusiness, b)
  196. //}
  197. continue
  198. }
  199. // 有签约
  200. using := false // 是否在任一存续期内
  201. for _, c := range cs {
  202. // 当前合约
  203. if today.Equal(c.SigningTime) || today.Equal(c.ExpiredTime) || (today.After(c.SigningTime) && today.Before(c.ExpiredTime)) {
  204. b.ContractId = c.EtaBusinessContractId
  205. b.SigningTime = c.SigningTime
  206. b.ExpiredTime = c.ExpiredTime
  207. b.ModifyTime = time.Now().Local()
  208. // 是否为首次签约
  209. if c.IsFirst == 1 {
  210. b.SigningStatus = eta_business.EtaBusinessSigningStatusFirst
  211. } else {
  212. b.SigningStatus = eta_business.EtaBusinessSigningStatusContinue
  213. }
  214. updateBusiness = append(updateBusiness, b)
  215. using = true
  216. break
  217. }
  218. }
  219. // 不存在任一合同期内
  220. if !using {
  221. b.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate
  222. updateBusiness = append(updateBusiness, b)
  223. }
  224. }
  225. // 更新签约状态
  226. if len(updateBusiness) > 0 {
  227. ob := new(eta_business.EtaBusiness)
  228. cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"}
  229. if e := ob.UpdateMulti(updateBusiness, cols); e != nil {
  230. err = fmt.Errorf("批量更新签约状态失败, Err: %s", e.Error())
  231. return
  232. }
  233. }
  234. return
  235. }