273 lines
7.8 KiB
Go
273 lines
7.8 KiB
Go
package service
|
|
|
|
import (
|
|
"admin/apps/game/api"
|
|
"admin/apps/game/domain"
|
|
"admin/internal/consts"
|
|
dbLib "admin/internal/db"
|
|
"admin/internal/errcode"
|
|
"admin/internal/event"
|
|
"admin/internal/global"
|
|
dto2 "admin/internal/model/dto"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
_ "github.com/ClickHouse/clickhouse-go/v2"
|
|
"github.com/jmoiron/sqlx"
|
|
"gorm.io/gorm"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type Service struct {
|
|
db *gorm.DB
|
|
resourceSvc *domain.CommonResourceService
|
|
projectSvc *domain.ProjectService
|
|
cdkeySvc *domain.CDKeyService
|
|
accountSvc *domain.AccountService
|
|
GameLogSvc *domain.GameLogService
|
|
}
|
|
|
|
func New(db *gorm.DB) (*Service, error) {
|
|
var metaDb *gorm.DB
|
|
var clickHouseSqlx *sqlx.DB
|
|
var err error
|
|
|
|
if global.GLOB_BOOT_FLAGS.BiMysqlAddr != "" {
|
|
metaDb, err = dbLib.NewDBNoMigrate("mysql",
|
|
global.GLOB_BOOT_FLAGS.BiMysqlAddr,
|
|
global.GLOB_BOOT_FLAGS.BiMysqlDb,
|
|
global.GLOB_BOOT_FLAGS.BiMysqlUser,
|
|
global.GLOB_BOOT_FLAGS.BiMysqlPass)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dbSource := fmt.Sprintf(
|
|
"tcp://%s?username=%s&password=%s&compress=true&database=%s",
|
|
global.GLOB_BOOT_FLAGS.BiCkAddr,
|
|
global.GLOB_BOOT_FLAGS.BiCkUser,
|
|
global.GLOB_BOOT_FLAGS.BiCkPass,
|
|
global.GLOB_BOOT_FLAGS.BiCkDb)
|
|
clickHouseSqlx, err = NewCkDb("clickhouse", dbSource, 5, 20)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
//"192.168.6.83", "9000", "root", "dev123", "databi")
|
|
}
|
|
|
|
svc := &Service{
|
|
db: db,
|
|
resourceSvc: domain.NewCommonResourceService(db),
|
|
projectSvc: domain.NewProjectService(db),
|
|
cdkeySvc: domain.NewCDKeyService(db),
|
|
accountSvc: domain.NewAccountService(db),
|
|
GameLogSvc: domain.NewGameLogSvc(db, metaDb, clickHouseSqlx),
|
|
}
|
|
api.RegisterGameApi(svc)
|
|
//err := svc.ensureProjectsDBData()
|
|
//if err != nil {
|
|
// return nil, err
|
|
//}
|
|
return svc, nil
|
|
}
|
|
|
|
// NewMySQL 创建一个连接MySQL的实体池
|
|
func NewCkDb(driverName, dbSource string, maxOpenConns, maxIdleConns int) (db *sqlx.DB, err error) {
|
|
|
|
db, err = sqlx.Open(driverName, dbSource)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("sqlx open driver %v source %v error:%v", driverName, dbSource, err)
|
|
}
|
|
if maxOpenConns > 0 {
|
|
db.SetMaxOpenConns(maxOpenConns)
|
|
}
|
|
|
|
if maxIdleConns > 0 {
|
|
db.SetMaxIdleConns(maxIdleConns)
|
|
}
|
|
|
|
err = db.Ping()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("sqlx ping driver %v source %v error:%v", driverName, dbSource, err)
|
|
}
|
|
|
|
go func() {
|
|
for {
|
|
err = db.Ping()
|
|
if err != nil {
|
|
log.Printf("mysql db can't connect!:%v", err)
|
|
}
|
|
time.Sleep(time.Minute)
|
|
}
|
|
}()
|
|
return
|
|
}
|
|
|
|
func (svc *Service) CommonList(ctx context.Context, projectId int, resourceName string, params *dto2.CommonListReq) (*dto2.CommonDtoList, error) {
|
|
|
|
params.ParsedWhereConditions = &dto2.ListWhereConditionInfo{}
|
|
if params.WhereConditions != "" {
|
|
err := json.Unmarshal([]byte(params.WhereConditions), params.ParsedWhereConditions)
|
|
if err != nil {
|
|
return nil, errcode.New(errcode.ParamsInvalid, "unmarshal list condition:%v error:%v",
|
|
params.WhereConditions, err)
|
|
}
|
|
}
|
|
|
|
switch resourceName {
|
|
case consts.ResourcesName_Project:
|
|
default:
|
|
params.ParsedWhereConditions.Conditions = append([]*dto2.GetWhereCondition{&dto2.GetWhereCondition{
|
|
Key: "ProjectId",
|
|
Op: "eq",
|
|
Value1: projectId,
|
|
}}, params.ParsedWhereConditions.Conditions...)
|
|
}
|
|
totalCount, fieldsDescInfo, rows, err := svc.resourceSvc.List(ctx, projectId, resourceName, params)
|
|
itemBags, err := svc.projectSvc.GetAllItemBag(projectId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto2.CommonDtoList{FieldsDesc: fieldsDescInfo, TotalCount: totalCount, Rows: rows, ItemBags: itemBags}, err
|
|
}
|
|
|
|
func (svc *Service) CommonPost(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) (dto2.CommonDtoValues, error) {
|
|
if resourceName != consts.ResourcesName_Project {
|
|
params["ProjectId"] = projectId
|
|
}
|
|
|
|
project, values, err := svc.resourceSvc.Create(ctx, projectId, resourceName, params)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//userName := ctx.Value("user_name").(string)
|
|
|
|
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{params})
|
|
|
|
evPayload := &event.EventPayload_UserGameExecute{
|
|
UserId: userId,
|
|
ProjectId: projectId,
|
|
ProjectName: project.Po.Name,
|
|
OpResourceType: resourceDesc,
|
|
OpResourceGroup: "系统",
|
|
OpResourceKey: keyDesc,
|
|
Method: "新增",
|
|
SrcDataList: []any{params},
|
|
}
|
|
if resourceName != consts.ResourcesName_Project {
|
|
evPayload.OpResourceGroup = project.Po.Name
|
|
}
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return values, err
|
|
}
|
|
|
|
func (svc *Service) CommonPut(ctx context.Context, projectId int, resourceName string, params dto2.CommonDtoValues) error {
|
|
if resourceName != consts.ResourcesName_Project {
|
|
params["ProjectId"] = projectId
|
|
}
|
|
project, err := svc.resourceSvc.Edit(ctx, projectId, resourceName, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//userName := ctx.Value("user_name").(string)
|
|
|
|
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{params})
|
|
|
|
evPayload := &event.EventPayload_UserGameExecute{
|
|
UserId: userId,
|
|
ProjectId: projectId,
|
|
ProjectName: project.Po.Name,
|
|
OpResourceType: resourceDesc,
|
|
OpResourceGroup: "系统",
|
|
OpResourceKey: keyDesc,
|
|
Method: "编辑",
|
|
SrcDataList: []any{params},
|
|
}
|
|
if resourceName != consts.ResourcesName_Project {
|
|
evPayload.OpResourceGroup = project.Po.Name
|
|
}
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return err
|
|
}
|
|
|
|
func (svc *Service) CommonDelete(ctx context.Context, projectId int, resourceName string, id int) error {
|
|
project, deletedEt, err := svc.resourceSvc.Delete(projectId, resourceName, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//userName := ctx.Value("user_name").(string)
|
|
|
|
delObj := deletedEt.ToCommonDto()
|
|
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, []dto2.CommonDtoValues{delObj})
|
|
|
|
evPayload := &event.EventPayload_UserGameExecute{
|
|
UserId: userId,
|
|
ProjectId: projectId,
|
|
ProjectName: project.Po.Name,
|
|
OpResourceType: resourceDesc,
|
|
OpResourceGroup: "系统",
|
|
OpResourceKey: keyDesc,
|
|
Method: "删除",
|
|
SrcDataList: []any{delObj},
|
|
}
|
|
if resourceName != consts.ResourcesName_Project {
|
|
evPayload.OpResourceGroup = project.Po.Name
|
|
}
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return err
|
|
}
|
|
|
|
func (svc *Service) CommonRowsSelection(ctx context.Context, projectId int, resourceName string, param *dto2.CommonRowsSelectionReq) (*dto2.CommonRowsSelectionRsp, error) {
|
|
project, rsp, err := svc.resourceSvc.RowsSelection(ctx, projectId, resourceName, param)
|
|
if err != nil {
|
|
return rsp, err
|
|
}
|
|
|
|
userId := ctx.Value("user_id").(int)
|
|
//userName := ctx.Value("user_name").(string)
|
|
|
|
srcDataList := make([]any, 0, len(param.Rows))
|
|
for _, v := range param.Rows {
|
|
srcDataList = append(srcDataList, v)
|
|
}
|
|
|
|
resourceDesc, keyDesc := svc.resourceSvc.GetResourceKeyByDto(resourceName, param.Rows)
|
|
btnInfo := svc.resourceSvc.GetResourceSpecBtnKey(resourceName, param.BtnKey)
|
|
|
|
evPayload := &event.EventPayload_UserGameExecute{
|
|
UserId: userId,
|
|
ProjectId: projectId,
|
|
ProjectName: project.Po.Name,
|
|
OpResourceType: resourceDesc,
|
|
OpResourceGroup: "系统",
|
|
OpResourceKey: keyDesc,
|
|
Method: param.BtnKey,
|
|
SrcDataList: srcDataList,
|
|
}
|
|
if btnInfo != nil {
|
|
evPayload.Method = btnInfo.Name
|
|
}
|
|
if resourceName != consts.ResourcesName_Project {
|
|
evPayload.OpResourceGroup = project.Po.Name
|
|
}
|
|
|
|
event.GetMgrInstance().Publish(event.EventTopic_UserGameExecute, evPayload)
|
|
|
|
return rsp, err
|
|
}
|
|
|
|
func (svc *Service) GetSupportResourcesList(permissions []string) []*api.ResourceInitInfo {
|
|
return svc.resourceSvc.GetSupportResourcesList(permissions)
|
|
}
|