linkEventLogic.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package logic
  2. import (
  3. "git.rtzhtech.cn/iss/public-lib/dao"
  4. "git.rtzhtech.cn/iss/public-lib/dto"
  5. "git.rtzhtech.cn/iss/public-lib/model"
  6. "git.rtzhtech.cn/iss/public-lib/svc"
  7. "git.rtzhtech.cn/iss/public-lib/utils"
  8. "github.com/gogf/gf/v2/util/gconv"
  9. "gorm.io/gen"
  10. "gorm.io/gen/field"
  11. "time"
  12. )
  13. type LinkEventLogic struct {
  14. SvcCtx *svc.ServiceContext
  15. }
  16. // NewLinkEventLogic
  17. // @Description: 联动事件
  18. // @return *LinkEventLogic
  19. func NewLinkEventLogic() *LinkEventLogic {
  20. return &LinkEventLogic{
  21. SvcCtx: svc.SvcCtx,
  22. }
  23. }
  24. // AddLinkEvent
  25. // @Description: 添加
  26. // @receiver l
  27. // @param in
  28. // @return error
  29. func (l *LinkEventLogic) AddLinkEvent(in *model.LinkEvent) error {
  30. in.ID = 0
  31. err := l.SvcCtx.LinkEvent.Create(in)
  32. return err
  33. }
  34. // DelLinkEvent
  35. // @Description: 删除
  36. // @receiver l
  37. // @param id
  38. // @return error
  39. func (l *LinkEventLogic) DelLinkEvent(req *dto.DelLinkEventReq) error {
  40. do := l.SvcCtx.LinkEvent
  41. var cond []gen.Condition
  42. if req.EventId > 0 {
  43. cond = append(cond, do.Eventid.Eq(req.EventId))
  44. }
  45. if req.StartTime != "" {
  46. t, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartTime, time.Local)
  47. if err != nil {
  48. return err
  49. }
  50. cond = append(cond, do.CreateAt.Gte(t))
  51. }
  52. if req.EndTime != "" {
  53. t, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndTime, time.Local)
  54. if err != nil {
  55. return err
  56. }
  57. cond = append(cond, do.CreateAt.Lte(t))
  58. }
  59. _, err := do.Where(cond...).Delete()
  60. return err
  61. }
  62. // ModifyLinkEvent
  63. // @Description: 修改
  64. // @receiver l
  65. // @param in
  66. // @param fields 更新字段,当field为null时,更新in的全部字段
  67. // @return error
  68. func (l *LinkEventLogic) ModifyLinkEvent(in *model.LinkEvent, fields ...string) error {
  69. do := l.SvcCtx.LinkEvent
  70. var err error
  71. _do := do.Omit(do.ID).Where(do.Eventid.Eq(in.Eventid))
  72. if fields == nil {
  73. m := gconv.Map(in)
  74. _, err = _do.Updates(m)
  75. return err
  76. }
  77. var fieldList []field.Expr
  78. for _, v := range fields {
  79. _f, ok := do.GetFieldByName(v)
  80. if ok {
  81. fieldList = append(fieldList, _f)
  82. }
  83. }
  84. _, err = _do.Select(fieldList...).Updates(in)
  85. return err
  86. }
  87. func (l *LinkEventLogic) getLinkEventCond(req *dto.GetLinkEventReq) (*dao.Condition, error) {
  88. _dao := l.SvcCtx.LinkEvent
  89. //_strategy := l.SvcCtx.LinkStrategy
  90. //_app := l.SvcCtx.SysApp
  91. _relationObj := l.SvcCtx.LinkRelationObj
  92. _relationAction := l.SvcCtx.LinkRelationAction
  93. _mpInfo := l.SvcCtx.DevMpinfo
  94. _cpInfo := l.SvcCtx.DevCpinfo
  95. _devInfo := l.SvcCtx.DevDevinfo
  96. cond := NewCondition(&req.LimitPage)
  97. if req.StartTime != "" {
  98. cond.Where[_dao.CreateAt.String()+" >= ?"] = req.StartTime
  99. }
  100. if req.EndTime != "" {
  101. cond.Where[_dao.CreateAt.String()+" <= ?"] = req.EndTime
  102. }
  103. var strategyId []int64
  104. isStrategyIdFirstQ := true //是否是第一次查询StrategyId标示
  105. if len(req.SrcAppId) > 0 {
  106. cond.Where[_dao.Srcappid.String()+" in ?"] = req.SrcAppId
  107. //var id []int64
  108. //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.In(req.SrcAppId...)).Scan(&id)
  109. //if err != nil {
  110. // return nil, err
  111. //}
  112. //
  113. //if req.RelationSrcDstAppId == 1 {
  114. // isStrategyIdFirstQ = false
  115. // strategyId = utils.RemoveRep(append(strategyId, id...))
  116. //} else {
  117. // strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  118. // if len(strategyId) == 0 {
  119. // return nil, nil
  120. // }
  121. //}
  122. }
  123. if len(req.DstAppId) > 0 {
  124. cond.Where[_dao.Dstappid.String()+" in ?"] = req.DstAppId
  125. //var id []int64
  126. //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.In(req.DstAppId...)).Scan(&id)
  127. //if err != nil {
  128. // return nil, err
  129. //}
  130. //
  131. //if req.RelationSrcDstAppId == 1 {
  132. // isStrategyIdFirstQ = false
  133. // strategyId = utils.RemoveRep(append(strategyId, id...))
  134. //} else {
  135. // strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  136. //}
  137. //if len(strategyId) == 0 {
  138. // return nil, nil
  139. //}
  140. }
  141. //if req.SrcAppName != "" {
  142. // var id []int64
  143. // var appId int32
  144. // err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.SrcAppName)).Scan(&appId)
  145. // if err != nil {
  146. // return nil, err
  147. // }
  148. // err = _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(appId)).Scan(&id)
  149. // if err != nil {
  150. // return nil, err
  151. // }
  152. // strategyId, err = l.removeSingleStrategyId(strategyId, id)
  153. // if err != nil {
  154. // return nil, err
  155. // }
  156. //}
  157. if req.SrcPositionName != "" {
  158. var id []int64
  159. err := _relationObj.Select(_relationObj.Strategyid).Where(_relationObj.Columns(_relationObj.Linkfromid).Eq(
  160. _mpInfo.Select(_mpInfo.Mpid).Where(_mpInfo.Positionname.Eq(req.SrcPositionName)),
  161. )).Scan(&id)
  162. if err != nil {
  163. return nil, err
  164. }
  165. strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  166. if len(strategyId) == 0 {
  167. return nil, nil
  168. }
  169. }
  170. //if req.DstAppName != "" {
  171. // var id []int64
  172. // var appId int32
  173. // err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.DstAppName)).Scan(&appId)
  174. // if err != nil {
  175. // return nil, err
  176. // }
  177. // err = _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(appId)).Scan(&id)
  178. // if err != nil {
  179. // return nil, err
  180. // }
  181. // strategyId, err = l.removeSingleStrategyId(strategyId, id)
  182. // if err != nil {
  183. // return nil, err
  184. // }
  185. //}
  186. if req.DstPositionName != "" {
  187. var id []int64
  188. err := _relationAction.Select(_relationAction.Strategyid).Where(_relationAction.Columns(_relationAction.Linktoid).Eq(
  189. _cpInfo.Select(_cpInfo.Cpid).Where(_cpInfo.Positionname.Eq(req.DstPositionName)),
  190. )).Scan(&id)
  191. if err != nil {
  192. return nil, err
  193. }
  194. strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  195. if len(strategyId) == 0 {
  196. return nil, nil
  197. }
  198. }
  199. if req.DstDeviceName != "" {
  200. var id []int64
  201. var deviceId []int32
  202. err := _devInfo.Select(_devInfo.Deviceid).Where(_devInfo.Devicename.Eq(req.DstDeviceName)).Scan(&deviceId)
  203. if err != nil {
  204. return nil, err
  205. }
  206. var cpId []int64
  207. err = _cpInfo.Select(_cpInfo.Cpid).Where(_cpInfo.Deviceid.In(deviceId...)).Scan(&cpId)
  208. if err != nil {
  209. return nil, err
  210. }
  211. err = _relationAction.Select(_relationAction.Strategyid).Where(_relationAction.Linktoid.In(cpId...)).Scan(&id)
  212. if err != nil {
  213. return nil, err
  214. }
  215. strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
  216. if len(strategyId) == 0 {
  217. return nil, nil
  218. }
  219. }
  220. if len(strategyId) > 0 {
  221. cond.Where[_dao.Strategyid.String()] = utils.RemoveRep(strategyId)
  222. }
  223. return cond, nil
  224. }
  225. // GetLinkEvent
  226. // @Description: 分页查询
  227. // @receiver l
  228. // @param where 查询条件
  229. // @param page
  230. // @param limit
  231. // @return []*model.LinkEvent
  232. // @return error
  233. func (l *LinkEventLogic) GetLinkEvent(req *dto.GetLinkEventReq) (*dto.GetLinkEventResp, error) {
  234. _dao := l.SvcCtx.LinkEvent
  235. _app := l.SvcCtx.SysApp
  236. _relationObj := l.SvcCtx.LinkRelationObj
  237. _mpInfo := l.SvcCtx.DevMpinfo
  238. _cpInfo := l.SvcCtx.DevCpinfo
  239. _devInfo := l.SvcCtx.DevDevinfo
  240. _linkStrategy := l.SvcCtx.LinkStrategy
  241. var linkEventList []*model.LinkEvent
  242. cond, err := l.getLinkEventCond(req)
  243. if cond == nil && err == nil {
  244. return &dto.GetLinkEventResp{}, err
  245. }
  246. if err != nil {
  247. return nil, err
  248. }
  249. err = _dao.Base.Find(cond, &linkEventList)
  250. if err != nil {
  251. return nil, err
  252. }
  253. resp := &dto.GetLinkEventResp{}
  254. err = _dao.Base.Count(cond, &resp.Total)
  255. if err != nil {
  256. return nil, err
  257. }
  258. for _, v := range linkEventList {
  259. tmp := &dto.LinkEventDto{}
  260. tmp.LinkEvent = *v
  261. tmpDo := _mpInfo.Where(_mpInfo.Columns(_mpInfo.Mpid).In(
  262. _relationObj.Select(_relationObj.Linkfromid).Where(_relationObj.Strategyid.Eq(v.Strategyid)),
  263. ))
  264. tmpDo.Select(_mpInfo.Positionname).Scan(&tmp.SrcPositionName)
  265. //_app.Select(_app.Nickname).Where(_app.Columns(_app.Appid).In(tmpDo.Select(_mpInfo.Appid))).Scan(&tmp.SrcAppName)
  266. //if tmp.SrcAppName == nil {
  267. // tmp.SrcAppName = []string{"策略已删除"}
  268. //}
  269. _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Srcappid)).Scan(&tmp.SrcAppName)
  270. _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Dstappid)).Scan(&tmp.DstAppName)
  271. devCpinfo, err := _cpInfo.Where(_cpInfo.Cpid.Eq(v.Linktoid)).Last()
  272. if err == nil {
  273. tmp.Dst = *devCpinfo
  274. tmp.DstPositionName = devCpinfo.Positionname
  275. //_app.Select(_app.Nickname).Where(_app.Appid.Eq(devCpinfo.Appid)).Scan(&tmp.DstAppName)
  276. _devInfo.Select(_devInfo.Devicename).Where(_devInfo.Deviceid.Eq(devCpinfo.Deviceid)).Scan(&tmp.DstDeviceName)
  277. } else if err.Error() == "record not found" {
  278. tmp.DstAppName = "测点已删除"
  279. }
  280. linkStrategy, err := _linkStrategy.Where(_linkStrategy.Strategyid.Eq(v.Strategyid)).Last()
  281. if err == nil {
  282. tmp.LinkStrategy = *linkStrategy
  283. }
  284. resp.List = append(resp.List, tmp)
  285. }
  286. return resp, err
  287. }
  288. // GetLinkEventCount
  289. // @Description:查询满足条件的总数
  290. // @receiver l
  291. // @param where 查询条件
  292. // @return int64
  293. // @return error
  294. func (l *LinkEventLogic) GetLinkEventCount(req *dto.GetLinkEventReq) (int64, error) {
  295. cond, err := l.getLinkEventCond(req)
  296. if err != nil {
  297. return 0, err
  298. }
  299. var count int64
  300. err = l.SvcCtx.LinkEvent.Base.Count(cond, &count)
  301. if err != nil {
  302. return 0, err
  303. }
  304. return count, err
  305. }