面包店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

hdl_staff.go 3.2 KiB

7 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package hdl
  2. import (
  3. "applet/app/admin/enum"
  4. "applet/app/admin/lib/validate"
  5. "applet/app/admin/md"
  6. "applet/app/db"
  7. "applet/app/db/model"
  8. "applet/app/e"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func StaffList(c *gin.Context) {
  13. var req md.StaffListReq
  14. err := c.ShouldBindJSON(&req)
  15. if err != nil {
  16. err = validate.HandleValidateErr(err)
  17. err1 := err.(e.E)
  18. e.OutErr(c, err1.Code, err1.Error())
  19. return
  20. }
  21. staffDb := db.StaffDb{}
  22. staffDb.Set()
  23. engine := staffDb.Db.Desc("id").Limit(req.Limit, (req.Page-1)*req.Limit)
  24. if req.Name != "" {
  25. engine.And("name like ?", "%"+req.Name+"%")
  26. }
  27. if req.IdNo != "" {
  28. engine.And("id_no like ?", "%"+req.IdNo+"%")
  29. }
  30. if req.Phone != "" {
  31. engine.And("phone like ?", "%"+req.Phone+"%")
  32. }
  33. var list []model.Staff
  34. count, err := engine.FindAndCount(&list)
  35. if err != nil {
  36. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  37. return
  38. }
  39. e.OutSuc(c, map[string]interface{}{
  40. "list": list,
  41. "state": []map[string]interface{}{
  42. {
  43. "name": enum.StaffSex(enum.StaffSexForMan).String(),
  44. "value": enum.StaffSexForMan,
  45. },
  46. {
  47. "name": enum.StaffSex(enum.StaffSexForWomen).String(),
  48. "value": enum.StaffSexForWomen,
  49. },
  50. },
  51. "total": count,
  52. }, nil)
  53. return
  54. }
  55. func StaffDelete(c *gin.Context) {
  56. var req md.StaffDeleteReq
  57. err := c.ShouldBindJSON(&req)
  58. if err != nil {
  59. err = validate.HandleValidateErr(err)
  60. err1 := err.(e.E)
  61. e.OutErr(c, err1.Code, err1.Error())
  62. return
  63. }
  64. staffDb := db.StaffDb{}
  65. staffDb.Set()
  66. _, err = staffDb.StaffDelete(req.Ids)
  67. if err != nil {
  68. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  69. return
  70. }
  71. e.OutSuc(c, "success", nil)
  72. return
  73. }
  74. func StaffAdd(c *gin.Context) {
  75. var req md.StaffAddReq
  76. err := c.ShouldBindJSON(&req)
  77. if err != nil {
  78. err = validate.HandleValidateErr(err)
  79. err1 := err.(e.E)
  80. e.OutErr(c, err1.Code, err1.Error())
  81. return
  82. }
  83. staffDb := db.StaffDb{}
  84. staffDb.Set()
  85. now := time.Now()
  86. staff := model.Staff{
  87. Name: req.Name,
  88. Sex: req.Sex,
  89. IdNo: req.IdNo,
  90. Phone: req.Phone,
  91. NativePlace: req.NativePlace,
  92. Department: req.Department,
  93. CreateAt: now.Format("2006-01-02 15:04:05"),
  94. UpdateAt: now.Format("2006-01-02 15:04:05"),
  95. }
  96. _, err = staffDb.StaffInsert(&staff)
  97. if err != nil {
  98. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  99. return
  100. }
  101. e.OutSuc(c, "success", nil)
  102. return
  103. }
  104. func StaffUpdate(c *gin.Context) {
  105. var req md.StaffUpdateReq
  106. err := c.ShouldBindJSON(&req)
  107. if err != nil {
  108. err = validate.HandleValidateErr(err)
  109. err1 := err.(e.E)
  110. e.OutErr(c, err1.Code, err1.Error())
  111. return
  112. }
  113. staffDb := db.StaffDb{}
  114. staffDb.Set()
  115. staff, err := staffDb.GetStaff(req.Id)
  116. if err != nil {
  117. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  118. return
  119. }
  120. if staff == nil {
  121. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  122. return
  123. }
  124. now := time.Now()
  125. staff.Name = req.Name
  126. staff.Sex = req.Sex
  127. staff.IdNo = req.IdNo
  128. staff.Phone = req.Phone
  129. staff.NativePlace = req.NativePlace
  130. staff.Department = req.Department
  131. staff.UpdateAt = now.Format("2006-01-02 15:04:05")
  132. _, err = staffDb.StaffUpdate(staff, "name", "sex", "id_no", "phone", "native_place", "department", "update_at")
  133. if err != nil {
  134. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  135. return
  136. }
  137. e.OutSuc(c, "success", nil)
  138. return
  139. }