From 1bc87f7ca26088731746881796dd93e0b7a0cba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Mon, 15 Aug 2022 19:45:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(park):=E5=AE=8C=E6=88=90=E5=88=97=E5=87=BA?= =?UTF-8?q?=E5=9B=AD=E5=8C=BA=E5=92=8C=E6=B7=BB=E5=8A=A0=E5=9B=AD=E5=8C=BA?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/park.go | 50 +++++++++++++++++++++++++++++++++++++++++++++- model/park.go | 3 ++- repository/user.go | 4 ++-- service/park.go | 18 +++++++++++++++++ 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 service/park.go diff --git a/controller/park.go b/controller/park.go index b87426a..ed45a5f 100644 --- a/controller/park.go +++ b/controller/park.go @@ -5,9 +5,14 @@ import ( "electricity_bill_calc/repository" "electricity_bill_calc/response" "electricity_bill_calc/security" + "electricity_bill_calc/service" + "electricity_bill_calc/utils" "net/http" "github.com/gin-gonic/gin" + "github.com/google/uuid" + "github.com/jinzhu/copier" + "github.com/shopspring/decimal" ) type _ParkController struct { @@ -22,6 +27,7 @@ func InitializeParkController(router *gin.Engine) { } ParkController.Router.GET("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser) ParkController.Router.GET("/parks/:uid", security.ManagementAuthorize, listAllParksUnderSpecificUser) + ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark) } func listAllParksUnderSessionUser(c *gin.Context) { @@ -31,7 +37,7 @@ func listAllParksUnderSessionUser(c *gin.Context) { result.Error(http.StatusUnauthorized, "用户会话无效。") return } - userSession, ok := session.(model.Session) + userSession, ok := session.(*model.Session) if !ok { result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。") return @@ -54,3 +60,45 @@ func listAllParksUnderSpecificUser(c *gin.Context) { } result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks}) } + +type _ParkCreationFormData struct { + Name string `json:"name" form:"name"` + Region *string `json:"region" form:"region"` + Address *string `json:"address" form:"address"` + Contact *string `json:"contact" form:"contact"` + Phone *string `json:"phone" from:"phone"` + Area decimal.NullDecimal `json:"area" from:"area"` + Capacity decimal.NullDecimal `json:"capacity" from:"capacity"` + Tenement decimal.NullDecimal `json:"tenement" from:"tenement"` + Category int `json:"category" form:"category"` + Submeter int `json:"submeter" form:"submeter"` +} + +func createNewPark(c *gin.Context) { + result := response.NewResult(c) + session, exists := c.Get("session") + if !exists { + result.Error(http.StatusUnauthorized, "用户会话无效。") + return + } + userSession, ok := session.(*model.Session) + if !ok { + result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。") + return + } + formData := new(_ParkCreationFormData) + c.BindJSON(formData) + newPark := new(model.Park) + copier.Copy(newPark, formData) + newPark.Id = uuid.New().String() + newPark.UserId = userSession.Uid + nameAbbr := utils.PinyinAbbr(newPark.Name) + newPark.Abbr = &nameAbbr + newPark.Enabled = true + err := service.ParkService.SaveNewPark(*newPark) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Success("新园区完成创建。") +} diff --git a/model/park.go b/model/park.go index 69c1820..f66c970 100644 --- a/model/park.go +++ b/model/park.go @@ -12,9 +12,10 @@ type Park struct { Name string `xorm:"vachar(70) not null" json:"name"` Abbr *string `xorm:"varchar(50)" json:"abbr"` Area decimal.NullDecimal `xorm:"numeric(14,2)" json:"area"` - TenementQuantity decimal.NullDecimal `xorm:"numeric(8,0)" json:"tenementQuantity"` + TenementQuantity decimal.NullDecimal `xorm:"numeric(8,0)" json:"tenement"` Capacity decimal.NullDecimal `xorm:"numeric(16,2)" json:"capacity"` Category int8 `xorm:"smallint not null" json:"category"` + SubmeterType int8 `xorm:"'meter_04kv_type' smallint not null" json:"meter04kvType"` Region *string `xorm:"varchar(10)" json:"region"` Address *string `xorm:"varchar(120)" json:"address"` Contact *string `xorm:"varchar(100)" json:"contact"` diff --git a/repository/user.go b/repository/user.go index c47cd9d..8e83836 100644 --- a/repository/user.go +++ b/repository/user.go @@ -32,7 +32,7 @@ func (_UserRepository) RetreiveUserDetail(uid string) (*model.UserDetail, error) return cachedUser, nil } user := new(model.UserDetail) - has, err := global.DBConn.ID(uid).Get(user) + has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user) if has { cache.CacheData(user, "user_detail", uid) } @@ -45,7 +45,7 @@ func (_UserRepository) FindUserByID(uid string) (*model.User, error) { return cachedUser, nil } user := new(model.User) - has, err := global.DBConn.ID(uid).Get(user) + has, err := global.DBConn.ID(uid).NoAutoCondition().Get(user) if has { cache.CacheData(user, "user", uid) } diff --git a/service/park.go b/service/park.go new file mode 100644 index 0000000..5a07930 --- /dev/null +++ b/service/park.go @@ -0,0 +1,18 @@ +package service + +import ( + "electricity_bill_calc/global" + "electricity_bill_calc/model" +) + +type _ParkService struct{} + +var ParkService _ParkService + +func (_ParkService) SaveNewPark(park model.Park) error { + _, err := global.DBConn.Insert(park) + if err != nil { + return err + } + return nil +}