company_contract.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package company_contract
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_task/models"
  7. "hongze/hongze_task/models/company"
  8. "hongze/hongze_task/models/company_approval"
  9. "hongze/hongze_task/models/company_contract"
  10. "hongze/hongze_task/services/alarm_msg"
  11. "hongze/hongze_task/services/cygx"
  12. "hongze/hongze_task/utils"
  13. "strings"
  14. "time"
  15. )
  16. // HandleCompanyContract 合同处理
  17. func HandleCompanyContract(cont context.Context) (err error) {
  18. defer func() {
  19. if err != nil {
  20. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  21. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  22. go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
  23. }
  24. }()
  25. today := utils.GetToday(utils.FormatDate)
  26. total, list, err := company_contract.GetStartContractListByStartDate(today)
  27. if err != nil {
  28. fmt.Println("HandleCompanyContract Err:" + err.Error())
  29. utils.FileLog.Info("HandleCompanyContract Err:%s" + err.Error())
  30. return
  31. }
  32. fmt.Printf("总共%d条数据待处理\n", total)
  33. errorList := make([]string, 0)
  34. for _, v := range list {
  35. //fmt.Println(k, v.CompanyId)
  36. //fmt.Println(v)
  37. companyApproval, err := company_approval.GetCompanyApprovalByCompanyContractId(v.CompanyContractId)
  38. if err != nil {
  39. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";err:", err))
  40. continue
  41. }
  42. if companyApproval == nil {
  43. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";找不到对应的审批单"))
  44. continue
  45. }
  46. //客户产品信息
  47. companyProduct, err := models.GetCompanyProduct(v.CompanyId, v.ProductId)
  48. if err != nil {
  49. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";寻找客户产品时异常", ";Err:"+err.Error()))
  50. continue
  51. }
  52. if companyProduct == nil {
  53. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";寻找客户产品时异常2", ";Err:"+err.Error()))
  54. continue
  55. }
  56. //companyProduct.EndDate
  57. //格式化客户当前产品结束日期
  58. //companyProductEndDate, err := time.Parse(utils.FormatDate, companyProduct.EndDate)
  59. //if err != nil {
  60. // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";格式化客户当前产品结束时间时异常 err:",err))
  61. // continue
  62. //}
  63. //格式化合同结束日期
  64. //contractEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
  65. //if err != nil {
  66. // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";格式化合同结束日期时异常 err:",err))
  67. // continue
  68. //}
  69. //如果 合同结束日期 小于 客户当前产品结束日期,那么不执行合同内容
  70. //if contractEndDate.Before(companyProductEndDate) {
  71. // errorList = append(errorList,fmt.Sprint("合同id:",v.CompanyContractId,";合同结束日期 小于 客户当前产品结束日期,不执行该合同内容"))
  72. // continue
  73. //}
  74. //申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新
  75. switch companyApproval.ApplyMethod {
  76. case 1:
  77. e := company.TryOutToFormal(v.CompanyId, v.ProductId, utils.AdminId, v.CompanyContractId, v.StartDate, v.EndDate, utils.RealName, v.ProductName, v.PackageType, v.RaiPackageType)
  78. if e != nil {
  79. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";TryOutToFormal Err: ", e.Error()))
  80. }
  81. case 5, 6:
  82. // 续约异常客户记录
  83. tmpErr := contactHandleCompanyRenewalRecord(v, today)
  84. if tmpErr != nil {
  85. errorList = append(errorList, tmpErr.Error())
  86. }
  87. e := company.ApplyServiceUpdate(v.CompanyId, v.ProductId, utils.AdminId, v.CompanyContractId, v.StartDate, v.EndDate, utils.RealName, v.ProductName, v.PackageType, v.RaiPackageType)
  88. if e != nil {
  89. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";ApplyServiceUpdate Err: ", e.Error()))
  90. }
  91. default:
  92. errorList = append(errorList, fmt.Sprint("合同id:", v.CompanyContractId, ";审批单类型异常"))
  93. continue
  94. }
  95. {
  96. updateSource := ``
  97. switch companyApproval.ApplyMethod {
  98. case 1: //试用转正式
  99. updateSource = `turn_positive`
  100. case 2: //冻结->试用
  101. updateSource = "thaw"
  102. case 3: //试用延期
  103. updateSource = "delay"
  104. case 4: //原销售申请领取流失客户
  105. updateSource = "apply_receive"
  106. case 5: //正式客户申请续约
  107. updateSource = "service_update"
  108. case 6: //正式客户新增补充协议
  109. updateSource = "add_agreement"
  110. }
  111. tmpStartDate, _ := time.ParseInLocation(utils.FormatDate, v.StartDate, time.Local)
  112. tmpEndDate, _ := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
  113. companyProductUpdateLog := &models.CompanyProductUpdateLog{
  114. Id: 0,
  115. CompanyId: companyProduct.CompanyId,
  116. ProductId: companyProduct.ProductId,
  117. Status: companyProduct.Status,
  118. SellerId: companyProduct.SellerId,
  119. SellerName: companyProduct.SellerName,
  120. Source: updateSource,
  121. IsFormal: companyProduct.IsFormal, //是否已经转正式,0是没有转正式,1是已经转过正式
  122. StartDate: tmpStartDate,
  123. EndDate: tmpEndDate,
  124. CreateTime: time.Now(),
  125. }
  126. go models.AddCompanyProductUpdateLog(companyProductUpdateLog)
  127. }
  128. //研选审批通过的时候研选扣点更新
  129. {
  130. cygx.YanXuanCompanyApproval(v.CompanyId)
  131. cygx.ActivitySpecialCompanyApproval(v.CompanyId, v.CompanyContractId, companyProduct.CompanyName) //审批通过的时候专项调研次数更新
  132. //cygx.HandleAllocationCompanyContractByYanXuan(v.CompanyContractId) //如果合同只有研选的时候,自动处理派点
  133. cygx.HandleCompanyContractPackageDifference(v.CompanyContractId) //更新与上一份合同的金额的对比 '增加套餐','减少套餐','维持套餐'
  134. cygx.HandleCompanyContractPermissionContractType(v.CompanyContractId) //更新合同权限表中的权限名称,以及对应的行业权限类型(行业新签、行业续约)
  135. }
  136. }
  137. //错误信息,批量返回错误信息
  138. if len(errorList) > 0 {
  139. err = errors.New(strings.Join(errorList, ";"))
  140. }
  141. return
  142. }
  143. // contactHandleCompanyRenewalRecord
  144. // @Description: 合同处理完成后的续约异常记录
  145. // @author: Roc
  146. // @datetime 2023-12-07 14:24:44
  147. // @param contractInfo *company_contract.CompanyContract
  148. // @param day string
  149. // @return err error
  150. func contactHandleCompanyRenewalRecord(contractInfo *company_contract.CompanyContract, day string) (err error) {
  151. // 判断合同类型是否是续约合同,如果不是的话,就不往下走了
  152. if contractInfo.ContractType != `续约合同` {
  153. return
  154. }
  155. // 获取早于当前合同结束日期的上一份合同
  156. lastContract, tmpErr := company_contract.GetLastContractListByEndDate(contractInfo.CompanyId, contractInfo.ProductId, contractInfo.EndDate)
  157. if tmpErr != nil {
  158. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";通过最近一份合同的日期获取早于该合同的最晚一份合同失败,ERR:", tmpErr))
  159. return
  160. }
  161. // 校验 上一份合同的结束日期 与 今天 相隔的天数
  162. betweenDay, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, day, lastContract.EndDate)
  163. if tmpErr != nil {
  164. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";计算两个日期相差的天数失败,ERR:", tmpErr))
  165. return
  166. }
  167. source := 2 // 正常续约
  168. // 如果间隔时间超过60天,那么标记为超时续约
  169. if betweenDay > 60 {
  170. source = 3 // 超时续约
  171. }
  172. // 如果间隔时间超过60天,那么标记为超时续约
  173. companyProductItem, tmpErr := models.GetCompanyProductItemByCompanyId(contractInfo.CompanyId, contractInfo.ProductId)
  174. if tmpErr != nil {
  175. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";GetCompanyProductItemByCompanyId失败,ERR:", tmpErr))
  176. return
  177. }
  178. var shareSellerId int
  179. var shareSellerName string
  180. if companyProductItem.IsShare == 1 {
  181. shareSellerId = companyProductItem.ShareSellerId
  182. shareSellerName = companyProductItem.ShareSeller
  183. }
  184. item := &models.CompanyRenewalRecord{
  185. Id: 0,
  186. CompanyId: contractInfo.CompanyId,
  187. ProductId: contractInfo.ProductId,
  188. Source: source,
  189. SellerId: companyProductItem.SellerId,
  190. SellerName: companyProductItem.SellerName,
  191. ShareSellerId: shareSellerId,
  192. ShareSellerName: shareSellerName,
  193. CreateTime: time.Now(),
  194. ModifyTime: time.Now(),
  195. }
  196. tmpErr = item.Add()
  197. if tmpErr != nil {
  198. err = errors.New(fmt.Sprint("合同id:", contractInfo.CompanyContractId, ";添加续约异常记录失败,ERR:", tmpErr))
  199. }
  200. return
  201. }
  202. // HandleCompanyRenewalRecord
  203. // @Description: 处理续约客户的问题
  204. // @author: Roc
  205. // @datetime 2023-12-07 10:56:24
  206. // @param cont context.Context
  207. // @return err error
  208. func HandleCompanyRenewalRecord(cont context.Context) (err error) {
  209. defer func() {
  210. if err != nil {
  211. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  212. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  213. go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
  214. }
  215. }()
  216. // 需求是60个自然后未续约的客户,所以要减去61天
  217. endDate := time.Now().AddDate(0, 0, -61).Format(utils.FormatDate)
  218. total, list, err := company_contract.GetContractListByEndDate(endDate)
  219. if err != nil {
  220. utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
  221. return
  222. }
  223. fmt.Printf("总共%d条数据待处理\n", total)
  224. if total <= 0 {
  225. return
  226. }
  227. companyIdList := make([]int, 0)
  228. for _, companyInfo := range list {
  229. companyIdList = append(companyIdList, companyInfo.CompanyId)
  230. }
  231. companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
  232. if err != nil {
  233. utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
  234. return
  235. }
  236. addList := make([]*models.CompanyRenewalRecord, 0)
  237. for _, v := range companyList {
  238. if v.Status != "正式" {
  239. var shareSellerId int
  240. var shareSellerName string
  241. if v.IsShare == 1 {
  242. shareSellerId = v.ShareSellerId
  243. shareSellerName = v.ShareSeller
  244. }
  245. addList = append(addList, &models.CompanyRenewalRecord{
  246. Id: 0,
  247. CompanyId: v.CompanyId,
  248. ProductId: v.ProductId,
  249. Source: 1,
  250. SellerId: v.SellerId,
  251. SellerName: v.SellerName,
  252. ShareSellerId: shareSellerId,
  253. ShareSellerName: shareSellerName,
  254. CreateTime: time.Now(),
  255. ModifyTime: time.Now(),
  256. })
  257. }
  258. }
  259. if len(addList) > 0 {
  260. err = models.MultiAddCompanyRenewalRecord(addList)
  261. }
  262. return
  263. }
  264. // handleCompanyRenewalRecord
  265. // @Description: 处理合同到期后还未续约的记录
  266. // @author: Roc
  267. // @datetime 2024-02-21 13:41:48
  268. // @param endDate string
  269. // @param source int
  270. // @return err error
  271. func handleCompanyRenewalRecord(endDate string, source int) (err error) {
  272. defer func() {
  273. if err != nil {
  274. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  275. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  276. go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", endDate, source, err.Error()), 3)
  277. }
  278. }()
  279. // 根据日期获取截止日期是该日期的合同列表
  280. total, list, err := company_contract.GetContractListByEndDate(endDate)
  281. if err != nil {
  282. utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
  283. return
  284. }
  285. fmt.Printf("总共%d条数据待处理\n", total)
  286. if total <= 0 {
  287. return
  288. }
  289. companyIdList := make([]int, 0)
  290. for _, companyInfo := range list {
  291. companyIdList = append(companyIdList, companyInfo.CompanyId)
  292. }
  293. companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
  294. if err != nil {
  295. utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
  296. return
  297. }
  298. addList := make([]*models.CompanyRenewalRecord, 0)
  299. for _, v := range companyList {
  300. if v.Status != "正式" {
  301. var shareSellerId int
  302. var shareSellerName string
  303. if v.IsShare == 1 {
  304. shareSellerId = v.ShareSellerId
  305. shareSellerName = v.ShareSeller
  306. }
  307. addList = append(addList, &models.CompanyRenewalRecord{
  308. Id: 0,
  309. CompanyId: v.CompanyId,
  310. ProductId: v.ProductId,
  311. Source: source,
  312. SellerId: v.SellerId,
  313. SellerName: v.SellerName,
  314. ShareSellerId: shareSellerId,
  315. ShareSellerName: shareSellerName,
  316. CreateTime: time.Now(),
  317. ModifyTime: time.Now(),
  318. })
  319. }
  320. }
  321. if len(addList) > 0 {
  322. err = models.MultiAddCompanyRenewalRecord(addList)
  323. }
  324. return
  325. }
  326. // handleCompanyRenewalRecord
  327. // @Description: 处理合同即将到期(还未过期)后还未续约的记录
  328. // @author: Roc
  329. // @datetime 2024-02-21 13:41:48
  330. // @param endDate string
  331. // @param source int
  332. // @return err error
  333. func handleCompanyExpiringRenewalRecord(contractDate string, source int) (err error) {
  334. defer func() {
  335. if err != nil {
  336. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  337. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  338. go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同即将到期(还未过期)后还未续约的记录异常 ErrMsg:%s", contractDate, source, err.Error()), 3)
  339. }
  340. }()
  341. // 根据日期获取截止日期是该日期的合同列表
  342. total, list, err := company_contract.GetContractListByEndDate(contractDate)
  343. if err != nil {
  344. utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
  345. return
  346. }
  347. fmt.Printf("总共%d条数据待处理\n", total)
  348. if total <= 0 {
  349. return
  350. }
  351. companyIdList := make([]int, 0)
  352. for _, companyInfo := range list {
  353. companyIdList = append(companyIdList, companyInfo.CompanyId)
  354. }
  355. // 已经续约了的客户
  356. afterContractCompanyIdMap := make(map[int]int)
  357. {
  358. // 根据客户id列表和上一份合同的结束日期,获取后续的合同
  359. _, afterContractList, tmpErr := company_contract.GetAfterCompanyContractListByCompanyIdListAndEndDate(contractDate, companyIdList)
  360. if tmpErr != nil {
  361. err = tmpErr
  362. utils.FileLog.Info("company_contract.GetAfterCompanyContractListByCompanyIdListAndEndDate Err:%s" + err.Error())
  363. return
  364. }
  365. for _, afterContract := range afterContractList {
  366. afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
  367. }
  368. }
  369. // 客户列表
  370. companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
  371. if err != nil {
  372. utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
  373. return
  374. }
  375. addList := make([]*models.CompanyRenewalRecord, 0)
  376. for _, v := range companyList {
  377. _, ok := afterContractCompanyIdMap[v.CompanyId]
  378. // 已经续约了,不处理
  379. if ok {
  380. continue
  381. }
  382. // 未续约客户,去记录
  383. var shareSellerId int
  384. var shareSellerName string
  385. if v.IsShare == 1 {
  386. shareSellerId = v.ShareSellerId
  387. shareSellerName = v.ShareSeller
  388. }
  389. addList = append(addList, &models.CompanyRenewalRecord{
  390. Id: 0,
  391. CompanyId: v.CompanyId,
  392. ProductId: v.ProductId,
  393. Source: source,
  394. SellerId: v.SellerId,
  395. SellerName: v.SellerName,
  396. ShareSellerId: shareSellerId,
  397. ShareSellerName: shareSellerName,
  398. CreateTime: time.Now(),
  399. ModifyTime: time.Now(),
  400. })
  401. }
  402. if len(addList) > 0 {
  403. err = models.MultiAddCompanyRenewalRecord(addList)
  404. }
  405. return
  406. }
  407. func HandleCompanyRenewalRecordV2(cont context.Context) (err error) {
  408. defer func() {
  409. if err != nil {
  410. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  411. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  412. go alarm_msg.SendAlarmMsg("合同处理 ErrMsg:"+err.Error(), 3)
  413. }
  414. }()
  415. // 1:续约异常客户;4:合同到期后一个月未续约客户;5:合同到期未续约客户;6:合同到期前一个月还未续约的客户;7:合同到期前两个月还未续约客户;8:合同到期前3个月还未续约客户;9:合同到期前4个月还未续约客户'
  416. // 下面的日期都减去1天,是为了加上当日
  417. // 1:需求是60个自然日后未续约的客户
  418. {
  419. endDate := time.Now().AddDate(0, 0, -60-1).Format(utils.FormatDate)
  420. handleCompanyRenewalRecord(endDate, 1)
  421. }
  422. // 4:需求是30个自然日后未续约的客户
  423. {
  424. endDate := time.Now().AddDate(0, 0, -30-1).Format(utils.FormatDate)
  425. handleCompanyRenewalRecord(endDate, 4)
  426. }
  427. // 5:需求是到期日后未续约的客户
  428. {
  429. endDate := time.Now().AddDate(0, 0, -1).Format(utils.FormatDate)
  430. handleCompanyRenewalRecord(endDate, 5)
  431. }
  432. // 即将到期还未到期的统计
  433. // 合同到期前一个月还未续约的客户;
  434. {
  435. endDate := time.Now().AddDate(0, 0, 30-1).Format(utils.FormatDate)
  436. handleCompanyExpiringRenewalRecord(endDate, 6)
  437. }
  438. // 7:合同到期前两个月还未续约客户;
  439. {
  440. endDate := time.Now().AddDate(0, 0, 60-1).Format(utils.FormatDate)
  441. handleCompanyExpiringRenewalRecord(endDate, 7)
  442. }
  443. // 8:合同到期前3个月还未续约客户;
  444. {
  445. endDate := time.Now().AddDate(0, 0, 90-1).Format(utils.FormatDate)
  446. handleCompanyExpiringRenewalRecord(endDate, 8)
  447. }
  448. // 9:合同到期前4个月还未续约客户
  449. {
  450. endDate := time.Now().AddDate(0, 0, 120-1).Format(utils.FormatDate)
  451. handleCompanyExpiringRenewalRecord(endDate, 9)
  452. }
  453. return
  454. }
  455. // 续约异常客户数据修复
  456. func FixData() {
  457. //firstTime := time.Date(2023, 1, 1, 0, 0, 0, 0, time.Local)
  458. firstTime := time.Date(2024, 2, 28, 0, 0, 0, 0, time.Local)
  459. endTime := time.Date(2024, 2, 28, 1, 0, 0, 0, time.Local)
  460. for currTime := firstTime; currTime.Before(endTime); currTime = currTime.AddDate(0, 0, 1) {
  461. currDate := currTime.Format(utils.FormatDate)
  462. fmt.Printf("结束%s的数据修复\n", currDate)
  463. // 4:需求是30个自然日后未续约的客户
  464. {
  465. handleFixCompanyRenewalRecord(currTime.AddDate(0, 0, -30-1).Format(utils.FormatDate), currTime, 4)
  466. }
  467. // 5:需求是到期日后未续约的客户
  468. {
  469. handleFixCompanyRenewalRecord(currTime.AddDate(0, 0, -1).Format(utils.FormatDate), currTime, 5)
  470. }
  471. //即将到期还未到期的统计
  472. //合同到期前一个月还未续约的客户;
  473. {
  474. handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 30-1).Format(utils.FormatDate), currTime, 6)
  475. }
  476. // 7:合同到期前两个月还未续约客户;
  477. {
  478. handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 60-1).Format(utils.FormatDate), currTime, 7)
  479. }
  480. // 8:合同到期前3个月还未续约客户;
  481. {
  482. handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 90-1).Format(utils.FormatDate), currTime, 8)
  483. }
  484. // 9:合同到期前4个月还未续约客户
  485. {
  486. handleFixCompanyExpiringRenewalRecord(currTime.AddDate(0, 0, 120-1).Format(utils.FormatDate), currTime, 9)
  487. }
  488. }
  489. fmt.Println("end fix")
  490. }
  491. // handleFixCompanyRenewalRecord
  492. // @Description: 修复合同到期后N个月还未续约的客户
  493. // @author: Roc
  494. // @datetime 2024-02-21 15:29:29
  495. // @param endDate string
  496. // @param source int
  497. // @return err error
  498. func handleFixCompanyRenewalRecord(endDate string, currTime time.Time, source int) (err error) {
  499. defer func() {
  500. if err != nil {
  501. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  502. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  503. go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", currTime.Format(utils.FormatDate), source, err.Error()), 3)
  504. }
  505. }()
  506. // 根据日期获取截止日期是该日期的合同列表
  507. total, list, err := company_contract.GetContractListByEndDate(endDate)
  508. if err != nil {
  509. utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
  510. return
  511. }
  512. fmt.Printf("总共%d条数据待处理\n", total)
  513. if total <= 0 {
  514. return
  515. }
  516. companyIdList := make([]int, 0)
  517. for _, companyInfo := range list {
  518. companyIdList = append(companyIdList, companyInfo.CompanyId)
  519. }
  520. // 已经续约了的合同列表
  521. afterContractCompanyIdMap := make(map[int]int)
  522. {
  523. // 根据客户id列表和上一份合同的结束日期,获取已经续约了的合同列表
  524. _, renewalList, tmpErr := company_contract.GetContractListByEndDateAndModifyTime(endDate, endDate, companyIdList)
  525. if tmpErr != nil {
  526. err = tmpErr
  527. utils.FileLog.Info("HandleCompanyRenewalRecord GetContractListByEndDateAndModifyTime Err:%s" + err.Error())
  528. return
  529. }
  530. for _, afterContract := range renewalList {
  531. afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
  532. }
  533. }
  534. // 获取客户列表
  535. companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
  536. if err != nil {
  537. utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
  538. return
  539. }
  540. addList := make([]*models.CompanyRenewalRecord, 0)
  541. for _, v := range companyList {
  542. _, ok := afterContractCompanyIdMap[v.CompanyId]
  543. // 已经续约了,不处理
  544. if ok {
  545. continue
  546. }
  547. var shareSellerId int
  548. var shareSellerName string
  549. if v.IsShare == 1 {
  550. shareSellerId = v.ShareSellerId
  551. shareSellerName = v.ShareSeller
  552. }
  553. addList = append(addList, &models.CompanyRenewalRecord{
  554. Id: 0,
  555. CompanyId: v.CompanyId,
  556. ProductId: v.ProductId,
  557. Source: source,
  558. SellerId: v.SellerId,
  559. SellerName: v.SellerName,
  560. ShareSellerId: shareSellerId,
  561. ShareSellerName: shareSellerName,
  562. CreateTime: currTime,
  563. ModifyTime: currTime,
  564. })
  565. }
  566. if len(addList) > 0 {
  567. err = models.MultiAddCompanyRenewalRecord(addList)
  568. }
  569. return
  570. }
  571. // handleFixCompanyExpiringRenewalRecord
  572. // @Description: 修复合同到期前N个月还未续约的客户
  573. // @author: Roc
  574. // @datetime 2024-02-21 15:29:29
  575. // @param endDate string
  576. // @param source int
  577. // @return err error
  578. func handleFixCompanyExpiringRenewalRecord(endDate string, currTime time.Time, source int) (err error) {
  579. currDate := currTime.Format(utils.FormatDate)
  580. defer func() {
  581. if err != nil {
  582. //fmt.Println("合同处理 ErrMsg:"+err.Error())
  583. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "合同处理 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  584. go alarm_msg.SendAlarmMsg(fmt.Sprintf("日期:%s;来源%d;处理合同到期后还未续约的记录异常 ErrMsg:%s", currDate, source, err.Error()), 3)
  585. }
  586. }()
  587. // 根据日期获取截止日期是该日期的合同列表
  588. total, list, err := company_contract.GetContractListByEndDate(endDate)
  589. if err != nil {
  590. utils.FileLog.Info("HandleCompanyRenewalRecord GetStartContractListByEndDate Err:%s" + err.Error())
  591. return
  592. }
  593. fmt.Printf("总共%d条数据待处理\n", total)
  594. if total <= 0 {
  595. return
  596. }
  597. companyIdList := make([]int, 0)
  598. for _, companyInfo := range list {
  599. companyIdList = append(companyIdList, companyInfo.CompanyId)
  600. }
  601. // 已经续约了的合同列表
  602. afterContractCompanyIdMap := make(map[int]int)
  603. {
  604. // 根据客户id列表和上一份合同的结束日期,获取已经续约了的合同列表
  605. _, renewalList, tmpErr := company_contract.GetContractListByEndDateAndModifyTime(endDate, currDate, companyIdList)
  606. if tmpErr != nil {
  607. err = tmpErr
  608. utils.FileLog.Info("HandleCompanyRenewalRecord GetContractListByEndDateAndModifyTime Err:%s" + err.Error())
  609. return
  610. }
  611. for _, afterContract := range renewalList {
  612. afterContractCompanyIdMap[afterContract.CompanyId] = afterContract.CompanyId
  613. }
  614. }
  615. // 获取客户列表
  616. companyList, err := models.GetCompanyProductItemListByCompanyIdList(companyIdList, 1)
  617. if err != nil {
  618. utils.FileLog.Info("HandleCompanyRenewalRecord GetCompanyProductListByCompanyIdList Err:%s" + err.Error())
  619. return
  620. }
  621. addList := make([]*models.CompanyRenewalRecord, 0)
  622. for _, v := range companyList {
  623. _, ok := afterContractCompanyIdMap[v.CompanyId]
  624. // 已经续约了,不处理
  625. if ok {
  626. continue
  627. }
  628. var shareSellerId int
  629. var shareSellerName string
  630. if v.IsShare == 1 {
  631. shareSellerId = v.ShareSellerId
  632. shareSellerName = v.ShareSeller
  633. }
  634. addList = append(addList, &models.CompanyRenewalRecord{
  635. Id: 0,
  636. CompanyId: v.CompanyId,
  637. ProductId: v.ProductId,
  638. Source: source,
  639. SellerId: v.SellerId,
  640. SellerName: v.SellerName,
  641. ShareSellerId: shareSellerId,
  642. ShareSellerName: shareSellerName,
  643. CreateTime: currTime,
  644. ModifyTime: currTime,
  645. })
  646. }
  647. if len(addList) > 0 {
  648. err = models.MultiAddCompanyRenewalRecord(addList)
  649. }
  650. return
  651. }