entaccountrecoverylogic.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. balance = &entity.Balance{}
  36. )
  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 || balance.Id == 0 {
  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": "权益回收", "source": balance.Id})
  55. updateNumb, err = orm.Table(ConsumeRecord).Insert(entity.Detailed{
  56. AccountId: balance.AccountId,
  57. CompanyId: balance.CompanyId,
  58. EmpowerId: balance.EmpowerId,
  59. ResourceType: balance.ResourceType,
  60. Number: balance.Number,
  61. CreateTime: time.Now().Local(),
  62. UserType: 7,
  63. Remarks: string(jsonData),
  64. UserId: balance.AccountId,
  65. Name: balance.Name,
  66. })
  67. if err != nil || updateNumb <= 0 {
  68. _ = orm.Rollback()
  69. return entity.ErrorCode, "新增流水异常"
  70. }
  71. _ = orm.Commit()
  72. return entity.SuccessCode, "操作成功"
  73. }()
  74. return &resourcesCenter.Response{
  75. Code: eCode,
  76. Message: msg,
  77. }, nil
  78. }