cooler_box.go 7.3 KB

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