LightStrategyService.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 int, tenantId string, 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. strategy.Status = 1
  74. if strategy.IsExistedBySN() {
  75. logger.Logger.Errorf("Create GetstrategyID err \n")
  76. return common.ParamsInvalidResponse("编码不能重复,请重新填写!", nil)
  77. }
  78. }
  79. //用于比较光照度重叠
  80. var overlapList []dao.LightCondition
  81. for _, lightCondition := range strategy.LightConditionList {
  82. if (lightCondition.ScopeStart != 0 && lightCondition.ScopeEnd == 0) || (lightCondition.
  83. ScopeStart == 0 && lightCondition.ScopeEnd != 0) {
  84. return common.ParamsInvalidResponse("光照度请填写完整", nil)
  85. } else if lightCondition.ScopeEnd > 0 && lightCondition.ScopeStart >= lightCondition.ScopeEnd {
  86. return common.ParamsInvalidResponse("光照度起始值不能大于结束值", nil)
  87. }
  88. overlapList = append(overlapList, lightCondition)
  89. if lightCondition.ID == 0 {
  90. lightCondition.CreateUser = userId
  91. lightCondition.CreateTime = time.Now()
  92. } else {
  93. lightCondition.UpdateUser = userId
  94. lightCondition.UpdateTime = time.Now()
  95. }
  96. lightCondition.LightId = req.ID
  97. }
  98. //光照条件查询是否区间重叠
  99. if len(overlapList) > 0 {
  100. sort.SliceIsSorted(overlapList, func(i, j int) bool {
  101. return overlapList[i].
  102. ScopeStart < overlapList[j].ScopeStart
  103. })
  104. tmp := -1
  105. for _, overLap := range overlapList {
  106. if tmp >= overLap.ScopeStart {
  107. return common.ParamsInvalidResponse("光照条件区间有重叠", 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 common.FailResponse(model.TimeConditionNull, nil)
  120. }
  121. if (timeCondition.StartTime == "" && timeCondition.EndTime != "") || (timeCondition.
  122. StartTime != "" && timeCondition.EndTime == "") {
  123. return common.FailResponse(model.TimeConditionNull, nil)
  124. } else if timeCondition.EndTime != "" && timeCondition.StartTime >= timeCondition.EndTime {
  125. return common.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. logger.Logger.Errorf("Create err = %s \n", err.Error())
  147. return common.FailResponse(err.Error(), nil)
  148. }
  149. } else {
  150. if err := strategy.Update(); err != nil {
  151. logger.Logger.Errorf("Update err = %s \n", err.Error())
  152. return common.FailResponse(err.Error(), nil)
  153. }
  154. }
  155. //存策略条件
  156. _ = TimeConditionService.Save(timeList, strategy.ID)
  157. _ = LightConditionService.Save(strategy.LightConditionList, strategy.ID)
  158. //todo 调用设备云端的新增或修改
  159. //todo 自动续期
  160. //strategy.IsAutomaticRenewal
  161. service.OperationHisService.Save(userId, tenantId, common.OperationCreate, common.ModuleTypeDevice,
  162. common.DeviceTypeLightStrategy, common.GetDeviceObject(strategy.ID, strategy.LightName), common.OperationSuccess)
  163. return common.SuccessResponse(common.Succeeded, nil)
  164. }
  165. func (s *lightStrategyService) List(searchValue string, current, size int) ([]model.LightStrategyDetail, int64, *common.Errors) {
  166. strategy := dao.LightStrategy{}
  167. if searchValue != "" {
  168. strategy.LightSn = searchValue
  169. }
  170. offset := (current - 1) * size
  171. limit := size
  172. strategies, total, err := strategy.GetStrategies(offset, limit)
  173. if err != nil {
  174. return nil, 0, common.FailResponse(err.Error(), nil)
  175. }
  176. timeConditions, _ := TimeConditionService.GetAll()
  177. timeConditionsMap := make(map[int]dao.TimeCondition)
  178. for _, timeCondition := range timeConditions {
  179. timeConditionsMap[timeCondition.LightId] = timeCondition
  180. }
  181. year := time.Now().Year()
  182. var details []model.LightStrategyDetail
  183. for _, strategy := range strategies {
  184. if strategy.IsAllYear == model.IsYear {
  185. strategy.StartTime = fmt.Sprintf("%d-01-01", year)
  186. strategy.StartTime = fmt.Sprintf("%d-12-31", year)
  187. }
  188. detail := model.LightStrategyDetail{LightStrategy: strategy}
  189. if timeCondition, isExist := timeConditionsMap[strategy.ID]; isExist {
  190. //开启日出日落
  191. if timeCondition.Sunshine == 2 {
  192. timeCondition.StartTime = "日落"
  193. timeCondition.EndTime = "日出"
  194. }
  195. str := []string{}
  196. str = append(str, fmt.Sprintf("开灯时段:%s ~ %s , 灯光亮度:%d%%", timeCondition.StartTime,
  197. timeCondition.EndTime, timeCondition.Luminance))
  198. detail.CombinationStr = str
  199. }
  200. lightStrategy := &model.LightStrategyDetail{
  201. LightStrategy: strategy,
  202. }
  203. lightStrategy.TimeConditionList = TimeConditionService.GetByLightId(strategy.ID)
  204. if len(lightStrategy.TimeConditionList) > 0 {
  205. lightStrategy.LightType = "时间"
  206. }
  207. detail.LightType = lightStrategy.LightType + "策略"
  208. details = append(details, detail)
  209. }
  210. return details, total, nil
  211. }
  212. func (s *lightStrategyService) Remove(userId int, tenantId string, id int) *common.Errors {
  213. // 创建查询实例
  214. strategy := &dao.LightStrategy{
  215. ID: id,
  216. IsDeleted: 1,
  217. UpdateUser: userId,
  218. UpdateTime: time.Now(),
  219. }
  220. err := strategy.Delete()
  221. if err != nil {
  222. return common.FailResponse(err.Error(), nil)
  223. }
  224. service.OperationHisService.Save(userId, tenantId, common.OperationRemove, common.ModuleTypeDevice,
  225. common.DeviceTypeLightStrategy, common.GetDeviceObject(strategy.ID, strategy.LightName), common.OperationSuccess)
  226. return nil
  227. }
  228. func (s *lightStrategyService) GetOne(id int) (*dao.LightStrategy, *common.Errors) {
  229. strategy := &dao.LightStrategy{
  230. ID: id,
  231. }
  232. err := strategy.GetStrategy()
  233. if err != nil {
  234. return nil, nil
  235. }
  236. return strategy, nil
  237. }