LightStrategyService.go 7.3 KB

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