LightStrategyService.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/util"
  7. "sort"
  8. "time"
  9. )
  10. // 中间件管理服务
  11. var LightStrategyService = new(lightStrategyService)
  12. type lightStrategyService struct{}
  13. func (s *lightStrategyService) Get(id int) (*model.LightStrategyDetail, *util.Errors) {
  14. // 创建查询实例
  15. strategy := &dao.LightStrategy{
  16. ID: id,
  17. }
  18. err := strategy.GetStrategy()
  19. if err != nil {
  20. return nil, util.FailResponse(err.Error(), nil)
  21. }
  22. lightStrategy := &model.LightStrategyDetail{
  23. LightStrategy: *strategy,
  24. }
  25. lightStrategy.LightConditionList = LightConditionService.BatchGet([]int{id})
  26. lightStrategy.TimeConditionList = TimeConditionService.BatchGet([]int{id})
  27. if len(lightStrategy.TimeConditionList) > 0 {
  28. lightStrategy.LightType = "时间"
  29. }
  30. lightStrategy.LightType = lightStrategy.LightType + "策略"
  31. lightingList := IntelligentLightingService.BatchGet([]int{id})
  32. for _, lighting := range lightingList {
  33. // 关联类型1=灯杆,2=灯杆分组
  34. if lighting.RelationType == 2 {
  35. lampPole, _ := LampPoleService.Get(lighting.Rid)
  36. lightStrategy.LampPoleDetailList = append(lightStrategy.LampPoleDetailList,
  37. model.LampPoleDetail{LampPole: *lampPole})
  38. } else {
  39. lampPoleGroup, _ := LampPoleGroupService.Get(lighting.Rid)
  40. lightStrategy.LampPoleGroupDetailList = append(lightStrategy.LampPoleGroupDetailList,
  41. model.LampPoleGroupDetail{LampPoleGroup: *lampPoleGroup})
  42. }
  43. }
  44. return lightStrategy, nil
  45. }
  46. func (s *lightStrategyService) CreateOrUpdate(req *model.LightStrategyDetail) *util.Errors {
  47. // 创建查询实例
  48. strategy := req
  49. if strategy.TenantID == "" {
  50. strategy.TenantID = "000000" // todo: 使用登录态
  51. }
  52. strategy.UpdateUser = "TODO" // todo: 使用登录态
  53. strategy.UpdateTime = time.Now()
  54. if len(strategy.LightConditionList) == 0 {
  55. for i := 0; i < len(strategy.LightConditionDetailList); i++ {
  56. var detail = strategy.LightConditionDetailList[i]
  57. condition := dao.LightCondition{
  58. ID: detail.ID,
  59. Remark: detail.Remark,
  60. LightId: detail.LightId,
  61. Luminance: detail.Luminance,
  62. ScopeStart: detail.ScopeStart,
  63. ScopeEnd: detail.ScopeEnd,
  64. }
  65. strategy.LightConditionList = append(strategy.LightConditionList, condition)
  66. }
  67. }
  68. if len(strategy.TimeConditionList) == 0 {
  69. for i := 0; i < len(strategy.TimeConditionDetailList); i++ {
  70. var detail = strategy.TimeConditionDetailList[i]
  71. condition := dao.TimeCondition{
  72. ID: detail.ID,
  73. Remark: detail.Remark,
  74. LightId: detail.LightId,
  75. Luminance: detail.Luminance,
  76. StartTime: detail.StartTime,
  77. EndTime: detail.EndTime,
  78. Sunshine: detail.Sunshine,
  79. }
  80. strategy.TimeConditionList = append(strategy.TimeConditionList, condition)
  81. }
  82. }
  83. if req.ID == 0 {
  84. strategy.CreateTime = time.Now()
  85. strategy.CreateUser = "TODO" // todo: 使用登录态
  86. if strategy.IsExistedBySN() {
  87. fmt.Printf("Create GetstrategyID err \n")
  88. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  89. }
  90. //todo 不能先存
  91. if err := strategy.Create(); err != nil {
  92. fmt.Printf("Create err = %s \n", err.Error())
  93. return util.FailResponse(err.Error(), nil)
  94. }
  95. return util.SuccessResponse(util.Succeeded, nil)
  96. }
  97. //todo 不能先存
  98. if err := strategy.Update(); err != nil {
  99. fmt.Printf("Update err = %s \n", err.Error())
  100. return util.FailResponse(err.Error(), nil)
  101. }
  102. //用于比较光照度重叠
  103. var overlapList []dao.LightCondition
  104. for _, lightCondition := range strategy.LightConditionList {
  105. if (lightCondition.ScopeStart != 0 && lightCondition.ScopeEnd == 0) || (lightCondition.
  106. ScopeStart == 0 && lightCondition.ScopeEnd != 0) {
  107. //todo 光照度请填写完整
  108. return nil
  109. } else if lightCondition.ScopeEnd > 0 && lightCondition.ScopeStart >= lightCondition.ScopeEnd {
  110. //todo 光照度起始值不能大于结束值
  111. return nil
  112. }
  113. overlapList = append(overlapList, lightCondition)
  114. if lightCondition.ID == 0 {
  115. lightCondition.CreateUser = "TODO" //todo 登录态
  116. lightCondition.CreateTime = time.Now()
  117. } else {
  118. lightCondition.UpdateUser = "TODO" //todo 登录态
  119. lightCondition.UpdateTime = time.Now()
  120. }
  121. lightCondition.LightId = req.ID
  122. }
  123. //光照条件查询是否区间重叠
  124. if len(overlapList) > 0 {
  125. sort.SliceIsSorted(overlapList, func(i, j int) bool {
  126. return overlapList[i].
  127. ScopeStart < overlapList[j].ScopeStart
  128. })
  129. tmp := -1
  130. for _, overLap := range overlapList {
  131. if tmp >= overLap.ScopeStart {
  132. //todo 区间有重叠
  133. return nil
  134. }
  135. tmp = overLap.ScopeEnd
  136. }
  137. }
  138. // 时间策略参数校验
  139. for _, timeCondition := range strategy.TimeConditionList {
  140. if timeCondition.Sunshine == 0 {
  141. timeCondition.Sunshine = 1
  142. }
  143. if timeCondition.Sunshine == 1 && (timeCondition.StartTime == "" && timeCondition.EndTime == "") {
  144. //todo 请将时间区间填写完整!
  145. return nil
  146. }
  147. if (timeCondition.StartTime == "" && timeCondition.EndTime != "") || (timeCondition.
  148. StartTime != "" && timeCondition.EndTime == "") {
  149. //todo 请将时间区间填写完整!
  150. return nil
  151. } else if timeCondition.EndTime != "" && timeCondition.StartTime >= timeCondition.EndTime {
  152. //todo 时间区间开始时间不能大于结束时间,请重新调整!
  153. return nil
  154. }
  155. if timeCondition.ID == 0 {
  156. timeCondition.CreateUser = "TODO" //todo 登录态
  157. timeCondition.CreateTime = time.Now()
  158. } else {
  159. timeCondition.UpdateUser = "TODO" //todo 登录态
  160. timeCondition.UpdateTime = time.Now()
  161. }
  162. timeCondition.LightId = req.ID
  163. }
  164. //todo create or update
  165. strategyDao := &dao.LightStrategy{}
  166. if strategy.ID == 0 {
  167. strategyDao.Create()
  168. } else {
  169. strategyDao.Update()
  170. }
  171. TimeConditionService.Save(strategy.TimeConditionList)
  172. LightConditionService.Save(strategy.LightConditionList)
  173. //todo 调用设备云端的新增或修改
  174. //todo operation record
  175. return util.SuccessResponse(util.Succeeded, nil)
  176. }
  177. func (s *lightStrategyService) List(searchValue string, current, size int) ([]dao.LightStrategy, *util.Errors) {
  178. strategy := dao.LightStrategy{}
  179. if searchValue != "" {
  180. strategy.LightSn = searchValue
  181. strategy.LightName = searchValue
  182. }
  183. offset := (current - 1) * size
  184. limit := size
  185. strategies, err := strategy.GetStrategies(offset, limit)
  186. if err != nil {
  187. return nil, util.FailResponse(err.Error(), nil)
  188. }
  189. return strategies, nil
  190. }
  191. func (s *lightStrategyService) Remove(id int) *util.Errors {
  192. // 创建查询实例
  193. strategy := &dao.LightStrategy{
  194. ID: id,
  195. IsDeleted: 1,
  196. UpdateUser: "TODO", // todo 使用登录态
  197. UpdateTime: time.Now(),
  198. }
  199. //todo
  200. // service.transformerService.CountRelation()
  201. //todo operation record
  202. err := strategy.Delete()
  203. if err != nil {
  204. return util.FailResponse(err.Error(), nil)
  205. }
  206. return nil
  207. }
  208. func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *util.Errors) {
  209. strategy := &dao.LightStrategy{
  210. ID: id,
  211. }
  212. err := strategy.GetStrategy()
  213. if err != nil {
  214. return nil, nil
  215. }
  216. return strategy, nil
  217. }