package device import ( "time" "com.snow.auto_monitor/app/constants/errorcode" common "com.snow.auto_monitor/app/http/controllers" proEnt "com.snow.auto_monitor/app/http/entities/device" proMod "com.snow.auto_monitor/app/models/device" proServ "com.snow.auto_monitor/app/services/device" "github.com/gin-gonic/gin" ) func GetById(c *gin.Context) { request := new(proEnt.GetListByIdReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } res, err := proServ.GetById(request.Id) if err != nil { common.Error500(c) return } var response *proEnt.GetListByIdResp = nil if res != nil { response = &proEnt.GetListByIdResp{ Id: res.Id, DeviceNo: res.DeviceNo, Phone: res.Phone, Code: res.Code, CreatedAt: res.CreatedAt.Format(time.RFC3339), } } common.Success(c, response) } func Search(c *gin.Context) { request := new(proEnt.SearcgReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } start, end := "", "" if len(request.CreatedAt) > 1 { start = request.CreatedAt[0] end = request.CreatedAt[1] } res, err := proServ.Search( request.Id, request.DeviceNo, request.Phone, request.Code, start, end, request.PageSize, request.PageNum, ) if err != nil { common.Error500(c) return } var response []*proEnt.SearchResp = nil if len(res) > 0 { for _, item := range res { response = append(response, &proEnt.SearchResp{ Id: item.Id, DeviceNo: item.DeviceNo, Phone: item.Phone, Code: item.Code, CreatedAt: item.CreatedAt.Format(time.RFC3339), }) } } total, err := proServ.CountAll( request.Id, request.DeviceNo, request.Phone, request.Code, start, end, ) if err != nil { common.Error500(c) return } common.SuccessWithList(c, response, total) } func Create(c *gin.Context) { request := new(proEnt.CreateReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } device := &proMod.Device{ DeviceNo: request.DeviceNo, Phone: request.Phone, Code: request.Code, } affected, err := proServ.Create(device) if err != nil && affected > 0 { common.Error500(c) return } response := &proEnt.CreateResp{ Id: device.Id, } common.Success(c, response) } func Update(c *gin.Context) { request := new(proEnt.UpdateReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } device := &proMod.Device{ Id: request.Id, DeviceNo: request.DeviceNo, Phone: request.Phone, Code: request.Code, } affected, err := proServ.Update(device) if err != nil && affected > 0 { common.Error500(c) return } response := &proEnt.UpdateResp{ Id: device.Id, } common.Success(c, response) } func Delete(c *gin.Context) { request := new(proEnt.DeleteReq) err := common.GenRequest(c, request) if err != nil { common.Error(c, errorcode.ParamError) return } affected, err := proServ.Delete(request.Id) if err != nil && affected > 0 { common.Error500(c) return } response := &proEnt.DeleteResp{ Id: request.Id, } common.Success(c, response) }