LightStrategyService.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.TimeConditionList = TimeConditionService.GetByLightId(id)
  26. if len(lightStrategy.TimeConditionList) > 0 {
  27. lightStrategy.LightType = "时间"
  28. }
  29. lightStrategy.LightType = lightStrategy.LightType + "策略"
  30. return lightStrategy, nil
  31. }
  32. func (s *lightStrategyService) CreateOrUpdate(userId int64, tenantId int, req *model.LightStrategyDetail) *util.Errors {
  33. // 创建查询实例
  34. strategy := req
  35. fmt.Printf("CreateOrUpdate strategy = %+v \n", strategy)
  36. strategy.TenantId = tenantId
  37. strategy.UpdateUser = userId
  38. strategy.UpdateTime = time.Now()
  39. if len(strategy.LightConditionList) == 0 {
  40. for i := 0; i < len(strategy.LightConditionDetailList); i++ {
  41. var detail = strategy.LightConditionDetailList[i]
  42. condition := dao.LightCondition{
  43. ID: detail.ID,
  44. Remark: detail.Remark,
  45. LightId: detail.LightId,
  46. Luminance: detail.Luminance,
  47. ScopeStart: detail.ScopeStart,
  48. ScopeEnd: detail.ScopeEnd,
  49. }
  50. strategy.LightConditionList = append(strategy.LightConditionList, condition)
  51. }
  52. }
  53. if len(strategy.TimeConditionList) == 0 {
  54. for i := 0; i < len(strategy.TimeConditionDetailList); i++ {
  55. var detail = strategy.TimeConditionDetailList[i]
  56. condition := dao.TimeCondition{
  57. ID: detail.ID,
  58. Remark: detail.Remark,
  59. LightId: detail.LightId,
  60. Luminance: detail.Luminance,
  61. StartTime: detail.StartTime,
  62. EndTime: detail.EndTime,
  63. Sunshine: detail.Sunshine,
  64. }
  65. strategy.TimeConditionList = append(strategy.TimeConditionList, condition)
  66. }
  67. }
  68. if req.ID == 0 {
  69. strategy.CreateTime = time.Now()
  70. strategy.CreateUser = userId
  71. if strategy.IsExistedBySN() {
  72. fmt.Printf("Create GetstrategyID err \n")
  73. return util.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  74. }
  75. }
  76. //用于比较光照度重叠
  77. var overlapList []dao.LightCondition
  78. for _, lightCondition := range strategy.LightConditionList {
  79. if (lightCondition.ScopeStart != 0 && lightCondition.ScopeEnd == 0) || (lightCondition.
  80. ScopeStart == 0 && lightCondition.ScopeEnd != 0) {
  81. //todo 光照度请填写完整
  82. return nil
  83. } else if lightCondition.ScopeEnd > 0 && lightCondition.ScopeStart >= lightCondition.ScopeEnd {
  84. //todo 光照度起始值不能大于结束值
  85. return nil
  86. }
  87. overlapList = append(overlapList, lightCondition)
  88. if lightCondition.ID == 0 {
  89. lightCondition.CreateUser = userId
  90. lightCondition.CreateTime = time.Now()
  91. } else {
  92. lightCondition.UpdateUser = userId
  93. lightCondition.UpdateTime = time.Now()
  94. }
  95. lightCondition.LightId = req.ID
  96. }
  97. //光照条件查询是否区间重叠
  98. if len(overlapList) > 0 {
  99. sort.SliceIsSorted(overlapList, func(i, j int) bool {
  100. return overlapList[i].
  101. ScopeStart < overlapList[j].ScopeStart
  102. })
  103. tmp := -1
  104. for _, overLap := range overlapList {
  105. if tmp >= overLap.ScopeStart {
  106. //todo 区间有重叠
  107. return nil
  108. }
  109. tmp = overLap.ScopeEnd
  110. }
  111. }
  112. // 时间策略参数校验
  113. var timeList []dao.TimeCondition
  114. for _, timeCondition := range strategy.TimeConditionList {
  115. if timeCondition.Sunshine == 0 {
  116. timeCondition.Sunshine = 1
  117. }
  118. if timeCondition.Sunshine == 1 && (timeCondition.StartTime == "" && timeCondition.EndTime == "") {
  119. return util.FailResponse(model.TimeConditionNull, nil)
  120. }
  121. if (timeCondition.StartTime == "" && timeCondition.EndTime != "") || (timeCondition.
  122. StartTime != "" && timeCondition.EndTime == "") {
  123. return util.FailResponse(model.TimeConditionNull, nil)
  124. } else if timeCondition.EndTime != "" && timeCondition.StartTime >= timeCondition.EndTime {
  125. return util.FailResponse(model.TimeConditionInvalid, nil)
  126. }
  127. if timeCondition.ID == 0 {
  128. timeCondition.CreateUser = userId
  129. timeCondition.CreateTime = time.Now()
  130. } else {
  131. timeCondition.UpdateUser = userId
  132. timeCondition.UpdateTime = time.Now()
  133. }
  134. timeCondition.LightId = req.ID
  135. if timeCondition.ID == 0 {
  136. timeCondition.CreateUser = userId
  137. timeCondition.CreateTime = time.Now()
  138. }
  139. timeCondition.UpdateUser = userId
  140. timeCondition.UpdateTime = time.Now()
  141. timeList = append(timeList, timeCondition)
  142. }
  143. //存数据库
  144. if strategy.ID == 0 {
  145. if err := strategy.Create(); err != nil {
  146. fmt.Printf("Create err = %s \n", err.Error())
  147. return util.FailResponse(err.Error(), nil)
  148. }
  149. } else {
  150. if err := strategy.Update(); err != nil {
  151. fmt.Printf("Update err = %s \n", err.Error())
  152. return util.FailResponse(err.Error(), nil)
  153. }
  154. }
  155. //存策略条件
  156. _ = TimeConditionService.Save(timeList)
  157. //_ = LightConditionService.Save(strategy.LightConditionList)
  158. //todo 调用设备云端的新增或修改
  159. //todo 自动续期
  160. //strategy.IsAutomaticRenewal
  161. //todo operation record
  162. return util.SuccessResponse(util.Succeeded, nil)
  163. }
  164. func (s *lightStrategyService) List(searchValue string, current, size int) ([]model.LightStrategyDetail, *util.Errors) {
  165. strategy := dao.LightStrategy{}
  166. if searchValue != "" {
  167. strategy.LightSn = searchValue
  168. }
  169. offset := (current - 1) * size
  170. limit := size
  171. strategies, err := strategy.GetStrategies(offset, limit)
  172. if err != nil {
  173. return nil, util.FailResponse(err.Error(), nil)
  174. }
  175. timeConditions, _ := TimeConditionService.GetAll()
  176. timeConditionsMap := make(map[int]dao.TimeCondition)
  177. for _, timeCondition := range timeConditions {
  178. timeConditionsMap[timeCondition.LightId] = timeCondition
  179. }
  180. year := time.Now().Year()
  181. var details []model.LightStrategyDetail
  182. for _, strategy := range strategies {
  183. if strategy.IsAllYear == model.IsYear {
  184. strategy.StartTime = fmt.Sprintf("%d-01-01", year)
  185. strategy.StartTime = fmt.Sprintf("%d-12-31", year)
  186. }
  187. detail := model.LightStrategyDetail{LightStrategy: strategy}
  188. if timeCondition, isExist := timeConditionsMap[strategy.ID]; isExist {
  189. //开启日出日落
  190. if timeCondition.Sunshine == 2 {
  191. timeCondition.StartTime = "日落"
  192. timeCondition.EndTime = "日出"
  193. }
  194. detail.CombinationStr = fmt.Sprintf("开灯时段:%s ~ %s , 灯光亮度:%d%%", timeCondition.StartTime,
  195. timeCondition.EndTime, timeCondition.Luminance)
  196. }
  197. details = append(details, detail)
  198. }
  199. return details, nil
  200. }
  201. func (s *lightStrategyService) Remove(userId int64, id int) *util.Errors {
  202. // 创建查询实例
  203. strategy := &dao.LightStrategy{
  204. ID: id,
  205. IsDeleted: 1,
  206. UpdateUser: userId,
  207. UpdateTime: time.Now(),
  208. }
  209. //todo operation record
  210. err := strategy.Delete()
  211. if err != nil {
  212. return util.FailResponse(err.Error(), nil)
  213. }
  214. return nil
  215. }
  216. func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *util.Errors) {
  217. strategy := &dao.LightStrategy{
  218. ID: id,
  219. }
  220. err := strategy.GetStrategy()
  221. if err != nil {
  222. return nil, nil
  223. }
  224. return strategy, nil
  225. }