forked from free-lancers/electricity_bill_calc_service
		
	feat(meter):基本完成表计修改接口。
This commit is contained in:
		| @@ -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表计信息已经更新。") | ||||
| } | ||||
|   | ||||
| @@ -7,7 +7,7 @@ import ( | ||||
| type Meter04KV struct { | ||||
| 	CreatedAndModified `xorm:"extends"` | ||||
| 	Code               string          `xorm:"varchar(120) pk not null" json:"code"` | ||||
| 	ParkId             string          `xorm:"varchar(120) not null" json:"parkId"` | ||||
| 	ParkId             string          `xorm:"varchar(120) pk not null" json:"parkId"` | ||||
| 	Address            *string         `xorm:"varchar(100)" json:"address"` | ||||
| 	CustomerName       *string         `xorm:"varchar(100)" json:"customerName"` | ||||
| 	ContactName        *string         `xorm:"varchar(70)" json:"contactName"` | ||||
|   | ||||
| @@ -6,6 +6,7 @@ import ( | ||||
| 	"electricity_bill_calc/model" | ||||
|  | ||||
| 	"xorm.io/builder" | ||||
| 	"xorm.io/xorm" | ||||
| ) | ||||
|  | ||||
| type _Meter04kVService struct{} | ||||
| @@ -46,3 +47,61 @@ func (_Meter04kVService) Get04kVMeterDetail(park, code string) (*model.Meter04KV | ||||
| 	} | ||||
| 	return meter, nil | ||||
| } | ||||
|  | ||||
| func (_Meter04kVService) insertNewMeter(tx *xorm.Session, meter model.Meter04KV) error { | ||||
| 	_, err := tx.Insert(meter) | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (_Meter04kVService) updateMeter(tx *xorm.Session, meter model.Meter04KV) error { | ||||
| 	_, err := tx. | ||||
| 		Where(builder.Eq{"code": meter.Code, "park_id": meter.ParkId}). | ||||
| 		Cols("address", "customer_name", "contact_name", "contact_phone", "ratio", "seq", "public_meter", "dilute", "enabled"). | ||||
| 		NoAutoCondition(). | ||||
| 		Update(meter) | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 	} | ||||
| 	return err | ||||
| } | ||||
|  | ||||
| func (m _Meter04kVService) CreateSingleMeter(meter model.Meter04KV) error { | ||||
| 	tx := global.DBConn.NewSession() | ||||
| 	if err := tx.Begin(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer tx.Close() | ||||
|  | ||||
| 	err := m.insertNewMeter(tx, meter) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	err = tx.Commit() | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| func (m _Meter04kVService) UpdateSingleMeter(meter *model.Meter04KV) error { | ||||
| 	tx := global.DBConn.NewSession() | ||||
| 	if err := tx.Begin(); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer tx.Close() | ||||
|  | ||||
| 	err := m.updateMeter(tx, *meter) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	err = tx.Commit() | ||||
| 	if err != nil { | ||||
| 		tx.Rollback() | ||||
| 		return err | ||||
| 	} | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user