LightStrategyService.go 7.8 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/app/system/service"
  7. "iot_manager_service/util/common"
  8. "iot_manager_service/util/logger"
  9. "sort"
  10. "time"
  11. )
  12. // 中间件管理服务
  13. var LightStrategyService = new(lightStrategyService)
  14. type lightStrategyService struct{}
  15. func (s *lightStrategyService) Get(id int) (*model.LightStrategyDetail, *common.Errors) {
  16. // 创建查询实例
  17. strategy := &dao.LightStrategy{
  18. ID: id,
  19. }
  20. err := strategy.GetStrategy()
  21. if err != nil {
  22. return nil, common.FailResponse(err.Error(), nil)
  23. }
  24. lightStrategy := &model.LightStrategyDetail{
  25. LightStrategy: *strategy,
  26. }
  27. lightStrategy.TimeConditionList = TimeConditionService.GetByLightId(id)
  28. if len(lightStrategy.TimeConditionList) > 0 {
  29. lightStrategy.LightType = "时间"
  30. }
  31. lightStrategy.LightType = lightStrategy.LightType + "策略"
  32. return lightStrategy, nil
  33. }
  34. func (s *lightStrategyService) CreateOrUpdate(userId int64, tenantId int, req *model.LightStrategyDetail) *common.Errors {
  35. // 创建查询实例
  36. strategy := req
  37. logger.Logger.Errorf("CreateOrUpdate strategy = %+v \n", strategy)
  38. strategy.TenantId = tenantId
  39. strategy.UpdateUser = userId
  40. strategy.UpdateTime = time.Now()
  41. if len(strategy.LightConditionList) == 0 {
  42. for i := 0; i < len(strategy.LightConditionDetailList); i++ {
  43. var detail = strategy.LightConditionDetailList[i]
  44. condition := dao.LightCondition{
  45. ID: detail.ID,
  46. Remark: detail.Remark,
  47. LightId: detail.LightId,
  48. Luminance: detail.Luminance,
  49. ScopeStart: detail.ScopeStart,
  50. ScopeEnd: detail.ScopeEnd,
  51. }
  52. strategy.LightConditionList = append(strategy.LightConditionList, condition)
  53. }
  54. }
  55. if len(strategy.TimeConditionList) == 0 {
  56. for i := 0; i < len(strategy.TimeConditionDetailList); i++ {
  57. var detail = strategy.TimeConditionDetailList[i]
  58. condition := dao.TimeCondition{
  59. ID: detail.ID,
  60. Remark: detail.Remark,
  61. LightId: detail.LightId,
  62. Luminance: detail.Luminance,
  63. StartTime: detail.StartTime,
  64. EndTime: detail.EndTime,
  65. Sunshine: detail.Sunshine,
  66. }
  67. strategy.TimeConditionList = append(strategy.TimeConditionList, condition)
  68. }
  69. }
  70. if req.ID == 0 {
  71. strategy.CreateTime = time.Now()
  72. strategy.CreateUser = userId
  73. if strategy.IsExistedBySN() {
  74. logger.Logger.Errorf("Create GetstrategyID err \n")
  75. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  76. }
  77. }
  78. //用于比较光照度重叠
  79. var overlapList []dao.LightCondition
  80. for _, lightCondition := range strategy.LightConditionList {
  81. if (lightCondition.ScopeStart != 0 && lightCondition.ScopeEnd == 0) || (lightCondition.
  82. ScopeStart == 0 && lightCondition.ScopeEnd != 0) {
  83. return common.ParamsInvalidResponse("光照度请填写完整", nil)
  84. } else if lightCondition.ScopeEnd > 0 && lightCondition.ScopeStart >= lightCondition.ScopeEnd {
  85. return common.ParamsInvalidResponse("光照度起始值不能大于结束值", 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. return common.ParamsInvalidResponse("光照条件区间有重叠", nil)
  107. }
  108. tmp = overLap.ScopeEnd
  109. }
  110. }
  111. // 时间策略参数校验
  112. var timeList []dao.TimeCondition
  113. for _, timeCondition := range strategy.TimeConditionList {
  114. if timeCondition.Sunshine == 0 {
  115. timeCondition.Sunshine = 1
  116. }
  117. if timeCondition.Sunshine == 1 && (timeCondition.StartTime == "" && timeCondition.EndTime == "") {
  118. return common.FailResponse(model.TimeConditionNull, nil)
  119. }
  120. if (timeCondition.StartTime == "" && timeCondition.EndTime != "") || (timeCondition.
  121. StartTime != "" && timeCondition.EndTime == "") {
  122. return common.FailResponse(model.TimeConditionNull, nil)
  123. } else if timeCondition.EndTime != "" && timeCondition.StartTime >= timeCondition.EndTime {
  124. return common.FailResponse(model.TimeConditionInvalid, nil)
  125. }
  126. if timeCondition.ID == 0 {
  127. timeCondition.CreateUser = userId
  128. timeCondition.CreateTime = time.Now()
  129. } else {
  130. timeCondition.UpdateUser = userId
  131. timeCondition.UpdateTime = time.Now()
  132. }
  133. timeCondition.LightId = req.ID
  134. if timeCondition.ID == 0 {
  135. timeCondition.CreateUser = userId
  136. timeCondition.CreateTime = time.Now()
  137. }
  138. timeCondition.UpdateUser = userId
  139. timeCondition.UpdateTime = time.Now()
  140. timeList = append(timeList, timeCondition)
  141. }
  142. //存数据库
  143. if strategy.ID == 0 {
  144. if err := strategy.Create(); err != nil {
  145. logger.Logger.Errorf("Create err = %s \n", err.Error())
  146. return common.FailResponse(err.Error(), nil)
  147. }
  148. } else {
  149. if err := strategy.Update(); err != nil {
  150. logger.Logger.Errorf("Update err = %s \n", err.Error())
  151. return common.FailResponse(err.Error(), nil)
  152. }
  153. }
  154. //存策略条件
  155. _ = TimeConditionService.Save(timeList)
  156. _ = LightConditionService.Save(strategy.LightConditionList)
  157. //todo 调用设备云端的新增或修改
  158. //todo 自动续期
  159. //strategy.IsAutomaticRenewal
  160. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  161. common.DeviceTypeLightStrategy, common.GetDeviceObject(strategy.ID, strategy.LightName), common.OperationSuccess)
  162. return common.SuccessResponse(common.Succeeded, nil)
  163. }
  164. func (s *lightStrategyService) List(searchValue string, current, size int) ([]model.LightStrategyDetail, *common.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, common.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, tenantId int, id int) *common.Errors {
  202. // 创建查询实例
  203. strategy := &dao.LightStrategy{
  204. ID: id,
  205. IsDeleted: 1,
  206. UpdateUser: userId,
  207. UpdateTime: time.Now(),
  208. }
  209. err := strategy.Delete()
  210. if err != nil {
  211. return common.FailResponse(err.Error(), nil)
  212. }
  213. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  214. common.DeviceTypeLightStrategy, common.GetDeviceObject(strategy.ID, strategy.LightName), common.OperationSuccess)
  215. return nil
  216. }
  217. func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *common.Errors) {
  218. strategy := &dao.LightStrategy{
  219. ID: id,
  220. }
  221. err := strategy.GetStrategy()
  222. if err != nil {
  223. return nil, nil
  224. }
  225. return strategy, nil
  226. }