company_product.go 31 KB

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