feat(park):完成列出园区和添加园区功能。
This commit is contained in:
parent
d1806d8cc6
commit
1bc87f7ca2
|
@ -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("新园区完成创建。")
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
18
service/park.go
Normal file
18
service/park.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user