mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-05-12 06:42:08 +08:00
iot init
This commit is contained in:
169
apps/device/services/device.go
Normal file
169
apps/device/services/device.go
Normal file
@@ -0,0 +1,169 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/tool"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
DeviceModel interface {
|
||||
Insert(data entity.Device) *entity.Device
|
||||
FindOne(id string) *entity.DeviceRes
|
||||
FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64)
|
||||
FindList(data entity.Device) *[]entity.DeviceRes
|
||||
Update(data entity.Device) *entity.Device
|
||||
UpdateStatus(id, linkStatus string)
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
deviceModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var DeviceModelDao DeviceModel = &deviceModelImpl{
|
||||
table: `devices`,
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device {
|
||||
//1 检查设备名称是否存在
|
||||
list := m.FindList(entity.Device{Name: data.Name})
|
||||
biz.IsTrue(list != nil && len(*list) == 0, "设备名称已经存在")
|
||||
//2 创建认证TOKEN IOTHUB使用
|
||||
etoken := getDeviceToken(&data)
|
||||
if data.DeviceType != global.GATEWAYS && data.DeviceType != global.MONITOR {
|
||||
data.Token = etoken.Token
|
||||
}
|
||||
//3 添加设备
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加设备失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func getDeviceToken(data *entity.Device) *tool.DeviceAuth {
|
||||
now := time.Now()
|
||||
etoken := &tool.DeviceAuth{
|
||||
DeviceId: data.Id,
|
||||
User: data.Owner,
|
||||
Name: data.Name,
|
||||
DeviceType: data.DeviceType,
|
||||
ProductId: data.Pid,
|
||||
}
|
||||
//设备有效期360天
|
||||
etoken.CreatedAt = now.Unix()
|
||||
etoken.ExpiredAt = now.Add(time.Hour * 24 * 365).Unix()
|
||||
if data.Token == "" {
|
||||
etoken.Token = etoken.MD5ID()
|
||||
} else {
|
||||
etoken.Token = data.Token
|
||||
}
|
||||
biz.ErrIsNil(global.RedisDb.Set(data.Id, etoken.GetMarshal(), time.Hour*24*365), "Redis 存储失败")
|
||||
return etoken
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) FindOne(id string) *entity.DeviceRes {
|
||||
resData := new(entity.DeviceRes)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Preload("Product").Preload("DeviceGroup").Error
|
||||
biz.ErrIsNil(err, "查询设备失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64) {
|
||||
list := make([]entity.DeviceRes, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Alias != "" {
|
||||
db = db.Where("alias = ?", data.Alias)
|
||||
}
|
||||
if data.Gid != "" {
|
||||
db = db.Where("gid = ?", data.Gid)
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Owner != "" {
|
||||
db = db.Where("owner = ?", data.Owner)
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
if data.LinkStatus != "" {
|
||||
db = db.Where("Link_status = ?", data.LinkStatus)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
if data.ParentId != "" {
|
||||
db = db.Where("parent_id = ?", data.ParentId)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Preload("Product").Preload("DeviceGroup").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询设备分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes {
|
||||
list := make([]entity.DeviceRes, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Alias != "" {
|
||||
db = db.Where("alias = ?", data.Alias)
|
||||
}
|
||||
if data.Gid != "" {
|
||||
db = db.Where("gid = ?", data.Gid)
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Owner != "" {
|
||||
db = db.Where("owner = ?", data.Owner)
|
||||
}
|
||||
if data.DeviceType != "" {
|
||||
db = db.Where("device_type = ?", data.DeviceType)
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
if data.LinkStatus != "" {
|
||||
db = db.Where("Link_status = ?", data.LinkStatus)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
if data.ParentId != "" {
|
||||
db = db.Where("parent_id = ?", data.ParentId)
|
||||
}
|
||||
db.Preload("Product").Preload("DeviceGroup")
|
||||
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询设备列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) Update(data entity.Device) *entity.Device {
|
||||
getDeviceToken(&data)
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改设备失败")
|
||||
return &data
|
||||
}
|
||||
func (m *deviceModelImpl) UpdateStatus(id, linkStatus string) {
|
||||
global.Db.Table(m.table).Where("id", id).Update("link_status", linkStatus).Update("last_time", time.Now())
|
||||
}
|
||||
|
||||
func (m *deviceModelImpl) Delete(ids []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Device{}, "id in (?)", ids).Error, "删除设备失败")
|
||||
for _, id := range ids {
|
||||
// 删除所有缓存
|
||||
global.RedisDb.Del(context.Background(), id)
|
||||
}
|
||||
}
|
||||
85
apps/device/services/device_alarm.go
Normal file
85
apps/device/services/device_alarm.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
type (
|
||||
DeviceAlarmModel interface {
|
||||
Insert(data entity.DeviceAlarm) error
|
||||
FindOne(id string) *entity.DeviceAlarm
|
||||
FindOneByType(deviceId, ty, state string) *entity.DeviceAlarm
|
||||
FindListPage(page, pageSize int, data entity.DeviceAlarm) (*[]entity.DeviceAlarm, int64)
|
||||
Update(data entity.DeviceAlarm) error
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
alarmModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var DeviceAlarmModelDao DeviceAlarmModel = &alarmModelImpl{
|
||||
table: `device_alarms`,
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) Insert(data entity.DeviceAlarm) error {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) FindOne(id string) *entity.DeviceAlarm {
|
||||
resData := new(entity.DeviceAlarm)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询设备告警失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) FindOneByType(deviceId, ty, state string) *entity.DeviceAlarm {
|
||||
resData := new(entity.DeviceAlarm)
|
||||
db := global.Db.Table(m.table).Where("device_id = ?", deviceId).Where("type = ? ", ty).Where("state = ? ", state)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询设备告警失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) FindListPage(page, pageSize int, data entity.DeviceAlarm) (*[]entity.DeviceAlarm, int64) {
|
||||
list := make([]entity.DeviceAlarm, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
if data.DeviceId != "" {
|
||||
db = db.Where("device_id = ?", data.DeviceId)
|
||||
}
|
||||
if data.ProductId != "" {
|
||||
db = db.Where("product_id = ?", data.ProductId)
|
||||
}
|
||||
if data.Level != "" {
|
||||
db = db.Where("level = ?", data.Level)
|
||||
}
|
||||
if data.Type != "" {
|
||||
db = db.Where("type like ?", "%"+data.Type+"%")
|
||||
}
|
||||
if data.State != "" {
|
||||
db = db.Where("state = ?", data.State)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("time").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询设备告警分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) Update(data entity.DeviceAlarm) error {
|
||||
err := global.Db.Table(m.table).
|
||||
Where("type = ?", data.Type).
|
||||
Where("device_id = ?", data.DeviceId).
|
||||
Updates(&data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *alarmModelImpl) Delete(id []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceAlarm{}, "id in (?)", id).Error, "删除设备告警失败")
|
||||
}
|
||||
83
apps/device/services/device_cmd_log.go
Normal file
83
apps/device/services/device_cmd_log.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
DeviceCmdLogModel interface {
|
||||
Insert(data entity.DeviceCmdLog) error
|
||||
FindOne(id string) *entity.DeviceCmdLog
|
||||
FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64)
|
||||
Update(data entity.DeviceCmdLog) error
|
||||
UpdateResp(id, responseContent, responseTime string) error
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
cmdLogModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var DeviceCmdLogModelDao DeviceCmdLogModel = &cmdLogModelImpl{
|
||||
table: `device_cmd_log`,
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) Insert(data entity.DeviceCmdLog) error {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) FindOne(id string) *entity.DeviceCmdLog {
|
||||
resData := new(entity.DeviceCmdLog)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询设备指令下发失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64) {
|
||||
list := make([]entity.DeviceCmdLog, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
if data.DeviceId != "" {
|
||||
db = db.Where("device_id = ?", data.DeviceId)
|
||||
}
|
||||
if data.CmdName != "" {
|
||||
db = db.Where("cmd_name like ?", "%"+data.CmdName+"%")
|
||||
}
|
||||
if data.Type != "" {
|
||||
db = db.Where("type = ?", data.Type)
|
||||
}
|
||||
if data.State != "" {
|
||||
db = db.Where("state = ?", data.State)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("request_time").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询设备指令下发分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) Update(data entity.DeviceCmdLog) error {
|
||||
err := global.Db.Table(m.table).
|
||||
Where("id = ?", data.Id).
|
||||
Updates(&data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) UpdateResp(id, responseContent, state string) error {
|
||||
responseTime := time.Now().Format("2006-01-02 15:04:05")
|
||||
err := global.Db.Table(m.table).
|
||||
Where("id = ?", id).
|
||||
Update("response_content", responseContent).
|
||||
Update("response_time", responseTime).Update("state", state).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *cmdLogModelImpl) Delete(id []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceCmdLog{}, "id in (?)", id).Error, "删除设备指令下发失败")
|
||||
}
|
||||
188
apps/device/services/device_group.go
Normal file
188
apps/device/services/device_group.go
Normal file
@@ -0,0 +1,188 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
type (
|
||||
DeviceGroupModel interface {
|
||||
Insert(data entity.DeviceGroup) *entity.DeviceGroup
|
||||
FindOne(id string) *entity.DeviceGroup
|
||||
FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64)
|
||||
FindList(data entity.DeviceGroup) *[]entity.DeviceGroup
|
||||
Update(data entity.DeviceGroup) *entity.DeviceGroup
|
||||
Delete(ids []string)
|
||||
SelectDeviceGroup(data entity.DeviceGroup) []entity.DeviceGroup
|
||||
SelectDeviceGroupLabel(data entity.DeviceGroup) []entity.DeviceGroupLabel
|
||||
}
|
||||
|
||||
deviceGroupModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var DeviceGroupModelDao DeviceGroupModel = &deviceGroupModelImpl{
|
||||
table: `device_groups`,
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) Insert(data entity.DeviceGroup) *entity.DeviceGroup {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加设备分组失败")
|
||||
|
||||
path := "/" + data.Id
|
||||
if data.Pid != "0" {
|
||||
vsg := m.FindOne(data.Pid)
|
||||
path = vsg.Path + path
|
||||
} else {
|
||||
path = "/0" + path
|
||||
}
|
||||
data.Path = path
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Model(&data).Updates(&data).Error, "修改设备分组信息失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) FindOne(id string) *entity.DeviceGroup {
|
||||
resData := new(entity.DeviceGroup)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询设备分组失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64) {
|
||||
list := make([]entity.DeviceGroup, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Path != "" {
|
||||
db = db.Where("path like %?%", "%"+data.Path+"%")
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询设备分组分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) FindList(data entity.DeviceGroup) *[]entity.DeviceGroup {
|
||||
list := make([]entity.DeviceGroup, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Path != "" {
|
||||
db = db.Where("path like %?%", "%"+data.Path+"%")
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("sort").Find(&list).Error, "查询设备分组列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) Update(data entity.DeviceGroup) *entity.DeviceGroup {
|
||||
one := m.FindOne(data.Id)
|
||||
|
||||
path := "/" + data.Id
|
||||
if data.Pid != "0" {
|
||||
vsg := m.FindOne(data.Pid)
|
||||
path = vsg.Path + path
|
||||
} else {
|
||||
path = "/0" + path
|
||||
}
|
||||
data.Path = path
|
||||
|
||||
if data.Path != "" && data.Path != one.Path {
|
||||
biz.ErrIsNil(errors.New("上级分组不允许修改!"), "上级分组不允许修改")
|
||||
}
|
||||
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改设备分组失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) Delete(s []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceGroup{}, "id in (?)", s).Error, "删除设备分组失败")
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) SelectDeviceGroup(data entity.DeviceGroup) []entity.DeviceGroup {
|
||||
list := m.FindList(data)
|
||||
sd := make([]entity.DeviceGroup, 0)
|
||||
li := *list
|
||||
for i := 0; i < len(li); i++ {
|
||||
if li[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
info := DiGuiDeviceGroup(list, li[i])
|
||||
sd = append(sd, info)
|
||||
}
|
||||
return sd
|
||||
}
|
||||
|
||||
func (m *deviceGroupModelImpl) SelectDeviceGroupLabel(data entity.DeviceGroup) []entity.DeviceGroupLabel {
|
||||
deptlist := m.FindList(data)
|
||||
|
||||
dl := make([]entity.DeviceGroupLabel, 0)
|
||||
deptl := *deptlist
|
||||
for i := 0; i < len(deptl); i++ {
|
||||
if deptl[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
e := entity.DeviceGroupLabel{}
|
||||
e.Id = deptl[i].Id
|
||||
e.Name = deptl[i].Name
|
||||
deptsInfo := DiGuiDeviceGroupLabel(deptlist, e)
|
||||
|
||||
dl = append(dl, deptsInfo)
|
||||
}
|
||||
return dl
|
||||
}
|
||||
|
||||
func DiGuiDeviceGroup(sglist *[]entity.DeviceGroup, menu entity.DeviceGroup) entity.DeviceGroup {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.DeviceGroup, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if menu.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
mi := entity.DeviceGroup{}
|
||||
mi.Id = list[j].Id
|
||||
mi.Pid = list[j].Pid
|
||||
mi.Path = list[j].Path
|
||||
mi.Name = list[j].Name
|
||||
mi.Sort = list[j].Sort
|
||||
mi.Status = list[j].Status
|
||||
mi.Description = list[j].Description
|
||||
ms := DiGuiDeviceGroup(sglist, mi)
|
||||
min = append(min, ms)
|
||||
}
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
func DiGuiDeviceGroupLabel(sglist *[]entity.DeviceGroup, dept entity.DeviceGroupLabel) entity.DeviceGroupLabel {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.DeviceGroupLabel, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
if dept.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
sg := entity.DeviceGroupLabel{list[j].Id, list[j].Name, []entity.DeviceGroupLabel{}}
|
||||
ms := DiGuiDeviceGroupLabel(sglist, sg)
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
dept.Children = min
|
||||
return dept
|
||||
}
|
||||
152
apps/device/services/product.go
Normal file
152
apps/device/services/product.go
Normal file
@@ -0,0 +1,152 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"log"
|
||||
"pandax/apps/device/entity"
|
||||
ruleEntity "pandax/apps/rule/entity"
|
||||
ruleService "pandax/apps/rule/services"
|
||||
"pandax/pkg/global"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
ProductModel interface {
|
||||
Insert(data entity.Product) *entity.Product
|
||||
FindOne(id string) *entity.ProductRes
|
||||
FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64)
|
||||
FindList(data entity.Product) *[]entity.ProductRes
|
||||
Update(data entity.Product) *entity.Product
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
productModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var ProductModelDao ProductModel = &productModelImpl{
|
||||
table: `products`,
|
||||
}
|
||||
|
||||
func (m *productModelImpl) Insert(data entity.Product) *entity.Product {
|
||||
// 添加产品及规则链到redis中
|
||||
if data.DeviceType != global.MONITOR {
|
||||
setProductRule(&data)
|
||||
}
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加产品失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
// 向redis中添加产品对应的规则链
|
||||
func setProductRule(data *entity.Product) {
|
||||
var rule *ruleEntity.RuleChain
|
||||
if data.RuleChainId == "" {
|
||||
rule = ruleService.RuleChainModelDao.FindOneByRoot()
|
||||
} else {
|
||||
rule = ruleService.RuleChainModelDao.FindOne(data.RuleChainId)
|
||||
}
|
||||
data.RuleChainId = rule.Id
|
||||
biz.ErrIsNil(global.RedisDb.Set(data.Id, rule.RuleDataJson, time.Hour*24*365), "Redis 存储失败")
|
||||
}
|
||||
|
||||
func (m *productModelImpl) FindOne(id string) *entity.ProductRes {
|
||||
resData := new(entity.ProductRes)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.Preload("ProductCategory").First(resData).Error
|
||||
biz.ErrIsNil(err, "查询产品失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64) {
|
||||
list := make([]entity.ProductRes, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
if data.DeviceType != "" {
|
||||
db = db.Where("device_type = ?", data.DeviceType)
|
||||
}
|
||||
if data.Id != "" {
|
||||
db = db.Where("id = ?", data.Id)
|
||||
}
|
||||
if data.ProductCategoryId != "" {
|
||||
db = db.Where("product_category_id = ?", data.ProductCategoryId)
|
||||
}
|
||||
if data.Owner != "" {
|
||||
db = db.Where("owner = ?", data.Owner)
|
||||
}
|
||||
if data.ProtocolName != "" {
|
||||
db = db.Where("protocol_name like ?", "%"+data.ProtocolName+"%")
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.RuleChainId != "" {
|
||||
db = db.Where("rule_chain_id = ?", data.RuleChainId)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Preload("ProductCategory").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询产品分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *productModelImpl) FindList(data entity.Product) *[]entity.ProductRes {
|
||||
list := make([]entity.ProductRes, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
if data.DeviceType != "" {
|
||||
db = db.Where("device_type = ?", data.DeviceType)
|
||||
}
|
||||
if data.Id != "" {
|
||||
db = db.Where("id = ?", data.Id)
|
||||
}
|
||||
if data.ProductCategoryId != "" {
|
||||
db = db.Where("product_category_id = ?", data.ProductCategoryId)
|
||||
}
|
||||
if data.Owner != "" {
|
||||
db = db.Where("owner = ?", data.Owner)
|
||||
}
|
||||
if data.ProtocolName != "" {
|
||||
db = db.Where("protocol_name like ?", "%"+data.ProtocolName+"%")
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.OrgId != "" {
|
||||
db = db.Where("org_id = ?", data.OrgId)
|
||||
}
|
||||
if data.RuleChainId != "" {
|
||||
db = db.Where("rule_chain_id = ?", data.RuleChainId)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("create_time").Preload("ProductCategory").Find(&list).Error, "查询产品列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *productModelImpl) Update(data entity.Product) *entity.Product {
|
||||
setProductRule(&data)
|
||||
// go的一些默认值 int 0 bool false 保存失败需要先转成map
|
||||
err := global.Db.Table(m.table).Where("id = ?", data.Id).Updates(data).Error
|
||||
log.Println("update", err)
|
||||
//biz.ErrIsNil(, "修改产品失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *productModelImpl) Delete(ids []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Product{}, "id in (?)", ids).Error, "删除产品失败")
|
||||
for _, id := range ids {
|
||||
// 删除所有缓存
|
||||
global.RedisDb.Del(context.Background(), id)
|
||||
}
|
||||
}
|
||||
190
apps/device/services/product_category.go
Normal file
190
apps/device/services/product_category.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
type (
|
||||
ProductCategoryModel interface {
|
||||
Insert(data entity.ProductCategory) *entity.ProductCategory
|
||||
FindOne(id string) *entity.ProductCategory
|
||||
FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64)
|
||||
FindList(data entity.ProductCategory) *[]entity.ProductCategory
|
||||
Update(data entity.ProductCategory) *entity.ProductCategory
|
||||
Delete(ids []string)
|
||||
SelectProductCategory(data entity.ProductCategory) []entity.ProductCategory
|
||||
SelectProductCategoryLabel(data entity.ProductCategory) []entity.ProductCategoryLabel
|
||||
}
|
||||
|
||||
productCategoryModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var ProductCategoryModelDao ProductCategoryModel = &productCategoryModelImpl{
|
||||
table: `product_categories`,
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) Insert(data entity.ProductCategory) *entity.ProductCategory {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加产品分组失败")
|
||||
|
||||
path := "/" + data.Id
|
||||
if data.Pid != "0" {
|
||||
vsg := m.FindOne(data.Pid)
|
||||
path = vsg.Path + path
|
||||
} else {
|
||||
path = "/0" + path
|
||||
}
|
||||
data.Path = path
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Model(&data).Updates(&data).Error, "修改产品分组信息失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) FindOne(id string) *entity.ProductCategory {
|
||||
resData := new(entity.ProductCategory)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询产品分组失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64) {
|
||||
list := make([]entity.ProductCategory, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Path != "" {
|
||||
db = db.Where("path like %?%", "%"+data.Path+"%")
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询产品分组分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) FindList(data entity.ProductCategory) *[]entity.ProductCategory {
|
||||
list := make([]entity.ProductCategory, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Path != "" {
|
||||
db = db.Where("path like %?%", "%"+data.Path+"%")
|
||||
}
|
||||
if data.Status != "" {
|
||||
db = db.Where("status = ?", data.Status)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("sort").Find(&list).Error, "查询产品分组列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) Update(data entity.ProductCategory) *entity.ProductCategory {
|
||||
one := m.FindOne(data.Id)
|
||||
|
||||
path := "/" + data.Id
|
||||
if data.Pid != "0" {
|
||||
vsg := m.FindOne(data.Pid)
|
||||
path = vsg.Path + path
|
||||
} else {
|
||||
path = "/0" + path
|
||||
}
|
||||
data.Path = path
|
||||
|
||||
if data.Path != "" && data.Path != one.Path {
|
||||
biz.ErrIsNil(errors.New("上级分组不允许修改!"), "上级分组不允许修改")
|
||||
}
|
||||
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品分组失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) Delete(s []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductCategory{}, "id in (?)", s).Error, "删除产品分组失败")
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) SelectProductCategory(data entity.ProductCategory) []entity.ProductCategory {
|
||||
list := m.FindList(data)
|
||||
sd := make([]entity.ProductCategory, 0)
|
||||
li := *list
|
||||
for i := 0; i < len(li); i++ {
|
||||
if li[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
info := DiGuiProductCategory(list, li[i])
|
||||
sd = append(sd, info)
|
||||
}
|
||||
return sd
|
||||
}
|
||||
|
||||
func (m *productCategoryModelImpl) SelectProductCategoryLabel(data entity.ProductCategory) []entity.ProductCategoryLabel {
|
||||
list := m.FindList(data)
|
||||
|
||||
dl := make([]entity.ProductCategoryLabel, 0)
|
||||
deptl := *list
|
||||
for i := 0; i < len(deptl); i++ {
|
||||
if deptl[i].Pid != "0" {
|
||||
continue
|
||||
}
|
||||
e := entity.ProductCategoryLabel{}
|
||||
e.Id = deptl[i].Id
|
||||
e.Name = deptl[i].Name
|
||||
deptsInfo := DiGuiProductCategoryLabel(list, e)
|
||||
|
||||
dl = append(dl, deptsInfo)
|
||||
}
|
||||
return dl
|
||||
}
|
||||
|
||||
func DiGuiProductCategory(sglist *[]entity.ProductCategory, menu entity.ProductCategory) entity.ProductCategory {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.ProductCategory, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
|
||||
if menu.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
mi := entity.ProductCategory{}
|
||||
mi.Id = list[j].Id
|
||||
mi.Pid = list[j].Pid
|
||||
mi.Path = list[j].Path
|
||||
mi.Name = list[j].Name
|
||||
mi.Sort = list[j].Sort
|
||||
mi.Status = list[j].Status
|
||||
mi.Description = list[j].Description
|
||||
ms := DiGuiProductCategory(sglist, mi)
|
||||
min = append(min, ms)
|
||||
}
|
||||
menu.Children = min
|
||||
return menu
|
||||
}
|
||||
func DiGuiProductCategoryLabel(sglist *[]entity.ProductCategory, dept entity.ProductCategoryLabel) entity.ProductCategoryLabel {
|
||||
list := *sglist
|
||||
|
||||
min := make([]entity.ProductCategoryLabel, 0)
|
||||
for j := 0; j < len(list); j++ {
|
||||
if dept.Id != list[j].Pid {
|
||||
continue
|
||||
}
|
||||
sg := entity.ProductCategoryLabel{}
|
||||
sg.Id = list[j].Id
|
||||
sg.Name = list[j].Name
|
||||
ms := DiGuiProductCategoryLabel(sglist, sg)
|
||||
min = append(min, ms)
|
||||
|
||||
}
|
||||
dept.Children = min
|
||||
return dept
|
||||
}
|
||||
127
apps/device/services/product_ota.go
Normal file
127
apps/device/services/product_ota.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"log"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
)
|
||||
|
||||
type (
|
||||
ProductOtaModel interface {
|
||||
Insert(data entity.ProductOta) *entity.ProductOta
|
||||
FindOne(id string) *entity.ProductOta
|
||||
FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64)
|
||||
FindList(data entity.ProductOta) *[]entity.ProductOta
|
||||
Update(data entity.ProductOta) *entity.ProductOta
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
otaModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var ProductOtaModelDao ProductOtaModel = &otaModelImpl{
|
||||
table: `product_ota`,
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) Insert(data entity.ProductOta) *entity.ProductOta {
|
||||
m.checkLatest(data)
|
||||
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加产品固件失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) checkLatest(data entity.ProductOta) {
|
||||
// 将原来的最新版更改为旧版本
|
||||
if data.IsLatest == true {
|
||||
latest := m.getLatest()
|
||||
m.updateLatest(latest.Id, false)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) FindOne(id string) *entity.ProductOta {
|
||||
resData := new(entity.ProductOta)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询产品固件失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64) {
|
||||
list := make([]entity.ProductOta, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Description != "" {
|
||||
db = db.Where("description = ?", data.Description)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
if data.Id != "" {
|
||||
db = db.Where("id = ?", data.Id)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Url != "" {
|
||||
db = db.Where("url = ?", data.Url)
|
||||
}
|
||||
if data.Version != "" {
|
||||
db = db.Where("version = ?", data.Version)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询产品固件分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) FindList(data entity.ProductOta) *[]entity.ProductOta {
|
||||
list := make([]entity.ProductOta, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Description != "" {
|
||||
db = db.Where("description = ?", data.Description)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
if data.Id != "" {
|
||||
db = db.Where("id = ?", data.Id)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Version != "" {
|
||||
db = db.Where("version = ?", data.Version)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询产品固件列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) getLatest() *entity.ProductOta {
|
||||
resData := new(entity.ProductOta)
|
||||
db := global.Db.Table(m.table).Where("is_latest = ?", true)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询产品固件失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) Update(data entity.ProductOta) *entity.ProductOta {
|
||||
m.checkLatest(data)
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品固件失败")
|
||||
return &data
|
||||
}
|
||||
func (m *otaModelImpl) updateLatest(id string, IsLatest bool) {
|
||||
log.Println(id, IsLatest)
|
||||
err := global.Db.Table(m.table).Where("id = ?", id).Update("is_latest", IsLatest).Error
|
||||
global.Log.Error("更新失败", err)
|
||||
}
|
||||
|
||||
func (m *otaModelImpl) Delete(ids []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductOta{}, "id in (?)", ids).Error, "删除产品固件失败")
|
||||
}
|
||||
114
apps/device/services/product_template.go
Normal file
114
apps/device/services/product_template.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"github.com/XM-GO/PandaKit/biz"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/pkg/global"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
ProductTemplateModel interface {
|
||||
Insert(data entity.ProductTemplate) *entity.ProductTemplate
|
||||
FindOne(id string) *entity.ProductTemplate
|
||||
FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64)
|
||||
FindListAttrs(data entity.ProductTemplate) *[]entity.ProductTemplate
|
||||
FindList(data entity.ProductTemplate) *[]entity.ProductTemplate
|
||||
Update(data entity.ProductTemplate) *entity.ProductTemplate
|
||||
Delete(ids []string)
|
||||
}
|
||||
|
||||
templateModelImpl struct {
|
||||
table string
|
||||
}
|
||||
)
|
||||
|
||||
var ProductTemplateModelDao ProductTemplateModel = &templateModelImpl{
|
||||
table: `product_templates`,
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) Insert(data entity.ProductTemplate) *entity.ProductTemplate {
|
||||
err := global.Db.Table(m.table).Create(&data).Error
|
||||
biz.ErrIsNil(err, "添加产品模型失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) FindOne(id string) *entity.ProductTemplate {
|
||||
resData := new(entity.ProductTemplate)
|
||||
db := global.Db.Table(m.table).Where("id = ?", id)
|
||||
err := db.First(resData).Error
|
||||
biz.ErrIsNil(err, "查询产品模型失败")
|
||||
return resData
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64) {
|
||||
list := make([]entity.ProductTemplate, 0)
|
||||
var total int64 = 0
|
||||
offset := pageSize * (page - 1)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Classify != "" {
|
||||
db = db.Where("classify in (?)", strings.Split(data.Classify, ","))
|
||||
}
|
||||
if data.Key != "" {
|
||||
db = db.Where("key = ?", data.Key)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Type != "" {
|
||||
db = db.Where("type = ?", data.Type)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
err := db.Count(&total).Error
|
||||
err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询产品模型分页列表失败")
|
||||
return &list, total
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) FindListAttrs(data entity.ProductTemplate) *[]entity.ProductTemplate {
|
||||
list := make([]entity.ProductTemplate, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
db = db.Where("classify in (?)", []string{"attributes", "telemetry"})
|
||||
err := db.Order("create_time").Find(&list).Error
|
||||
biz.ErrIsNil(err, "查询产品模型分页列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) FindList(data entity.ProductTemplate) *[]entity.ProductTemplate {
|
||||
list := make([]entity.ProductTemplate, 0)
|
||||
db := global.Db.Table(m.table)
|
||||
// 此处填写 where参数判断
|
||||
if data.Classify != "" {
|
||||
db = db.Where("classify in (?)", strings.Split(data.Classify, ","))
|
||||
}
|
||||
if data.Key != "" {
|
||||
db = db.Where("key = ?", data.Key)
|
||||
}
|
||||
if data.Name != "" {
|
||||
db = db.Where("name like ?", "%"+data.Name+"%")
|
||||
}
|
||||
if data.Type != "" {
|
||||
db = db.Where("type = ?", data.Type)
|
||||
}
|
||||
if data.Pid != "" {
|
||||
db = db.Where("pid = ?", data.Pid)
|
||||
}
|
||||
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询产品模型列表失败")
|
||||
return &list
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) Update(data entity.ProductTemplate) *entity.ProductTemplate {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品模型失败")
|
||||
return &data
|
||||
}
|
||||
|
||||
func (m *templateModelImpl) Delete(ids []string) {
|
||||
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductTemplate{}, "id in (?)", ids).Error, "删除产品模型失败")
|
||||
}
|
||||
Reference in New Issue
Block a user