entaccountaddlogic.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/jyResourcesCenter/entity"
  4. "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/config"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/util/gconv"
  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 EntAccountAddLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. const (
  20. ConsumeRecord = "consume_record" //流水表
  21. AccountResources = "account_resources" //结存表
  22. )
  23. func NewEntAccountAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EntAccountAddLogic {
  24. return &EntAccountAddLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. // EntAccountAdd 新增权益账号
  31. func (l *EntAccountAddLogic) EntAccountAdd(in *resourcesCenter.EntAccountAdd) (*resourcesCenter.Response, error) {
  32. eCode, msg := func() (int64, string) {
  33. if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 0 {
  34. return entity.ErrorCode, "参数异常"
  35. }
  36. orm := entity.Engine.NewSession()
  37. var entAccountCount int64 = 0
  38. _, err := orm.Table(AccountResources).Select("count(1) as dataNumber").Where("companyId=? AND resourceType=? AND endTime>= ?", in.CompanyId, in.ResourceType, time.Now().Format("2006-01-02")).Get(&entAccountCount)
  39. if err != nil {
  40. return entity.ErrorCode, "账户查询异常"
  41. }
  42. if err = orm.Begin(); err != nil {
  43. return entity.ErrorCode, "数据库操作异常"
  44. }
  45. for i := 0; i < gconv.Int(in.AccountNum); i++ {
  46. var tUserId string
  47. if entAccountCount == 0 && i == 0 { //当企业是首次初始化时,需要给当前账号权限
  48. tUserId = in.AccountId
  49. } else {
  50. tUserId = fmt.Sprintf("wait_%s", uuid.New().String()) //待分配
  51. }
  52. var (
  53. detailed = entity.Detailed{
  54. AccountId: tUserId,
  55. CompanyId: in.CompanyId,
  56. DepartmentId: in.DepartmentId,
  57. Name: in.Name,
  58. ResourceType: in.ResourceType,
  59. Number: in.Number,
  60. CreateTime: time.Now().Local(),
  61. UserType: 1,
  62. Remarks: in.Remarks,
  63. UserId: tUserId,
  64. }
  65. balance = entity.Balance{
  66. AccountId: tUserId,
  67. CompanyId: in.CompanyId,
  68. DepartmentId: in.DepartmentId,
  69. Name: in.Name,
  70. ResourceType: in.ResourceType,
  71. Number: in.Number,
  72. Spec: in.Spec,
  73. AppId: in.AppId,
  74. EndTime: in.EndTime,
  75. SourceType: 1,
  76. }
  77. )
  78. insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed)
  79. if err != nil || insertNumb <= 0 {
  80. fmt.Println("新增流水失败:", err)
  81. _ = orm.Rollback()
  82. return entity.ErrorCode, "新增流水失败"
  83. }
  84. insertNumb, err = orm.Table(AccountResources).Insert(&balance)
  85. if err != nil || insertNumb <= 0 {
  86. fmt.Println("结存查询失败:", err)
  87. _ = orm.Rollback()
  88. return entity.ErrorCode, "结存新增失败"
  89. }
  90. }
  91. _ = orm.Commit()
  92. return entity.SuccessCode, "操作成功"
  93. }()
  94. return &resourcesCenter.Response{
  95. Code: eCode,
  96. Message: msg,
  97. }, nil
  98. }