vedio.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "time"
  8. "smart-enterprise-management/consts"
  9. )
  10. type Vssdevtbl struct {
  11. Id int64 `gorm:"column:ID;PRIMARY_KEY" json:"id" form:"id"`
  12. Devpubid string `gorm:"column:DevPubID" json:"devpubid" form:"devpubid"`
  13. Devnickname string `gorm:"column:DevNickname" json:"devnickname" form:"devnickname"`
  14. Username string `gorm:"column:UserName" json:"username" form:"username"`
  15. Passwd string `gorm:"column:Passwd" json:"passwd" form:"passwd"`
  16. Corpid string `gorm:"column:CorpID" json:"corpid" form:"corpid"`
  17. Ip string `gorm:"column:IP" json:"ip" form:"ip"`
  18. Port int64 `gorm:"column:Port" json:"port" form:"port"`
  19. Alive int64 `gorm:"column:Alive" json:"alive" form:"alive"`
  20. Subcatalogtime int64 `gorm:"column:SubCatalogTime" json:"subcatalogtime" form:"subcatalogtime"`
  21. Devmode string `gorm:"column:DevMode" json:"devmode" form:"devmode"`
  22. Mediatransport string `gorm:"column:MediaTransport" json:"mediatransport" form:"mediatransport"`
  23. Dmarker int64 `gorm:"column:DMarker" json:"dmarker" form:"dmarker"`
  24. UpdateTime time.Time `gorm:"column:UpdateTime" json:"updatetime" form:"updatetime"`
  25. ChannelCount int `gorm:"column:ChannelCount"`
  26. VerifyTime time.Time `gorm:"column:VerifyTime"`
  27. VerifyStatus uint32 `gorm:"column:VerifyStatus"`
  28. }
  29. func (Vssdevtbl) TableName() string {
  30. return "VSSDevTbl"
  31. }
  32. func (p *Vssdevtbl) Insert(db *gorm.DB) error {
  33. return db.Create(p).Error
  34. }
  35. func (p *Vssdevtbl) Del(db *gorm.DB, where map[string]interface{}) error {
  36. cond, val, err := whereBuild(where)
  37. if err != nil {
  38. return err
  39. }
  40. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  41. }
  42. func (p *Vssdevtbl) Find(db *gorm.DB, where map[string]interface{}) error {
  43. cond, val, err := whereBuild(where)
  44. if err != nil {
  45. return err
  46. }
  47. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  48. }
  49. func (p *Vssdevtbl) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  50. cond, val, err := whereBuild(where)
  51. if err != nil {
  52. return err
  53. }
  54. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  55. }
  56. func (p *Vssdevtbl) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  57. cond, val, err := whereBuild(where)
  58. if err != nil {
  59. return err
  60. }
  61. ps := []Vssdevtbl{}
  62. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  63. if err != nil {
  64. return err
  65. }
  66. if len(ps) > 0 {
  67. *p = ps[0]
  68. }
  69. return nil
  70. }
  71. func (p *Vssdevtbl) Save(db *gorm.DB) error {
  72. return db.Save(p).Error
  73. }
  74. func (p *Vssdevtbl) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  75. if len(where) > 0 {
  76. cond, val, err := whereBuild(where)
  77. if err != nil {
  78. return 0, err
  79. }
  80. ret := int64(0)
  81. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  82. return ret, err
  83. }
  84. ret := int64(0)
  85. err := db.Table(p.TableName()).Count(&ret).Error
  86. return ret, err
  87. }
  88. func (p *Vssdevtbl) List(db *gorm.DB, where map[string]interface{}, page int) (list []Vssdevtbl, err error) {
  89. offset := (int(page) - 1) *PageSize
  90. if len(where) > 0 {
  91. cond, val, err := whereBuild(where)
  92. if err != nil {
  93. return list, err
  94. }
  95. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(offset).Find(&list)
  96. return list, result.Error
  97. }
  98. result := db.Table(p.TableName()).Limit(PageSize).Offset(offset).Find(&list)
  99. return list, result.Error
  100. }
  101. type Vsschanneltbl struct {
  102. ID int64 `gorm:"column:ID"`
  103. Devpubid string `gorm:"column:DevPubID"`
  104. Restype int64 `gorm:"column:ResType"`
  105. Nickname string `gorm:"column:Nickname"`
  106. Chanpubid string `gorm:"column:ChanPubID"`
  107. Alive int64 `gorm:"column:Alive"`
  108. Corpid string `gorm:"column:CorpID"`
  109. Model string `gorm:"column:Model"`
  110. Owner string `gorm:"column:Owner"`
  111. Civilcode string `gorm:"column:CivilCode"`
  112. Address string `gorm:"column:Address"`
  113. Parental int64 `gorm:"column:Parental"`
  114. Parentid string `gorm:"column:ParentId"`
  115. Ip string `gorm:"column:IP"`
  116. Port int64 `gorm:"column:Port"`
  117. Longitude float64 `gorm:"column:Longitude"`
  118. Latitude float64 `gorm:"column:Latitude"`
  119. Altitude float64 `gorm:"column:Altitude"`
  120. Ptztype int64 `gorm:"column:PTZType"`
  121. Roomtype int64 `gorm:"column:RoomType"`
  122. Directiontype int64 `gorm:"column:DirectionType"`
  123. Streamtype int64 `gorm:"column:StreamType"`
  124. Chanrtspurl string `gorm:"column:ChanRtspUrl"`
  125. Realrtspurl string `gorm:"column:RealRtspUrl"`
  126. Dmarker int64 `gorm:"column:DMarker"`
  127. Updatetime time.Time `gorm:"column:UpdateTime"`
  128. }
  129. func (Vsschanneltbl) TableName() string {
  130. return "VSSChannelTbl"
  131. }
  132. func (p *Vsschanneltbl) Insert(db *gorm.DB) error {
  133. return db.Create(p).Error
  134. }
  135. func (p *Vsschanneltbl) Del(db *gorm.DB, where map[string]interface{}) error {
  136. cond, val, err := whereBuild(where)
  137. if err != nil {
  138. return err
  139. }
  140. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  141. }
  142. func (p *Vsschanneltbl) Find(db *gorm.DB, where map[string]interface{}) error {
  143. cond, val, err := whereBuild(where)
  144. if err != nil {
  145. return err
  146. }
  147. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  148. }
  149. func (p *Vsschanneltbl) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  150. cond, val, err := whereBuild(where)
  151. if err != nil {
  152. return err
  153. }
  154. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  155. }
  156. func (p *Vsschanneltbl) Save(db *gorm.DB) error {
  157. return db.Save(p).Error
  158. }
  159. func (p *Vsschanneltbl) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  160. if len(where) > 0 {
  161. cond, val, err := whereBuild(where)
  162. if err != nil {
  163. return 0, err
  164. }
  165. ret := int64(0)
  166. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  167. return ret, err
  168. }
  169. ret := int64(0)
  170. err := db.Table(p.TableName()).Count(&ret).Error
  171. return ret, err
  172. }
  173. func (p *Vsschanneltbl) List(db *gorm.DB, where map[string]interface{}, page int32) (list []Vsschanneltbl, err error) {
  174. offset := int(page - 1) *PageSize
  175. if len(where) > 0 {
  176. cond, val, err := whereBuild(where)
  177. if err != nil {
  178. return list, err
  179. }
  180. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(offset).Find(&list)
  181. return list, result.Error
  182. }
  183. result := db.Table(p.TableName()).Limit(PageSize).Offset(offset).Find(&list)
  184. return list, result.Error
  185. }
  186. func DelVedio(db *gorm.DB, projectId int64) error {
  187. sql := fmt.Sprintf("delete from VSSChannelTbl where DevPubID in(select DevPubID from VSSDevTbl where ProjectId=%d)", projectId)
  188. err := db.Exec(sql).Error
  189. if err != nil {
  190. return err
  191. }
  192. sql = fmt.Sprintf("delete from VSSDevTbl where ProjectId=%d", projectId)
  193. err = db.Exec(sql).Error
  194. if err != nil {
  195. return err
  196. }
  197. return nil
  198. }
  199. type VedioDeviceItem struct {
  200. Id int64
  201. Sn string
  202. TypeCode int32
  203. TypeName string
  204. ProjectName string
  205. ProjectId int64
  206. SafetyRecordNo string
  207. CreatedTime time.Time
  208. Status int32
  209. State int32
  210. ProjectApproveTime time.Time
  211. Key string
  212. ProviderName string
  213. SocialCode string
  214. Name string
  215. Ip string
  216. Port int
  217. MediaTransport string
  218. ChannelCount int
  219. }
  220. func vedioDeviceListSql(req VedioDeviceListRequest)(string, string, []interface{}) {
  221. sql := fmt.Sprintf("select t1.ID as id, t1.DevNickName as name, t1.DevPubID as sn, t1.ProjectId as project_id, t1.VerifyTime as project_approve_time, t1.MediaTransport as media_transport, t1.IP as ip, t1.Port as port, t1.ChannelCount as channel_count, "+
  222. "t1.CreatedAt as created_time, t1.Alive as state, t1.VerifyStatus as status, t2.SafetyNo as safety_record_no, t2.Name as project_name, t3.Name as provider_name, t3.SocialCode as social_code from VSSDevTbl as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  223. countSql := fmt.Sprintf("select count(1) as count from VSSDevTbl as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID")
  224. args := []interface{}{}
  225. whereArray := []string{}
  226. if req.ProviderId > 0 {
  227. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  228. }
  229. if req.Filter != "" {
  230. whereArray = append(whereArray, fmt.Sprintf("(t1.DevPubID like '%%%s%%' or t1.NickName like '%%%s%%')", req.Filter, req.Filter))
  231. }
  232. if req.Cid > 0 {
  233. whereArray = append(whereArray, fmt.Sprintf("t2.Cid=%d", req.Cid))
  234. }
  235. if req.State >= 0 {
  236. whereArray = append(whereArray, fmt.Sprintf("t1.Alive=%d", req.State))
  237. }
  238. if req.ProjectId > 0 {
  239. whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId = %d", req.ProjectId))
  240. }
  241. if req.CanDel {
  242. req.IsAll = true
  243. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  244. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  245. }
  246. if len(req.StatusFilter) > 0 {
  247. args = append(args, req.StatusFilter)
  248. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  249. }
  250. where := ""
  251. for _, v := range whereArray {
  252. if where == "" {
  253. where = fmt.Sprintf(" where %s", v)
  254. continue
  255. }
  256. where = fmt.Sprintf("%s and %s", where, v)
  257. }
  258. offset := (req.Page - 1) *int32(PageSize)
  259. if req.IsAll {
  260. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  261. countSql = fmt.Sprintf("%s %s", countSql, where)
  262. } else {
  263. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  264. countSql = fmt.Sprintf("%s %s", countSql, where)
  265. }
  266. return sql, countSql, args
  267. }
  268. type VedioDeviceListRequest struct {
  269. ProviderId int64
  270. Filter string
  271. StatusFilter []int32
  272. IsAll bool
  273. CanDel bool
  274. Page int32
  275. ProjectId int64
  276. Cid int64
  277. State int32
  278. }
  279. func VedioDeviceList(db *gorm.DB, req VedioDeviceListRequest) ([]VedioDeviceItem, int64, error) {
  280. type ResultCount struct {
  281. Count int64
  282. }
  283. array := []ResultCount{}
  284. ret := []VedioDeviceItem{}
  285. var err error
  286. sql, countSql, args := vedioDeviceListSql(req)
  287. err = db.Raw(countSql, args...).Scan(&array).Error
  288. if err != nil {
  289. return nil, 0, err
  290. }
  291. if len(array) == 0 {
  292. return nil, 0, nil
  293. }
  294. if array[0].Count == 0 {
  295. return nil, 0, nil
  296. }
  297. err = db.Raw(sql, args...).Scan(&ret).Error
  298. if err != nil {
  299. return nil, array[0].Count, err
  300. }
  301. return ret, array[0].Count, nil
  302. }