瀏覽代碼

增加and is_deleted = 0

terry 2 年之前
父節點
當前提交
4c72a4479c

+ 2 - 2
app/controller/cameraController.go

@@ -14,7 +14,7 @@ var Camera = new(cameraCtl)
 type cameraCtl struct{}
 
 func (c *cameraCtl) Detail(ctx *gin.Context) {
-	var req *model.ReqCamera
+	var req *model.CameraDetail
 	if err := ctx.ShouldBind(&req); err != nil {
 		ctx.JSON(http.StatusOK, nil)
 		return
@@ -26,7 +26,7 @@ func (c *cameraCtl) List(ctx *gin.Context) {
 
 func (c *cameraCtl) CreateOrUpdate(ctx *gin.Context) {
 	// 参数验证
-	var req *model.ReqCamera
+	var req *model.CameraDetail
 	if err := ctx.ShouldBind(&req); err != nil {
 		ctx.JSON(http.StatusOK, utils.ParamsInvalidResponse(err.Error(), nil))
 		return

+ 51 - 47
app/dao/cameraDao.go

@@ -4,64 +4,68 @@ import "time"
 
 //CameraDevice 网关下挂载的设备, 摄像头
 type CameraDevice struct {
-	ID               int       `gorm:"primary_key"`                  //编号
-	DeviceName       string    `gorm:"type:varchar(64)"`             //设备名称
-	DeviceSN         string    `gorm:"type:varchar(64)"`             //设备序列号
-	CameraType       int       `gorm:"type:int"`                     //摄像机类型 0=枪机,1=球机
-	GatewayId        string    `gorm:"type:varchar(32)"`             //所属网关
-	LampPoleId       int       `gorm:"type:int"`                     //归属灯杆 灯杆ID
-	LampPoleName     string    `gorm:"type:varchar(64)"`             //灯杆名称
-	LampPoleSn       string    `gorm:"type:varchar(64)"`             //灯杆sn
-	LampPoleLocation string    `gorm:"type:varchar(255)"`            //灯杆安装位置
-	LampLat          float64   `gorm:"type:double(17,14)"`           //经度
-	LampLng          float64   `gorm:"type:double(17,14)"`           //纬度
-	GroupId          int       `gorm:"type:int"`                     //灯杆分组ID
-	BrandId          int       `gorm:"type:int"`                     //设备品牌
-	ModelId          int       `gorm:"type:int"`                     //设备型号
-	RatedPower       float32   `gorm:"type:float(8,2);default 0.00"` //功率
-	MonitorAddress   string    `gorm:"type:varchar(255)"`            //监控地址 ip:端口
-	IPAddress        string    `gorm:"type:varchar(40)"`             //IP地址
-	InstallTime      time.Time `gorm:"type:date"`                    //安装时间
-	TenantId         string    `gorm:"type:varchar(12)"`             //租户ID
-	CreateTime       time.Time `gorm:"type:datetime"`                //新增时间
-	CreateUser       string    `gorm:"type:varchar(60)"`             //新增记录操作用户ID
-	UpdateTime       time.Time `gorm:"type:datetime"`                //修改时间
-	UpdateUser       string    `gorm:"type:varchar(60)"`             //修改用户
-	IsDeleted        int       `gorm:"type:int;default 0"`           //是否删除 0=未删除,1=删除
-	Status           int       `gorm:"type:int"`                     //状态 0=正常,1=异常
-	Tag              string    `gorm:"type:varchar(255)"`            //标签,(备用,逗号区分)
-	IsEnable         int       `gorm:"type:int;default 2"`           //启用禁用:1启用,2禁用
-	StreamId         string    `gorm:"type:varchar(100)"`            //流id
+	ID               int       `gorm:"primary_key" json:"id"`                          //编号
+	DeviceName       string    `gorm:"type:varchar(64)" json:"deviceName"`             //设备名称
+	DeviceSN         string    `gorm:"type:varchar(64)" json:"deviceSn"`               //设备序列号
+	CameraType       int       `gorm:"type:int" json:"cameraType"`                     //摄像机类型 0=枪机,1=球机
+	GatewayId        string    `gorm:"type:varchar(32)" json:"gatewayId"`              //所属网关
+	LampPoleId       int       `gorm:"type:int" json:"lampPoleId"`                     //归属灯杆 灯杆ID
+	LampPoleName     string    `gorm:"type:varchar(64)" json:"lampPoleName"`           //灯杆名称
+	LampPoleSn       string    `gorm:"type:varchar(64)" json:"lampPoleSn"`             //灯杆sn
+	LampPoleLocation string    `gorm:"type:varchar(255)" json:"lampPoleLocation"`      //灯杆安装位置
+	LampLat          float64   `gorm:"type:double(17,14)" json:"lampLat"`              //经度
+	LampLng          float64   `gorm:"type:double(17,14)" json:"lampLng"`              //纬度
+	GroupId          int       `gorm:"type:int" json:"groupId"`                        //灯杆分组ID
+	BrandId          int       `gorm:"type:int" json:"brandId"`                        //设备品牌
+	ModelId          int       `gorm:"type:int" json:"modelId"`                        //设备型号
+	RatedPower       float32   `gorm:"type:float(8,2);default 0.00" json:"ratedPower"` //功率
+	MonitorAddress   string    `gorm:"type:varchar(255)" json:"monitorAddress"`        //监控地址 ip:端口
+	IPAddress        string    `gorm:"type:varchar(40)" json:"ipAddress"`              //IP地址
+	InstallTime      time.Time `gorm:"type:date" json:"installTime"`                   //安装时间
+	TenantId         string    `gorm:"type:varchar(12)" json:"tenantId"`               //租户ID
+	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"`                   //标签,(备用,逗号区分)
+	IsEnable         int       `gorm:"type:int;default 2" json:"isEnable"`             //启用禁用:1启用,2禁用
+	StreamId         int       `gorm:"type:int" json:"stream_id"`                      //流id
 }
 
 func (CameraDevice) TableName() string {
 	return "t_dev_camera"
 }
 
-func (c CameraDevice) Delete() error {
-	return GDb.Model(&c).Updates(map[string]interface{}{"state": 0}).Error
+func (c CameraDevice) IsExistedBySN() bool {
+	var count = 0
+	_ = GDb.Model(&c).Where("device_sn = ? and is_deleted = ?",
+		c.DeviceSN, c.IsDeleted).Count(&count).Error
+	return count > 0
 }
 
-func (c CameraDevice) IsExistedByCode() (bool, error) {
-	var count = 0
-	err := GDb.Model(&c).Where(" id = ? ", c.ID).Count(&count).Error
-	return count > 0, err
+func (c *CameraDevice) Create() error {
+	return GDb.Model(&c).Save(&c).Error
 }
 
-func (c CameraDevice) SaveFromWeb() error {
-	has, err := c.IsExistedByCode()
-	if err != nil {
-		return err
-	}
-	if has { //更新
-		return GDb.Model(&c).Updates(map[string]interface{}{"device_name": c.DeviceName, "gateway_id": c.GatewayId,
-			"tenant_id": c.TenantId, "status": c.Status}).Error
-	} else { //插入
-		return GDb.Create(&c).Error
-	}
+func (c *CameraDevice) Update() error {
+	return GDb.Model(&c).Where(" id = ? ", c.ID).Update(&c).Error
 }
 
-func (c CameraDevice) GetCameraDevice() error {
-	err := GDb.Model(&c).Where(" id = ? ", c.ID).Scan(&c).Error
+func (c *CameraDevice) GetDevice() error {
+	err := GDb.Model(&c).Where(" id = ? ", c.ID).First(&c).Error
 	return err
 }
+
+func (c CameraDevice) GetDevices(offset, limit int) ([]Gateway, error) {
+	var devices []Gateway
+	db := GDb.Model(&c)
+	if c.DeviceSN != "" {
+		db = db.Where("(device_name like ? or device_sn like ?) and is_deleted = 0", "%"+c.DeviceSN+"%",
+			"%"+c.DeviceSN+"%")
+	}
+
+	err := db.Offset(offset).Limit(limit).Find(&devices).Error
+	return devices, err
+}

+ 1 - 1
app/dao/garbageDao.go

@@ -87,7 +87,7 @@ func (c *Garbage) GetDevice() error {
 
 func (c Garbage) GetDevices(offset, limit int) ([]Garbage, error) {
 	var devices []Garbage
-	err := GDb.Model(&c).Where(" device_name like ? ", "%"+c.DeviceName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where(" device_name like ? and is_deleted = 0", "%"+c.DeviceName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 1 - 1
app/dao/garbageWayGroupDao.go

@@ -65,7 +65,7 @@ func (c *GarbageWayGroup) GetDevice() error {
 
 func (c GarbageWayGroup) GetDevices(offset, limit int) ([]GarbageWayGroup, error) {
 	var devices []GarbageWayGroup
-	err := GDb.Model(&c).Where(" group_name like ? ", "%"+c.GroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where(" group_name like ? and is_deleted = 0", "%"+c.GroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 1 - 1
app/dao/gatewayDao.go

@@ -83,7 +83,7 @@ func (c Gateway) GetDevices(offset, limit int) ([]Gateway, error) {
 	var devices []Gateway
 	db := GDb.Model(&c)
 	if c.GatewaySN != "" {
-		db = db.Where("gateway_name like ? or gateway_sn like ?", "%"+c.GatewaySN+"%", "%"+c.GatewaySN+"%")
+		db = db.Where("(gateway_name like ? or gateway_sn like ?) and is_deleted = 0", "%"+c.GatewaySN+"%", "%"+c.GatewaySN+"%")
 	}
 
 	err := db.Offset(offset).Limit(limit).Find(&devices).Error

+ 1 - 1
app/dao/lampPoleDao.go

@@ -86,7 +86,7 @@ func (c LampPole) GetDevices(offset, limit int) ([]LampPole, error) {
 
 	db := GDb.Model(&c)
 	if c.PoleSN != "" {
-		db = db.Where("pole_name like ? or pole_sn like ?", "%"+c.PoleSN+"%", "%"+c.PoleSN+"%")
+		db = db.Where("(pole_name like ? or pole_sn like ?) and is_deleted = 0", "%"+c.PoleSN+"%", "%"+c.PoleSN+"%")
 	}
 	if c.GroupId != -1 {
 		db = db.Where("group_id = ?", c.GroupId)

+ 1 - 1
app/dao/lampPoleGroupDao.go

@@ -67,7 +67,7 @@ func (c *LampPoleGroup) GetDevice() error {
 
 func (c LampPoleGroup) GetDevices(offset, limit int) ([]LampPoleGroup, error) {
 	var devices []LampPoleGroup
-	err := GDb.Model(&c).Where(" pole_group_name like ? ", "%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where(" pole_group_name like ? and is_deleted = 0", "%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 1 - 1
app/dao/lightControlDao.go

@@ -94,7 +94,7 @@ func (c LightControl) GetDevices(offset, limit int) ([]LightControl, error) {
 
 	db := GDb.Model(&c)
 	if c.SN != "" {
-		db = db.Where("name like ? or sn like ?", "%"+c.SN+"%", "%"+c.SN+"%")
+		db = db.Where("(name like ? or sn like ?) and is_deleted = 0", "%"+c.SN+"%", "%"+c.SN+"%")
 	}
 	if c.ControlType != 0 {
 		db = db.Where("control_type = ?", c.ControlType)

+ 2 - 1
app/dao/onDemandGroupDao.go

@@ -68,7 +68,8 @@ func (c *OnDemandGroup) GetDevice() error {
 
 func (c OnDemandGroup) GetDevices(offset, limit int) ([]OnDemandGroup, error) {
 	var devices []OnDemandGroup
-	err := GDb.Model(&c).Where("pole_group_name like ? or sn like ?", "%"+c.PoleGroupName+"%", "%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where("(pole_group_name like ? or sn like ?) and is_deleted = 0", "%"+c.PoleGroupName+"%",
+		"%"+c.PoleGroupName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 2 - 1
app/dao/switchBoxDao.go

@@ -81,7 +81,8 @@ func (c *SwitchBox) GetDevice() error {
 
 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
+	err := GDb.Model(&c).Where("(box_name like ?) and is_deleted = 0",
+		"%"+c.BoxName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 1 - 1
app/dao/transformerDao.go

@@ -75,7 +75,7 @@ func (c *Transformer) GetDevice() error {
 
 func (c Transformer) GetDevices(offset, limit int) ([]Transformer, error) {
 	var devices []Transformer
-	err := GDb.Model(&c).Where(" trans_name like ? ", "%"+c.TransName+"%").Offset(offset).Limit(limit).Find(&devices).Error
+	err := GDb.Model(&c).Where(" trans_name like ? and is_deleted = 0", "%"+c.TransName+"%").Offset(offset).Limit(limit).Find(&devices).Error
 	return devices, err
 }
 

+ 4 - 9
app/model/camera.go

@@ -1,12 +1,7 @@
 package model
 
-type ReqCamera struct {
-	Code    int    `json:"code"`    //设备编号,禁止修改
-	GID     string `json:"gid"`     //网关ID
-	Tenant  string `json:"tenant"`  //租户ID
-	Name    string `json:"name"`    //设备名称
-	Brand   int    `json:"brand"`   //品牌
-	Model   int    `json:"model"`   //型号
-	DevType int    `json:"devtype"` //设备类型
-	State   int    `json:"state"`   //1启用,0禁用
+import "iot_manager_service/app/dao"
+
+type CameraDetail struct {
+	dao.CameraDevice
 }

+ 45 - 12
app/service/cameraService.go

@@ -1,8 +1,10 @@
 package service
 
 import (
-	"iot_manager_service/app/dao"
+	"fmt"
 	"iot_manager_service/app/model"
+	"iot_manager_service/app/utils"
+	"time"
 )
 
 // 中间件管理服务
@@ -10,17 +12,48 @@ var CameraService = new(cameraService)
 
 type cameraService struct{}
 
-func (s *cameraService) CreateOrUpdate(req *model.ReqCamera) error {
-	// 创建查询实例
-	device := dao.CameraDevice{
-		ID:         req.Code, //设备ID,DID
-		DeviceName: req.Name, //集控器名称
-		GatewayId:  req.GID,  //网关ID
-		TenantId:   req.Tenant,
-		Status:     req.State, //1启用,0禁用
+func (s *cameraService) CreateOrUpdate(req *model.CameraDetail) *utils.Errors {
+	device := req.CameraDevice
+	if device.TenantId == "" {
+		device.TenantId = "000000" // todo: 使用登录态
 	}
-	if err := device.SaveFromWeb(); err != nil {
-		return err
+	device.UpdateUser = "TODO" // todo: 使用登录态
+	device.UpdateTime = time.Now()
+	if device.LampPoleId != 0 {
+		lampPole, err := LampPoleService.GetOne(device.LampPoleId)
+		if err == nil {
+			device.LampLat = lampPole.PoleLat
+			device.LampLng = lampPole.PoleLng
+			device.LampPoleName = lampPole.PoleName
+			device.LampPoleSn = lampPole.PoleSN
+			device.LampPoleLocation = lampPole.InstallLocation
+			device.GroupId = lampPole.GroupId
+		} else {
+			fmt.Printf("LampPoleService.GetOne err = %v \n", err)
+		}
 	}
-	return nil
+
+	if device.ID == 0 {
+		device.CreateTime = time.Now()
+		device.CreateUser = "TODO" // todo: 使用登录态
+
+		if device.IsExistedBySN() {
+			fmt.Printf("Create IsExistedBySN \n")
+			return utils.ParamsInvalidResponse(model.RepeatedPrompts, nil)
+		}
+		fmt.Printf("device = %+v \n", device)
+		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)
+	}
+
+	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)
 }