-
Notifications
You must be signed in to change notification settings - Fork 36
/
main.go
executable file
·253 lines (245 loc) · 9.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"hrms/handler"
"hrms/resource"
"log"
"net/http"
"os"
"strings"
)
func InitConfig() error {
config := &resource.Config{}
vip := viper.New()
vip.AddConfigPath("./config")
vip.SetConfigType("yaml")
// 环境判断
env := os.Getenv("HRMS_ENV")
if env == "" || env == "dev" {
// 开发环境
vip.SetConfigName("config-dev")
}
if env == "prod" {
// 生产环境
vip.SetConfigName("config-prod")
}
if env == "self" {
// 生产环境
vip.SetConfigName("config-self")
}
err := vip.ReadInConfig()
if err != nil {
log.Printf("[config.Init] err = %v", err)
return err
}
if err := vip.Unmarshal(config); err != nil {
log.Printf("[config.Init] err = %v", err)
return err
}
log.Printf("[config.Init] 初始化配置成功,config=%v", config)
resource.HrmsConf = config
return nil
}
func InitGin() error {
server := gin.Default()
// 静态资源及模板配置
htmlInit(server)
// 初始化路由
routerInit(server)
err := server.Run(fmt.Sprintf(":%v", resource.HrmsConf.Gin.Port))
if err != nil {
log.Printf("[InitGin] err = %v", err)
}
log.Printf("[InitGin] success")
return err
}
func routerInit(server *gin.Engine) {
// 测试
server.GET("/ping", handler.Ping)
// 权限重定向
server.GET("/authority_render/:modelName", handler.RenderAuthority)
// 首页重定向
server.GET("/index", handler.Index)
// 账户相关
accountGroup := server.Group("/account")
accountGroup.POST("/login", handler.Login)
accountGroup.POST("/quit", handler.Quit)
// 部门相关
departGroup := server.Group("/depart")
departGroup.POST("/create", handler.DepartCreate)
departGroup.DELETE("/del/:dep_id", handler.DepartDel)
departGroup.POST("/edit", handler.DepartEdit)
departGroup.GET("/query/:dep_id", handler.DepartQuery)
// 职级相关
rankGroup := server.Group("/rank")
rankGroup.POST("/create", handler.RankCreate)
rankGroup.DELETE("/del/:rank_id", handler.RankDel)
rankGroup.POST("/edit", handler.RankEdit)
rankGroup.GET("/query/:rank_id", handler.RankQuery)
// 员工信息相关
staffGroup := server.Group("/staff")
staffGroup.POST("/create", handler.StaffCreate)
staffGroup.POST("/excel_export", handler.ExcelExport)
staffGroup.DELETE("/del/:staff_id", handler.StaffDel)
staffGroup.POST("/edit", handler.StaffEdit)
staffGroup.GET("/query/:staff_id", handler.StaffQuery)
staffGroup.GET("/query_by_name/:staff_name", handler.StaffQueryByName)
staffGroup.GET("/query_by_dep/:dep_name", handler.StaffQueryByDep)
staffGroup.GET("/query_by_staff_id/:staff_id", handler.StaffQueryByStaffId)
// 密码管理信息相关
passwordGroup := server.Group("/password")
passwordGroup.GET("/query/:staff_id", handler.PasswordQuery)
passwordGroup.POST("/edit", handler.PasswordEdit)
// 授权信息相关
authorityGroup := server.Group("/authority")
authorityGroup.POST("/create", handler.AddAuthorityDetail)
authorityGroup.POST("/edit", handler.UpdateAuthorityDetailById)
authorityGroup.GET("/query_by_user_type/:user_type", handler.GetAuthorityDetailListByUserType)
authorityGroup.POST("/query_by_user_type_and_model", handler.GetAuthorityDetailByUserTypeAndModel)
authorityGroup.POST("/set_admin/:staff_id", handler.SetAdminByStaffId)
authorityGroup.POST("/set_normal/:staff_id", handler.SetNormalByStaffId)
// 通知相关
notificationGroup := server.Group("/notification")
notificationGroup.POST("/create", handler.CreateNotification)
notificationGroup.DELETE("/delete/:notice_id", handler.DeleteNotificationById)
notificationGroup.POST("/edit", handler.UpdateNotificationById)
notificationGroup.GET("/query/:notice_title", handler.GetNotificationByTitle)
// 分公司相关
companyGroup := server.Group("/company")
companyGroup.GET("/query", handler.BranchCompanyQuery)
// 薪资相关
salaryGroup := server.Group("/salary")
salaryGroup.POST("/create", handler.CreateSalary)
salaryGroup.DELETE("/delete/:salary_id", handler.DelSalary)
salaryGroup.POST("/edit", handler.UpdateSalaryById)
salaryGroup.GET("/query/:staff_id", handler.GetSalaryByStaffId)
// 薪资发放相关
salaryRecordGroup := server.Group("/salary_record")
//salaryRecordGroup.POST("/create", handler.CreateSalaryRecord)
//salaryRecordGroup.DELETE("/delete/:salary_record_id", handler.DelSalaryRecord)
//salaryRecordGroup.POST("/edit", handler.UpdateSalaryRecordById)
salaryRecordGroup.GET("/query/:staff_id", handler.GetSalaryRecordByStaffId)
salaryRecordGroup.GET("/get_salary_record_is_pay_by_id/:id", handler.GetSalaryRecordIsPayById)
salaryRecordGroup.GET("/pay_salary_record_by_id/:id", handler.PaySalaryRecordById)
salaryRecordGroup.GET("/query_history/:staff_id", handler.GetHadPaySalaryRecordByStaffId)
// 考勤相关
attendGroup := server.Group("/attendance_record")
attendGroup.POST("/create", handler.CreateAttendRecord)
attendGroup.DELETE("/delete/:attendance_id", handler.DelAttendRecordByAttendId)
attendGroup.POST("/edit", handler.UpdateAttendRecordById)
attendGroup.GET("/query/:staff_id", handler.GetAttendRecordByStaffId)
attendGroup.GET("/query_history/:staff_id", handler.GetAttendRecordHistoryByStaffId)
attendGroup.GET("/get_attend_record_is_pay/:staff_id/:date", handler.GetAttendRecordIsPayByStaffIdAndDate)
attendGroup.GET("/approve/query/:leader_staff_id", handler.GetAttendRecordApproveByLeaderStaffId)
attendGroup.GET("/approve_accept/:attendId", handler.ApproveAccept)
attendGroup.GET("/approve_reject/:attendId", handler.ApproveReject)
// 招聘信息相关
recruitmentGroup := server.Group("/recruitment")
recruitmentGroup.POST("/create", handler.CreateRecruitment)
recruitmentGroup.DELETE("/delete/:recruitment_id", handler.DelRecruitmentByRecruitmentId)
recruitmentGroup.POST("/edit", handler.UpdateRecruitmentById)
recruitmentGroup.GET("/query/:job_name", handler.GetRecruitmentByJobName)
// 候选人管理相关
candidateGroup := server.Group("/candidate")
candidateGroup.POST("/create", handler.CreateCandidate)
candidateGroup.DELETE("/delete/:candidate_id", handler.DelCandidateByCandidateId)
candidateGroup.POST("/edit", handler.UpdateCandidateById)
candidateGroup.GET("/query_by_name/:name", handler.GetCandidateByName)
candidateGroup.GET("/query_by_staff_id/:staff_id", handler.GetCandidateByStaffId)
candidateGroup.GET("/reject/:id", handler.SetCandidateRejectById)
candidateGroup.GET("/accept/:id", handler.SetCandidateAcceptById)
// 考试管理相关
exampleGroup := server.Group("/example")
exampleGroup.POST("/create", handler.CreateExample)
exampleGroup.POST("/parse_example_content", handler.ParseExampleContent)
exampleGroup.DELETE("/delete/:example_id", handler.DelExample)
exampleGroup.POST("/edit", handler.UpdateExampleById)
exampleGroup.GET("/query/:name", handler.GetExampleByName)
exampleGroup.GET("/render_example/:id", handler.RenderExample)
// 考试成绩相关
exampleScoreGroup := server.Group("/example_score")
exampleScoreGroup.POST("/create", handler.CreateExampleScore)
exampleScoreGroup.GET("/query_by_name/:name", handler.GetExampleHistoryByName)
exampleScoreGroup.GET("/query_by_staff_id/:staff_id", handler.GetExampleHistoryByStafId)
}
func htmlInit(server *gin.Engine) {
// 静态资源
server.StaticFS("/static", http.Dir("./static"))
server.StaticFS("/views", http.Dir("./views"))
// HTML模板加载
server.LoadHTMLGlob("views/*")
// 404页面
server.NoRoute(func(c *gin.Context) {
c.HTML(404, "404.html", nil)
})
}
func InitGorm() error {
// "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
// 对每个分公司数据库进行连接
dbNames := resource.HrmsConf.Db.DbName
dbNameList := strings.Split(dbNames, ",")
for index, dbName := range dbNameList {
dsn := fmt.Sprintf(
"%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
resource.HrmsConf.Db.User,
resource.HrmsConf.Db.Password,
resource.HrmsConf.Db.Host,
resource.HrmsConf.Db.Port,
dbName,
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
// 全局禁止表名复数
SingularTable: true,
},
// 日志等级
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
log.Printf("[InitGorm] err = %v", err)
return err
}
// 添加到映射表中
resource.DbMapper[dbName] = db
// 第一个是默认DB,用以启动程序选择分公司
if index == 0 {
resource.DefaultDb = db
}
log.Printf("[InitGorm] 分公司数据库%v注册成功", dbName)
}
//fmt.Println(resource.DbMapper["hrms_C001"])
log.Printf("[InitGorm] success")
return nil
}
//func InitMongo() error {
// mongo := resource.HrmsConf.Mongo
// var err error
// resource.MongoClient, err = qmgo.NewClient(context.Background(), &qmgo.Config{
// Uri: fmt.Sprintf("mongodb://%v:%v", mongo.IP, mongo.Port),
// Database: mongo.Dataset,
// })
// if err != nil {
// return err
// }
// return nil
//}
func main() {
if err := InitConfig(); err != nil {
log.Fatal(err)
}
if err := InitGorm(); err != nil {
log.Fatal(err)
}
if err := InitGin(); err != nil {
log.Fatal(err)
}
//if err := InitMongo(); err != nil {
// log.Fatal(err)
//}
}