123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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) {
- var devices []*dao.OnDemandGroup
- err := utils.Redis.Get(getOnDemandGroupListRedisKey("000000")).Scan(&devices)
- if err == nil {
- return devices, nil
- }
- device := &dao.OnDemandGroup{
- TenantId: "000000", // todo 使用登录态
- IsDeleted: 0,
- }
- devices, err = device.GetAllDevices()
- if err != nil {
- return nil, utils.FailResponse(err.Error(), nil)
- }
- _ = utils.Redis.Set(getOnDemandGroupListRedisKey("000000"), devices, 0).Err()
- return devices, nil
- }
- func getOnDemandGroupListRedisKey(tenantId string) string {
- return fmt.Sprintf(model.OnDemandGroupList, tenantId)
- }
- 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
- }
|