entaccountgivenlogic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 == "" {
  28. return entity.ErrorCode, "参数异常"
  29. }
  30. var waitBalance *entity.Balance //等待分配的数量
  31. orm := entity.Engine.NewSession()
  32. _, err := orm.Table(AccountResources).Where("companyId=? AND resourceType=? AND number>0 AND endTime>= ? AND accountId like 'wait_%'", in.CompanyId, in.ResourceType, time.Now().Format("2006-01-02")).OrderBy("id asc").Limit(1).Get(waitBalance)
  33. if err != nil || waitBalance == nil {
  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": "权益分配"})
  49. updateNumb, err = orm.Table(ConsumeRecord).Insert(entity.Detailed{
  50. AccountId: in.OperateAccountId,
  51. CompanyId: waitBalance.CompanyId,
  52. ResourceType: waitBalance.ResourceType,
  53. Number: waitBalance.Number,
  54. CreateTime: time.Now().Local(),
  55. UserType: 5,
  56. Remarks: string(jsonData),
  57. UserId: in.OperateAccountId,
  58. Name: waitBalance.Name,
  59. })
  60. if err != nil || updateNumb <= 0 {
  61. _ = orm.Rollback()
  62. return entity.ErrorCode, "新增流水异常"
  63. }
  64. return entity.SuccessCode, "操作成功"
  65. }()
  66. return &resourcesCenter.Response{
  67. Code: eCode,
  68. Message: msg,
  69. }, nil
  70. }