cooler_box.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. package service
  2. import (
  3. "cold-logistics/app/admin/model"
  4. "cold-logistics/app/admin/service/dto"
  5. "cold-logistics/common/actions"
  6. cDto "cold-logistics/common/dto"
  7. "cold-logistics/common/global"
  8. "cold-logistics/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. return nil
  42. }
  43. // Get 获取CoolerBox对象
  44. func (e *CoolerBox) Get(d *dto.CoolerBoxGetReq, CoolerBoxModel *model.CoolerBox, p *actions.DataPermission) error {
  45. err := e.Orm.
  46. Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  47. First(CoolerBoxModel, d.GetId()).Error
  48. if err != nil {
  49. e.Log.Errorf("db error: %s", err)
  50. if errors.Is(err, gorm.ErrRecordNotFound) {
  51. return global.GetNotFoundOrNoPermissionErr
  52. }
  53. return global.GetFailedErr
  54. }
  55. return nil
  56. }
  57. // Insert 创建CoolerBox对象
  58. func (e *CoolerBox) Insert(c *dto.CoolerBoxInsertReq, p *actions.DataPermission) error {
  59. var err error
  60. var data model.CoolerBox
  61. tx := e.Orm.Begin()
  62. defer func() {
  63. if err != nil {
  64. tx.Rollback()
  65. } else {
  66. tx.Commit()
  67. }
  68. }()
  69. var k int64
  70. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  71. if err != nil {
  72. e.Log.Errorf("db error: %s", err)
  73. return global.CreateFailedErr
  74. }
  75. if k > 0 {
  76. err = errors.New("该Sn已绑定!")
  77. e.Log.Errorf("db error: %s", err)
  78. return err
  79. }
  80. var device nats_server.Device
  81. var company model.SysDept
  82. device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  83. if err != nil {
  84. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  85. e.Log.Errorf("db error: %s", err)
  86. return err
  87. }
  88. company, err = model.GetCompanyById(p.DeptId)
  89. if err != nil {
  90. e.Log.Errorf("db error: %s", err)
  91. return err
  92. }
  93. if device.T_pid != company.Id {
  94. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  95. e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  96. return err
  97. }
  98. // 添加保温箱
  99. c.Generate(&data)
  100. data.HistorySn = []string{data.Sn}
  101. err = tx.Create(&data).Error
  102. if err != nil {
  103. e.Log.Errorf("db error: %s", err)
  104. return global.CreateFailedErr
  105. }
  106. c.Id = data.Id
  107. return nil
  108. }
  109. func (e *CoolerBox) BatchInsert(c *dto.CoolerBoxBatchInsertReq) error {
  110. var err error
  111. tx := e.Orm.Begin()
  112. defer func() {
  113. if err != nil {
  114. tx.Rollback()
  115. } else {
  116. tx.Commit()
  117. }
  118. }()
  119. for _, coolerBox := range c.List {
  120. var data model.CoolerBox
  121. var k int64
  122. err = tx.Model(&data).Where("sn = ?", coolerBox.Sn).Count(&k).Error
  123. if err != nil {
  124. e.Log.Errorf("db error: %s", err)
  125. return global.CreateFailedErr
  126. }
  127. if k > 0 {
  128. continue
  129. }
  130. // 添加保温箱
  131. coolerBox.CreateBy = c.CreateBy
  132. coolerBox.DeptId = c.DeptId
  133. coolerBox.Status = c.Status
  134. coolerBox.Generate(&data)
  135. data.HistorySn = []string{data.Sn}
  136. err = tx.Create(&data).Error
  137. if err != nil {
  138. e.Log.Errorf("db error: %s", err)
  139. return global.CreateFailedErr
  140. }
  141. }
  142. return nil
  143. }
  144. // Update 修改CoolerBox对象
  145. func (e *CoolerBox) Update(c *dto.CoolerBoxUpdateReq, p *actions.DataPermission) error {
  146. var err error
  147. tx := e.Orm.Begin()
  148. defer func() {
  149. if err != nil {
  150. tx.Rollback()
  151. } else {
  152. tx.Commit()
  153. }
  154. }()
  155. var CoolerBoxModel = model.CoolerBox{}
  156. // 查询保温箱是否存在
  157. err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  158. First(&CoolerBoxModel, c.GetId()).Error
  159. if err != nil {
  160. e.Log.Errorf("db error: %s", err)
  161. if errors.Is(err, gorm.ErrRecordNotFound) {
  162. return global.UpdateNotFoundOrNoPermissionErr
  163. }
  164. return global.UpdateFailedErr
  165. }
  166. if CoolerBoxModel.Sn != c.Sn {
  167. var k int64
  168. var data = model.CoolerBox{}
  169. err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
  170. if err != nil {
  171. e.Log.Errorf("db error: %s", err)
  172. return global.CreateFailedErr
  173. }
  174. if k > 0 {
  175. err = errors.New("该Sn已绑定!")
  176. e.Log.Errorf("db error: %s", err)
  177. return err
  178. }
  179. var device nats_server.Device
  180. var company model.SysDept
  181. device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
  182. if err != nil {
  183. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  184. e.Log.Errorf("db error: %s", err)
  185. return err
  186. }
  187. company, err = model.GetCompanyById(p.DeptId)
  188. if err != nil {
  189. e.Log.Errorf("db error: %s", err)
  190. return err
  191. }
  192. if device.T_pid != company.Id {
  193. err = errors.New("获取SN信息失败,请检查SN是否正确!")
  194. e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
  195. return err
  196. }
  197. CoolerBoxModel.HistorySn = append(CoolerBoxModel.HistorySn, c.Sn)
  198. }
  199. c.Generate(&CoolerBoxModel)
  200. err = tx.Save(&CoolerBoxModel).Error
  201. if err != nil {
  202. e.Log.Errorf("db error: %s", err)
  203. return global.UpdateFailedErr
  204. }
  205. c.Id = CoolerBoxModel.Id
  206. return nil
  207. }
  208. // Remove 删除CoolerBox
  209. func (e *CoolerBox) Remove(c *dto.CoolerBoxDeleteReq, p *actions.DataPermission) error {
  210. var err error
  211. tx := e.Orm.Begin()
  212. defer func() {
  213. if err != nil {
  214. tx.Rollback()
  215. } else {
  216. tx.Commit()
  217. }
  218. }()
  219. var CoolerBoxModel model.CoolerBox
  220. // 查询保温箱是否存在
  221. err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
  222. First(&CoolerBoxModel, c.GetId()).Error
  223. if err != nil {
  224. e.Log.Errorf("db error: %s", err)
  225. if errors.Is(err, gorm.ErrRecordNotFound) {
  226. return global.DeleteNotFoundOrNoPermissionErr
  227. }
  228. return global.DeleteFailedErr
  229. }
  230. db := tx.Delete(&CoolerBoxModel)
  231. if err = db.Error; err != nil {
  232. e.Log.Errorf("db error: %s", err)
  233. return global.DeleteFailedErr
  234. }
  235. if db.RowsAffected == 0 {
  236. return global.DeleteNotFoundOrNoPermissionErr
  237. }
  238. return nil
  239. }