electricity_bill_calc_service/controller/meter04kv.go

207 lines
6.9 KiB
Go

package controller
import (
"electricity_bill_calc/excel"
"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/samber/lo"
"github.com/shopspring/decimal"
)
func InitializeMeter04kVController(router *gin.Engine) {
router.GET("/park/:pid/meter/template", security.EnterpriseAuthorize, download04kvMeterArchiveTemplate)
router.GET("/park/:pid/meters", security.EnterpriseAuthorize, ListPaged04kVMeter)
router.GET("/park/:pid/meter/:code", security.EnterpriseAuthorize, fetch04kVMeterDetail)
router.POST("/park/:pid/meter", security.EnterpriseAuthorize, createSingle04kVMeter)
router.PUT("/park/:pid/meter/:code", security.EnterpriseAuthorize, modifySingle04kVMeter)
router.POST("/park/:pid/meter/batch", security.EnterpriseAuthorize, batchImport04kVMeterArchive)
}
func download04kvMeterArchiveTemplate(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
if !ensureParkBelongs(c, result, requestParkId) {
return
}
parkDetail, err := service.ParkService.FetchParkDetail(requestParkId)
if err != nil {
result.NotFound("未找到指定的园区信息。")
return
}
c.Status(http.StatusOK)
c.Header("Content-Type", "application/octet-stream")
c.Header("Content-Transfer-Encoding", "binary")
c.FileAttachment("./assets/meter_04kv_template.xlsx", fmt.Sprintf("%s-户表档案.xlsx", parkDetail.Name))
}
func ListPaged04kVMeter(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
if !ensureParkBelongs(c, result, requestParkId) {
return
}
requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
result.NotAccept("查询参数[page]格式不正确。")
return
}
requestKeyword := c.DefaultQuery("keyword", "")
meters, totalItem, err := service.Meter04kVService.ListMeterDetail(requestParkId, requestKeyword, requestPage)
if err != nil {
result.NotFound(err.Error())
return
}
result.Json(
http.StatusOK,
"已获取到符合条件的0.4kV表计集合。",
response.NewPagedResponse(requestPage, totalItem).ToMap(),
gin.H{"meters": meters},
)
}
func fetch04kVMeterDetail(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
if !ensureParkBelongs(c, result, requestParkId) {
return
}
requestMeterCode := c.Param("code")
meter, err := service.Meter04kVService.Get04kVMeterDetail(requestParkId, requestMeterCode)
if err != nil {
result.NotFound(err.Error())
return
}
if meter == nil {
result.Json(http.StatusNotFound, "指定的表计信息未能找到。", gin.H{"meter": nil})
return
}
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")
if !ensureParkBelongs(c, result, requestParkId) {
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")
if !ensureParkBelongs(c, result, requestParkId) {
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表计信息已经更新。")
}
func batchImport04kVMeterArchive(c *gin.Context) {
result := response.NewResult(c)
requestParkId := c.Param("pid")
if !ensureParkBelongs(c, result, requestParkId) {
return
}
uploadedFile, err := c.FormFile("data")
if err != nil {
result.NotAccept("没有接收到上传的档案文件。")
return
}
archiveFile, err := uploadedFile.Open()
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
analyzer, err := excel.NewMeterArchiveExcelAnalyzer(archiveFile)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
records, errs := analyzer.Analysis(*new(model.Meter04KV))
if len(errs) > 0 {
result.Json(http.StatusNotAcceptable, "上传的表计档案文件存在错误。", gin.H{"errors": errs})
return
}
mergedMeters := lo.Map(records, func(meter model.Meter04KV, index int) model.Meter04KV {
meter.ParkId = requestParkId
meter.Enabled = true
return meter
})
errs = service.Meter04kVService.DuplicateMeterCodeValidate(mergedMeters)
if len(errs) > 0 {
result.Json(http.StatusNotAcceptable, "上传的表计档案文件存在错误。", gin.H{"errors": errs})
return
}
err = service.Meter04kVService.BatchCreateMeter(mergedMeters)
if err != nil {
result.Error(http.StatusInternalServerError, err.Error())
return
}
result.Json(http.StatusOK, "上传的表计档案已经全部导入。", gin.H{"errors": make([]excel.ExcelAnalysisError, 0)})
}