瀏覽代碼

供电箱接口

hxz 2 年之前
父節點
當前提交
b14dd00487

+ 71 - 4
app/controller/switchBoxController.go

@@ -2,6 +2,13 @@ package controller
 
 import (
 	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/dao"
+	"iot_manager_service/app/model"
+	"iot_manager_service/app/service"
+	"iot_manager_service/app/utils"
+	"math"
+	"net/http"
+	"strconv"
 )
 
 // 配电箱基本信息管理对象
@@ -10,25 +17,85 @@ var SwitchBox = new(switchBoxCtl)
 type switchBoxCtl struct{}
 
 func (c *switchBoxCtl) Detail(ctx *gin.Context) {
+	id, e := strconv.Atoi(ctx.Query("id"))
+	if e != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(e.Error(), nil))
+		return
+	}
+
+	device, err := service.SwitchBoxService.Get(id)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, device))
 }
 
 func (c *switchBoxCtl) List(ctx *gin.Context) {
+	searchValue := ctx.Query("searchValue")
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+	devices, err := service.SwitchBoxService.List(searchValue, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(devices)) / float64(size))
+	rsp := model.RsqSwitchBoxList{
+		Current: current,
+		Size:    size,
+		Total:   len(devices),
+		Pages:   int(pages),
+	}
+	for _, device := range devices {
+
+		rsp.Records = append(rsp.Records, device)
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, rsp))
 }
 
 func (c *switchBoxCtl) CreateOrUpdate(ctx *gin.Context) {
+	var req dao.SwitchBox
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.SwitchBoxService.CreateOrUpdate(req)
+	ctx.JSON(http.StatusOK, err)
 }
 
 func (c *switchBoxCtl) Remove(ctx *gin.Context) {
+	var req *model.ReqSwitchBoxRemove
+	if err := ctx.ShouldBindJSON(&req); err != nil {
+		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
+		return
+	}
+	err := service.SwitchBoxService.Remove(req.IDs)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, nil))
 }
 
+func (c *switchBoxCtl) GetList(ctx *gin.Context) {
+	devices, err := service.SwitchBoxService.GetList()
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, utils.SuccessResponse(utils.Succeeded, devices))
+}
 func (c *switchBoxCtl) ImportExcel(ctx *gin.Context) {
 }
-
 func (c *switchBoxCtl) ExportExcel(ctx *gin.Context) {
 }
-
 func (c *switchBoxCtl) ExportTemplate(ctx *gin.Context) {
-}
 
-func (c *switchBoxCtl) GetList(ctx *gin.Context) {
 }

+ 92 - 0
app/dao/switchBoxDao.go

