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