| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package instrument
- import (
- "dashoo.cn/micro_libary/response"
- "dashoo.cn/modi_webapi/app/service/instrument"
- "dashoo.cn/modi_webapi/library/request"
- "fmt"
- "github.com/gogf/gf/net/ghttp"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- "time"
- )
- type Controller struct {
- }
- func (c *Controller) GetAllInstrument(r *ghttp.Request) {
- page := request.GetPageInfo(r)
- where := ""
- if productway := r.GetString("ProductWay"); productway != "" {
- if where == "" {
- where = fmt.Sprintf(" ProductWay = %v", productway)
- } else {
- where += fmt.Sprintf(" AND ProductWay = %v", productway)
- }
- }
- if name := r.GetString("Name"); name != "" {
- if where == "" {
- where = fmt.Sprintf(" Name LIKE '%%%v%%'", name)
- } else {
- where += fmt.Sprintf(" AND Name LIKE '%%%v%%'", name)
- }
- }
- var result []instrument.Entity
- if err := instrument.GetAllInstrument(page, where, &result); err != nil {
- if err.Error() == "sql: no rows in result set" {
- response.Json(r, 0, "")
- return
- }
- response.Json(r, 1, err.Error())
- } else {
- count, err1 := instrument.FindInstrumentCount(where)
- if err1 != nil {
- response.Json(r, 1, err1.Error())
- } else {
- var records response.PagedRecords
- records.Size = page.Size
- records.Current = page.Current
- records.Total = count
- records.Records = result
- response.Json(r, 0, "ok", records)
- }
- }
- }
- func (c *Controller) GetOneInstrument(r *ghttp.Request) {
- id := r.GetInt("id")
- if result, err := instrument.FindOne(id); err != nil {
- response.Json(r, 1, err.Error())
- r.ExitAll()
- } else {
- var records response.PagedRecords
- records.Records = result
- response.Json(r, 0, "", records)
- }
- }
- func (c *Controller) AddInstrument(r *ghttp.Request) {
- Instrument := new(instrument.Entity)
- if err := r.Parse(Instrument); err != nil {
- // 数据验证错误
- if v, ok := err.(*gvalid.Error); ok {
- response.Json(r, 1, v.FirstString())
- r.ExitAll()
- }
- // 其他错误
- response.Json(r, 1, err.Error())
- r.ExitAll()
- }
- realName := r.GetParamVar("realname").String()
- userId := r.GetParamVar("userid").Int()
- currentTime := gtime.Now()
- Instrument.CreateOn = currentTime
- Instrument.CreateBy = realName
- Instrument.CreateUserId = userId
- Instrument.ModifiedOn = currentTime
- Instrument.ModifiedBy = realName
- Instrument.ModifiedUserId = userId
- hh, _ := time.ParseDuration("1h")
- Instrument.CalibrationTime = Instrument.CalibrationTime.Add(hh*8)
- if result, err := instrument.Insert(Instrument); err != nil {
- response.Json(r, 1, "保存失败")
- } else {
- var records response.PagedRecords
- id, _ := result.LastInsertId()
- Instrument.Id = int(id)
- records.Records = Instrument
- response.Json(r, 0, "保存成功", records)
- }
- }
- func (c *Controller) UpdateInstrument(r *ghttp.Request) {
- Instrument := new(instrument.Entity)
- if err := r.Parse(Instrument); err != nil {
- // 数据验证错误
- if v, ok := err.(*gvalid.Error); ok {
- response.Json(r, 1, v.FirstString())
- r.ExitAll()
- }
- // 其他错误
- response.Json(r, 1, err.Error())
- r.ExitAll()
- }
- realName := r.GetParamVar("realname").String()
- userId := r.GetParamVar("userid").Int()
- currentTime := gtime.Now()
- Instrument.ModifiedOn = currentTime
- Instrument.ModifiedBy = realName
- Instrument.ModifiedUserId = userId
- hh, _ := time.ParseDuration("1h")
- Instrument.CalibrationTime = Instrument.CalibrationTime.Add(hh*8)
- if _, err := instrument.Replace(Instrument); err != nil {
- response.Json(r, 1, "保存失败")
- } else {
- var records response.PagedRecords
- //id, _ := result.LastInsertId()
- //theChargeRecord.Id = int(id)
- records.Records = Instrument
- response.Json(r, 0, "保存成功", records)
- }
- }
- func (c *Controller) DeleteInstrument(r *ghttp.Request) {
- id := r.GetInt("id")
- if _, err := instrument.Delete(fmt.Sprintf("Id=%v", id)); err != nil {
- response.Json(r, 1, err.Error())
- r.ExitAll()
- } else {
- response.Json(r, 0, "该记录已删除!")
- }
- }
|