初始化
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
|
||||
*
|
||||
* Project: adapter
|
||||
* File: config.go
|
||||
* Last: 2025-05-16 16:15:18
|
||||
* Author: wangcheng@bj-hardman.com
|
||||
*/
|
||||
|
||||
package db
|
||||
|
||||
type DatabaseConf struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Dbname string
|
||||
Debug bool
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Beijing Hardman Automation Equipment Co., LTD. All rights reserved.
|
||||
*
|
||||
* Project: adapter
|
||||
* File: db.go
|
||||
* Last: 2025-05-16 16:13:58
|
||||
* Author: wangcheng@bj-hardman.com
|
||||
*/
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
xlog "bj_power_mes/common/logx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func MustNewDB(c DatabaseConf) (*ent.Client, *sql.DB) {
|
||||
ec, db, err := NewDB(c)
|
||||
logx.Must(err)
|
||||
return ec, db
|
||||
}
|
||||
|
||||
func NewDB(c DatabaseConf) (*ent.Client, *sql.DB, error) {
|
||||
return NewDBWithParams(c.User, c.Password, c.Host, c.Port, c.Dbname, c.Debug)
|
||||
}
|
||||
|
||||
func NewDBWithParams(username, password, host string, port int, dbname string, debug bool) (*ent.Client, *sql.DB, error) {
|
||||
dsn := fmt.Sprintf("postgresql://%s:%s@%s:%d/%s",
|
||||
username,
|
||||
password,
|
||||
host,
|
||||
port,
|
||||
dbname,
|
||||
)
|
||||
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
_, err = db.Query("select 1")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
if debug {
|
||||
logger := &xlog.EntLogger{Logger: slog.Default()}
|
||||
return ent.NewClient(ent.Driver(dialect.DebugWithContext(drv, logger.DebugWithContext))), db, nil
|
||||
}
|
||||
return ent.NewClient(ent.Driver(drv)), db, nil
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/workpiece"
|
||||
)
|
||||
|
||||
func TestNewDBWithParams(t *testing.T) {
|
||||
client, _, err := NewDBWithParams("postgres", "postgres", "127.0.0.1", 5432, "bj_power_mes", false)
|
||||
if err != nil {
|
||||
t.Errorf("NewDBWithParams() error = %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
defer client.Close()
|
||||
ctx := context.Background()
|
||||
slot, err := nextEmptyTempSlot(ctx, client)
|
||||
if err != nil {
|
||||
t.Errorf("nextEmptyTempSlot() error = %v", err)
|
||||
return
|
||||
}
|
||||
t.Logf("nextEmptyTempSlot() = %v", slot)
|
||||
|
||||
}
|
||||
|
||||
// tempSlotCapacity 暂存台槽位总数。
|
||||
const tempSlotCapacity = 8
|
||||
|
||||
// nextEmptyTempSlot 通过数据库查询已占用的 tempSlot,找第一个空位(1-8)。
|
||||
func nextEmptyTempSlot(ctx context.Context, entClient *ent.Client) (int, error) {
|
||||
occupied, err := entClient.Workpiece.Query().
|
||||
Where(workpiece.TempSlotNotNil()).
|
||||
Select(workpiece.FieldTempSlot).
|
||||
All(ctx)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("查询暂存台占用失败: %w", err)
|
||||
}
|
||||
|
||||
used := make(map[int]struct{}, len(occupied))
|
||||
for _, wp := range occupied {
|
||||
used[wp.TempSlot] = struct{}{}
|
||||
}
|
||||
|
||||
for i := 1; i <= tempSlotCapacity; i++ {
|
||||
if _, ok := used[i]; !ok {
|
||||
return i, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("暂存台无空位")
|
||||
}
|
||||
Reference in New Issue
Block a user