eta_business.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package eta_business
  2. import (
  3. "fmt"
  4. "hongze/hz_crm_api/models/company"
  5. "hongze/hz_crm_api/models/eta_business"
  6. "hongze/hz_crm_api/services/alarm_msg"
  7. "hongze/hz_crm_api/utils"
  8. "strings"
  9. "time"
  10. )
  11. // CheckEtaBusinessOperateAuth 校验ETA商家操作权限
  12. func CheckEtaBusinessOperateAuth(roleCode string) (ok bool, err error) {
  13. confKey := "eta_business_auth_role"
  14. conf, e := company.GetConfigDetailByCode(confKey)
  15. if e != nil {
  16. err = fmt.Errorf("获取权限配置失败, Err: %s", e.Error())
  17. return
  18. }
  19. if conf.ConfigValue == "" {
  20. err = fmt.Errorf("权限配置信息有误")
  21. return
  22. }
  23. authArr := strings.Split(conf.ConfigValue, ",")
  24. if utils.InArrayByStr(authArr, roleCode) {
  25. ok = true
  26. }
  27. return
  28. }
  29. // UpdateEtaBusinessAfterSigning 签约后的后置操作
  30. func UpdateEtaBusinessAfterSigning(businessId int) (err error) {
  31. if businessId <= 0 {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. tips := "签约后更新商家信息失败, Err: " + err.Error()
  37. utils.FileLog.Info("%s", tips)
  38. go alarm_msg.SendAlarmMsg(tips, 3)
  39. }
  40. }()
  41. businessOb := new(eta_business.EtaBusiness)
  42. business, e := businessOb.GetItemById(businessId)
  43. if e != nil {
  44. if e.Error() == utils.ErrNoRow() {
  45. return
  46. }
  47. err = fmt.Errorf("获取商家信息失败, Err: %s", e.Error())
  48. return
  49. }
  50. contracts := make([]*eta_business.EtaBusinessContract, 0)
  51. {
  52. ob := new(eta_business.EtaBusinessContract)
  53. cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
  54. pars := make([]interface{}, 0)
  55. pars = append(pars, businessId)
  56. // 按签约时间倒序
  57. list, e := ob.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", eta_business.EtaBusinessContractColumns.SigningTime))
  58. if e != nil {
  59. err = fmt.Errorf("获取商家合同列表失败, Err: " + e.Error())
  60. return
  61. }
  62. contracts = list
  63. }
  64. if len(contracts) == 0 {
  65. // 重置签约信息
  66. cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"}
  67. business.ContractId = 0
  68. business.SigningStatus = eta_business.EtaBusinessSigningStatusWait
  69. business.SigningTime = time.Time{}
  70. business.ExpiredTime = time.Time{}
  71. business.ModifyTime = time.Now().Local()
  72. if e = business.Update(cols); e != nil {
  73. err = fmt.Errorf("更新商家信息失败, Err: %s", e.Error())
  74. }
  75. return
  76. }
  77. strToday := time.Now().Format(utils.FormatDate)
  78. today, _ := time.ParseInLocation(utils.FormatDate, strToday, time.Local)
  79. using := false // 是否在任一存续期内
  80. for _, c := range contracts {
  81. // 当前合约
  82. if today.Equal(c.SigningTime) || today.Equal(c.ExpiredTime) || (today.After(c.SigningTime) && today.Before(c.ExpiredTime)) {
  83. business.ContractId = c.EtaBusinessContractId
  84. business.SigningTime = c.SigningTime
  85. business.ExpiredTime = c.ExpiredTime
  86. business.ModifyTime = time.Now().Local()
  87. // 是否为首次签约
  88. if c.IsFirst == 1 {
  89. business.SigningStatus = eta_business.EtaBusinessSigningStatusFirst
  90. } else {
  91. business.SigningStatus = eta_business.EtaBusinessSigningStatusContinue
  92. }
  93. using = true
  94. break
  95. }
  96. }
  97. // 不存在任一合同期内, 取签约时间最后的合同(已按时间排序)
  98. if !using && len(contracts) > 0 {
  99. business.ContractId = contracts[0].EtaBusinessContractId
  100. business.SigningTime = contracts[0].SigningTime
  101. business.ExpiredTime = contracts[0].ExpiredTime
  102. if contracts[0].IsFirst == 1 {
  103. business.SigningStatus = eta_business.EtaBusinessSigningStatusFirst
  104. } else {
  105. business.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate
  106. }
  107. business.ModifyTime = time.Now().Local()
  108. }
  109. cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"}
  110. if e = business.Update(cols); e != nil {
  111. err = fmt.Errorf("更新商家信息失败, Err: %s", e.Error())
  112. }
  113. return
  114. }