entaccountgivenlogic.go 2.6 KB

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