device.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. "github.com/tidwall/gjson"
  6. "smart-supplier-management/consts"
  7. "time"
  8. "fmt"
  9. "github.com/jinzhu/gorm"
  10. )
  11. type TDevice struct {
  12. Id int64 `gorm:"column:id" json:"id" form:"id"`
  13. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  14. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  15. Name string `gorm:"column:name" json:"name" form:"name"`
  16. Sn string `gorm:"column:sn" json:"sn" form:"sn"`
  17. Addr string `gorm:"column:addr" json:"addr" form:"addr"`
  18. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  19. Lon float64 `gorm:"column:lon" json:"lon" form:"lon"`
  20. Lat float64 `gorm:"column:lat" json:"lat" form:"lat"`
  21. XCoord float64 `gorm:"column:x_coord" json:"x_coord" form:"x_coord"`
  22. YCoord float64 `gorm:"column:y_coord" json:"y_coord" form:"y_coord"`
  23. Status int64 `gorm:"column:status" json:"status" form:"status"`
  24. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  25. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  26. VerifyStatus int64 `gorm:"column:verify_status" json:"verify_status" form:"verify_status"`
  27. Key string `gorm:"column:key" json:"key" form:"key"`
  28. ProjectApproveTime time.Time `json:"project_approve_time"`
  29. }
  30. const (
  31. DeviceTypeVedio = 9
  32. )
  33. func (TDevice) TableName() string {
  34. return "t_device"
  35. }
  36. func (p *TDevice) Insert(db *gorm.DB) error {
  37. return db.Create(p).Error
  38. }
  39. func (p *TDevice) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error {
  40. sql := fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d group by type", projectId)
  41. if deviceType > 0 {
  42. sql = fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d and type = %d group by type", projectId, deviceType)
  43. }
  44. err := db.Raw(sql).Scan(result).Error
  45. return err
  46. }
  47. func (p *TDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  48. cond, val, err := whereBuild(where)
  49. if err != nil {
  50. return err
  51. }
  52. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  53. }
  54. func (p *TDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  55. cond, val, err := whereBuild(where)
  56. if err != nil {
  57. return err
  58. }
  59. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  60. }
  61. func (p *TDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  62. cond, val, err := whereBuild(where)
  63. if err != nil {
  64. return err
  65. }
  66. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  67. }
  68. func (p *TDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  69. cond, val, err := whereBuild(where)
  70. if err != nil {
  71. return err
  72. }
  73. ps := []TDevice{}
  74. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  75. if err != nil {
  76. return err
  77. }
  78. if len(ps) > 0 {
  79. *p = ps[0]
  80. }
  81. return nil
  82. }
  83. func (p *TDevice) Save(db *gorm.DB) error {
  84. return db.Save(p).Error
  85. }
  86. func (p *TDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  87. if len(where) > 0 {
  88. cond, val, err := whereBuild(where)
  89. if err != nil {
  90. return 0, err
  91. }
  92. ret := int64(0)
  93. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  94. return ret, err
  95. }
  96. ret := int64(0)
  97. err := db.Table(p.TableName()).Count(&ret).Error
  98. return ret, err
  99. }
  100. func (p *TDevice) List(db *gorm.DB, where map[string]interface{}, page int) (list []TDevice, err error) {
  101. if len(where) > 0 {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. return list, err
  105. }
  106. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  107. return list, result.Error
  108. }
  109. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  110. return list, result.Error
  111. }
  112. func (p *TDevice) All(db *gorm.DB) (list []TDevice, err error) {
  113. result := db.Table(p.TableName()).Find(&list)
  114. return list, result.Error
  115. }
  116. type DeviceItem struct {
  117. Id int64
  118. Sn string
  119. TypeCode int32
  120. TypeName string
  121. ProjectName string
  122. ProjectId int64
  123. SafetyRecordNo string
  124. CreatedTime time.Time
  125. Status int32
  126. State int32
  127. ProjectApproveTime time.Time
  128. Key string
  129. ProviderName string
  130. SocialCode string
  131. Name string
  132. Ip string
  133. Port int
  134. MediaTransport string
  135. ChannelCount int
  136. }
  137. func listRawSqlForVedio(req ListRawRequest)(string, string, []interface{}) {
  138. sql := fmt.Sprintf("select t1.id as id, t1.key, t1.name as name, t1.sn as sn, t1.device_code as type_code, t1.project_id as project_id, t1.project_approve_time, t4.MediaTransport as media_transport, t4.IP as ip, t4.Port as port, t4.channel_count, "+
  139. "t1.created_at as created_time, t4.Alive as state, t1.verify_status as status, t2.safety_record_no, t2.name as project_name, t3.name as provider_name, t3.social_code from t_device as t1 left join t_project as t2 on t1.project_id=t2.id left join t_provider as t3 on t1.provider_id=t3.id left join VSSDevTbl as t4 on t4.DevPubID=t1.sn")
  140. countSql := fmt.Sprintf("select count(1) as count from t_device as t1 left join t_project as t2 on t1.project_id=t2.id ")
  141. args := []interface{}{}
  142. whereArray := []string{}
  143. if req.ProviderId > 0 {
  144. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  145. }
  146. whereArray = append(whereArray, fmt.Sprintf("t1.device_code = %d", DeviceTypeVedio))
  147. if req.Filter != "" {
  148. whereArray = append(whereArray, fmt.Sprintf("(t1.sn like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  149. }
  150. if req.ProjectId > 0 {
  151. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  152. }
  153. if req.CanDel {
  154. req.IsAll = true
  155. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  156. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from t_project_job where device_id=t1.id and type = 1 and (status = 0 or status = 1)) = 0"))
  157. }
  158. if len(req.StatusFilter) > 0 {
  159. args = append(args, req.StatusFilter)
  160. whereArray = append(whereArray, fmt.Sprintf("t1.verify_status in(?)"))
  161. }
  162. where := ""
  163. for _, v := range whereArray {
  164. if where == "" {
  165. where = fmt.Sprintf(" where %s", v)
  166. continue
  167. }
  168. where = fmt.Sprintf("%s and %s", where, v)
  169. }
  170. offset := (req.Page - 1) *int32(PageSize)
  171. if req.IsAll {
  172. sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where)
  173. countSql = fmt.Sprintf("%s %s", countSql, where)
  174. } else {
  175. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  176. countSql = fmt.Sprintf("%s %s", countSql, where)
  177. }
  178. return sql, countSql, args
  179. }
  180. func listRawSql(req ListRawRequest)(string, string, []interface{}) {
  181. if req.IsVedio {
  182. return listRawSqlForVedio(req)
  183. }
  184. sql := fmt.Sprintf("select t1.id as id, t1.key, t1.name as name, t1.sn as sn, t1.device_code as type_code, t1.project_id as project_id, t1.project_approve_time, "+
  185. "t1.created_at as created_time, t1.status as state, t1.verify_status as status, t2.safety_record_no, t2.name as project_name, t3.name as provider_name, t3.social_code from t_device as t1 left join t_project as t2 on t1.project_id=t2.id left join t_provider as t3 on t1.provider_id=t3.id")
  186. countSql := fmt.Sprintf("select count(1) as count from t_device as t1 left join t_project as t2 on t1.project_id=t2.id ")
  187. args := []interface{}{}
  188. whereArray := []string{}
  189. if req.ProviderId > 0 {
  190. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  191. }
  192. if !req.CanDel {
  193. whereArray = append(whereArray, fmt.Sprintf("t1.device_code <> %d", DeviceTypeVedio))
  194. }
  195. if req.TypeCode > 0 {
  196. whereArray = append(whereArray, fmt.Sprintf("t1.device_code = %d", req.TypeCode))
  197. }
  198. if req.Filter != "" {
  199. whereArray = append(whereArray, fmt.Sprintf("(t1.sn like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  200. }
  201. if req.ProjectId > 0 {
  202. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  203. }
  204. if req.CanDel {
  205. req.IsAll = true
  206. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  207. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from t_project_job where device_id=t1.id and type = 1 and (status = 0 or status = 1)) = 0"))
  208. }
  209. if len(req.StatusFilter) > 0 {
  210. args = append(args, req.StatusFilter)
  211. whereArray = append(whereArray, fmt.Sprintf("t1.verify_status in(?)"))
  212. }
  213. where := ""
  214. for _, v := range whereArray {
  215. if where == "" {
  216. where = fmt.Sprintf(" where %s", v)
  217. continue
  218. }
  219. where = fmt.Sprintf("%s and %s", where, v)
  220. }
  221. offset := (req.Page - 1) *int32(PageSize)
  222. if req.IsAll {
  223. sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where)
  224. countSql = fmt.Sprintf("%s %s", countSql, where)
  225. } else {
  226. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  227. countSql = fmt.Sprintf("%s %s", countSql, where)
  228. }
  229. return sql, countSql, args
  230. }
  231. type ListRawRequest struct {
  232. ProviderId int64
  233. Filter string
  234. StatusFilter []int32
  235. IsAll bool
  236. CanDel bool
  237. Page int32
  238. TypeCode int32
  239. ProjectId int64
  240. IsVedio bool
  241. }
  242. func (p *TDevice) ListRaw(db *gorm.DB, req ListRawRequest) ([]DeviceItem, int64, error) {
  243. type ResultCount struct {
  244. Count int64
  245. }
  246. array := []ResultCount{}
  247. ret := []DeviceItem{}
  248. var err error
  249. sql, countSql, args := listRawSql(req)
  250. err = db.Raw(countSql, args...).Scan(&array).Error
  251. if err != nil {
  252. return nil, 0, err
  253. }
  254. if len(array) == 0 {
  255. return nil, 0, nil
  256. }
  257. if array[0].Count == 0 {
  258. return nil, 0, nil
  259. }
  260. err = db.Raw(sql, args...).Scan(&ret).Error
  261. if err != nil {
  262. return nil, array[0].Count, err
  263. }
  264. return ret, array[0].Count, nil
  265. }
  266. type DeviceDelJobItem struct {
  267. Id int64
  268. Sn string
  269. TypeCode int32
  270. TypeName string
  271. ProjectName string
  272. ProviderName string
  273. SocialCode string
  274. SafetyRecordNo string
  275. CreatedTime time.Time
  276. Status int32
  277. ApproveTime time.Time
  278. Origin string
  279. Reason string
  280. Feedback string
  281. DeviceName string
  282. }
  283. func listDelJobSql(req ListRawRequest)(string, string, []interface{}) {
  284. sql := "select t1.origin, t1.id, t1.status, t1.reason, t1.feedback, t1.updated_at as approve_time, t1.created_at as created_time, t2.name as provider_name, t2.social_code, t3.safety_record_no, t3.name as project_name " +
  285. "from t_project_job as t1 left join t_provider as t2 on t1.provider_id=t2.id left join t_project as t3 on t3.id=t1.project_id"
  286. countSql := "select count(1) as count " +
  287. "from t_project_job as t1 left join t_provider as t2 on t1.provider_id=t2.id left join t_project as t3 on t3.id=t1.project_id "
  288. args := []interface{}{}
  289. whereArray := []string{"t1.type = 1"}
  290. if req.ProviderId > 0 {
  291. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  292. }
  293. if req.Filter != "" {
  294. whereArray = append(whereArray, fmt.Sprintf("(t1.content like '%%%s%%' or t2.social_code like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  295. }
  296. if len(req.StatusFilter) > 0 {
  297. args = append(args, req.StatusFilter)
  298. whereArray = append(whereArray, fmt.Sprintf("t1.status in(?)"))
  299. }
  300. if req.ProjectId > 0 {
  301. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  302. }
  303. where := ""
  304. for _, v := range whereArray {
  305. if where == "" {
  306. where = fmt.Sprintf(" where %s", v)
  307. continue
  308. }
  309. where = fmt.Sprintf("%s and %s", where, v)
  310. }
  311. offset := (req.Page - 1) *int32(PageSize)
  312. if req.IsAll {
  313. sql = fmt.Sprintf("%s %s", sql, where)
  314. countSql = fmt.Sprintf("%s %s", countSql, where)
  315. } else {
  316. sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset)
  317. countSql = fmt.Sprintf("%s %s", countSql, where)
  318. }
  319. return sql, countSql, args
  320. }
  321. func (p *TDevice) ListDelJob(db *gorm.DB, req ListRawRequest) ([]DeviceDelJobItem, int64, error) {
  322. type ResultCount struct {
  323. Count int64
  324. }
  325. array := []ResultCount{}
  326. ret := []DeviceDelJobItem{}
  327. var err error
  328. sql, countSql, args := listDelJobSql(req)
  329. err = db.Raw(countSql, args...).Scan(&array).Error
  330. if err != nil {
  331. return nil, 0, err
  332. }
  333. if len(array) == 0 {
  334. return nil, 0, nil
  335. }
  336. if array[0].Count == 0 {
  337. return nil, 0, nil
  338. }
  339. err = db.Raw(sql, args...).Scan(&ret).Error
  340. if err != nil {
  341. return nil, array[0].Count, err
  342. }
  343. for i, v := range ret {
  344. ret[i].Sn = gjson.GetBytes([]byte(v.Origin), "sn").String()
  345. ret[i].TypeCode = int32(gjson.GetBytes([]byte(v.Origin), "device_code").Int())
  346. ret[i].DeviceName = gjson.GetBytes([]byte(v.Origin), "name").String()
  347. }
  348. return ret, array[0].Count, nil
  349. }
  350. func GetNextNumberOfVedio(db *gorm.DB, prefix string) (int64, error) {
  351. sql := fmt.Sprintf("select max(right(DevPubID, 6)+0) as max from VSSDevTbl where DevPubID like '%s%%'", prefix)
  352. type Result struct {
  353. Max int64
  354. }
  355. array := []Result{}
  356. err := db.Raw(sql).Scan(&array).Error
  357. if err != nil {
  358. return 0, err
  359. }
  360. if len(array) == 0 {
  361. return 1, nil
  362. }
  363. return array[0].Max + 1, nil
  364. }
  365. func GetNextNumberOfChannel(db *gorm.DB, prefix string) (int64, error) {
  366. sql := fmt.Sprintf("select max(right(ChanPubID, 6)+0) as max from VSSChannelTbl where ChanPubID like '%s%%'", prefix)
  367. type Result struct {
  368. Max int64
  369. }
  370. array := []Result{}
  371. err := db.Raw(sql).Scan(&array).Error
  372. if err != nil {
  373. return 0, err
  374. }
  375. if len(array) == 0 {
  376. return 1, nil
  377. }
  378. return array[0].Max + 1, nil
  379. }