feat(meter):基本完成表计修改接口。

This commit is contained in:
徐涛
2022-08-16 22:05:20 +08:00
parent 4c71f5f205
commit 0fbe021252
3 changed files with 161 additions and 1 deletions

View File

@@ -1,14 +1,18 @@
package controller
import (
"electricity_bill_calc/model"
"electricity_bill_calc/response"
"electricity_bill_calc/security"
"electricity_bill_calc/service"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/jinzhu/copier"
"github.com/shopspring/decimal"
)
type _Meter04kVController struct {
@@ -25,6 +29,8 @@ func InitializeMeter04kVController(router *gin.Engine) {
Meter04kVController.Router.GET("/park/:pid/meter/template", security.EnterpriseAuthorize, download04kvMeterArchiveTemplate)
Meter04kVController.Router.GET("/park/:pid/meters", security.EnterpriseAuthorize, ListPaged04kVMeter)
Meter04kVController.Router.GET("/park/:pid/meter/:code", security.EnterpriseAuthorize, fetch04kVMeterDetail)
Meter04kVController.Router.POST("/park/:pid/meter", security.EnterpriseAuthorize, createSingle04kVMeter)
Meter04kVController.Router.PUT("/park/:pid/meter/:code", security.EnterpriseAuthorize, modifySingle04kVMeter)
}
func download04kvMeterArchiveTemplate(c *gin.Context) {
@@ -120,3 +126,98 @@ func fetch04kVMeterDetail(c *gin.Context) {
}
result.Json(http.StatusOK, "指定的表计信息已找到。", gin.H{"meter": meter})
}
type _MeterModificationFormData struct {
Address *string `json:"address" form:"address"`
CustomerName *string `json:"customerName" form:"customerName"`
ContactName *string `json:"contactName" form:"contactName"`
ContactPhone *string `json:"contactPhone" form:"contactPhone"`
Ratio decimal.Decimal `json:"ratio" form:"ratio"`
Seq int `json:"seq" form:"seq"`
IsPublicMeter bool `json:"isPublicMeter" form:"isPublicMeter"`
WillDilute bool `json:"willDilute" form:"willDilute"`
Enabled bool `json:"enabled" form:"enabled"`
}
type _MeterCreationFormData struct {
Code string `json:"code" form:"code"`
Address *string `json:"address" form:"address"`
CustomerName *string `json:"customerName" form:"customerName"`
ContactName *string `json:"contactName" form:"contactName"`
ContactPhone *string `json:"contactPhone" form:"contactPhone"`
Ratio decimal.Decimal `json:"ratio" form:"ratio"`
Seq int `json:"seq" form:"seq"`
IsPublicMeter bool `json:"isPublicMeter" form:"isPublicMeter"`
WillDilute bool `json:"willDilute" form:"willDilute"`
Enabled bool `json:"enabled" form:"enabled"`
}
func createSingle04kVMeter(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
userSession, err := _retreiveSession(c)
if err != nil {
result.Unauthorized(err.Error())
return
}
sure, err := service.ParkService.EnsurePark(userSession.Uid, requestParkId)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
if !sure {
result.Unauthorized("不能访问不属于自己的园区。")
return
}
formData := new(_MeterCreationFormData)
c.BindJSON(formData)
log.Printf("[controller|debug] form: %+v", formData)
newMeter := new(model.Meter04KV)
copier.Copy(newMeter, formData)
newMeter.ParkId = requestParkId
log.Printf("[controller|debug] meter: %+v", newMeter)
err = service.Meter04kVService.CreateSingleMeter(*newMeter)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Created("新0.4kV表计已经添加完成。")
}
func modifySingle04kVMeter(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
userSession, err := _retreiveSession(c)
if err != nil {
result.Unauthorized(err.Error())
return
}
sure, err := service.ParkService.EnsurePark(userSession.Uid, requestParkId)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
if !sure {
result.Unauthorized("不能访问不属于自己的园区。")
return
}
requestMeterCode := c.Param("code")
meterDetail, err := service.Meter04kVService.Get04kVMeterDetail(requestParkId, requestMeterCode)
if err != nil {
result.NotFound(err.Error())
return
}
if meterDetail == nil {
result.NotFound("指定表计的信息为找到,不能修改。")
return
}
formData := new(_MeterModificationFormData)
c.BindJSON(formData)
copier.Copy(meterDetail, formData)
err = service.Meter04kVService.UpdateSingleMeter(meterDetail)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Updated("指定0.4kV表计信息已经更新。")
}