Yi 2 лет назад
Родитель
Сommit
d1980cee9b

+ 23 - 0
apiserver/model/electricity.go

@@ -0,0 +1,23 @@
+package model
+
+type Electricity struct {
+	Id              int64   `json:"id"`
+	MeterId         int64   `json:"meterId"`
+	PastureId       int64   `json:"pastureId"`
+	MeasureId       int64   `json:"measureId"`
+	ElecNumber      string  `json:"elecNumber"`
+	ElecName        string  `json:"elecName"`
+	EndAmount       string  `json:"endAmount"`
+	ElecConsumption float64 `json:"elecConsumption"`
+	Price           float64 `json:"price"`
+	SumPrice        float64 `json:"sumPrice"`
+	LastAmount      string  `json:"lastAmount"`
+	EmployeId       int64   `json:"employeId"`
+	Date            string  `json:"date"`
+	Note            string  `json:"note"`
+	IsDel           int32   `json:"isDel"`
+}
+
+func (e *Electricity) TableName() string {
+	return "electricity"
+}

+ 14 - 0
apiserver/model/iot/iot_number.go

@@ -0,0 +1,14 @@
+package iot
+
+const (
+	DataTypeWater       = "water"
+	DataTypeElectricity = "electricity"
+)
+
+type WaterElectricityRequest struct {
+	IotNumber string  `json:"iot_number"` // iot 编号
+	DataType  uint32  `json:"data_type"`  // 数据类型 1 水 2 电
+	EndAmount uint64  `json:"end_amount"` // 用量
+	Price     float64 `json:"price"`      // 单价
+	PastureId int64   `json:"pasture_id"` // 牧场id
+}

+ 36 - 0
apiserver/model/measure.go

@@ -0,0 +1,36 @@
+package model
+
+type Measure struct {
+	Id             int64   `json:"id" gorm:"id"`
+	PId            int     `json:"pId" gorm:"pId"`
+	MeterCode      string  `json:"meterCode" gorm:"meterCode"`
+	MeterName      string  `json:"meterName" gorm:"meterName"`
+	MeterType      string  `json:"meterType" gorm:"meterType"`
+	UseTypeId      int64   `json:"useTypeId" gorm:"useTypeId"`
+	PastureId      int64   `json:"pastureId" gorm:"pastureId"`
+	DepartmentId   string  `json:"departmentId" gorm:"departmentId"`
+	DepartmentName string  `json:"departmentName" gorm:"departmentName"`
+	EmployeId      int64   `json:"employeId" gorm:"employeId"`
+	StartAmount    string  `json:"startAmount" gorm:"startAmount"`
+	Location       string  `json:"location" gorm:"location"`
+	Price          float64 `json:"price" gorm:"price"`
+	Rate           int64   `json:"rate" gorm:"rate"`
+	MaxAmount      int64   `json:"MaxAmount" gorm:"MaxAmount"`
+	IsInfo         int32   `json:"isInfo" gorm:"isInfo"`
+	Enable         int32   `json:"enable" gorm:"enable"`
+	UseType        string  `json:"useType" gorm:"useType"`
+	MeterNumber    string  `json:"meterNumber" gorm:"meterNumber"`
+	IsDel          int64   `json:"isDel" gorm:"isDel"`
+	LastAmount     string  `json:"lastAmount" gorm:"lastAmount"`
+	LastDate       string  `json:"lastDate" gorm:"lastDate"`
+	LastRecodeId   int64   `json:"lastRecodeId" gorm:"lastRecodeId"`
+	EndAmount      string  `json:"endAmount" gorm:"endAmount"`
+	EndDate        string  `json:"endDate" gorm:"endDate"`
+	Multiple       string  `json:"Multiple" gorm:"Multiple"`
+	EmpName        string  `json:"empName" gorm:"empName"`
+	DeleteTime     string  `json:"deleteTime" gorm:"deleteTime"`
+}
+
+func (m *Measure) TableName() string {
+	return "measure"
+}

+ 22 - 0
apiserver/model/water.go

@@ -0,0 +1,22 @@
+package model
+
+type Water struct {
+	Id               int64   `json:"id"`
+	PastureId        int64   `json:"pastureId"`
+	MeasureId        int64   `json:"measureId"`
+	WaterNumber      string  `json:"waterNumber"`
+	WaterName        string  `json:"waterName"`
+	EndAmount        string  `json:"endAmount"`
+	WaterConsumption float64 `json:"waterConsumption"`
+	Price            float64 `json:"price"`
+	SumPrice         float64 `json:"sumPrice"`
+	LastAmount       string  `json:"lastAmount"`
+	EmployeId        int64   `json:"employeId"`
+	Date             string  `json:"date"`
+	Note             string  `json:"note"`
+	IsDel            int32   `json:"isDel"`
+}
+
+func (w *Water) TableName() string {
+	return "water"
+}

+ 1 - 1
apiserver/routers/router.go

@@ -51,6 +51,7 @@ func InitRouter() *gin.Engine {
 	r.POST("/authdata/provider", ProviderExec)
 	r.POST("/authdata/equipmentExecAdd", EquipmentExecAdd)
 	r.POST("/authdata/contractExec", ContractExec)
+	r.POST("/water_electricity/iot_number", IotNumber)
 
 	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) // API 注释
 