@@ -0,0 +1,92 @@
+package dao
+
+import (
+	"github.com/jinzhu/gorm"
+	"time"
+)
+
+// SwitchBox 供电箱
+type SwitchBox struct {
+	ID             int       `gorm:"primary_key" json:"id"`                             //编号
+	BoxName        string    `gorm:"type:varchar(64)" json:"boxName"`                   //供电箱名称
+	BoxSN          string    `gorm:"type:varchar(60)" json:"boxSN"`                     //设备序列号
+	BrandId        int       `gorm:"type:int" json:"brandId"`                           //品牌
+	ModelId        int       `gorm:"type:int" json:"modelId"`                           //配电箱型号
+	TransformerId  int       `gorm:"type:int" json:"transformerId"`                     //变压器ID
+	TransformerNum string    `gorm:"type:varchar(60)" json:"transformerNum"`            //变压器出线口编号
+	RatedPower     float32   `gorm:"type:double(8, 2) default 0.00 " json:"ratedPower"` //功率
+	ProvinceName   string    `gorm:" type:varchar(12)" json:"provinceName"`             //省份
+	CityName       string    `gorm:"type:varchar(12)" json:"cityName"`                  //市
+	DistrictName   string    `gorm:"type:varchar(12)" json:"districtName"`              //区
+	BoxLocation    string    `gorm:"type:varchar(100)" json:"boxLocation"`              //详细位置
+	PoleLng        float64   `gorm:"type:double(17, 14) " json:"poleLng"`               //经度
+	PoleLat        float64   `gorm:"type:double(17, 14)  " json:"poleLat"`              //纬度
+	MonitorAddress string    `gorm:"type:varchar(255)   " json:"monitorAddress"`        //监控地址
+	TenantId       string    `gorm:"type:varchar(12)" json:"tenantId"`                  //租户id
+	InstallTime    time.Time `gorm:"type:date" json:"installTime"`                      //安装时间
+	CreateTime     time.Time `gorm:"type:datetime" json:"createTime"`                   //新增时间
+	CreateUser     string    `gorm:"type:varchar(60)" json:"createUser"`                //新增记录操作用户ID
+	UpdateTime     time.Time `gorm:"type:datetime" json:"updateTime"`                   //修改时间
+	UpdateUser     string    `gorm:"type:varchar(60)" json:"updateUser"`                //修改用户
+	IsDeleted      int       `gorm:"type:int;default 0" json:"isDeleted"`               //是否删除 0=未删除,1=删除
+	Status         int       `gorm:"type:int" json:"status"`                            //状态 0=正常,1=异常
+	Tag            string    `gorm:"type:varchar(255)" json:"tag"`                      //标签,保留字段(逗号区分)
+	BoxPhotoUrl    string    `gorm:"type:varchar(255)" json:"boxPhotoUrl"`              //供电箱照片地址
+}
+
+func (SwitchBox) TableName() string {
+	return "t_dev_switch_box"
+}
+
+func (c SwitchBox) Delete() error {
+	return GDb.Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
+		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
+}
+
+func (c SwitchBox) IsExistedBySN() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where("  box_sn = ? and is_deleted = ?",
+		c.BoxSN, c.IsDeleted).Count(&count).Error
+	return count > 0
+}
+
+func (c SwitchBox) IsExistedByNameAndCode() bool {
+	var devices []SwitchBox
+	err := GDb.Model(&c).Where(" box_sn = ? and is_deleted = ?",
+		c.BoxSN, c.IsDeleted).Find(&devices).Error
+	//如果查询不到,返回相应的错误
+	if gorm.IsRecordNotFoundError(err) {
+		return false
+	}
+	for _, d := range devices {
+		if d.ID != c.ID {
+			return true
+		}
+	}
+	return false
+}
+
+func (c *SwitchBox) Create() error {
+	return GDb.Model(&c).Save(&c).Error
+}
+
+func (c *SwitchBox) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
+}
+
+func (c *SwitchBox) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+	return err
+}
+
+func (c SwitchBox) GetDevices(offset, limit int) ([]SwitchBox, error) {
+	var devices []SwitchBox
+	err := GDb.Model(&c).Where(" box_name like ? ", "%"+c.BoxName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}
+
+func (c SwitchBox) GetAllDevices() ([]SwitchBox, error) {
+	var devices []SwitchBox
+	err := GDb.Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&devices).Error
+	return devices, err
+}

+ 1 - 1
app/dao/transformerDao.go

@@ -37,7 +37,7 @@ func (c Transformer) Delete() error {
 		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
 }
 
