vedio.go 10 KB

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