cooperate.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package db
  2. import (
  3. "context"
  4. "github.com/sirupsen/logrus"
  5. "time"
  6. "youngee_b_api/model/gorm_model"
  7. )
  8. // CreateCooperateInfo 创建商家-服务商合作关系
  9. func CreateCooperateInfo(ctx context.Context, cooperateInfo *gorm_model.EnterpriseSupplierCooperate) error {
  10. db := GetWriteDB(ctx)
  11. err := db.Create(&cooperateInfo).Error
  12. if err != nil {
  13. return err
  14. }
  15. return nil
  16. }
  17. // GetCooperateInfoByIds 根据服务商ID和邀约状态在合作库中查找合作数据
  18. func GetCooperateInfoByIds(ctx context.Context, supplierId int, cooperateStatus int, pageSize, pageNum int64) ([]*gorm_model.EnterpriseSupplierCooperate, int64, error) {
  19. db := GetReadDB(ctx)
  20. whereCondition := gorm_model.EnterpriseSupplierCooperate{
  21. SupplierId: supplierId,
  22. CooperateStatus: cooperateStatus,
  23. }
  24. // 1. 按条件过滤
  25. db = db.Debug().Model(gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition)
  26. // 2. 返回当前页数据并统计总数
  27. var total int64
  28. var cooperateInfp []*gorm_model.EnterpriseSupplierCooperate
  29. if err := db.Count(&total).Error; err != nil {
  30. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  31. return nil, 0, err
  32. }
  33. limit := pageSize
  34. offset := pageSize * pageNum // assert pageNum start with 0
  35. err := db.Order("enterprise_id desc").Limit(int(limit)).Offset(int(offset)).Find(&cooperateInfp).Error
  36. if err != nil {
  37. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  38. return nil, 0, err
  39. }
  40. return cooperateInfp, total, nil
  41. }
  42. // UpdateCooperateInfo 更新合作关系状态
  43. func UpdateCooperateInfo(ctx context.Context, cooperateId int, cooperateStatus int, supplierId int, subAccountId int) error {
  44. db := GetWriteDB(ctx)
  45. whereCondition := gorm_model.EnterpriseSupplierCooperate{CooperateId: cooperateId}
  46. if cooperateStatus == 2 {
  47. var cooperateInfo *gorm_model.EnterpriseSupplierCooperate
  48. cooperateInfo = &gorm_model.EnterpriseSupplierCooperate{}
  49. cooperateInfo.CooperateStatus = cooperateStatus
  50. var currentTime time.Time
  51. currentTime = time.Now()
  52. cooperateInfo.AgreeTime = &currentTime
  53. if subAccountId != 0 {
  54. cooperateInfo.SOperator = subAccountId
  55. cooperateInfo.SOperatorType = 2
  56. } else {
  57. cooperateInfo.SOperator = supplierId
  58. cooperateInfo.SOperatorType = 1
  59. }
  60. err := db.Model(&gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition).Updates(cooperateInfo).Error
  61. if err != nil {
  62. return err
  63. }
  64. } else if cooperateStatus == 3 {
  65. var cooperateInfo *gorm_model.EnterpriseSupplierCooperate
  66. cooperateInfo = &gorm_model.EnterpriseSupplierCooperate{}
  67. cooperateInfo.CooperateStatus = cooperateStatus
  68. var currentTime time.Time
  69. currentTime = time.Now()
  70. cooperateInfo.AgreeTime = &currentTime
  71. if subAccountId != 0 {
  72. cooperateInfo.SOperator = subAccountId
  73. cooperateInfo.SOperatorType = 2
  74. } else {
  75. cooperateInfo.SOperator = supplierId
  76. cooperateInfo.SOperatorType = 1
  77. }
  78. err := db.Model(&gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition).Updates(cooperateInfo).Error
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  85. // FindCooperateInfoBySupplierAndEnterprise 查找服务商-商家是否建立合作关系
  86. func FindCooperateInfoBySupplierAndEnterprise(ctx context.Context, supplierId int, enterpriseId string) (int64, error) {
  87. db := GetReadDB(ctx)
  88. whereCondition := gorm_model.EnterpriseSupplierCooperate{
  89. EnterpriseId: enterpriseId,
  90. SupplierId: supplierId,
  91. }
  92. db = db.Debug().Model(gorm_model.EnterpriseSupplierCooperate{}).Where(whereCondition)
  93. var total int64
  94. if err := db.Count(&total).Error; err != nil {
  95. logrus.WithContext(ctx).Errorf("[GetCooperateInfoByIds] error query mysql total, err:%+v", err)
  96. return 0, err
  97. }
  98. return total, nil
  99. }
  100. // CountCooperateBySupplierId 合作列表数量统计
  101. func CountCooperateBySupplierId(ctx context.Context, supplierId int, cooperateStatus int) (int64, error) {
  102. db := GetReadDB(ctx)
  103. var cooperateNum int64
  104. err := db.Model(gorm_model.EnterpriseSupplierCooperate{}).Where("supplier_id = ? and cooperate_status = ?", supplierId, cooperateStatus).Count(&cooperateNum).Error
  105. if err != nil {
  106. logrus.WithContext(ctx).Errorf("[CreateMessageByTask] error read mysql, err:%+v", err)
  107. return 0, err
  108. }
  109. return cooperateNum, nil
  110. }