company_product.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/services/alarm_msg"
  7. "hongze/hongze_task/services/company"
  8. "hongze/hongze_task/services/cygx"
  9. "hongze/hongze_task/utils"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // 客户自动冻结->试用两个月结束后,进入冻结
  15. func CompanyFreeze(cont context.Context) (err error) {
  16. defer func() {
  17. if err != nil {
  18. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "试用两个月结束后,进入冻结 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  19. }
  20. }()
  21. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  22. items, err := models.GetCompanyNeedFreeze(endDate)
  23. if err != nil {
  24. fmt.Println("GetCompanyNeedFreeze Err:" + err.Error())
  25. utils.FileLog.Info("GetCompanyNeedFreeze Err:%s" + err.Error())
  26. return
  27. }
  28. for k, v := range items {
  29. fmt.Println(k, v.CompanyId)
  30. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  31. if err != nil {
  32. return err
  33. }
  34. if count > 0 {
  35. continue
  36. }
  37. //试用转冻结
  38. companyReportPermissionList, err := models.CompanyFreeze(v.CompanyId, v.ProductId)
  39. if err != nil {
  40. utils.FileLog.Info("CompanyFreeze Err:%s" + err.Error())
  41. return err
  42. }
  43. //新增操作记录
  44. {
  45. remark := "试用转冻结"
  46. operation := "freeze"
  47. approveContent := ""
  48. go AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
  49. v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
  50. //新增 客户产品权限 试用 转 冻结 的日志
  51. for _, v := range companyReportPermissionList {
  52. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
  53. }
  54. }
  55. }
  56. return
  57. }
  58. // 客户自动流失->冻结超3个月未处理
  59. func CompanyLoss(cont context.Context) (err error) {
  60. defer func() {
  61. if err != nil {
  62. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "自动流失->冻结超3个月未处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  63. }
  64. }()
  65. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  66. items, err := models.GetCompanyNeedLoss(endDate)
  67. if err != nil {
  68. fmt.Println("GetCompanyNeedLoss Err:" + err.Error())
  69. utils.FileLog.Info("GetCompanyNeedLoss Err:%s" + err.Error())
  70. return
  71. }
  72. for k, v := range items {
  73. fmt.Println(k, v.CompanyId)
  74. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  75. if err != nil {
  76. return err
  77. }
  78. if count > 0 {
  79. continue
  80. }
  81. err = models.CompanyLoss(v.CompanyId, v.ProductId)
  82. if err != nil {
  83. utils.FileLog.Info("CompanyLoss Err:%s" + err.Error())
  84. return err
  85. }
  86. //新增操作记录
  87. {
  88. remark := "冻结转流失"
  89. operation := "loss"
  90. approveContent := "流失"
  91. AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
  92. v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
  93. }
  94. }
  95. return
  96. }
  97. // 正式客户自动试用->合同到期未续约转试用
  98. func CompanyTryOut(cont context.Context) (err error) {
  99. defer func() {
  100. if err != nil {
  101. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "正式客户自动试用->合同到期未续约转试用 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  102. }
  103. }()
  104. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  105. items, err := models.GetCompanyNeedTryOut(endDate)
  106. if err != nil {
  107. fmt.Println("GetCompanyNeedTryOut Err:" + err.Error())
  108. utils.FileLog.Info("GetCompanyNeedTryOut Err:%s" + err.Error())
  109. return
  110. }
  111. for k, v := range items {
  112. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  113. if err != nil {
  114. return err
  115. }
  116. if count > 0 {
  117. continue
  118. }
  119. fmt.Println(k, v.CompanyId)
  120. // 更新专项调研点数,需要在状态变更之前
  121. if v.ProductId == 2 {
  122. cygx.ActivitySpecialCompanyTryOutReduce(v.CompanyId)
  123. }
  124. //客户正式转试用
  125. companyReportPermissionList, err := models.CompanyTryOut(v.CompanyId, v.ProductId)
  126. if err != nil {
  127. utils.FileLog.Info("CompanyLoss Err:%s" + err.Error())
  128. return err
  129. }
  130. //新增操作记录
  131. {
  132. remark := "正式转试用"
  133. operation := "try_out"
  134. approveContent := "试用"
  135. AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
  136. v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
  137. //新增 客户产品权限 正式 转 试用 的日志
  138. for _, v := range companyReportPermissionList {
  139. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
  140. }
  141. }
  142. {
  143. //正式转试用,删除不续约归因内容
  144. company.DeleteCompanNoRenewedAscribe(v.CompanyId, v.ProductId)
  145. }
  146. //正式转试用定时任务更新研选扣点
  147. if v.ProductId == 2 {
  148. cygx.YanXuanCompanyCompanyTryOut(v.CompanyId)
  149. }
  150. { // 更新用户是否签约过
  151. UpdateCompanyProductIsSigning(v.CompanyId, v.ProductId)
  152. }
  153. }
  154. return
  155. }
  156. func AddCompanyOperationRecord(companyId, sysUserId, productId, approveAdminId int, companyName, productName, sysUserRealName, remark, operation, approveContent, approveUserRealName, approveRemark, status string) (err error) {
  157. defer func() {
  158. if err != nil {
  159. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "services:AddCompanyOperationRecord;Err"+err.Error(), utils.EmailSendToUsers)
  160. }
  161. }()
  162. record := new(models.CompanyOperationRecord)
  163. record.CompanyId = companyId
  164. record.CompanyName = companyName
  165. record.SysUserId = sysUserId
  166. record.SysRealName = sysUserRealName
  167. record.Remark = remark
  168. record.Operation = operation
  169. record.CreateTime = time.Now()
  170. record.ProductId = productId
  171. record.ProductName = productName
  172. record.ApproveUserId = approveAdminId
  173. record.ApproveRealName = approveUserRealName
  174. record.ApproveContent = approveContent
  175. record.ApproveRemark = approveRemark
  176. record.Status = status
  177. _, err = models.AddCompanyOperationRecord(record)
  178. return
  179. }
  180. // 客户老数据同步
  181. func CompanyOldDataSync() {
  182. companyItems, err := models.GetCompanyOldDataSync()
  183. if err != nil {
  184. fmt.Println("GetCompanyOldDataSync Err:" + err.Error())
  185. return
  186. }
  187. productId := 1
  188. productName := "ficc"
  189. for k, v := range companyItems {
  190. fmt.Println(k, v.CompanyName, v.CompanyId)
  191. productItem := new(models.CompanyProduct)
  192. productItem.CompanyId = v.CompanyId
  193. productItem.ProductId = productId
  194. productItem.ProductName = productName
  195. productItem.CompanyName = v.CompanyName
  196. //客户标签,1:付费客户,2:试用客户,3:流失客户,4:潜在客户,5:永续
  197. //'试用','永续','冻结','流失','正式','潜在'
  198. status := ""
  199. if v.CompanyType == 1 {
  200. status = "正式"
  201. } else if v.CompanyType == 2 {
  202. status = "试用"
  203. } else if v.CompanyType == 3 {
  204. status = "流失"
  205. } else if v.CompanyType == 4 {
  206. status = "潜在"
  207. } else if v.CompanyType == 5 {
  208. status = "永续"
  209. } else {
  210. fmt.Println("无效的客户状态:"+status, v.CompanyType)
  211. return
  212. }
  213. if status == "" {
  214. fmt.Println("无效的客户状态:"+status, v.CompanyType)
  215. return
  216. }
  217. productItem.Status = status
  218. //获取销售
  219. seller, err := models.GetAdminByAdminId(v.SellsId)
  220. if err != nil && err.Error() != utils.ErrNoRow() {
  221. fmt.Println("获取销售失败,Err:"+err.Error(), v.SellsId)
  222. return
  223. }
  224. var adminId, groupId, departmentId int
  225. var sellerRealName string
  226. if seller == nil {
  227. fmt.Println("销售不存在:", v.SellsId)
  228. //return
  229. } else {
  230. adminId = seller.AdminId
  231. sellerRealName = seller.RealName
  232. groupId = seller.GroupId
  233. departmentId = seller.DepartmentId
  234. }
  235. productItem.SellerId = adminId
  236. productItem.SellerName = sellerRealName
  237. productItem.GroupId = groupId
  238. productItem.DepartmentId = departmentId
  239. productItem.CreateTime = time.Now()
  240. productItem.ModifyTime = time.Now()
  241. productItem.StartDate = v.StartDate
  242. productItem.EndDate = v.EndDate
  243. productItem.LoseReason = v.LoseReason
  244. productItem.LossTime = v.LossTime
  245. _, err = models.AddCompanyProduct(productItem)
  246. if err != nil {
  247. fmt.Println("新增客户产品失败,Err:" + err.Error())
  248. return
  249. }
  250. //权限处理
  251. err = models.ModifyCompanyOldPermission(v.StartDate, v.EndDate, productName, v.CompanyId, productId)
  252. if err != nil {
  253. fmt.Println("客户权限处理失败,Err:" + err.Error())
  254. return
  255. }
  256. if k > 500 {
  257. return
  258. }
  259. }
  260. }
  261. // CompanyRemind 正式/试用客户--到期提醒
  262. func CompanyRemind(cont context.Context) (err error) {
  263. defer func() {
  264. if err != nil {
  265. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  266. }
  267. }()
  268. //获取销售
  269. sellers, err := models.GetSellersOpenId()
  270. for k, v := range sellers {
  271. fmt.Println(k, v.AdminId, v.Mobile)
  272. CompanyRemind60Day(v)
  273. time.Sleep(5 * time.Second)
  274. CompanyRemind30Day(v)
  275. time.Sleep(5 * time.Second)
  276. CompanyRemind15Day(v)
  277. time.Sleep(5 * time.Second)
  278. CompanyRemind7Day(v)
  279. time.Sleep(5 * time.Second)
  280. CompanyRemind1Day(v)
  281. time.Sleep(5 * time.Second)
  282. }
  283. return
  284. }
  285. // CompanyRemind60Day 60天后到期客户
  286. func CompanyRemind60Day(seller *models.Sellers) {
  287. var err error
  288. defer func() {
  289. if err != nil {
  290. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  291. }
  292. }()
  293. companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
  294. remindType := 5
  295. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
  296. //remindEndDate := "2020-12-31" //time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
  297. remindEndDate := time.Now().AddDate(0, 0, 60).Format(utils.FormatDate)
  298. companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
  299. if err != nil {
  300. return
  301. }
  302. emailContents := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于60天后到期,请注意查看</p>"
  303. emailContents += "<table border='1'><tr><td width='200'>60天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
  304. var isSend bool
  305. msgContent := ``
  306. for _, v := range companyItems {
  307. endTime := v.EndDate
  308. if v.Status == "正式" {
  309. endTime = v.ContractEndDate
  310. }
  311. emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
  312. msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
  313. isSend = true
  314. // 数据入库
  315. companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
  316. //CompanyRemindRecordId: 0,
  317. Type: remindType,
  318. SellerId: seller.AdminId,
  319. SellerName: seller.RealName,
  320. CompanyId: v.CompanyId,
  321. CompanyName: v.CompanyName,
  322. Status: v.Status,
  323. EndDate: endTime,
  324. UniqueCode: uniqueCode,
  325. CreateTime: time.Now(),
  326. })
  327. }
  328. emailContents += "</table></br>"
  329. if isSend {
  330. if seller.Email != "" {
  331. utils.SendEmailByHongze("到期前60天提醒", emailContents, seller.Email, "", "")
  332. }
  333. if seller.OpenId != "" {
  334. first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于60天后到期,请注意查看"
  335. //keyword1 := "到期前30天提醒"
  336. keyword1 := fmt.Sprintf(`【%d】客户到期前60天提醒,点击查看`, len(companyItems))
  337. keyword2 := remindEndDate
  338. remark := msgContent
  339. openIdList := make([]*models.OpenIdList, 0)
  340. openIdItem := new(models.OpenIdList)
  341. openIdItem.OpenId = seller.OpenId
  342. openIdList = append(openIdList, openIdItem)
  343. SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  344. }
  345. }
  346. // 数据入库
  347. if len(companyRemindRecordList) > 0 {
  348. models.AddMultiCompanyRemindRecord(companyRemindRecordList)
  349. }
  350. }
  351. // CompanyRemind30Day 30天后到期客户
  352. func CompanyRemind30Day(seller *models.Sellers) {
  353. var err error
  354. defer func() {
  355. if err != nil {
  356. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  357. }
  358. }()
  359. companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
  360. remindType := 4
  361. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
  362. //remindEndDate := "2020-12-31" //time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
  363. remindEndDate := time.Now().AddDate(0, 0, 30).Format(utils.FormatDate)
  364. companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
  365. if err != nil {
  366. return
  367. }
  368. emailContents := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于30天后到期,请注意查看</p>"
  369. emailContents += "<table border='1'><tr><td width='200'>30天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
  370. var isSend bool
  371. msgContent := ``
  372. for _, v := range companyItems {
  373. endTime := v.EndDate
  374. if v.Status == "正式" {
  375. endTime = v.ContractEndDate
  376. }
  377. emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
  378. msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
  379. isSend = true
  380. // 数据入库
  381. companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
  382. //CompanyRemindRecordId: 0,
  383. Type: remindType,
  384. SellerId: seller.AdminId,
  385. SellerName: seller.RealName,
  386. CompanyId: v.CompanyId,
  387. CompanyName: v.CompanyName,
  388. Status: v.Status,
  389. EndDate: endTime,
  390. UniqueCode: uniqueCode,
  391. CreateTime: time.Now(),
  392. })
  393. }
  394. emailContents += "</table></br>"
  395. if isSend {
  396. if seller.Email != "" {
  397. utils.SendEmailByHongze("到期前30天提醒", emailContents, seller.Email, "", "")
  398. }
  399. if seller.OpenId != "" {
  400. first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于30天后到期,请注意查看"
  401. //keyword1 := "到期前30天提醒"
  402. keyword1 := fmt.Sprintf(`【%d】客户到期前30天提醒,点击查看`, len(companyItems))
  403. keyword2 := remindEndDate
  404. remark := msgContent
  405. openIdList := make([]*models.OpenIdList, 0)
  406. openIdItem := new(models.OpenIdList)
  407. openIdItem.OpenId = seller.OpenId
  408. openIdList = append(openIdList, openIdItem)
  409. SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  410. }
  411. }
  412. // 数据入库
  413. if len(companyRemindRecordList) > 0 {
  414. models.AddMultiCompanyRemindRecord(companyRemindRecordList)
  415. }
  416. }
  417. // CompanyRemind15Day 15天后到期客户
  418. func CompanyRemind15Day(seller *models.Sellers) {
  419. var err error
  420. defer func() {
  421. if err != nil {
  422. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  423. }
  424. }()
  425. companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
  426. remindType := 3
  427. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
  428. remindEndDate := time.Now().AddDate(0, 0, 15).Format(utils.FormatDate)
  429. companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
  430. if err != nil {
  431. return
  432. }
  433. var isSend bool
  434. msgContent := ``
  435. emailContents := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于15天后到期,请注意查看</p>"
  436. emailContents += "<table border='1'><tr><td width='200'>15天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
  437. for _, v := range companyItems {
  438. endTime := v.EndDate
  439. if v.Status == "正式" {
  440. endTime = v.ContractEndDate
  441. }
  442. emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
  443. msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
  444. isSend = true
  445. // 数据入库
  446. companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
  447. //CompanyRemindRecordId: 0,
  448. Type: remindType,
  449. SellerId: seller.AdminId,
  450. SellerName: seller.RealName,
  451. CompanyId: v.CompanyId,
  452. CompanyName: v.CompanyName,
  453. Status: v.Status,
  454. EndDate: endTime,
  455. UniqueCode: uniqueCode,
  456. CreateTime: time.Now(),
  457. })
  458. }
  459. emailContents += "</table></br>"
  460. if isSend {
  461. if seller.Email != "" {
  462. utils.SendEmailByHongze("到期前15天提醒", emailContents, seller.Email, "", "")
  463. }
  464. if seller.OpenId != "" {
  465. first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于15天后到期,请注意查看"
  466. //keyword1 := "到期前15天提醒"
  467. keyword1 := fmt.Sprintf(`【%d】客户到期前15天提醒,点击查看`, len(companyItems))
  468. keyword2 := remindEndDate
  469. remark := msgContent
  470. openIdList := make([]*models.OpenIdList, 0)
  471. openIdItem := new(models.OpenIdList)
  472. openIdItem.OpenId = seller.OpenId
  473. openIdList = append(openIdList, openIdItem)
  474. SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  475. }
  476. }
  477. // 数据入库
  478. if len(companyRemindRecordList) > 0 {
  479. models.AddMultiCompanyRemindRecord(companyRemindRecordList)
  480. }
  481. }
  482. // CompanyRemind7Day 7天后到期客户名称
  483. func CompanyRemind7Day(seller *models.Sellers) {
  484. var err error
  485. defer func() {
  486. if err != nil {
  487. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  488. }
  489. }()
  490. companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
  491. remindType := 2
  492. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
  493. remindEndDate := time.Now().AddDate(0, 0, 7).Format(utils.FormatDate)
  494. companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
  495. if err != nil {
  496. return
  497. }
  498. var isSend bool
  499. msgContent := ``
  500. emailContents := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于7天后到期,请注意查看</p>"
  501. emailContents += "<table border='1'><tr><td width='200'>7天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
  502. for _, v := range companyItems {
  503. endTime := v.EndDate
  504. if v.Status == "正式" {
  505. endTime = v.ContractEndDate
  506. }
  507. emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
  508. msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
  509. isSend = true
  510. // 数据入库
  511. companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
  512. //CompanyRemindRecordId: 0,
  513. Type: remindType,
  514. SellerId: seller.AdminId,
  515. SellerName: seller.RealName,
  516. CompanyId: v.CompanyId,
  517. CompanyName: v.CompanyName,
  518. Status: v.Status,
  519. EndDate: endTime,
  520. UniqueCode: uniqueCode,
  521. CreateTime: time.Now(),
  522. })
  523. }
  524. emailContents += "</table></br>"
  525. if isSend {
  526. if seller.Email != "" {
  527. utils.SendEmailByHongze("到期前7天提醒", emailContents, seller.Email, "", "")
  528. }
  529. if seller.OpenId != "" {
  530. first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于7天后到期,请注意查看"
  531. //keyword1 := "到期前7天提醒"
  532. keyword1 := fmt.Sprintf(`【%d】客户到期前7天提醒,点击查看`, len(companyItems))
  533. keyword2 := remindEndDate
  534. remark := msgContent
  535. openIdList := make([]*models.OpenIdList, 0)
  536. openIdItem := new(models.OpenIdList)
  537. openIdItem.OpenId = seller.OpenId
  538. openIdList = append(openIdList, openIdItem)
  539. SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  540. }
  541. }
  542. // 数据入库
  543. if len(companyRemindRecordList) > 0 {
  544. models.AddMultiCompanyRemindRecord(companyRemindRecordList)
  545. }
  546. }
  547. // CompanyRemind1Day到期当天提醒
  548. func CompanyRemind1Day(seller *models.Sellers) {
  549. var err error
  550. defer func() {
  551. if err != nil {
  552. go utils.SendEmail(utils.APPNAME+"失败提醒"+utils.RunMode, "到期提醒失败:CompanyRemind;Err"+err.Error(), utils.EmailSendToUsers)
  553. }
  554. }()
  555. companyRemindRecordList := make([]*models.CompanyRemindRecord, 0)
  556. remindType := 1
  557. uniqueCode := fmt.Sprint(seller.AdminId, time.Now().Format(utils.FormatDateUnSpace), remindType, utils.GetRandString(5))
  558. remindEndDate := time.Now().AddDate(0, 0, 0).Format(utils.FormatDate)
  559. companyItems, err := models.GetRemindCompany(seller.AdminId, remindEndDate)
  560. if err != nil {
  561. return
  562. }
  563. var isSend bool
  564. msgContent := ``
  565. emailContents := "<div><p>您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于今天后到期,请注意查看</p>"
  566. emailContents += "<table border='1'><tr><td width='200'>1天后到期客户名称</td><td width='200'>到期日期</td><td width='200'>销售人员</td><td>客户类型</td></tr>"
  567. for _, v := range companyItems {
  568. endTime := v.EndDate
  569. if v.Status == "正式" {
  570. endTime = v.ContractEndDate
  571. }
  572. emailContents += `<tr><td>` + v.CompanyName + `</td><td>` + endTime + `</td><td>` + seller.RealName + `</td><td>` + v.Status + `</td></tr>`
  573. msgContent += `客户:` + v.CompanyName + ";状态:" + v.Status + "\n"
  574. isSend = true
  575. // 数据入库
  576. companyRemindRecordList = append(companyRemindRecordList, &models.CompanyRemindRecord{
  577. //CompanyRemindRecordId: 0,
  578. Type: remindType,
  579. SellerId: seller.AdminId,
  580. SellerName: seller.RealName,
  581. CompanyId: v.CompanyId,
  582. CompanyName: v.CompanyName,
  583. Status: v.Status,
  584. EndDate: endTime,
  585. UniqueCode: uniqueCode,
  586. CreateTime: time.Now(),
  587. })
  588. }
  589. emailContents += "</table></br>"
  590. if isSend {
  591. if seller.Email != "" {
  592. utils.SendEmailByHongze("到期前1天提醒", emailContents, seller.Email, "", "")
  593. }
  594. if seller.OpenId != "" {
  595. first := "您有【" + strconv.Itoa(len(companyItems)) + "】 客户将于1天后到期,请注意查看"
  596. //keyword1 := "到期前1天提醒"
  597. keyword1 := fmt.Sprintf(`【%d】客户到期前1天提醒,点击查看`, len(companyItems))
  598. keyword2 := remindEndDate
  599. remark := msgContent
  600. openIdList := make([]*models.OpenIdList, 0)
  601. openIdItem := new(models.OpenIdList)
  602. openIdItem.OpenId = seller.OpenId
  603. openIdList = append(openIdList, openIdItem)
  604. SendWxMsgWithCompanyRemind(first, keyword1, keyword2, remark, uniqueCode, openIdList)
  605. }
  606. }
  607. // 数据入库
  608. if len(companyRemindRecordList) > 0 {
  609. models.AddMultiCompanyRemindRecord(companyRemindRecordList)
  610. }
  611. }
  612. // StaticCompanyTryDay 定时任务每天更新客户试用总天数
  613. func StaticCompanyTryDay(cont context.Context) (err error) {
  614. errList := make([]string, 0)
  615. defer func() {
  616. if len(errList) > 0 {
  617. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "定时任务每天更新客户试用总天数 ErrMsg:"+strings.Join(errList, "\n"), utils.EmailSendToUsers)
  618. }
  619. }()
  620. list, err := models.GetCompanyProductTryOutUpdateNoStopGroup()
  621. if err != nil {
  622. fmt.Println("获取客户变更数据失败:", err)
  623. return
  624. }
  625. for _, v := range list {
  626. isAdd := false //是否要增加一天,默认不加
  627. permissionList, tmpErr := models.GetCompanyReportPermissionByStatus(v.CompanyId, v.ProductId, "试用")
  628. if tmpErr != nil {
  629. errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";寻找对应的试用品种失败,Err:", tmpErr))
  630. continue
  631. }
  632. currPermissionIdList := make([]int, 0) //当前试用的品种
  633. for _, permission := range permissionList {
  634. currPermissionIdList = append(currPermissionIdList, permission.ChartPermissionId)
  635. }
  636. //获取所有未停止的记录列表
  637. logList, tmpErr := models.GetCompanyProductTryOutUpdateNoStopListByEndDate(v.CompanyId, v.ProductId)
  638. if tmpErr != nil {
  639. errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";寻找客户的试用日志失败,Err:", tmpErr))
  640. continue
  641. }
  642. logPermissionIdList := make([]int, 0) //当前日志中的试用的品种
  643. for _, log := range logList {
  644. //获取所有未停止的品种试用记录列表
  645. logPermissionList, tmpErr := models.GetCompanyProductTryOutPermissionUpdateNoStopListByEndDate(log.Id)
  646. if tmpErr != nil {
  647. errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";logId:", v.Id, ";寻找客户的试用品种日志失败,Err:", tmpErr))
  648. continue
  649. }
  650. lenLogPermissionList := len(logPermissionList) //当前日志存在试用的品种数量
  651. stopPermission := 0 //当前日志需要停止的品种数量
  652. currTime := time.Now() //当前时间
  653. for _, logPermission := range logPermissionList {
  654. if utils.InArrayByInt(logPermissionIdList, logPermission.ChartPermissionId) {
  655. // 如果已经被其他记录使用了,那么就将当前记录给标记停止
  656. logPermission.IsStop = 1
  657. // 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
  658. if currTime.After(logPermission.EndDate) {
  659. logPermission.RealEndDate = currTime.AddDate(0, 0, -1)
  660. } else {
  661. logPermission.RealEndDate = time.Now()
  662. }
  663. logPermission.Update([]string{"IsStop", "RealEndDate"})
  664. stopPermission++
  665. continue
  666. } else if !utils.InArrayByInt(currPermissionIdList, logPermission.ChartPermissionId) {
  667. // 如果该品种不在当前客户的品种里面,那么也要将当前记录给标记停止
  668. logPermission.IsStop = 1
  669. // 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
  670. if currTime.After(logPermission.EndDate) {
  671. logPermission.RealEndDate = currTime.AddDate(0, 0, -1)
  672. } else {
  673. logPermission.RealEndDate = time.Now()
  674. }
  675. logPermission.Update([]string{"IsStop", "RealEndDate"})
  676. stopPermission++
  677. continue
  678. }
  679. // 说明当天还是试用状态,需要变更实际结束日期
  680. logPermission.RealEndDate = currTime
  681. logPermission.Update([]string{"RealEndDate"})
  682. // 剩下的说明还处于试用状态,需要添加1天试用期,且需要把该品种加入到当前日志中的试用的品种列表
  683. isAdd = true
  684. logPermissionIdList = append(logPermissionIdList, logPermission.ChartPermissionId)
  685. }
  686. //如果当前日志存在试用的品种数量 == 当前日志需要停止的品种数量
  687. // 那么当前日志也是处于停用状态
  688. if lenLogPermissionList == stopPermission {
  689. log.IsStop = 1
  690. // 如果当前时间晚于结束日期,那么实际结束日期就是今天之前一天,否则就是当天
  691. if currTime.After(log.EndDate) {
  692. log.RealEndDate = currTime.AddDate(0, 0, -1)
  693. } else {
  694. log.RealEndDate = time.Now()
  695. }
  696. log.Update([]string{"IsStop", "RealEndDate"})
  697. }
  698. }
  699. // 如果需要添加,那么将该客户品种添加1天
  700. if isAdd {
  701. }
  702. // 更新客户产品的试用天数
  703. tmpErr = models.AddCompanyProductTryOutDayTotal(v.CompanyId, v.ProductId)
  704. if tmpErr != nil {
  705. errList = append(errList, fmt.Sprint("company_id:", v.CompanyId, ";product_id:", v.ProductId, ";logId:", v.Id, ";更新客户产品的试用天数,Err:", tmpErr))
  706. continue
  707. }
  708. }
  709. return
  710. }
  711. // 更新用户是否签约过
  712. func UpdateCompanyProductIsSigning(companyId, productId int) {
  713. var err error
  714. defer func() {
  715. if err != nil {
  716. msg := fmt.Sprint("companyId:", companyId, "productId:", productId)
  717. go alarm_msg.SendAlarmMsg("更新用户是否签约过失败:UpdateCompanyProductIsSigning "+err.Error()+msg, 2)
  718. }
  719. }()
  720. err = models.UpdateCompanyProductIsSigningById(companyId, productId)
  721. return
  722. }
  723. // 权益正式客户自动转X类试用(永续)->合同到期未续约转X类试用(永续)
  724. func CompanyTryOutXClassRai(cont context.Context) (err error) {
  725. defer func() {
  726. if err != nil {
  727. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "正式客户自动试用->合同到期未续约转试用 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  728. }
  729. }()
  730. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  731. items, err := models.GetCompanyNeedTryOutXClassRai(endDate)
  732. if err != nil {
  733. fmt.Println("GetCompanyNeedTryOutRai Err:" + err.Error())
  734. utils.FileLog.Info("GetCompanyNeedTryOutRai Err:%s" + err.Error())
  735. return
  736. }
  737. for k, v := range items {
  738. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  739. if err != nil {
  740. return err
  741. }
  742. if count > 0 {
  743. continue
  744. }
  745. fmt.Println(k, v.CompanyId)
  746. // 更新专项调研点数,需要在状态变更之前
  747. if v.ProductId == 2 {
  748. cygx.ActivitySpecialCompanyTryOutReduce(v.CompanyId)
  749. }
  750. //客户正式转试用
  751. companyReportPermissionList, err := models.CompanyTryOutXClassRai(v.CompanyId, v.ProductId)
  752. if err != nil {
  753. utils.FileLog.Info("CompanyTryOutXClassRai Err:%s" + err.Error())
  754. return err
  755. }
  756. //新增操作记录
  757. {
  758. remark := "正式转X类试用"
  759. operation := "try_out_x_class"
  760. approveContent := "X类试用"
  761. AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
  762. v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
  763. //新增 客户产品权限 正式 转 试用 的日志
  764. for _, v := range companyReportPermissionList {
  765. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
  766. }
  767. }
  768. {
  769. //正式转试用,删除不续约归因内容
  770. company.DeleteCompanNoRenewedAscribe(v.CompanyId, v.ProductId)
  771. }
  772. //正式转试用定时任务更新研选扣点
  773. {
  774. cygx.YanXuanCompanyCompanyTryOut(v.CompanyId)
  775. }
  776. }
  777. return
  778. }
  779. // 客户自动冻结->X类试用(永续)三个月结束后,进入冻结
  780. func CompanyFreezeXClassRai(cont context.Context) (err error) {
  781. defer func() {
  782. if err != nil {
  783. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "X类试用(永续)三个月结束后,进入冻结ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  784. }
  785. }()
  786. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  787. items, err := models.GetCompanyNeedFreezeXClassRai(endDate)
  788. if err != nil {
  789. fmt.Println("GetCompanyNeedFreezeXClassRai Err:" + err.Error())
  790. utils.FileLog.Info("GetCompanyNeedFreezeXClassRai Err:%s" + err.Error())
  791. return
  792. }
  793. fmt.Println(len(items))
  794. //return
  795. for k, v := range items {
  796. fmt.Println(k, v.CompanyId)
  797. count, err := models.GetCompanyApprovalCount(v.CompanyId, v.ProductId)
  798. if err != nil {
  799. return err
  800. }
  801. if count > 0 {
  802. continue
  803. }
  804. //试用转冻结
  805. companyReportPermissionList, err := models.CompanyFreezeXClassRai(v.CompanyId, v.ProductId)
  806. if err != nil {
  807. utils.FileLog.Info("CompanyFreeze Err:%s" + err.Error())
  808. return err
  809. }
  810. //新增操作记录
  811. {
  812. remark := "X类试用转冻结"
  813. operation := "x_class_freeze"
  814. approveContent := ""
  815. go AddCompanyOperationRecord(v.CompanyId, utils.AdminId, v.ProductId, utils.AdminId, v.CompanyName,
  816. v.ProductName, utils.RealName, remark, operation, approveContent, approveContent, "", v.Status)
  817. //新增 客户产品权限 试用 转 冻结 的日志
  818. for _, v := range companyReportPermissionList {
  819. _ = AddCompanyReportPermissionLog(v.CompanyReportPermissionId, v.CompanyId, v.ProductId, utils.AdminId, v.ProductName, utils.RealName, remark, operation, v.Status)
  820. }
  821. }
  822. }
  823. return
  824. }