feat(park):修改园区信息功能已经完成。
This commit is contained in:
parent
1bc87f7ca2
commit
ddda0ac196
|
@ -28,6 +28,7 @@ func InitializeParkController(router *gin.Engine) {
|
||||||
ParkController.Router.GET("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser)
|
ParkController.Router.GET("/parks", security.EnterpriseAuthorize, listAllParksUnderSessionUser)
|
||||||
ParkController.Router.GET("/parks/:uid", security.ManagementAuthorize, listAllParksUnderSpecificUser)
|
ParkController.Router.GET("/parks/:uid", security.ManagementAuthorize, listAllParksUnderSpecificUser)
|
||||||
ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark)
|
ParkController.Router.POST("/park", security.EnterpriseAuthorize, createNewPark)
|
||||||
|
ParkController.Router.PUT("/park/:pid", security.EnterpriseAuthorize, modifyPark)
|
||||||
}
|
}
|
||||||
|
|
||||||
func listAllParksUnderSessionUser(c *gin.Context) {
|
func listAllParksUnderSessionUser(c *gin.Context) {
|
||||||
|
@ -61,7 +62,7 @@ func listAllParksUnderSpecificUser(c *gin.Context) {
|
||||||
result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks})
|
result.Json(http.StatusOK, "已获取到指定用户下的园区。", gin.H{"parks": parks})
|
||||||
}
|
}
|
||||||
|
|
||||||
type _ParkCreationFormData struct {
|
type _ParkInfoFormData struct {
|
||||||
Name string `json:"name" form:"name"`
|
Name string `json:"name" form:"name"`
|
||||||
Region *string `json:"region" form:"region"`
|
Region *string `json:"region" form:"region"`
|
||||||
Address *string `json:"address" form:"address"`
|
Address *string `json:"address" form:"address"`
|
||||||
|
@ -86,7 +87,7 @@ func createNewPark(c *gin.Context) {
|
||||||
result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。")
|
result.Failure(http.StatusInternalServerError, "内部缓存错误,需要重新登录。")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
formData := new(_ParkCreationFormData)
|
formData := new(_ParkInfoFormData)
|
||||||
c.BindJSON(formData)
|
c.BindJSON(formData)
|
||||||
newPark := new(model.Park)
|
newPark := new(model.Park)
|
||||||
copier.Copy(newPark, formData)
|
copier.Copy(newPark, formData)
|
||||||
|
@ -102,3 +103,38 @@ func createNewPark(c *gin.Context) {
|
||||||
}
|
}
|
||||||
result.Success("新园区完成创建。")
|
result.Success("新园区完成创建。")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func modifyPark(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
|
||||||
|
}
|
||||||
|
requestParkId := c.Param("pid")
|
||||||
|
formData := new(_ParkInfoFormData)
|
||||||
|
c.BindJSON(formData)
|
||||||
|
park, err := repository.ParkRepo.FetchParkDetail(requestParkId)
|
||||||
|
if err != nil {
|
||||||
|
result.Error(http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if userSession.Uid != park.UserId {
|
||||||
|
result.NotAccept("不能修改不属于自己的园区。")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
copier.Copy(park, formData)
|
||||||
|
nameAbbr := utils.PinyinAbbr(formData.Name)
|
||||||
|
park.Abbr = &nameAbbr
|
||||||
|
err = service.ParkService.UpdateParkInfo(park)
|
||||||
|
if err != nil {
|
||||||
|
result.Error(http.StatusInternalServerError, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
result.Updated("指定园区资料已更新。")
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ type Park struct {
|
||||||
Deleted `xorm:"extends"`
|
Deleted `xorm:"extends"`
|
||||||
Id string `xorm:"varchar(120) pk not null" json:"id"`
|
Id string `xorm:"varchar(120) pk not null" json:"id"`
|
||||||
UserId string `xorm:"varchar(120) not null" json:"userId"`
|
UserId string `xorm:"varchar(120) not null" json:"userId"`
|
||||||
Name string `xorm:"vachar(70) not null" json:"name"`
|
Name string `xorm:"varchar(70) not null" json:"name"`
|
||||||
Abbr *string `xorm:"varchar(50)" json:"abbr"`
|
Abbr *string `xorm:"varchar(50)" json:"abbr"`
|
||||||
Area decimal.NullDecimal `xorm:"numeric(14,2)" json:"area"`
|
Area decimal.NullDecimal `xorm:"numeric(14,2)" json:"area"`
|
||||||
TenementQuantity decimal.NullDecimal `xorm:"numeric(8,0)" json:"tenement"`
|
TenementQuantity decimal.NullDecimal `xorm:"numeric(8,0)" json:"tenement"`
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"electricity_bill_calc/exceptions"
|
||||||
"electricity_bill_calc/global"
|
"electricity_bill_calc/global"
|
||||||
"electricity_bill_calc/model"
|
"electricity_bill_calc/model"
|
||||||
|
|
||||||
|
@ -22,3 +23,15 @@ func (_ParkRepository) ListAllParkBelongsTo(uid string) ([]model.Park, error) {
|
||||||
}
|
}
|
||||||
return parks, nil
|
return parks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_ParkRepository) FetchParkDetail(pid string) (*model.Park, error) {
|
||||||
|
var park = &model.Park{}
|
||||||
|
has, err := global.DBConn.ID(pid).NoAutoCondition().Get(park)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
return nil, exceptions.NewNotFoundError("未找到符合条件的园区记录。")
|
||||||
|
}
|
||||||
|
return park, nil
|
||||||
|
}
|
||||||
|
|
|
@ -16,3 +16,11 @@ func (_ParkService) SaveNewPark(park model.Park) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (_ParkService) UpdateParkInfo(park *model.Park) error {
|
||||||
|
_, err := global.DBConn.ID(park.Id).Cols("name", "abbr", "region", "address", "contact", "phone", "capacity", "tenement_quantity", "category", "meter_04kv_type").Update(park)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user