package logic import ( "app.yhyue.com/moapp/jyResourcesCenter/entity" "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/config" "context" "fmt" "github.com/gogf/gf/v2/util/gconv" "github.com/google/uuid" "time" "app.yhyue.com/moapp/jyResourcesCenter/rpc/internal/svc" "app.yhyue.com/moapp/jyResourcesCenter/rpc/resourcesCenter" "github.com/zeromicro/go-zero/core/logx" ) type EntAccountAddLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } const ( ConsumeRecord = "consume_record" //流水表 AccountResources = "account_resources" //结存表 ) func NewEntAccountAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EntAccountAddLogic { return &EntAccountAddLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // EntAccountAdd 新增权益账号 func (l *EntAccountAddLogic) EntAccountAdd(in *resourcesCenter.EntAccountAdd) (*resourcesCenter.Response, error) { eCode, msg := func() (int64, string) { if config.ConfigJson.ProductMap[in.ResourceType] == nil || in.CompanyId == 0 { return entity.ErrorCode, "参数异常" } orm := entity.Engine.NewSession() var entAccountCount int64 = 0 _, 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) if err != nil { return entity.ErrorCode, "账户查询异常" } if err = orm.Begin(); err != nil { return entity.ErrorCode, "数据库操作异常" } for i := 0; i < gconv.Int(in.AccountNum); i++ { var tUserId string if entAccountCount == 0 && i == 0 { //当企业是首次初始化时,需要给当前账号权限 tUserId = in.AccountId } else { tUserId = fmt.Sprintf("wait_%s", uuid.New().String()) //待分配 } var ( detailed = entity.Detailed{ AccountId: tUserId, CompanyId: in.CompanyId, DepartmentId: in.DepartmentId, Name: in.Name, ResourceType: in.ResourceType, Number: in.Number, CreateTime: time.Now().Local(), UserType: 1, Remarks: in.Remarks, UserId: tUserId, } balance = entity.Balance{ AccountId: tUserId, CompanyId: in.CompanyId, DepartmentId: in.DepartmentId, Name: in.Name, ResourceType: in.ResourceType, Number: in.Number, Spec: in.Spec, AppId: in.AppId, EndTime: in.EndTime, SourceType: 1, } ) insertNumb, err := orm.Table(ConsumeRecord).Insert(&detailed) if err != nil || insertNumb <= 0 { fmt.Println("新增流水失败:", err) _ = orm.Rollback() return entity.ErrorCode, "新增流水失败" } insertNumb, err = orm.Table(AccountResources).Insert(&balance) if err != nil || insertNumb <= 0 { fmt.Println("结存查询失败:", err) _ = orm.Rollback() return entity.ErrorCode, "结存新增失败" } } _ = orm.Commit() return entity.SuccessCode, "操作成功" }() return &resourcesCenter.Response{ Code: eCode, Message: msg, }, nil }