database.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. package main
  2. import (
  3. "context"
  4. "os"
  5. "path/filepath"
  6. "runtime"
  7. "syscall"
  8. "time"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/bson/primitive"
  11. "go.mongodb.org/mongo-driver/mongo"
  12. "go.mongodb.org/mongo-driver/mongo/options"
  13. )
  14. var databaseManager = new(MongoDbManager)
  15. type MongoDbManager struct {
  16. client *mongo.Client
  17. Admins *mongo.Collection
  18. Departments *mongo.Collection
  19. Employees *mongo.Collection
  20. WorkingDayStatistics *mongo.Collection
  21. StatisticsTables *mongo.Collection
  22. WorkingDayInfos *mongo.Collection
  23. HolidayInfos *mongo.Collection
  24. ViewCodes *mongo.Collection
  25. EmailSettings *mongo.Collection
  26. DomainSettings *mongo.Collection
  27. MessageTemplates *mongo.Collection
  28. AttendanceDatas *mongo.Collection
  29. DashBoards *mongo.Collection
  30. }
  31. func (manager *MongoDbManager) AddAttendanceData(attendanceData *AttendanceData) {
  32. _, err := manager.AttendanceDatas.InsertOne(context.TODO(), *attendanceData)
  33. if err != nil {
  34. Logger.Error(err.Error())
  35. panic(any(err))
  36. }
  37. }
  38. func (manager *MongoDbManager) GetAttendanceDatas(employeeId string, start time.Time, end time.Time) []*AttendanceData {
  39. var result []*AttendanceData
  40. sort := bson.D{{Key: "day", Value: -1}}
  41. findOptions := options.Find()
  42. findOptions.SetSort(sort)
  43. filter := bson.M{"employeeid": employeeId, "day": bson.M{"$gte": start, "$lte": end}}
  44. cursor, err := manager.AttendanceDatas.Find(context.TODO(), filter, findOptions)
  45. if err == nil {
  46. defer func() {
  47. err := cursor.Close(context.TODO())
  48. if err != nil {
  49. return
  50. }
  51. }()
  52. for cursor.Next(context.TODO()) {
  53. var attendanceData AttendanceData
  54. err := cursor.Decode(&attendanceData)
  55. if err != nil {
  56. Logger.Error(err.Error())
  57. return nil
  58. }
  59. result = append(result, &attendanceData)
  60. }
  61. if err := cursor.Err(); err != nil {
  62. Logger.Error(err.Error())
  63. return nil
  64. } else {
  65. return result
  66. }
  67. }
  68. return nil
  69. }
  70. func (manager *MongoDbManager) GetEmployeeCount() int64 {
  71. count, err := manager.Employees.EstimatedDocumentCount(context.TODO())
  72. if err != nil {
  73. Logger.Error(err.Error())
  74. panic(any(err))
  75. }
  76. return count
  77. }
  78. func (manager *MongoDbManager) GetStruggleEmployeeCount() int64 {
  79. filter := bson.D{{Key: "isstruggle", Value: true}}
  80. count, err := manager.Employees.CountDocuments(context.TODO(), filter)
  81. if err != nil {
  82. Logger.Error(err.Error())
  83. panic(any(err))
  84. }
  85. return count
  86. }
  87. func (manager *MongoDbManager) AddHolidayInfo(holidayInfo *HolidayInfo) {
  88. _, err := manager.HolidayInfos.InsertOne(context.TODO(), *holidayInfo)
  89. if err != nil {
  90. Logger.Error(err.Error())
  91. panic(any(err))
  92. }
  93. }
  94. func (manager *MongoDbManager) DeleteHolidayInfo(holidayInfoId string) {
  95. filter := bson.D{{Key: "id", Value: holidayInfoId}}
  96. _, err := manager.HolidayInfos.DeleteOne(context.TODO(), filter)
  97. if err != nil {
  98. Logger.Error(err.Error())
  99. panic(any(err))
  100. }
  101. }
  102. func (manager *MongoDbManager) GetHolidayInfo(day time.Time) *HolidayInfo {
  103. filter := bson.D{{Key: "holiday", Value: day}}
  104. result := manager.HolidayInfos.FindOne(context.TODO(), filter)
  105. if result != nil {
  106. var holidayInfo = new(HolidayInfo)
  107. err := result.Decode(holidayInfo)
  108. if err == nil {
  109. return holidayInfo
  110. }
  111. }
  112. return nil
  113. }
  114. func (manager *MongoDbManager) UpdateHolidayInfo(holidayInfo *HolidayInfo) {
  115. filter := bson.D{{Key: "id", Value: holidayInfo.Id}}
  116. update := bson.D{{Key: "$set", Value: bson.D{{Key: "title", Value: holidayInfo.Title}, {Key: "holiday", Value: holidayInfo.Holiday}, {Key: "isadjusted", Value: holidayInfo.IsAdjusted}, {Key: "adjustedday", Value: holidayInfo.AdjustedDay}, {Key: "isnationallegal", Value: holidayInfo.IsNationalLegal}}}}
  117. _, err := manager.HolidayInfos.UpdateOne(context.TODO(), filter, update)
  118. if err != nil {
  119. Logger.Error(err.Error())
  120. panic(any(err))
  121. }
  122. }
  123. func (manager *MongoDbManager) GetHolidayDayInfos(start time.Time, end time.Time) []*HolidayInfo {
  124. var result []*HolidayInfo
  125. sort := bson.D{{Key: "holiday", Value: -1}}
  126. findOptions := options.Find()
  127. findOptions.SetSort(sort)
  128. filter := bson.M{"holiday": bson.M{"$gte": start, "$lte": end}}
  129. cursor, err := manager.HolidayInfos.Find(context.TODO(), filter, findOptions)
  130. if err == nil {
  131. defer func() {
  132. err := cursor.Close(context.TODO())
  133. if err != nil {
  134. return
  135. }
  136. }()
  137. for cursor.Next(context.TODO()) {
  138. var holidayInfo HolidayInfo
  139. err := cursor.Decode(&holidayInfo)
  140. if err != nil {
  141. Logger.Error(err.Error())
  142. return nil
  143. }
  144. result = append(result, &holidayInfo)
  145. }
  146. if err := cursor.Err(); err != nil {
  147. Logger.Error(err.Error())
  148. return nil
  149. } else {
  150. return result
  151. }
  152. }
  153. return nil
  154. }
  155. func (manager *MongoDbManager) AddEmployee(employee *Employee) {
  156. _, err := manager.Employees.InsertOne(context.TODO(), *employee)
  157. if err != nil {
  158. Logger.Error(err.Error())
  159. panic(any(err))
  160. }
  161. }
  162. func (manager *MongoDbManager) DeleteEmployee(employeeId string) {
  163. filter := bson.D{{Key: "id", Value: employeeId}}
  164. _, err := manager.Employees.DeleteOne(context.TODO(), filter)
  165. if err != nil {
  166. Logger.Error(err.Error())
  167. panic(any(err))
  168. }
  169. }
  170. func (manager *MongoDbManager) UpdateEmployee(employee *Employee) {
  171. filter := bson.D{{Key: "id", Value: employee.Id}}
  172. update := bson.D{{Key: "$set", Value: bson.D{{Key: "name", Value: employee.Name}, {Key: "title", Value: employee.Title}, {Key: "email", Value: employee.Email}, {Key: "attendancerules", Value: employee.AttendanceRules}, {Key: "departmentid", Value: employee.DepartmentId}}}}
  173. opts := options.Update().SetUpsert(false)
  174. _, err := manager.Employees.UpdateOne(context.TODO(), filter, update, opts)
  175. if err != nil {
  176. Logger.Error(err.Error())
  177. panic(any(err))
  178. }
  179. }
  180. func (manager *MongoDbManager) UpdateEmployeeDepartment(employee *Employee) {
  181. filter := bson.D{{Key: "id", Value: employee.Id}}
  182. update := bson.D{{Key: "$set", Value: bson.D{{Key: "departmentid", Value: employee.DepartmentId}}}}
  183. opts := options.Update().SetUpsert(false)
  184. _, err := manager.Employees.UpdateOne(context.TODO(), filter, update, opts)
  185. if err != nil {
  186. Logger.Error(err.Error())
  187. panic(any(err))
  188. }
  189. }
  190. func (manager *MongoDbManager) UpdateEmployeeIsStruggle(employee *Employee) {
  191. filter := bson.D{{Key: "id", Value: employee.Id}}
  192. update := bson.D{{Key: "$set", Value: bson.D{{Key: "isstruggle", Value: employee.IsStruggle}}}}
  193. opts := options.Update().SetUpsert(false)
  194. _, err := manager.Employees.UpdateOne(context.TODO(), filter, update, opts)
  195. if err != nil {
  196. Logger.Error(err.Error())
  197. panic(any(err))
  198. }
  199. }
  200. func (manager *MongoDbManager) UpdateEmployeeIsReviewer(employee *Employee) {
  201. filter := bson.D{{Key: "id", Value: employee.Id}}
  202. update := bson.D{{Key: "$set", Value: bson.D{{Key: "isreviewer", Value: employee.IsReviewer}}}}
  203. opts := options.Update().SetUpsert(false)
  204. _, err := manager.Employees.UpdateOne(context.TODO(), filter, update, opts)
  205. if err != nil {
  206. Logger.Error(err.Error())
  207. panic(any(err))
  208. }
  209. }
  210. func (manager *MongoDbManager) GetEmployeeById(employeeId string) *Employee {
  211. filter := bson.D{{Key: "id", Value: employeeId}}
  212. result := manager.Employees.FindOne(context.TODO(), filter)
  213. if result != nil {
  214. var employee = new(Employee)
  215. err := result.Decode(employee)
  216. if err == nil {
  217. return employee
  218. }
  219. }
  220. return nil
  221. }
  222. func (manager *MongoDbManager) GetEmployeeByEmail(email string) *Employee {
  223. filter := bson.D{{Key: "email", Value: email}}
  224. result := manager.Employees.FindOne(context.TODO(), filter)
  225. if result != nil {
  226. var employee = new(Employee)
  227. err := result.Decode(employee)
  228. if err == nil {
  229. return employee
  230. }
  231. }
  232. return nil
  233. }
  234. func (manager *MongoDbManager) GetEmployeeByName(employeeName string) *Employee {
  235. filter := bson.D{{Key: "name", Value: employeeName}}
  236. result := manager.Employees.FindOne(context.TODO(), filter)
  237. if result != nil {
  238. var employee = new(Employee)
  239. err := result.Decode(employee)
  240. if err == nil {
  241. return employee
  242. }
  243. }
  244. return nil
  245. }
  246. func (manager *MongoDbManager) GetEmployeeMany(startIndex int, count int) []*Employee {
  247. var result []*Employee
  248. sort := bson.D{{Key: "name", Value: 1}}
  249. filter := bson.D{{}}
  250. findOptions := options.Find()
  251. findOptions.SetSort(sort)
  252. findOptions.SetLimit(int64(count))
  253. findOptions.SetSkip(int64(startIndex))
  254. cursor, err := manager.Employees.Find(context.TODO(), filter, findOptions)
  255. if err == nil {
  256. defer func() {
  257. err := cursor.Close(context.TODO())
  258. if err != nil {
  259. return
  260. }
  261. }()
  262. for cursor.Next(context.TODO()) {
  263. var employee Employee
  264. err := cursor.Decode(&employee)
  265. if err != nil {
  266. Logger.Error(err.Error())
  267. return nil
  268. }
  269. result = append(result, &employee)
  270. }
  271. if err := cursor.Err(); err != nil {
  272. Logger.Error(err.Error())
  273. return nil
  274. } else {
  275. return result
  276. }
  277. }
  278. return nil
  279. }
  280. func (manager *MongoDbManager) GetEmployeeManyByName(name string, startIndex int, count int) []*Employee {
  281. var result []*Employee
  282. sort := bson.D{{Key: "name", Value: 1}}
  283. filter := bson.D{{Key: "name", Value: name}}
  284. findOptions := options.Find()
  285. findOptions.SetSort(sort)
  286. findOptions.SetLimit(int64(count))
  287. findOptions.SetSkip(int64(startIndex))
  288. cursor, err := manager.Employees.Find(context.TODO(), filter, findOptions)
  289. if err == nil {
  290. defer func() {
  291. err := cursor.Close(context.TODO())
  292. if err != nil {
  293. return
  294. }
  295. }()
  296. for cursor.Next(context.TODO()) {
  297. var employee Employee
  298. err := cursor.Decode(&employee)
  299. if err != nil {
  300. Logger.Error(err.Error())
  301. return nil
  302. }
  303. result = append(result, &employee)
  304. }
  305. if err := cursor.Err(); err != nil {
  306. Logger.Error(err.Error())
  307. return nil
  308. } else {
  309. return result
  310. }
  311. }
  312. return nil
  313. }
  314. func (manager *MongoDbManager) GetReviewers() []*Employee {
  315. var result []*Employee
  316. sort := bson.D{{Key: "name", Value: 1}}
  317. filter := bson.D{{Key: "isreviewer", Value: true}}
  318. findOptions := options.Find()
  319. findOptions.SetSort(sort)
  320. cursor, err := manager.Employees.Find(context.TODO(), filter, findOptions)
  321. if err == nil {
  322. defer func() {
  323. err := cursor.Close(context.TODO())
  324. if err != nil {
  325. return
  326. }
  327. }()
  328. for cursor.Next(context.TODO()) {
  329. var employee Employee
  330. err := cursor.Decode(&employee)
  331. if err != nil {
  332. Logger.Error(err.Error())
  333. return nil
  334. }
  335. result = append(result, &employee)
  336. }
  337. if err := cursor.Err(); err != nil {
  338. Logger.Error(err.Error())
  339. return nil
  340. } else {
  341. return result
  342. }
  343. }
  344. return nil
  345. }
  346. func (manager *MongoDbManager) GetEmployeeManyByDepartmentId(departmentId int) []*Employee {
  347. var result []*Employee
  348. sort := bson.D{{Key: "name", Value: 1}}
  349. filter := bson.D{{Key: "departmentid", Value: departmentId}}
  350. findOptions := options.Find()
  351. findOptions.SetSort(sort)
  352. cursor, err := manager.Employees.Find(context.TODO(), filter, findOptions)
  353. if err == nil {
  354. defer func() {
  355. err := cursor.Close(context.TODO())
  356. if err != nil {
  357. return
  358. }
  359. }()
  360. for cursor.Next(context.TODO()) {
  361. var employee Employee
  362. err := cursor.Decode(&employee)
  363. if err != nil {
  364. Logger.Error(err.Error())
  365. return nil
  366. }
  367. result = append(result, &employee)
  368. }
  369. if err := cursor.Err(); err != nil {
  370. Logger.Error(err.Error())
  371. return nil
  372. } else {
  373. return result
  374. }
  375. }
  376. return nil
  377. }
  378. func (manager *MongoDbManager) GetStruggleEmployeeMany() []*Employee {
  379. var result []*Employee
  380. sort := bson.D{{Key: "name", Value: 1}}
  381. filter := bson.D{{Key: "isstruggle", Value: true}}
  382. findOptions := options.Find()
  383. findOptions.SetSort(sort)
  384. cursor, err := manager.Employees.Find(context.TODO(), filter, findOptions)
  385. if err == nil {
  386. defer func() {
  387. err := cursor.Close(context.TODO())
  388. if err != nil {
  389. return
  390. }
  391. }()
  392. for cursor.Next(context.TODO()) {
  393. var employee Employee
  394. err := cursor.Decode(&employee)
  395. if err != nil {
  396. Logger.Error(err.Error())
  397. return nil
  398. }
  399. result = append(result, &employee)
  400. }
  401. if err := cursor.Err(); err != nil {
  402. Logger.Error(err.Error())
  403. return nil
  404. } else {
  405. return result
  406. }
  407. }
  408. return nil
  409. }
  410. func (manager *MongoDbManager) AddWorkingDayInfo(workingDayInfo *WorkingDayInfo) {
  411. _, err := manager.WorkingDayInfos.InsertOne(context.TODO(), *workingDayInfo)
  412. if err != nil {
  413. Logger.Error(err.Error())
  414. panic(any(err))
  415. }
  416. }
  417. func (manager *MongoDbManager) DeleteWorkingDayInfo(workingDayInfoId string) {
  418. filter := bson.D{{Key: "id", Value: workingDayInfoId}}
  419. _, err := manager.WorkingDayInfos.DeleteOne(context.TODO(), filter)
  420. if err != nil {
  421. Logger.Error(err.Error())
  422. panic(any(err))
  423. }
  424. }
  425. func (manager *MongoDbManager) UpdateWorkingDayInfo(workingDayInfo *WorkingDayInfo) {
  426. filter := bson.D{{Key: "id", Value: workingDayInfo.Id}}
  427. update := bson.D{{Key: "$set", Value: bson.D{{Key: "adjusted", Value: workingDayInfo.Adjusted}, {Key: "adjustedObtainedunits", Value: workingDayInfo.AdjustedObtainedUnits}, {Key: "adjustdescription", Value: workingDayInfo.AdjustDescription}}}}
  428. _, err := manager.WorkingDayInfos.UpdateOne(context.TODO(), filter, update)
  429. if err != nil {
  430. Logger.Error(err.Error())
  431. panic(any(err))
  432. }
  433. }
  434. func (manager *MongoDbManager) GetWorkingDayInfoById(workingDayInfoId string) *WorkingDayInfo {
  435. filter := bson.D{{Key: "id", Value: workingDayInfoId}}
  436. result := manager.WorkingDayInfos.FindOne(context.TODO(), filter)
  437. if result != nil {
  438. var workingDayInfo = new(WorkingDayInfo)
  439. err := result.Decode(workingDayInfo)
  440. if err == nil {
  441. return workingDayInfo
  442. }
  443. }
  444. return nil
  445. }
  446. func (manager *MongoDbManager) GetWorkingDayInfoMany(employeeId string, statisticsId string) []*WorkingDayInfo {
  447. var result []*WorkingDayInfo
  448. sort := bson.D{{Key: "day", Value: 1}}
  449. filter := bson.M{"employeeid": employeeId, "statisticsid": statisticsId}
  450. findOptions := options.Find()
  451. findOptions.SetSort(sort)
  452. cursor, err := manager.WorkingDayInfos.Find(context.TODO(), filter, findOptions)
  453. if err == nil {
  454. defer func() {
  455. err := cursor.Close(context.TODO())
  456. if err != nil {
  457. return
  458. }
  459. }()
  460. for cursor.Next(context.TODO()) {
  461. var workingDayInfo WorkingDayInfo
  462. err := cursor.Decode(&workingDayInfo)
  463. if err != nil {
  464. Logger.Error(err.Error())
  465. return nil
  466. }
  467. result = append(result, &workingDayInfo)
  468. }
  469. if err := cursor.Err(); err != nil {
  470. Logger.Error(err.Error())
  471. return nil
  472. } else {
  473. return result
  474. }
  475. }
  476. return nil
  477. }
  478. func (manager *MongoDbManager) AddWorkingDayStatistics(workingDayStatistics *WorkingDayStatistics) {
  479. _, err := manager.WorkingDayStatistics.InsertOne(context.TODO(), *workingDayStatistics)
  480. if err != nil {
  481. Logger.Error(err.Error())
  482. panic(any(err))
  483. }
  484. }
  485. func (manager *MongoDbManager) GetWorkingDayStatisticsById(workingDayStatisticsId string) *WorkingDayStatistics {
  486. filter := bson.D{{Key: "id", Value: workingDayStatisticsId}}
  487. result := manager.WorkingDayStatistics.FindOne(context.TODO(), filter)
  488. if result != nil {
  489. var workingDayStatistics = new(WorkingDayStatistics)
  490. err := result.Decode(workingDayStatistics)
  491. if err == nil {
  492. return workingDayStatistics
  493. }
  494. }
  495. return nil
  496. }
  497. func (manager *MongoDbManager) GetWorkingDayStatisticsByIds(workingDayStatisticsIds []string) []*WorkingDayStatistics {
  498. var result []*WorkingDayStatistics
  499. for i := 0; i < len(workingDayStatisticsIds); i++ {
  500. result = append(result, manager.GetWorkingDayStatisticsById(workingDayStatisticsIds[i]))
  501. }
  502. return result
  503. }
  504. func (manager *MongoDbManager) GetWorkingDayStatisticsByStartEnd(employeeId string, start, end time.Time) *WorkingDayStatistics {
  505. filter := bson.M{"employee.id": employeeId, "day": bson.M{"$gte": start, "$lte": end}}
  506. result := manager.WorkingDayInfos.FindOne(context.TODO(), filter)
  507. if result != nil {
  508. var workingDayStatistics = new(WorkingDayStatistics)
  509. err := result.Decode(workingDayStatistics)
  510. if err == nil {
  511. return workingDayStatistics
  512. }
  513. }
  514. return nil
  515. }
  516. func (manager *MongoDbManager) UpdateWorkingDayStatistics(workingDayStatistics *WorkingDayStatistics) {
  517. filter := bson.D{{Key: "id", Value: workingDayStatistics.Id}}
  518. update := bson.M{
  519. "$set": bson.M{
  520. "adjusted": workingDayStatistics.Adjusted,
  521. "adjustedby": workingDayStatistics.AdjustedBy,
  522. "adjustdescription": workingDayStatistics.AdjustDescription,
  523. "comment": workingDayStatistics.Comment,
  524. "adjustedreachedcount": workingDayStatistics.AdjustedReachedCount,
  525. "reviewer": workingDayStatistics.Reviewer,
  526. },
  527. }
  528. _, err := manager.WorkingDayStatistics.UpdateOne(context.TODO(), filter, update)
  529. if err != nil {
  530. Logger.Error(err.Error())
  531. panic(any(err))
  532. }
  533. }
  534. func (manager *MongoDbManager) UpdateWorkingDayStatisticsComment(workingDayStatisticsId string, comment string) {
  535. filter := bson.D{{Key: "id", Value: workingDayStatisticsId}}
  536. update := bson.M{
  537. "$set": bson.M{
  538. "comment": comment,
  539. },
  540. }
  541. _, err := manager.WorkingDayStatistics.UpdateOne(context.TODO(), filter, update)
  542. if err != nil {
  543. Logger.Error(err.Error())
  544. panic(any(err))
  545. }
  546. }
  547. func (manager *MongoDbManager) AddStatisticsTable(statisticsTable *StatisticsTable) {
  548. _, err := manager.StatisticsTables.InsertOne(context.TODO(), *statisticsTable)
  549. if err != nil {
  550. Logger.Error(err.Error())
  551. panic(any(err))
  552. }
  553. }
  554. func (manager *MongoDbManager) GetStatisticsTableByTitle(title string) *StatisticsTable {
  555. filter := bson.D{{Key: "title", Value: title}}
  556. result := manager.StatisticsTables.FindOne(context.TODO(), filter)
  557. if result != nil {
  558. var statisticsTable = new(StatisticsTable)
  559. err := result.Decode(statisticsTable)
  560. if err == nil {
  561. return statisticsTable
  562. }
  563. }
  564. return nil
  565. }
  566. func (manager *MongoDbManager) GetStatisticsTableById(id string) *StatisticsTable {
  567. filter := bson.D{{Key: "id", Value: id}}
  568. result := manager.StatisticsTables.FindOne(context.TODO(), filter)
  569. if result != nil {
  570. var statisticsTable = new(StatisticsTable)
  571. err := result.Decode(statisticsTable)
  572. if err == nil {
  573. return statisticsTable
  574. }
  575. }
  576. return nil
  577. }
  578. func (manager *MongoDbManager) DeleteStatisticsTable(statisticsTableId string) {
  579. filter := bson.D{{Key: "id", Value: statisticsTableId}}
  580. _, err := manager.StatisticsTables.DeleteOne(context.TODO(), filter)
  581. if err != nil {
  582. Logger.Error(err.Error())
  583. panic(any(err))
  584. }
  585. }
  586. func (manager *MongoDbManager) GetStatisticsTableMany() []*StatisticsTable {
  587. var result []*StatisticsTable
  588. filter := bson.D{}
  589. sort := bson.D{{Key: "createtime", Value: -1}}
  590. findOptions := options.Find()
  591. findOptions.SetSort(sort)
  592. findOptions.SetLimit(int64(12))
  593. findOptions.SetSkip(int64(0))
  594. cursor, err := manager.StatisticsTables.Find(context.TODO(), filter, findOptions)
  595. if err == nil {
  596. defer func() {
  597. err := cursor.Close(context.TODO())
  598. if err != nil {
  599. return
  600. }
  601. }()
  602. for cursor.Next(context.TODO()) {
  603. var statisticsTable StatisticsTable
  604. err := cursor.Decode(&statisticsTable)
  605. if err != nil {
  606. Logger.Error(err.Error())
  607. return nil
  608. }
  609. result = append(result, &statisticsTable)
  610. }
  611. if err := cursor.Err(); err != nil {
  612. Logger.Error(err.Error())
  613. return nil
  614. } else {
  615. return result
  616. }
  617. }
  618. return nil
  619. }
  620. func (manager *MongoDbManager) AddDepartment(dep *Department) {
  621. _, err := manager.Departments.InsertOne(context.TODO(), *dep)
  622. if err != nil {
  623. Logger.Error(err.Error())
  624. panic(any(err))
  625. }
  626. }
  627. func (manager *MongoDbManager) DeleteDepartment(depId string) {
  628. filter := bson.D{{Key: "id", Value: depId}}
  629. _, err := manager.Departments.DeleteOne(context.TODO(), filter)
  630. if err != nil {
  631. Logger.Error(err.Error())
  632. panic(any(err))
  633. }
  634. }
  635. func (manager *MongoDbManager) UpdateDepartment(dep *Department) {
  636. filter := bson.D{{Key: "id", Value: dep.Id}}
  637. update := bson.D{{Key: "$set", Value: bson.D{{Key: "name", Value: dep.Name}, {Key: "parentid", Value: dep.ParentId}}}}
  638. opts := options.Update().SetUpsert(false)
  639. _, err := manager.Departments.UpdateOne(context.TODO(), filter, update, opts)
  640. if err != nil {
  641. Logger.Error(err.Error())
  642. panic(any(err))
  643. }
  644. }
  645. func (manager *MongoDbManager) UpdateDepartmentParent(dep *Department) {
  646. filter := bson.D{{Key: "id", Value: dep.Id}}
  647. update := bson.D{{Key: "$set", Value: bson.D{{Key: "parentid", Value: dep.ParentId}}}}
  648. opts := options.Update().SetUpsert(false)
  649. _, err := manager.Departments.UpdateOne(context.TODO(), filter, update, opts)
  650. if err != nil {
  651. Logger.Error(err.Error())
  652. panic(any(err))
  653. }
  654. }
  655. func (manager *MongoDbManager) GetDepartmentById(depId int) *Department {
  656. filter := bson.D{{Key: "id", Value: depId}}
  657. result := manager.Departments.FindOne(context.TODO(), filter)
  658. if result != nil {
  659. var dep = new(Department)
  660. err := result.Decode(dep)
  661. if err == nil {
  662. return dep
  663. }
  664. }
  665. return nil
  666. }
  667. func (manager *MongoDbManager) GetDepartmentsByParentId(parentId int) []*Department {
  668. var result []*Department
  669. sort := bson.D{{Key: "name", Value: -1}}
  670. filter := bson.D{{Key: "parentid", Value: parentId}}
  671. findOptions := options.Find()
  672. findOptions.SetSort(sort)
  673. cursor, err := manager.Departments.Find(context.TODO(), filter, findOptions)
  674. if err == nil {
  675. defer func() { cursor.Close(context.TODO()) }()
  676. for cursor.Next(context.TODO()) {
  677. var department Department
  678. err := cursor.Decode(&department)
  679. if err != nil {
  680. Logger.Error(err.Error())
  681. return nil
  682. }
  683. result = append(result, &department)
  684. }
  685. if err := cursor.Err(); err != nil {
  686. Logger.Error(err.Error())
  687. return nil
  688. } else {
  689. return result
  690. }
  691. }
  692. return nil
  693. }
  694. func (manager *MongoDbManager) GetDepartmentByName(depName string) *Department {
  695. filter := bson.D{{Key: "name", Value: depName}}
  696. result := manager.Departments.FindOne(context.TODO(), filter)
  697. if result != nil {
  698. var dep = new(Department)
  699. err := result.Decode(dep)
  700. if err == nil {
  701. return dep
  702. }
  703. }
  704. return nil
  705. }
  706. func (manager *MongoDbManager) UpdateAdminPassword(admin *Admin) {
  707. filter := bson.D{{Key: "id", Value: admin.Id}}
  708. update := bson.D{{Key: "$set", Value: bson.D{{Key: "password", Value: admin.Password}}}}
  709. opts := options.Update().SetUpsert(false)
  710. _, err := manager.Admins.UpdateOne(context.TODO(), filter, update, opts)
  711. if err != nil {
  712. Logger.Error(err.Error())
  713. panic(any(err))
  714. }
  715. }
  716. func (manager *MongoDbManager) AddAdmin(admin *Admin) {
  717. _, err := manager.Admins.InsertOne(context.TODO(), *admin)
  718. if err != nil {
  719. Logger.Error(err.Error())
  720. panic(any(err))
  721. }
  722. }
  723. func (manager *MongoDbManager) GetAdminByName(name string) *Admin {
  724. filter := bson.M{"name": primitive.Regex{Pattern: name, Options: "i"}}
  725. result := manager.Admins.FindOne(context.TODO(), filter)
  726. if result != nil {
  727. var admin = new(Admin)
  728. err := result.Decode(admin)
  729. if err == nil {
  730. return admin
  731. }
  732. }
  733. return nil
  734. }
  735. func (manager *MongoDbManager) GetAdminById(id string) *Admin {
  736. filter := bson.D{{Key: "id", Value: id}}
  737. result := manager.Admins.FindOne(context.TODO(), filter)
  738. if result != nil {
  739. var admin = new(Admin)
  740. err := result.Decode(admin)
  741. if err == nil {
  742. return admin
  743. }
  744. }
  745. return nil
  746. }
  747. func (manager *MongoDbManager) AddViewCode(viewCode *ViewCode) {
  748. _, err := manager.ViewCodes.InsertOne(context.TODO(), *viewCode)
  749. if err != nil {
  750. Logger.Error(err.Error())
  751. panic(any(err))
  752. }
  753. }
  754. func (manager *MongoDbManager) GetViewCode(code string) *ViewCode {
  755. filter := bson.D{{Key: "code", Value: code}}
  756. result := manager.ViewCodes.FindOne(context.TODO(), filter)
  757. if result != nil {
  758. var viewCode = new(ViewCode)
  759. err := result.Decode(viewCode)
  760. if err == nil {
  761. return viewCode
  762. }
  763. }
  764. return nil
  765. }
  766. func (manager *MongoDbManager) AddEmailSetting(emailSetting *EmailSetting) {
  767. _, err := manager.EmailSettings.InsertOne(context.TODO(), *emailSetting)
  768. if err != nil {
  769. Logger.Error(err.Error())
  770. panic(any(err))
  771. }
  772. }
  773. func (manager *MongoDbManager) UpdateEmailSetting(emailSetting *EmailSetting) {
  774. filter := bson.D{{}}
  775. update := bson.D{{Key: "$set", Value: bson.D{{Key: "host", Value: emailSetting.Host}, {Key: "port", Value: emailSetting.Port}, {Key: "sender", Value: emailSetting.Sender}, {Key: "senderpassword", Value: emailSetting.SenderPassword}}}}
  776. opts := options.Update().SetUpsert(false)
  777. _, err := manager.EmailSettings.UpdateOne(context.TODO(), filter, update, opts)
  778. if err != nil {
  779. Logger.Error(err.Error())
  780. panic(any(err))
  781. }
  782. }
  783. func (manager *MongoDbManager) GetEmailSetting() *EmailSetting {
  784. filter := bson.D{{}}
  785. result := manager.EmailSettings.FindOne(context.TODO(), filter)
  786. if result != nil {
  787. var emailSetting = new(EmailSetting)
  788. err := result.Decode(emailSetting)
  789. if err == nil {
  790. return emailSetting
  791. }
  792. }
  793. return nil
  794. }
  795. func (manager *MongoDbManager) AddDomainSetting(domainSetting *DomainSetting) {
  796. _, err := manager.DomainSettings.InsertOne(context.TODO(), *domainSetting)
  797. if err != nil {
  798. Logger.Error(err.Error())
  799. panic(any(err))
  800. }
  801. }
  802. func (manager *MongoDbManager) UpdateDomainSetting(domainSetting *DomainSetting) {
  803. filter := bson.D{{}}
  804. update := bson.D{{Key: "$set", Value: bson.D{{Key: "domain", Value: domainSetting.Domain}}}}
  805. opts := options.Update().SetUpsert(false)
  806. _, err := manager.DomainSettings.UpdateOne(context.TODO(), filter, update, opts)
  807. if err != nil {
  808. Logger.Error(err.Error())
  809. panic(any(err))
  810. }
  811. }
  812. func (manager *MongoDbManager) AddMessageTemplate(template *MessageTemplate) {
  813. _, err := manager.MessageTemplates.InsertOne(context.TODO(), *template)
  814. if err != nil {
  815. Logger.Error(err.Error())
  816. panic(any(err))
  817. }
  818. }
  819. func (manager *MongoDbManager) DeleteMessageTemplate(template *MessageTemplate) {
  820. filter := bson.D{{Key: "id", Value: template.Id}}
  821. _, err := manager.MessageTemplates.DeleteOne(context.TODO(), filter)
  822. if err != nil {
  823. Logger.Error(err.Error())
  824. panic(any(err))
  825. }
  826. }
  827. func (manager *MongoDbManager) GetMessageTemplate(name string) *MessageTemplate {
  828. filter := bson.D{{Key: "name", Value: name}}
  829. result := manager.MessageTemplates.FindOne(context.TODO(), filter)
  830. if result != nil {
  831. var template = new(MessageTemplate)
  832. err := result.Decode(template)
  833. if err == nil {
  834. return template
  835. }
  836. }
  837. return nil
  838. }
  839. func (manager *MongoDbManager) UpdateMessageTemplate(template *MessageTemplate) {
  840. filter := bson.D{{Key: "name", Value: template.Name}}
  841. update := bson.D{{Key: "$set", Value: bson.D{{Key: "content", Value: template.Content}}}}
  842. opts := options.Update().SetUpsert(false)
  843. _, err := manager.MessageTemplates.UpdateOne(context.TODO(), filter, update, opts)
  844. if err != nil {
  845. Logger.Error(err.Error())
  846. panic(any(err))
  847. }
  848. }
  849. func (manager *MongoDbManager) GetDomainSetting() *DomainSetting {
  850. filter := bson.D{{}}
  851. result := manager.DomainSettings.FindOne(context.TODO(), filter)
  852. if result != nil {
  853. var domainSetting = new(DomainSetting)
  854. err := result.Decode(domainSetting)
  855. if err == nil {
  856. return domainSetting
  857. }
  858. }
  859. return nil
  860. }
  861. func (manager *MongoDbManager) CreateClient() *mongo.Client {
  862. clientOptions := options.Client().ApplyURI("mongodb://localhost:27017").SetServerSelectionTimeout(3 * time.Second)
  863. client, err := mongo.Connect(context.TODO(), clientOptions)
  864. if err != nil {
  865. Logger.Error(err.Error())
  866. return nil
  867. }
  868. err = client.Ping(context.TODO(), nil)
  869. if err != nil {
  870. Logger.Error(err.Error())
  871. return nil
  872. }
  873. return client
  874. }
  875. func (manager *MongoDbManager) StartMongo() {
  876. path, err := os.Executable()
  877. if err != nil {
  878. Logger.Error("Can not get the right path: " + err.Error())
  879. }
  880. dir := filepath.Dir(path)
  881. dbDir := filepath.Join(dir, "database")
  882. if _, err := os.Stat(dbDir); os.IsNotExist(err) {
  883. if err = os.MkdirAll(dbDir, os.ModePerm); err == nil {
  884. if runtime.GOOS != "windows" {
  885. if err = os.Chmod(dbDir, os.ModePerm); err != nil {
  886. Logger.Error("Set dir mode failed: " + err.Error())
  887. return
  888. }
  889. }
  890. } else {
  891. Logger.Error("Create dir mode failed: " + err.Error())
  892. return
  893. }
  894. }
  895. mongoDb := filepath.Join(dir, "mongod.exe")
  896. procAttr := new(os.ProcAttr)
  897. procAttr.Dir = dir
  898. procAttr.Files = []*os.File{os.Stdin, os.Stdout, os.Stderr}
  899. procAttr.Sys = &syscall.SysProcAttr{}
  900. startArgs := []string{mongoDb, "--dbpath", filepath.Join(dir, "database"), "--port", "27017", "--maxConns", "1024"}
  901. if runtime.GOOS == "windows" {
  902. _, err := os.StartProcess(mongoDb, startArgs, procAttr)
  903. if err != nil {
  904. Logger.Error("Start mongod failed: " + err.Error())
  905. return
  906. }
  907. } else {
  908. mongoDb = filepath.Join(dir, "mongod")
  909. startArgs[0] = mongoDb
  910. if err = os.Chmod(mongoDb, os.ModePerm); err != nil {
  911. Logger.Error("Set file mode failed: " + err.Error())
  912. return
  913. }
  914. _, err := os.StartProcess(mongoDb, startArgs, procAttr)
  915. if err != nil {
  916. Logger.Error("Start mongod failed: " + err.Error())
  917. return
  918. }
  919. }
  920. }
  921. func (manager *MongoDbManager) AddOrUpdateDashBoardItem(itemName string, value int) {
  922. item := manager.GetDashBoardItem(itemName)
  923. if item == nil {
  924. item = new(DashBoardItem)
  925. item.Id = NewUUId()
  926. item.ItemName = itemName
  927. item.Data = value
  928. _, err := manager.DashBoards.InsertOne(context.TODO(), item)
  929. if err != nil {
  930. Logger.Error(err.Error())
  931. panic(any(err))
  932. }
  933. } else {
  934. filter := bson.D{{Key: "itemname", Value: itemName}}
  935. update := bson.D{{Key: "$set", Value: bson.D{{Key: "data", Value: value}}}}
  936. opts := options.Update().SetUpsert(false)
  937. _, err := manager.DashBoards.UpdateOne(context.TODO(), filter, update, opts)
  938. if err != nil {
  939. Logger.Error(err.Error())
  940. panic(any(err))
  941. }
  942. }
  943. }
  944. func (manager *MongoDbManager) GetDashBoardItem(itemName string) *DashBoardItem {
  945. filter := bson.D{{Key: "itemname", Value: itemName}}
  946. result := manager.DashBoards.FindOne(context.TODO(), filter)
  947. if result != nil {
  948. var item = new(DashBoardItem)
  949. err := result.Decode(item)
  950. if err == nil {
  951. return item
  952. }
  953. }
  954. return nil
  955. }
  956. func (manager *MongoDbManager) Initialize() {
  957. client := manager.CreateClient()
  958. for client == nil {
  959. manager.StartMongo()
  960. time.Sleep(1 * time.Second)
  961. client = manager.CreateClient()
  962. }
  963. manager.client = client
  964. manager.Admins = client.Database("sss").Collection("Admin")
  965. manager.Admins.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  966. manager.Admins.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"name": 1}, Options: nil})
  967. manager.Employees = client.Database("sss").Collection("Employee")
  968. manager.Employees.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  969. manager.Employees.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"departmentid": 1}, Options: nil})
  970. manager.Employees.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"isreviewer": 1}, Options: nil})
  971. manager.Employees.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"isstruggle": 1}, Options: nil})
  972. manager.WorkingDayInfos = client.Database("sss").Collection("WorkingDayInfo")
  973. manager.WorkingDayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  974. manager.WorkingDayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"statisticsid": 1}, Options: nil})
  975. manager.WorkingDayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"day": 1}, Options: nil})
  976. manager.WorkingDayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{{Key: "employeeid", Value: 1}, {Key: "statisticsid", Value: 1}}, Options: nil})
  977. manager.WorkingDayStatistics = client.Database("sss").Collection("WorkingDayStatistics")
  978. manager.WorkingDayStatistics.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  979. manager.StatisticsTables = client.Database("sss").Collection("WorkingDayStatisticsCollection")
  980. manager.StatisticsTables.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  981. manager.StatisticsTables.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"title": 1}, Options: nil})
  982. manager.Departments = client.Database("sss").Collection("Department")
  983. manager.Departments.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  984. manager.Departments.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"parentid": 1}, Options: nil})
  985. manager.HolidayInfos = client.Database("sss").Collection("HolidayInfo")
  986. manager.HolidayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  987. manager.HolidayInfos.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"holiday": 1}, Options: nil})
  988. manager.ViewCodes = client.Database("sss").Collection("ViewCode")
  989. manager.ViewCodes.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"id": 1}, Options: nil})
  990. manager.ViewCodes.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"code": 1}, Options: nil})
  991. manager.EmailSettings = client.Database("sss").Collection("EmailSetting")
  992. manager.DomainSettings = client.Database("sss").Collection("DomainSetting")
  993. manager.MessageTemplates = client.Database("sss").Collection("MessageTemplate")
  994. manager.DashBoards = client.Database("sss").Collection("DashBoard")
  995. manager.AttendanceDatas = client.Database("sss").Collection("AttendanceData")
  996. manager.AttendanceDatas.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"employeeid": 1}, Options: nil})
  997. manager.AttendanceDatas.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.M{"day": 1}, Options: nil})
  998. manager.AttendanceDatas.Indexes().CreateOne(context.TODO(), mongo.IndexModel{Keys: bson.D{{Key: "employeeid", Value: 1}, {Key: "day", Value: 1}}, Options: nil})
  999. //Check admin account
  1000. sysAdmin := manager.GetAdminByName("admin")
  1001. if sysAdmin == nil {
  1002. sysAdmin = new(Admin)
  1003. sysAdmin.Id = NewUUId()
  1004. sysAdmin.Name = "admin"
  1005. sysAdmin.Password = "admin"
  1006. manager.AddAdmin(sysAdmin)
  1007. }
  1008. rootAdmin := manager.GetAdminByName("root")
  1009. if rootAdmin == nil {
  1010. rootAdmin = new(Admin)
  1011. rootAdmin.Id = NewUUId()
  1012. rootAdmin.Name = "root"
  1013. rootAdmin.Password = "root@123"
  1014. manager.AddAdmin(rootAdmin)
  1015. }
  1016. }
  1017. func (manager *MongoDbManager) Close() {
  1018. err := manager.client.Disconnect(context.TODO())
  1019. if err != nil {
  1020. Logger.Error(err.Error())
  1021. }
  1022. Logger.Info("Connection to MongoDB closed.")
  1023. }