@@ -69,7 +70,6 @@ func InitRouter() *gin.Engine {
 	eqm.POST("/login", api.Auth)
 
 	apiV1 := r.Group("/authdata")
-
 	apiV1.Use(jwt.JWT()) // token 验证
 
 	{

+ 30 - 0
apiserver/routers/water_electricity.go

@@ -0,0 +1,30 @@
+package routers
+
+import (
+	"net/http"
+
+	"kpt.xdmy/apiserver/service"
+
+	"github.com/gin-gonic/gin"
+	"kpt.xdmy/apiserver/model/iot"
+	"kpt.xdmy/pkg/app"
+	"kpt.xdmy/pkg/e"
+)
+
+func IotNumber(c *gin.Context) {
+	params := &iot.WaterElectricityRequest{}
+	appG := app.Gin{C: c}
+	if err := c.BindJSON(params); err != nil {
+		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
+		return
+	}
+
+	if err := service.WaterOrElectricity(c, params); err != nil {
+		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_FAIL, nil)
+		return
+	}
+
+	appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{
+		"success": true,
+	})
+}

+ 1 - 1
apiserver/service/contract.go

@@ -114,8 +114,8 @@ func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.P
 	return contractInfo
 }
 
-// http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
 // AutoContractToASP 合同信息自动同步到ASP
+// http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
 func AutoContractToASP() {
 	sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
 

+ 66 - 0
apiserver/service/water_electricity.go

@@ -0,0 +1,66 @@
+package service
+
+import (
+	"context"
+	"errors"
+	"time"
+
+	"gorm.io/gorm"
+
+	"kpt.xdmy/apiserver/model"
+
+	"kpt.xdmy/apiserver/model/iot"
+)
+
+func WaterOrElectricity(ctx context.Context, req *iot.WaterElectricityRequest) error {
+	measure := &model.Measure{}
+	if err := s.d.DB.Where("iotNumber = ? ", req.IotNumber).First(measure).Error; err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) || measure.Id <= 0 {
+			return errors.New("iot编号错误")
+		}
+		return err
+	}
+
+	var newData interface{}
+	if measure.MeterType == "电表" && req.DataType == 2 {
+		newData = &model.Electricity{
+			PastureId:       measure.PastureId,
+			MeasureId:       measure.Id,
+			ElecNumber:      measure.MeterNumber,
+			ElecName:        measure.MeterName,
+			EndAmount:       "",
+			ElecConsumption: float64(req.EndAmount),
+			Price:           req.Price,
+			SumPrice:        req.Price * float64(req.EndAmount),
+			LastAmount:      "",
+			EmployeId:       measure.EmployeId,
+			Date:            time.Now().AddDate(0, 0, -1).Format("2006-01-02"),
+			Note:            "",
+		}
+	}
+
+	if measure.MeterType == "水表" && req.DataType == 1 {
+		newData = &model.Water{
+			PastureId:        measure.PastureId,
+			MeasureId:        measure.Id,
+			WaterNumber:      measure.MeterNumber,
+			WaterName:        measure.MeterName,
+			EndAmount:        "",
+			WaterConsumption: float64(req.EndAmount),
+			Price:            req.Price,
+			SumPrice:         req.Price * float64(req.EndAmount),
+			LastAmount:       "",
+			EmployeId:        measure.EmployeId,
+			Date:             time.Now().AddDate(0, 0, -1).Format("2006-01-02"),
+			Note:             "",
+		}
+	}
+
+	if newData != nil {
+		if err := s.d.DB.Create(newData).Error; err != nil {
+			return err
+		}
+	}
+
+	return nil
+}

+ 2 - 1
cmd/main.go

@@ -3,12 +3,13 @@ package main
 import (
 	"flag"
 	"fmt"
-	"kpt.xdmy/apiserver/dao"
 	"log"
 	"net/http"
 	"os"
 	"strings"
 
+	"kpt.xdmy/apiserver/dao"
+
 	// logrus "github.com/sirupsen/logrus"
 
 	"github.com/kardianos/service"

+ 9 - 0
migration/v0002_alter_measure.sql

@@ -0,0 +1,9 @@
+ALTER TABLE `measure`
+    ADD COLUMN `iotNumber` varchar(255) NOT NULL DEFAULT '' COMMENT 'IOT 编号' AFTER `pId`;
+
+
+## apisql表查询语句
+SELECT sqlstr,params FROM `apisql` WHERE sqlname = 'insertMeasure' AND ENABLE>0
+## 需要更改的内容如下
+## insert into measure (departmentId,departmentName, location,  startAmount,lastAmount,endAmount, meterName, meterType,useType, price, employeId, pastureId,meterNumber ,lastDate,endDate,Multiple,iotNumber) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+## departmentId,departmentName,location,startAmount,startAmount,startAmount,formName,formType,useType,price,employeId, pastureId,formNumber,inputDatetime,inputDatetime,Multiple,iotNumber