mpInfoLogic.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. "github.com/gogf/gf/v2/util/gconv"
  8. "gorm.io/gen/field"
  9. )
  10. type MpInfoLogic struct {
  11. SvcCtx *svc.ServiceContext
  12. }
  13. // NewMpInfoLogic
  14. // @Description: 测点
  15. // @return *MpInfoLogic
  16. func NewMpInfoLogic() *MpInfoLogic {
  17. return &MpInfoLogic{
  18. SvcCtx: svc.SvcCtx,
  19. }
  20. }
  21. // AddMpInfo
  22. // @Description: 添加
  23. // @receiver l
  24. // @param in
  25. // @return error
  26. func (l *MpInfoLogic) AddMpInfo(in *model.DevMpinfo) error {
  27. in.ID = 0
  28. err := l.SvcCtx.DevMpinfo.Create(in)
  29. return err
  30. }
  31. // DelMpInfo
  32. // @Description: 删除
  33. // @receiver l
  34. // @param mpId 测点id
  35. // @return error
  36. func (l *MpInfoLogic) DelMpInfo(mpId int64) error {
  37. do := l.SvcCtx.DevMpinfo
  38. _linkRelationObj := l.SvcCtx.LinkRelationObj
  39. _alarmRelation := l.SvcCtx.AlarmRelation
  40. //删除关联的 联动、告警
  41. _linkRelationObj.Where(_linkRelationObj.Linkfromid.Eq(mpId)).Delete()
  42. _alarmRelation.Where(_alarmRelation.Mpid.Eq(mpId)).Delete()
  43. _, err := do.Where(do.Mpid.Eq(mpId)).Delete()
  44. return err
  45. }
  46. // ModifyMpInfo
  47. // @Description: 修改
  48. // @receiver l
  49. // @param in 可修改除测点ID,App id外的信息
  50. // @param fields 更新字段,当field为null时,更新in的全部字段
  51. // @return error
  52. func (l *MpInfoLogic) ModifyMpInfo(in *model.DevMpinfo, fields ...string) error {
  53. do := l.SvcCtx.DevMpinfo
  54. var err error
  55. _do := do.Omit(do.ID, do.Appid).Where(do.Mpid.Eq(in.Mpid))
  56. if fields == nil {
  57. m := gconv.Map(in)
  58. _, err = _do.Updates(m)
  59. return err
  60. }
  61. var fieldList []field.Expr
  62. for _, v := range fields {
  63. _f, ok := do.GetFieldByName(v)
  64. if ok {
  65. fieldList = append(fieldList, _f)
  66. }
  67. }
  68. _, err = _do.Select(fieldList...).Updates(in)
  69. return err
  70. }
  71. func (l *MpInfoLogic) getMpInfoCond(req *dto.GetMpInfoReq) *dao.Condition {
  72. _dao := l.SvcCtx.DevMpinfo
  73. cond := NewCondition(&req.LimitPage)
  74. if req.MpName != "" {
  75. cond.Where[_dao.Mpname.String()] = req.MpName
  76. }
  77. if req.ZoneName != "" {
  78. cond.Where[_dao.Zonename.String()] = req.ZoneName
  79. }
  80. if req.PositionName != "" {
  81. cond.Where[_dao.Positionname.String()] = req.PositionName
  82. }
  83. if req.AppId > 0 {
  84. cond.Where[_dao.Appid.String()] = req.AppId
  85. }
  86. if req.ModelId > 0 {
  87. cond.Where[_dao.Modelid.String()] = req.ModelId
  88. }
  89. if req.DeviceId > 0 {
  90. cond.Where[_dao.Deviceid.String()] = req.DeviceId
  91. }
  92. return cond
  93. }
  94. // GetMpInfoInfo
  95. // @Description: 分页查询
  96. // @receiver l
  97. // @param where 查询条件
  98. // @param page
  99. // @param limit
  100. // @return []*model.DevDevinfo
  101. // @return error
  102. func (l *MpInfoLogic) GetMpInfo(req *dto.GetMpInfoReq) (*dto.GetMpInfoResp, error) {
  103. _dao := l.SvcCtx.DevMpinfo
  104. cond := l.getMpInfoCond(req)
  105. resp := &dto.GetMpInfoResp{}
  106. err := _dao.Base.Find(cond, &resp.List)
  107. if err != nil {
  108. return nil, err
  109. }
  110. err = _dao.Base.Count(cond, &resp.Total)
  111. return resp, err
  112. }
  113. // GetMpInfoCount
  114. // @Description:查询满足条件的总数
  115. // @receiver l
  116. // @param where 查询条件
  117. // @return int64
  118. // @return error
  119. func (l *MpInfoLogic) GetMpInfoCount(req *dto.GetMpInfoReq) (int64, error) {
  120. cond := l.getMpInfoCond(req)
  121. var count int64
  122. err := l.SvcCtx.DevMpinfo.Base.Count(cond, &count)
  123. if err != nil {
  124. return 0, err
  125. }
  126. return count, err
  127. }