Files
2021-11-25 13:53:18 +08:00

253 lines
7.9 KiB
Go

package common
import (
"ArmedPolice/app/common/model"
"ArmedPolice/config"
"ArmedPolice/serve/orm"
"ArmedPolice/utils"
"fmt"
)
type synchronized struct {
iModel model.IModel
iValues func() interface{}
Catch func() interface{}
}
type caches struct {
iModel model.IModel
iValues func() interface{}
toCache func(values interface{})
}
type (
// structForSysConfig 系统配置
structForSysConfig struct {
ID uint64 `json:"id"`
Kind int `json:"kind"`
Name string `json:"name"`
Key string `json:"key"`
Value string `json:"value"`
IsDeleted int `json:"is_deleted"`
}
// structForSysMenu 菜单配置
structForSysMenu struct {
ID uint64 `json:"id"`
ParentID uint64 `json:"parent_id"`
Name string `json:"name"`
Kind int `json:"kind"`
Link string `json:"link"`
Component string `json:"component"`
Icon string `json:"icon"`
Auth int `json:"auth"`
Sort int `json:"sort"`
Remark string `json:"remark"`
Status int `json:"status"`
IsDeleted int `json:"is_deleted"`
}
// structForSysRole 角色配置
structForSysRole struct {
ID uint64 `json:"id"`
TenantID uint64 `json:"tenant_id"`
ParentID uint64 `json:"parent_id"`
Name string `json:"name"`
Remark string `json:"remark"`
Sort int `json:"sort"`
IsDeleted int `json:"is_deleted"`
}
// structForWorkSchedule 工单流程配置
structForWorkSchedule struct {
ID uint64 `json:"id"`
Kind int `json:"kind"`
Title string `json:"title"`
Stage int `json:"stage"`
Step int `json:"step"`
Target int `json:"target"`
TargetValue string `json:"target_value"`
IsCountersign int `json:"is_countersign"`
IsDeleted int `json:"is_deleted"`
}
)
func initModel() {
db := orm.GetDB()
function := func(synchronized ...*synchronized) {
for _, v := range synchronized {
if !db.Migrator().HasTable(v.iModel) {
_ = db.Migrator().CreateTable(v.iModel)
if v.iValues != nil && v.iValues() != nil {
err := db.Table(v.iModel.TableName()).Create(v.iValues()).Error
if err != nil {
fmt.Println("=======================================")
fmt.Println(err)
fmt.Println("=======================================")
}
}
} else if v.Catch != nil && v.Catch() != nil {
v.Catch()
}
}
}
function(
&synchronized{iModel: model.NewSysTenant(), iValues: func() interface{} {
snowflake, _ := utils.NewSnowflake(1)
return &model.SysTenant{Key: utils.IntToString(snowflake.GetID()), Name: "中国武警总队", Remark: "中国武警总队"}
}},
&synchronized{iModel: model.NewSysConfig(), iValues: func() interface{} {
values := make([]*structForSysConfig, 0)
out := make([]*model.SysConfig, 0)
utils.LoadConfig("./json/sys_config.json", &values, func(i interface{}) {
for _, v := range values {
out = append(out, &model.SysConfig{
Model: model.Model{ID: v.ID},
Kind: model.SysConfigKind(v.Kind),
Name: v.Name,
Key: v.Key,
Value: v.Value,
ModelDeleted: model.ModelDeleted{IsDeleted: model.DeleteStatus(v.IsDeleted)},
})
}
})
return out
}},
&synchronized{iModel: model.NewSysMenu(), iValues: func() interface{} {
values := make([]*structForSysMenu, 0)
out := make([]*model.SysMenu, 0)
utils.LoadConfig("./json/sys_menu.json", &values, func(i interface{}) {
for _, v := range values {
out = append(out, &model.SysMenu{
Model: model.Model{ID: v.ID},
SysMenuBasic: model.SysMenuBasic{
ParentID: v.ParentID,
Name: v.Name,
Kind: model.SysMenuKind(v.Kind),
Link: v.Link,
Component: v.Component,
Icon: v.Icon,
},
Auth: model.SysMenuAuth(v.Auth),
Sort: v.Sort,
Remark: v.Remark,
Status: model.SysMenuStatus(v.Status),
ModelDeleted: model.ModelDeleted{IsDeleted: model.DeleteStatus(v.IsDeleted)},
})
}
})
return out
}}, &synchronized{iModel: model.NewSysAuth()},
&synchronized{iModel: model.NewSysUser(), iValues: func() interface{} {
return &model.SysUser{ModelTenant: model.ModelTenant{TenantID: 1}, Account: "admin", Name: "超级管理员",
Mobile: "13888888888", Password: "123456", IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"}
}},
&synchronized{iModel: model.NewSysRole(), iValues: func() interface{} {
out := make([]*model.SysRole, 0)
values := make([]*structForSysRole, 0)
utils.LoadConfig("./json/sys_role.json", &values, func(i interface{}) {
for _, v := range values {
out = append(out, &model.SysRole{
Model: model.Model{ID: v.ID},
ModelTenant: model.ModelTenant{TenantID: v.TenantID},
ParentID: v.ParentID,
Name: v.Name,
Remark: v.Remark,
Sort: v.Sort,
ModelDeleted: model.ModelDeleted{IsDeleted: model.DeleteStatus(v.IsDeleted)},
})
}
})
return out
}}, &synchronized{iModel: model.NewSysRoleMenu()}, &synchronized{iModel: model.NewSysRoleAuth()},
&synchronized{iModel: model.NewSysUserRole()},
// 日志管理
&synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()},
&synchronized{iModel: model.NewSysBreakdown()},
// 公告管理
&synchronized{iModel: model.NewManageNotice()},
// 功能信息
&synchronized{iModel: model.NewManageSupplier()}, &synchronized{iModel: model.NewManageSupplierEvaluate()},
&synchronized{iModel: model.NewManageEquipment()}, &synchronized{iModel: model.NewManageEquipmentMaterial()},
&synchronized{iModel: model.NewManageMaterial()}, &synchronized{iModel: model.NewManageMaterialSupplier()},
&synchronized{iModel: model.NewManageMaterialPurchase()}, &synchronized{iModel: model.NewManageMaterialWarehouse()},
&synchronized{iModel: model.NewWorkInstance()}, &synchronized{iModel: model.NewWorkMaterial()}, &synchronized{iModel: model.NewWorkProgress()},
&synchronized{iModel: model.NewWorkSchedule(), iValues: func() interface{} {
out := make([]*model.WorkSchedule, 0)
values := make([]*structForWorkSchedule, 0)
utils.LoadConfig("./json/work_schedule.json", &values, func(i interface{}) {
for _, v := range values {
out = append(out, &model.WorkSchedule{
Model: model.Model{ID: v.ID},
Kind: model.WorkScheduleKind(v.Kind),
Title: v.Title,
Stage: v.Stage,
Step: v.Step,
Target: model.WorkScheduleTarget(v.Target),
TargetValue: v.TargetValue,
IsCountersign: model.WorkScheduleCountersign(v.IsCountersign),
ModelDeleted: model.ModelDeleted{IsDeleted: model.DeleteStatus(v.IsDeleted)},
})
}
})
return out
},
}, &synchronized{iModel: model.NewWorkPurchase()},
&synchronized{iModel: model.NewWorkRepair()}, &synchronized{iModel: model.NewWorkRepairDetail()},
)
}
func initCacheMode() {
db := orm.GetDB()
function := func(cache ...*caches) {
for _, v := range cache {
if db.Migrator().HasTable(v.iModel) {
if v.iValues != nil {
if values := v.iValues(); values != nil {
v.toCache(values)
}
}
}
}
}
function(
&caches{iModel: model.NewSysConfig(), iValues: func() interface{} {
out := make([]*model.SysConfig, 0)
_ = model.Find(model.NewSysConfig(), &out)
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysConfig)
for _, v := range out {
config.SystemConfig[v.Key] = v.Value
}
}},
&caches{iModel: model.NewSysBreakdown(), iValues: func() interface{} {
out := make([]*model.SysBreakdown, 0)
_ = model.Find(model.NewSysBreakdown(), &out)
return out
}, toCache: func(values interface{}) {
out := values.([]*model.SysBreakdown)
for _, v := range out {
config.SystemBreakdown[v.ID] = v.Title
}
}},
)
}
func Init() {
if *config.Init {
initModel()
}
initCacheMode()
}