|
@@ -0,0 +1,125 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "iot_manager_service/app/dao"
|
|
|
+ "iot_manager_service/app/model"
|
|
|
+ "iot_manager_service/app/utils"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// 中间件管理服务
|
|
|
+var OnDemandGroupService = new(onDemandGroupService)
|
|
|
+
|
|
|
+type onDemandGroupService struct{}
|
|
|
+
|
|
|
+func (s *onDemandGroupService) Get(id int) (*dao.OnDemandGroup, *utils.Errors) {
|
|
|
+ // 创建查询实例
|
|
|
+ device := &dao.OnDemandGroup{
|
|
|
+ ID: id,
|
|
|
+ }
|
|
|
+ err := device.GetDevice()
|
|
|
+ if err != nil {
|
|
|
+ return nil, utils.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return device, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *onDemandGroupService) CreateOrUpdate(req *dao.OnDemandGroup) *utils.Errors {
|
|
|
+ device := req
|
|
|
+ if device.TenantId == "" {
|
|
|
+ device.TenantId = "000000" // todo: 使用登录态
|
|
|
+ }
|
|
|
+ device.UpdateUser = "TODO" // todo: 使用登录态
|
|
|
+ device.UpdateTime = time.Now()
|
|
|
+
|
|
|
+ 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)
|
|
|
+}
|
|
|
+
|
|
|
+func (s *onDemandGroupService) List(searchValue string, current, size int) ([]dao.OnDemandGroup, *utils.Errors) {
|
|
|
+ device := dao.OnDemandGroup{}
|
|
|
+
|
|
|
+ if searchValue != "" {
|
|
|
+ device.PoleGroupName = 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 *onDemandGroupService) Remove(id int) *utils.Errors {
|
|
|
+ // 创建查询实例
|
|
|
+ device := &dao.OnDemandGroup{
|
|
|
+ ID: id,
|
|
|
+ IsDeleted: 1,
|
|
|
+ UpdateUser: "TODO", // todo 使用登录态
|
|
|
+ UpdateTime: time.Now(),
|
|
|
+ }
|
|
|
+
|
|
|
+ //todo operation record
|
|
|
+ err := device.Delete()
|
|
|
+ if err != nil {
|
|
|
+ return utils.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *onDemandGroupService) GetList() ([]*dao.OnDemandGroup, *utils.Errors) {
|
|
|
+ // todo use redis cache
|
|
|
+ device := &dao.OnDemandGroup{
|
|
|
+ TenantId: "000000", // todo 使用登录态
|
|
|
+ IsDeleted: 0,
|
|
|
+ }
|
|
|
+ devices, err := device.GetAllDevices()
|
|
|
+ if err != nil {
|
|
|
+ return nil, utils.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ return devices, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *onDemandGroupService) GroupNumberList(id int) ([]model.RspOnDemandGroupNumber, *utils.Errors) {
|
|
|
+ // 创建查询实例
|
|
|
+ device := &dao.OnDemandGroup{
|
|
|
+ ID: id,
|
|
|
+ }
|
|
|
+ err := device.GetDevice()
|
|
|
+ if err != nil {
|
|
|
+ return nil, utils.FailResponse(err.Error(), nil)
|
|
|
+ }
|
|
|
+ var list []model.RspOnDemandGroupNumber
|
|
|
+ for i := 1; i <= device.LampPoleCount; i++ {
|
|
|
+ list = append(list, model.RspOnDemandGroupNumber{Name: strconv.Itoa(i), Value: i})
|
|
|
+ }
|
|
|
+
|
|
|
+ return list, nil
|
|
|
+}
|