ice_locker.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package service
  2. import (
  3. "cold-delivery/app/admin/model"
  4. "cold-delivery/app/admin/service/dto"
  5. "cold-delivery/common/actions"
  6. cDto "cold-delivery/common/dto"
  7. "cold-delivery/common/global"
  8. "cold-delivery/common/nats/nats_server"
  9. "errors"
  10. "gogs.baozhida.cn/zoie/OAuth-core/service"
  11. "gorm.io/gorm"
  12. )
  13. type IceLocker struct {
  14. service.Service
  15. }
  16. // GetPage 获取IceLocker列表
  17. func (e *IceLocker) GetPage(c *dto.IceLockerGetPageReq, list *[]model.IceLocker, count *int64, p *actions.DataPermission) error {
  18. var err error
  19. var data model.IceLocker
  20. if c.PageSize == 9999 {
  21. err = e.Orm.Model(&data).
  22. Scopes(
  23. actions.Permission(data.TableName(), p),
  24. ).
  25. Find(list).Limit(-1).Offset(-1).
  26. Count(count).Error
  27. if err != nil {
  28. e.Log.Errorf("db error: %s", err)
  29. return global.GetFailedErr
  30. }
  31. } else {
  32. err = e.Orm.Model(&data).
  33. Scopes(
  34. cDto.MakeCondition(c.GetNeedSearch()),
  35. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  36. actions.Permission(data.TableName(), p),
  37. ).
  38. Find(list).Limit(-1).Offset(-1).
  39. Count(count).Error
  40. }
  41. if err != nil {
  42. e.Log.Errorf("db error: %s", err)
  43. return global.GetFailedErr
  44. }
  45. if c.ShowTemp {
  46. // 获取公司秘钥
  47. var company model.SysDept
  48. company, err = model.GetCompanyById(p.DeptId)
  49. if err != nil {
  50. e.Log.Errorf("db error: %s", err)
  51. return model.GetCompanyKeyErr
  52. }
  53. for i := 0; i < len(*list); i++ {
  54. deviceData, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey((*list)[i].Sn, company.ColdKey)
  55. if len(deviceData) > 0 {
  56. (*list)[i].DeviceData = deviceData[0].T_DeviceSensorData
  57. }
  58. }
  59. }
  60. return nil
  61. }
  62. // Get 获取IceLocker对象
  63. func (e *IceLocker) Get(d *dto.IceLockerGetReq, iceLockerModel *model.IceLocker, p *actions.DataPermission) error {
  64. err := e.Orm.
  65. Scopes(actions.Permission(iceLockerModel.TableName(), p)).
  66. First(iceLockerModel, d.GetId()).Error
  67. if err != nil {
  68. e.Log.Errorf("db error: %s", err)
  69. if errors.Is(err, gorm.ErrRecordNotFound) {
  70. return global.GetNotFoundOrNoPermissionErr
  71. }
  72. return global.GetFailedErr
  73. }
  74. return nil
  75. }
  76. // Insert 创建IceLocker对象
  77. func (e *IceLocker) Insert(c *dto.IceLockerInsertReq, p *actions.DataPermission) error {
  78. var err error
  79. var data model.IceLocker
  80. tx := e.Orm.Begin()
  81. defer func() {
  82. if err != nil {
  83. tx.Rollback()
  84. } else {
  85. tx.Commit()
  86. }
  87. }()
  88. var k int64
  89. err = tx.Model(&data).Where("name = ? and dept_id = ?", c.Name, c.DeptId).Count(&k).Error
  90. if err != nil {
  91. e.Log.Errorf("db error: %s", err)
  92. return global.CreateFailedErr
  93. }
  94. if k > 0 {
  95. err = errors.New("冷冻柜名称已存在!")
  96. e.Log.Errorf("db error: %s", err)
  97. return err
  98. }
  99. //var device nats_server.Device
  100. //var company model.SysDept
  101. //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  102. //if err != nil {
  103. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  104. // e.Log.Errorf("db error: %s", err)
  105. // return err
  106. //}
  107. //company, err = model.GetCompanyById(p.DeptId)
  108. //if err != nil {
  109. // e.Log.Errorf("db error: %s", err)
  110. // return err
  111. //}
  112. //if device.T_pid != company.Id {
  113. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  114. // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  115. // return err
  116. //}
  117. // 添加冷冻柜
  118. c.Generate(&data)
  119. err = tx.Create(&data).Error
  120. if err != nil {
  121. e.Log.Errorf("db error: %s", err)
  122. return global.CreateFailedErr
  123. }
  124. c.Id = data.Id
  125. return nil
  126. }
  127. // Update 修改IceLocker对象
  128. func (e *IceLocker) Update(c *dto.IceLockerUpdateReq, p *actions.DataPermission) error {
  129. var err error
  130. tx := e.Orm.Begin()
  131. defer func() {
  132. if err != nil {
  133. tx.Rollback()
  134. } else {
  135. tx.Commit()
  136. }
  137. }()
  138. var iceLockerModel = model.IceLocker{}
  139. // 查询冷冻柜是否存在
  140. err = e.Orm.Scopes(actions.Permission(iceLockerModel.TableName(), p)).
  141. First(&iceLockerModel, c.GetId()).Error
  142. if err != nil {
  143. e.Log.Errorf("db error: %s", err)
  144. if errors.Is(err, gorm.ErrRecordNotFound) {
  145. return global.UpdateNotFoundOrNoPermissionErr
  146. }
  147. return global.UpdateFailedErr
  148. }
  149. if iceLockerModel.Name != c.Name {
  150. var k int64
  151. var data = model.IceLocker{}
  152. err = tx.Model(&data).Where("name = ? and dept_id = ?", c.Name, iceLockerModel.DeptId).Count(&k).Error
  153. if err != nil {
  154. e.Log.Errorf("db error: %s", err)
  155. return global.CreateFailedErr
  156. }
  157. if k > 0 {
  158. err = errors.New("冷冻柜名称已存在!")
  159. e.Log.Errorf("db error: %s", err)
  160. return err
  161. }
  162. }
  163. if iceLockerModel.Sn != c.Sn && len(c.Sn) > 0 {
  164. var k int64
  165. var data = model.IceLocker{}
  166. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  167. if err != nil {
  168. e.Log.Errorf("db error: %s", err)
  169. return global.CreateFailedErr
  170. }
  171. if k > 0 {
  172. err = errors.New("该Sn已绑定!")
  173. e.Log.Errorf("db error: %s", err)
  174. return err
  175. }
  176. //var device nats_server.Device
  177. //var company model.SysDept
  178. //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  179. //if err != nil {
  180. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  181. // e.Log.Errorf("db error: %s", err)
  182. // return err
  183. //}
  184. //company, err = model.GetCompanyById(p.DeptId)
  185. //if err != nil {
  186. // e.Log.Errorf("db error: %s", err)
  187. // return err
  188. //}
  189. //if device.T_pid != company.Id {
  190. // err = errors.New("获取SN信息失败,请检查SN是否正确!")
  191. // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  192. // return err
  193. //}
  194. iceLockerModel.HistorySn = append(iceLockerModel.HistorySn, c.Sn)
  195. }
  196. c.Generate(&iceLockerModel)
  197. err = tx.Save(&iceLockerModel).Error
  198. if err != nil {
  199. e.Log.Errorf("db error: %s", err)
  200. return global.UpdateFailedErr
  201. }
  202. c.Id = iceLockerModel.Id
  203. return nil
  204. }
  205. // Remove 删除IceLocker
  206. func (e *IceLocker) Remove(c *dto.IceLockerDeleteReq, p *actions.DataPermission) error {
  207. var err error
  208. tx := e.Orm.Begin()
  209. defer func() {
  210. if err != nil {
  211. tx.Rollback()
  212. } else {
  213. tx.Commit()
  214. }
  215. }()
  216. var iceRaftModel model.IceRaft
  217. var count int64
  218. err = e.Orm.Model(&iceRaftModel).
  219. Scopes(
  220. actions.Permission(iceRaftModel.TableName(), p),
  221. IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusWaitUse),
  222. IceRaftNameRecordIceLockerIdScopes(c.Id),
  223. ).
  224. Where("ice_raft.status = '2'").
  225. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  226. Count(&count).Error
  227. if err != nil {
  228. e.Log.Errorf("db error: %s", err)
  229. return global.DeleteFailedErr
  230. }
  231. if count > 0 {
  232. err = errors.New("该冷冻柜下有冰排待使用中,无法删除!")
  233. return err
  234. }
  235. var iceLockerModel model.IceLocker
  236. // 查询冷冻柜是否存在
  237. err = e.Orm.Scopes(actions.Permission(iceLockerModel.TableName(), p)).
  238. First(&iceLockerModel, c.GetId()).Error
  239. if err != nil {
  240. e.Log.Errorf("db error: %s", err)
  241. if errors.Is(err, gorm.ErrRecordNotFound) {
  242. return global.DeleteNotFoundOrNoPermissionErr
  243. }
  244. return global.DeleteFailedErr
  245. }
  246. db := tx.Delete(&iceLockerModel)
  247. if err = db.Error; err != nil {
  248. e.Log.Errorf("db error: %s", err)
  249. return global.DeleteFailedErr
  250. }
  251. if db.RowsAffected == 0 {
  252. return global.DeleteNotFoundOrNoPermissionErr
  253. }
  254. return nil
  255. }
  256. func (e *IceLocker) BatchInsert(c *dto.IceLockerBatchInsertReq) error {
  257. var err error
  258. tx := e.Orm.Begin()
  259. defer func() {
  260. if err != nil {
  261. tx.Rollback()
  262. } else {
  263. tx.Commit()
  264. }
  265. }()
  266. for _, iceLocker := range c.List {
  267. var data model.IceLocker
  268. var k int64
  269. err = tx.Model(&data).Where("sn = ?", iceLocker.Sn).Count(&k).Error
  270. if err != nil {
  271. e.Log.Errorf("db error: %s", err)
  272. return global.CreateFailedErr
  273. }
  274. if k > 0 {
  275. //err = errors.New("该Sn已绑定!")
  276. continue
  277. }
  278. // 添加冷冻柜
  279. iceLocker.CreateBy = c.CreateBy
  280. iceLocker.DeptId = c.DeptId
  281. iceLocker.Status = c.Status
  282. iceLocker.Generate(&data)
  283. data.HistorySn = []string{data.Sn}
  284. err = tx.Create(&data).Error
  285. if err != nil {
  286. e.Log.Errorf("db error: %s", err)
  287. return global.CreateFailedErr
  288. }
  289. }
  290. return nil
  291. }