|
- package hdl
-
- import (
- "applet/app/admin/enum"
- "applet/app/admin/lib/validate"
- "applet/app/admin/md"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/e"
- "github.com/gin-gonic/gin"
- "time"
- )
-
- func StaffList(c *gin.Context) {
- var req md.StaffListReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- staffDb := db.StaffDb{}
- staffDb.Set()
- engine := staffDb.Db.Desc("id").Limit(req.Limit, (req.Page-1)*req.Limit)
- if req.Name != "" {
- engine.And("name like ?", "%"+req.Name+"%")
- }
- if req.IdNo != "" {
- engine.And("id_no like ?", "%"+req.IdNo+"%")
- }
- if req.Phone != "" {
- engine.And("phone like ?", "%"+req.Phone+"%")
- }
-
- var list []model.Staff
- count, err := engine.FindAndCount(&list)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, map[string]interface{}{
- "list": list,
- "state": []map[string]interface{}{
- {
- "name": enum.StaffSex(enum.StaffSexForMan).String(),
- "value": enum.StaffSexForMan,
- },
- {
- "name": enum.StaffSex(enum.StaffSexForWomen).String(),
- "value": enum.StaffSexForWomen,
- },
- },
- "total": count,
- }, nil)
- return
- }
-
- func StaffDelete(c *gin.Context) {
- var req md.StaffDeleteReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- staffDb := db.StaffDb{}
- staffDb.Set()
- _, err = staffDb.StaffDelete(req.Ids)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
-
- func StaffAdd(c *gin.Context) {
- var req md.StaffAddReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- staffDb := db.StaffDb{}
- staffDb.Set()
- now := time.Now()
- staff := model.Staff{
- Name: req.Name,
- Sex: req.Sex,
- IdNo: req.IdNo,
- Phone: req.Phone,
- NativePlace: req.NativePlace,
- Department: req.Department,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- _, err = staffDb.StaffInsert(&staff)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
-
- e.OutSuc(c, "success", nil)
- return
- }
-
- func StaffUpdate(c *gin.Context) {
- var req md.StaffUpdateReq
- err := c.ShouldBindJSON(&req)
- if err != nil {
- err = validate.HandleValidateErr(err)
- err1 := err.(e.E)
- e.OutErr(c, err1.Code, err1.Error())
- return
- }
- staffDb := db.StaffDb{}
- staffDb.Set()
- staff, err := staffDb.GetStaff(req.Id)
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- if staff == nil {
- e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
- return
- }
- now := time.Now()
- staff.Name = req.Name
- staff.Sex = req.Sex
- staff.IdNo = req.IdNo
- staff.Phone = req.Phone
- staff.NativePlace = req.NativePlace
- staff.Department = req.Department
- staff.UpdateAt = now.Format("2006-01-02 15:04:05")
- _, err = staffDb.StaffUpdate(staff, "name", "sex", "id_no", "phone", "native_place", "department", "update_at")
- if err != nil {
- e.OutErr(c, e.ERR_DB_ORM, err.Error())
- return
- }
- e.OutSuc(c, "success", nil)
- return
- }
|