-func (c Transformer) IsExistedByCode() bool {
+func (c Transformer) IsExistedBySN() bool {
 	var count = 0
 	_ = GDb.Model(&c).Where("  trans_sn = ? and is_deleted = ?",
 		c.TransSN, c.IsDeleted).Count(&count).Error

+ 15 - 0
app/model/switchBox.go

@@ -0,0 +1,15 @@
+package model
+
+import "iot_manager_service/app/dao"
+
+type RsqSwitchBoxList struct {
+	Records []dao.SwitchBox `json:"records"` //记录列表
+	Current int             `json:"current"` //当前分页
+	Size    int             `json:"size"`    //每页数量
+	Pages   int             `json:"pages"`   //总页数
+	Total   int             `json:"total"`   //总数
+}
+
+type ReqSwitchBoxRemove struct {
+	IDs int `json:"ids"` //分组编码
+}

+ 106 - 0
app/service/switchBoxService.go

@@ -0,0 +1,106 @@
+package service
+
+import (
+	"fmt"
+	"iot_manager_service/app/dao"
+	"iot_manager_service/app/utils"
+	"time"
+)
+
+var SwitchBoxService = new(switchBoxService)
+
+type switchBoxService struct{}
+
+func (s *switchBoxService) Get(id int) (*dao.SwitchBox, *utils.Errors) {
+	// 创建查询实例
+	device := &dao.SwitchBox{
+		ID: id,
+	}
+	err := device.GetDevice()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return device, nil
+
+}
+
+func (s *switchBoxService) CreateOrUpdate(req dao.SwitchBox) *utils.Errors {
+	// 创建查询实例
+	device := req
+	if device.TenantId == "" {
+		device.TenantId = "000000" // todo: 使用登录态
+	}
+	if req.ID == 0 {
+		device.CreateTime = time.Now()
+		device.CreateUser = "TODO" // todo: 使用登录态
+		if device.IsExistedBySN() {
+			fmt.Printf("Create GetDeviceID err \n")
+			return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
+		}
+		if err := device.Create(); err != nil {
+			fmt.Printf("Create err = %s \n", err.Error())
+			return utils.FailResponse(err.Error(), nil)
+		}
+		return utils.SuccessResponse(utils.Succeeded, nil)
+	}
+	//更新
+	device.UpdateUser = "TODO" // todo: 使用登录态
+	device.UpdateTime = time.Now()
+	if device.IsExistedByNameAndCode() {
+		return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
+	}
+
+	if err := device.Update(); err != nil {
+		fmt.Printf("Update err = %s \n", err.Error())
+		return utils.FailResponse(err.Error(), nil)
+	}
+	//todo operation record
+	return utils.SuccessResponse(utils.Succeeded, nil)
+}
+func (s *switchBoxService) List(searchValue string, current, size int) ([]dao.SwitchBox, *utils.Errors) {
+	device := dao.SwitchBox{}
+	if searchValue != "" {
+		device.BoxSN = searchValue
+		device.BoxName = searchValue
+	}
+
+	offset := (current - 1) * size
+	limit := size
+	devices, err := device.GetDevices(offset, limit)
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+	return devices, nil
+}
+func (s *switchBoxService) Remove(id int) *utils.Errors {
+	// 创建查询实例
+	device := &dao.SwitchBox{
+		ID:         id,
+		IsDeleted:  1,
+		UpdateUser: "TODO", // todo 使用登录态
+		UpdateTime: time.Now(),
+	}
+	//todo
+	// service.transformerService.CountRelation()
+
+	//todo operation record
+	err := device.Delete()
+	if err != nil {
+		return utils.FailResponse(err.Error(), nil)
+	}
+	return nil
+}
+
+func (s *switchBoxService) GetList() ([]dao.SwitchBox, *utils.Errors) {
+	// todo use redis cache
+	device := &dao.SwitchBox{
+		TenantId:  "000000", // todo 使用登录态
+		IsDeleted: 0,
+	}
+	devices, err := device.GetAllDevices()
+	if err != nil {
+		return nil, utils.FailResponse(err.Error(), nil)
+	}
+
+	return devices, nil
+}

+ 1 - 1
app/service/transformerService.go

@@ -30,7 +30,7 @@ func (s *transformerService) CreateOrUpdate(req dao.Transformer) *utils.Errors {
 	if req.ID == 0 {
 		device.CreateTime = time.Now()
 		device.CreateUser = "TODO" // todo: 使用登录态
-		if device.IsExistedByCode() {
+		if device.IsExistedBySN() {
 			fmt.Printf("Create GetDeviceID err \n")
 			return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
 		}