dust_device.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. "smart-supplier-management/consts"
  8. "time"
  9. "smart-supplier-management/errors"
  10. )
  11. type DustDeviceInfo struct {
  12. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"ID"`
  13. Code string `gorm:"column:Code" json:"code"`
  14. Name string `gorm:"column:Name" json:"name"`
  15. DeviceId string `gorm:"column:DeviceId" json:"deviceid"`
  16. DeviceModel string `gorm:"column:DeviceModel" json:"devicemodel"`
  17. DeviceName string `gorm:"column:DeviceName" json:"devicename"`
  18. Manufacturer string `gorm:"column:Manufacturer" json:"manufacturer"`
  19. Batch string `gorm:"column:Batch" json:"batch"`
  20. SN string `gorm:"column:SN" json:"sn"`
  21. SIM string `gorm:"column:SIM" json:"sim"`
  22. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  23. Unit string `gorm:"column:Unit" json:"unit"`
  24. Person string `gorm:"column:Person" json:"person"`
  25. Phone string `gorm:"column:phone" json:"phone"`
  26. Remark string `gorm:"column:remark" json:"remark"`
  27. Longitude string `gorm:"column:Longitude" json:"longitude"`
  28. Latitude string `gorm:"column:Latitude" json:"latitude"`
  29. Url string `gorm:"column:Url" json:"url"`
  30. VerifyTime time.Time `gorm:"column:VerifyTime"`
  31. VerifyStatus int `gorm:"column:VerifyStatus"`
  32. Key string `gorm:"column:Key"`
  33. ProjectId int64 `gorm:"column:ProjectId"`
  34. ProviderId int64 `gorm:"column:ProviderId"`
  35. CreatedAt time.Time `gorm:"column:CreatedAt"`
  36. }
  37. const (
  38. DeviceTypeVedio = 9
  39. DeviceTypeDust = 5
  40. DeviceTypeVehicle = 2
  41. DeviceTypeAttendance = 3
  42. DeviceTypeTower = 1
  43. DeviceTypeLift = 4
  44. )
  45. func (DustDeviceInfo) TableName() string {
  46. return "db_smart_v2.DustDeviceInfo"
  47. }
  48. func (p *DustDeviceInfo) Insert(db *gorm.DB) error {
  49. return db.Table(p.TableName()).Create(p).Error
  50. }
  51. /*
  52. func (p *Dustdeviceinfo) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error {
  53. 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)
  54. if deviceType > 0 {
  55. 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)
  56. }
  57. err := db.Raw(sql).Scan(result).Error
  58. return err
  59. }
  60. */
  61. func (p *DustDeviceInfo) Del(db *gorm.DB, where 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...).Delete(p).Error
  67. }
  68. func (p *DustDeviceInfo) Find(db *gorm.DB, where map[string]interface{}) error {
  69. cond, val, err := whereBuild(where)
  70. if err != nil {
  71. return err
  72. }
  73. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  74. }
  75. func (p *DustDeviceInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  76. cond, val, err := whereBuild(where)
  77. if err != nil {
  78. return err
  79. }
  80. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  81. }
  82. func (p *DustDeviceInfo) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  83. cond, val, err := whereBuild(where)
  84. if err != nil {
  85. return err
  86. }
  87. ps := []DustDeviceInfo{}
  88. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  89. if err != nil {
  90. return err
  91. }
  92. if len(ps) > 0 {
  93. *p = ps[0]
  94. }
  95. return nil
  96. }
  97. func (p *DustDeviceInfo) Save(db *gorm.DB) error {
  98. return db.Save(p).Error
  99. }
  100. func (p *DustDeviceInfo) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  101. if len(where) > 0 {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. return 0, err
  105. }
  106. ret := int64(0)
  107. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  108. return ret, err
  109. }
  110. ret := int64(0)
  111. err := db.Table(p.TableName()).Count(&ret).Error
  112. return ret, err
  113. }
  114. func (p *DustDeviceInfo) List(db *gorm.DB, where map[string]interface{}, page int) (list []DustDeviceInfo, err error) {
  115. if len(where) > 0 {
  116. cond, val, err := whereBuild(where)
  117. if err != nil {
  118. return list, err
  119. }
  120. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  121. return list, result.Error
  122. }
  123. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  124. return list, result.Error
  125. }
  126. func (p *DustDeviceInfo) All(db *gorm.DB) (list []DustDeviceInfo, err error) {
  127. result := db.Table(p.TableName()).Find(&list)
  128. return list, result.Error
  129. }
  130. type DustDeviceItem struct {
  131. Id int64
  132. Sn string
  133. TypeCode int32
  134. TypeName string
  135. ProjectName string
  136. ProjectId int64
  137. SafetyRecordNo string
  138. CreatedTime time.Time
  139. Status int32
  140. State int32
  141. ProjectApproveTime time.Time
  142. Key string
  143. ProviderName string
  144. SocialCode string
  145. Name string
  146. Manufacturer string
  147. Url string
  148. Batch string
  149. Person string
  150. Phone string
  151. Unit string
  152. Lon string
  153. Lat string
  154. Model string
  155. Comment string
  156. }
  157. func dustDeviceListSql(req DustDeviceListRequest)(string, string, []interface{}) {
  158. sql := fmt.Sprintf("select t1.ID as id, t1.DeviceModel as model, t1.Remark as comment, t1.Batch as batch, t1.Person as person, t1.Phone as phone, t1.Manufacturer as manufacturer, t1.Longitude as lon, t1.Latitude as lat, t1.Url as url, t1.Unit as unit, t1.Key as 'key', t1.DeviceName as name, t1.SN as sn, t1.VerifyTime as project_approve_time, "+
  159. "t1.CreatedAt as created_time, t1.DeviceState 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 DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  160. countSql := fmt.Sprintf("select count(1) as count from DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ")
  161. args := []interface{}{}
  162. whereArray := []string{}
  163. if req.ProviderId > 0 {
  164. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  165. }
  166. if req.Filter != "" {
  167. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t2.SafetyNo like '%%%s%%' or t2.Name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  168. }
  169. if req.CanDel {
  170. req.IsAll = true
  171. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  172. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  173. }
  174. if len(req.StatusFilter) > 0 {
  175. args = append(args, req.StatusFilter)
  176. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  177. }
  178. where := ""
  179. for _, v := range whereArray {
  180. if where == "" {
  181. where = fmt.Sprintf(" where %s", v)
  182. continue
  183. }
  184. where = fmt.Sprintf("%s and %s", where, v)
  185. }
  186. offset := (req.Page - 1) *int32(PageSize)
  187. if req.IsAll {
  188. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  189. countSql = fmt.Sprintf("%s %s", countSql, where)
  190. } else {
  191. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  192. countSql = fmt.Sprintf("%s %s", countSql, where)
  193. }
  194. return sql, countSql, args
  195. }
  196. type DustDeviceListRequest struct {
  197. ProviderId int64
  198. Filter string
  199. StatusFilter []int32
  200. IsAll bool
  201. CanDel bool
  202. Page int32
  203. ProjectId int64
  204. }
  205. func (p *DustDeviceInfo) DustDeviceList(db *gorm.DB, req DustDeviceListRequest) ([]DustDeviceItem, int64, error) {
  206. type ResultCount struct {
  207. Count int64
  208. }
  209. array := []ResultCount{}
  210. ret := []DustDeviceItem{}
  211. var err error
  212. sql, countSql, args := dustDeviceListSql(req)
  213. err = db.Raw(countSql, args...).Scan(&array).Error
  214. if err != nil {
  215. return nil, 0, err
  216. }
  217. if len(array) == 0 {
  218. return nil, 0, nil
  219. }
  220. if array[0].Count == 0 {
  221. return nil, 0, nil
  222. }
  223. err = db.Raw(sql, args...).Scan(&ret).Error
  224. if err != nil {
  225. return nil, array[0].Count, err
  226. }
  227. return ret, array[0].Count, nil
  228. }
  229. func GetNextNumberOfVedioNew(db *gorm.DB, prefix string) (int64, error) {
  230. sql1 := fmt.Sprintf("select right(DevPubID, 6)+1 as ret from VSSDevTbl t1 where not exists (select right(DevPubID, 6) from VSSDevTbl t2 where right(t2.DevPubID, 6)+0=right(t1.DevPubID, 6)+1 and DevPubID like '%s%%') and DevPubID like '%s%%' order by ret limit 1 ;", prefix, prefix)
  231. sql2 := fmt.Sprintf("select right(DevPubID, 6)-1 as ret from VSSDevTbl t1 where not exists (select right(DevPubID, 6) from VSSDevTbl t2 where right(t2.DevPubID, 6)+0=right(t1.DevPubID, 6)-1 and DevPubID like '%s%%') and DevPubID like '%s%%' order by ret desc limit 1 ;", prefix, prefix)
  232. type Result struct {
  233. Ret int64
  234. }
  235. array := []Result{}
  236. err := db.Raw(sql1).Scan(&array).Error
  237. if err != nil {
  238. return 0, err
  239. }
  240. if len(array) == 0 {
  241. return 1, nil
  242. }
  243. if array[0].Ret <= 999999 {
  244. return array[0].Ret, nil
  245. }
  246. array = []Result{}
  247. err = db.Raw(sql2).Scan(&array).Error
  248. if err != nil {
  249. return 0, err
  250. }
  251. if len(array) == 0 {
  252. return 0, nil
  253. }
  254. return array[0].Ret, nil
  255. }
  256. func GetNextNumberOfChannelNew(db *gorm.DB, prefix string) (int64, error) {
  257. sql1 := fmt.Sprintf("select right(ChanPubID, 6)+1 as ret from VSSChannelTbl t1 where not exists (select right(ChanPubID, 6) from VSSChannelTbl t2 where right(t2.ChanPubID, 6)+0=right(t1.ChanPubID, 6)+1 and ChanPubID like '%s%%') and ChanPubID like '%s%%' order by ret limit 1 ;", prefix, prefix)
  258. sql2 := fmt.Sprintf("select right(ChanPubID, 6)-1 as ret from VSSChannelTbl t1 where not exists (select right(ChanPubID, 6) from VSSChannelTbl t2 where right(t2.ChanPubID, 6)+0=right(t1.ChanPubID, 6)-1 and ChanPubID like '%s%%') and ChanPubID like '%s%%' order by ret desc limit 1 ;", prefix, prefix)
  259. type Result struct {
  260. Ret int64
  261. }
  262. array := []Result{}
  263. err := db.Raw(sql1).Scan(&array).Error
  264. if err != nil {
  265. return 0, errors.DataBaseError
  266. }
  267. if len(array) == 0 {
  268. return 1, nil
  269. }
  270. if array[0].Ret <= 999999 {
  271. return array[0].Ret, nil
  272. }
  273. array = []Result{}
  274. err = db.Raw(sql2).Scan(&array).Error
  275. if err != nil {
  276. return 0, err
  277. }
  278. if len(array) == 0 {
  279. return 0, nil
  280. }
  281. return array[0].Ret, nil
  282. }
  283. func GetNextNumberOfVedioOld(db *gorm.DB, prefix string) (int64, error) {
  284. sql := fmt.Sprintf("select max(right(DevPubID, 6)+0) as max from VSSDevTbl where DevPubID like '%s%%'", prefix)
  285. type Result struct {
  286. Max int64
  287. }
  288. array := []Result{}
  289. err := db.Raw(sql).Scan(&array).Error
  290. if err != nil {
  291. return 0, err
  292. }
  293. if len(array) == 0 {
  294. return 1, nil
  295. }
  296. return array[0].Max + 1, nil
  297. }
  298. func GetNextNumberOfChannelOld(db *gorm.DB, prefix string) (int64, error) {
  299. sql := fmt.Sprintf("select max(right(ChanPubID, 6)+0) as max from VSSChannelTbl where ChanPubID like '%s%%'", prefix)
  300. type Result struct {
  301. Max int64
  302. }
  303. array := []Result{}
  304. err := db.Raw(sql).Scan(&array).Error
  305. if err != nil {
  306. return 0, err
  307. }
  308. if len(array) == 0 {
  309. return 1, nil
  310. }
  311. return array[0].Max + 1, nil
  312. }