123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/dao"
- "iot_manager_service/app/utils"
- "time"
- )
- var ZigbeerService = new(zigbeeService)
- type zigbeeService struct{}
- func (s *zigbeeService) Get(id int) (*dao.Zigbee, *utils.Errors) {
- // 创建查询实例
- device := &dao.Zigbee{
- ID: id,
- }
- err := device.GetDevice()
- if err != nil {
- return nil, utils.FailResponse(err.Error(), nil)
- }
- return device, nil
- }
- func (s *zigbeeService) CreateOrUpdate(req dao.Zigbee) *utils.Errors {
- // 创建查询实例
- device := req
- if req.ID == 0 {
- device.CreateTime = time.Now()
- device.CreateUser = "TODO" // todo: 使用登录态
- if device.IsExistedBySN() {
- fmt.Printf("Create GetDeviceID err \n")
- return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
- }
- 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)
- }
- device.UpdateUser = "TODO" // todo: 使用登录态
- device.UpdateTime = time.Now()
- if device.IsExistedByNameAndCode() {
- return utils.ParamsInvalidResponse("编码不能重复,请重新填写!", 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 *zigbeeService) List(searchValue string, current, size int) ([]dao.Zigbee, *utils.Errors) {
- device := dao.Zigbee{}
- if searchValue != "" {
- device.SN = searchValue
- device.Name = 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 *zigbeeService) Remove(id int) *utils.Errors {
- // 创建查询实例
- device := &dao.Zigbee{
- ID: id,
- IsDeleted: 1,
- UpdateUser: "TODO", // todo 使用登录态
- UpdateTime: time.Now(),
- }
- //todo
- // service.transformerService.CountRelation()
- //todo operation record
- err := device.Delete()
- if err != nil {
- return utils.FailResponse(err.Error(), nil)
- }
- return nil
- }
- func (s *zigbeeService) GetList() ([]*dao.Zigbee, *utils.Errors) {
- device := &dao.Zigbee{
- TenantId: "000000", // todo 使用登录态
- IsDeleted: 0,
- }
- devices, err := device.GetAllDevices()
- if err != nil {
- return nil, utils.FailResponse(err.Error(), nil)
- }
- return devices, nil
- }
|