123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/device/model"
- "iot_manager_service/util"
- "time"
- )
- // 中间件管理服务
- var LampPoleService = new(lampPoleService)
- type lampPoleService struct{}
- func (s *lampPoleService) Get(id int) (*dao.LampPole, *util.Errors) {
- // 创建查询实例
- device := &dao.LampPole{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- //todo runstate 需要使用各个设备的状态来处理
- return device, nil
- }
- func (s *lampPoleService) GetRelevanceDetail(id int) (*model.LampPoleDetail, *util.Errors) {
- // 创建查询实例
- device := &dao.LampPole{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- //todo get gateway ipBroadcast lightcontroller... list
- return &model.LampPoleDetail{
- LampPole: *device,
- AlarmTerminalList: nil,
- CameraList: nil,
- CaptureUnitList: nil,
- GatewayList: nil,
- InfoBoardList: nil,
- IpBroadcastList: nil,
- LightControlList: nil,
- SensorList: nil,
- ZigbeeList: nil,
- }, nil
- }
- func (s *lampPoleService) CreateOrUpdate(userId int64, tenantId int, req *dao.LampPole) *util.Errors {
- device := req
- device.TenantId = tenantId
- device.CoordType = 1
- device.UpdateUser = userId
- device.UpdateTime = time.Now()
- if device.ID == 0 {
- device.CreateTime = time.Now()
- device.CreateUser = userId
- if device.IsExistedBySN() {
- fmt.Printf("Create IsExistedBySN \n")
- return util.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 util.FailResponse(err.Error(), nil)
- }
- return util.SuccessResponse(util.Succeeded, nil)
- }
- if err := device.Update(); err != nil {
- fmt.Printf("Update err = %s \n", err.Error())
- return util.FailResponse(err.Error(), nil)
- }
- //todo operation record
- return util.SuccessResponse(util.Succeeded, nil)
- }
- func (s *lampPoleService) List(searchValue, groupId, boxId string, current, size int) ([]dao.LampPole, *util.Errors) {
- device := dao.LampPole{}
- if searchValue != "" {
- device.PoleSN = searchValue
- }
- device.GroupId = util.StringToInt(groupId)
- device.BoxId = util.StringToInt(boxId)
- offset := (current - 1) * size
- limit := size
- devices, err := device.GetDevices(offset, limit)
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return devices, nil
- }
- func (s *lampPoleService) Remove(userId int64, id int) *util.Errors {
- // 创建查询实例
- device := &dao.LampPole{
- ID: id,
- IsDeleted: 1,
- UpdateUser: userId,
- UpdateTime: time.Now(),
- }
- //todo
- // service.lampPoleService.CountRelation()
- //todo operation record
- err := device.Delete()
- if err != nil {
- return util.FailResponse(err.Error(), nil)
- }
- return nil
- }
- func (s *lampPoleService) GetList(tenantId int) ([]*dao.LampPole, *util.Errors) {
- device := &dao.LampPole{
- TenantId: tenantId,
- IsDeleted: 0,
- }
- devices, err := device.GetAllDevices()
- for _, device := range devices {
- device.PoleName = device.PoleName + "(" + device.PoleSN + ")"
- }
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return devices, nil
- }
- func (s *lampPoleService) GetOne(id int) (*dao.LampPole, *util.Errors) {
- device := &dao.LampPole{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, util.FailResponse(err.Error(), nil)
- }
- return device, nil
- }
- func (s *lampPoleService) CountLampPole(groupId int) int {
- device := &dao.LampPole{
- GroupId: groupId,
- }
- return device.Count()
- }
|