entaccountrecoverylogic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jyResourcesCenter/entity"
  4. "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/config"
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/google/uuid"
  9. "time"
  10. "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc"
  11. "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type EntAccountRecoveryLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewEntAccountRecoveryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EntAccountRecoveryLogic {
  20. return &EntAccountRecoveryLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. // EntAccountRecovery 权益收回
  27. func (l *EntAccountRecoveryLogic) EntAccountRecovery(in *resourcesCenter.EntOperateReq) (*resourcesCenter.Response, error) {
  28. eCode, msg := func() (int64, string) {
  29. if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 0 || in.OperateAccountId == "" {
  30. return entity.ErrorCode, "参数异常"
  31. }
  32. var (
  33. err error
  34. orm = entity.Engine.NewSession()
  35. )
  36. var balance *entity.Balance
  37. _, err = orm.Table(AccountResources).
  38. Where("companyId=? AND resourceType=? AND number>0 AND endTime>= ? AND accountId = ?", in.CompanyId, in.ResourceType, time.Now().Format("2006-01-02"), in.OperateAccountId).Get(balance)
  39. if err != nil || balance == nil {
  40. return entity.ErrorCode, "账户查询异常"
  41. }
  42. if err = orm.Begin(); err != nil {
  43. return entity.ErrorCode, "数据库操作异常"
  44. }
  45. updateNumb, err := orm.Table(AccountResources).Cols("accountId").
  46. Where(" id=?", balance.Id).
  47. Update(&map[string]interface{}{
  48. "accountId": fmt.Sprintf("wait_%s", uuid.New().String()),
  49. })
  50. if err != nil || updateNumb <= 0 {
  51. _ = orm.Rollback()
  52. return entity.ErrorCode, "回收异常"
  53. }
  54. jsonData, _ := json.Marshal(map[string]interface{}{"describe": "权益回收"})
  55. updateNumb, err = orm.Table(ConsumeRecord).Insert(entity.Detailed{
  56. AccountId: balance.AccountId,
  57. CompanyId: balance.CompanyId,
  58. ResourceType: balance.ResourceType,
  59. Number: balance.Number,
  60. CreateTime: time.Now().Local(),
  61. UserType: 5,
  62. Remarks: string(jsonData),
  63. UserId: balance.AccountId,
  64. Name: balance.Name,
  65. })
  66. if err != nil || updateNumb <= 0 {
  67. _ = orm.Rollback()
  68. return entity.ErrorCode, "新增流水异常"
  69. }
  70. _ = orm.Commit()
  71. return entity.SuccessCode, "操作成功"
  72. }()
  73. return &resourcesCenter.Response{
  74. Code: eCode,
  75. Message: msg,
  76. }, nil
  77. }