97 lines
3.5 KiB
Go
97 lines
3.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"bj_power_mes/ent/permission"
|
|
"bj_power_mes/ent/processstep"
|
|
"bj_power_mes/ent/role"
|
|
"bj_power_mes/ent/stationprocess"
|
|
"bj_power_mes/ent/user"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// Seed 初始化:超级管理员、默认角色、默认菜单权限、12 道工序与工位派工
|
|
func (s *Service) Seed(ctx context.Context) error {
|
|
// 1. 默认角色
|
|
roles := []struct{ name, code string }{
|
|
{"超级管理员", "SUPER_ADMIN"},
|
|
{"生产操作员", "OPERATOR"},
|
|
{"质检员", "INSPECTOR"},
|
|
}
|
|
for _, r := range roles {
|
|
if s.ctx.EntClient.Role.Query().Where(role.Code(r.code)).ExistX(ctx) {
|
|
continue
|
|
}
|
|
codes := []string{"*"}
|
|
if r.code == "OPERATOR" {
|
|
codes = []string{"produce.workorder", "produce.bom", "produce.material", "produce.scan", "produce.trace", "produce.torque", "produce.plc", "produce.product"}
|
|
} else if r.code == "INSPECTOR" {
|
|
codes = []string{"produce.trace", "produce.torque", "sys.eventlog"}
|
|
}
|
|
_ = s.ctx.EntClient.Role.Create().SetName(r.name).SetCode(r.code).SetPermissionCodes(codes).Exec(ctx)
|
|
}
|
|
|
|
// 2. 超级管理员账号 admin / Hardman_2026
|
|
if !s.ctx.EntClient.User.Query().Where(user.Username("admin")).ExistX(ctx) {
|
|
hash, _ := bcrypt.GenerateFromPassword([]byte("Hardman_2026"), bcrypt.DefaultCost)
|
|
adminRole, _ := s.ctx.EntClient.Role.Query().Where(role.Code("SUPER_ADMIN")).First(ctx)
|
|
roleId := 0
|
|
if adminRole != nil {
|
|
roleId = adminRole.ID
|
|
}
|
|
_ = s.ctx.EntClient.User.Create().
|
|
SetUsername("admin").SetPassword(string(hash)).SetName("系统管理员").
|
|
SetRoleId(roleId).SetStatus("ENABLED").Exec(ctx)
|
|
}
|
|
|
|
// 3. 默认菜单权限
|
|
menus := []struct{ code, name, typ, path string }{
|
|
{"produce.workorder", "工单管理", "MENU", "/work-order"},
|
|
{"produce.bom", "BOM", "MENU", "/bom"},
|
|
{"produce.material", "备料单", "MENU", "/material-request"},
|
|
{"produce.plc", "PLC工序下发", "MENU", "/plc-send"},
|
|
{"produce.torque", "拧紧查询", "MENU", "/torque"},
|
|
{"produce.scan", "扫码报工", "MENU", "/scan"},
|
|
{"produce.trace", "工件追溯", "MENU", "/trace"},
|
|
{"produce.product", "产品类型", "MENU", "/product-type"},
|
|
{"sys.eventlog", "操作日志", "MENU", "/event-log"},
|
|
{"sys.rbac", "角色权限管理", "MENU", "/rbac"},
|
|
}
|
|
for _, m := range menus {
|
|
if s.ctx.EntClient.Permission.Query().Where(permission.Code(m.code)).ExistX(ctx) {
|
|
continue
|
|
}
|
|
_ = s.ctx.EntClient.Permission.Create().
|
|
SetCode(m.code).SetName(m.name).SetType(m.typ).SetPath(m.path).Exec(ctx)
|
|
}
|
|
|
|
// 4. 12 道工序步骤模板(每道工序一个"装配完成"步骤 + 拧紧台阶步骤)
|
|
seedProcessSteps(ctx, s)
|
|
// 5. 12 工位 ↔ 工序 派工(工位N 默认做工序N)
|
|
for i := 1; i <= 12; i++ {
|
|
if s.ctx.EntClient.StationProcess.Query().Where(stationprocess.StationNo(i), stationprocess.ProcessCode(i)).ExistX(ctx) {
|
|
continue
|
|
}
|
|
_ = s.ctx.EntClient.StationProcess.Create().
|
|
SetStationNo(i).SetProcessCode(i).SetEffectDate("").Exec(ctx)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func seedProcessSteps(ctx context.Context, s *Service) {
|
|
for i := 1; i <= 12; i++ {
|
|
cnt, _ := s.ctx.EntClient.ProcessStep.Query().Where(processstep.ProcessCode(i)).Count(ctx)
|
|
if cnt > 0 {
|
|
continue
|
|
}
|
|
_ = s.ctx.EntClient.ProcessStep.Create().
|
|
SetProcessCode(i).SetSeq(1).SetName("装配完成").SetCollectType("NONE").Exec(ctx)
|
|
if i == 3 { // 示例:工序3 增加一个拧紧采集步骤
|
|
_ = s.ctx.EntClient.ProcessStep.Create().
|
|
SetProcessCode(3).SetSeq(2).SetName("拧紧扭矩").SetCollectType("AUTO").SetIsTorque(true).Exec(ctx)
|
|
}
|
|
}
|
|
}
|