ice_raft.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. model2 "cold-delivery/common/model"
  9. "errors"
  10. "fmt"
  11. "gogs.baozhida.cn/zoie/OAuth-core/service"
  12. "gorm.io/gorm"
  13. "time"
  14. )
  15. type IceRaft struct {
  16. service.Service
  17. }
  18. // GetPage 获取IceRaft列表
  19. func (e *IceRaft) GetPage(c *dto.IceRaftGetPageReq, list *[]model.IceRaft, count *int64, p *actions.DataPermission) error {
  20. var err error
  21. var data model.IceRaft
  22. if c.PageSize == 9999 {
  23. err = e.Orm.Model(&data).
  24. Scopes(
  25. actions.Permission(data.TableName(), p),
  26. ).
  27. Find(list).Limit(-1).Offset(-1).
  28. Count(count).Error
  29. } else {
  30. err = e.Orm.Model(&data).
  31. Scopes(
  32. cDto.MakeCondition(c.GetNeedSearch()),
  33. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  34. actions.Permission(data.TableName(), p),
  35. ).
  36. Find(list).Limit(-1).Offset(-1).
  37. Count(count).Error
  38. }
  39. if err != nil {
  40. e.Log.Errorf("db error: %s", err)
  41. return global.GetFailedErr
  42. }
  43. return nil
  44. }
  45. func IceRaftNameRecordStatusScopes(status string) func(db *gorm.DB) *gorm.DB {
  46. return func(db *gorm.DB) *gorm.DB {
  47. if len(status) == 0 {
  48. return db
  49. }
  50. if status == "0" {
  51. return db.Where("ice_raft_record.status is null")
  52. }
  53. return db.Where("ice_raft_record.status = ?", status)
  54. }
  55. }
  56. func IceRaftNameRecordIceLockerIdScopes(iceLockerId int) func(db *gorm.DB) *gorm.DB {
  57. return func(db *gorm.DB) *gorm.DB {
  58. if iceLockerId == 0 {
  59. return db
  60. }
  61. return db.Where("ice_raft_record.ice_locker_id = ? and ice_raft_record.cooler_box_id = 0", iceLockerId)
  62. }
  63. }
  64. func IceRaftNameRecordCoolerBoxIdScopes(coolerBoxId int) func(db *gorm.DB) *gorm.DB {
  65. return func(db *gorm.DB) *gorm.DB {
  66. if coolerBoxId == 0 {
  67. return db
  68. }
  69. return db.Where("ice_raft_record.cooler_box_id = ?", coolerBoxId)
  70. }
  71. }
  72. func (e *IceRaft) GetRecordPage(c *dto.IceRaftGetNewestRecordPageReq, list *[]model.IceRaft, count *int64, p *actions.DataPermission) error {
  73. var err error
  74. var data model.IceRaft
  75. if c.PageSize == 9999 {
  76. err = e.Orm.Model(&data).
  77. Scopes(
  78. actions.Permission(data.TableName(), p),
  79. ).
  80. Where("ice_raft.status = '2'").
  81. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  82. Preload("IceRaftRecord.IceLocker").
  83. Preload("IceRaftRecord.CoolerBox").
  84. Order("FIELD(ice_raft_record.status,'2','','1','3','4')").
  85. Find(list).Limit(-1).Offset(-1).
  86. Count(count).Error
  87. } else {
  88. err = e.Orm.Model(&data).
  89. Scopes(
  90. cDto.MakeCondition(c.GetNeedSearch()),
  91. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  92. actions.Permission(data.TableName(), p),
  93. IceRaftNameRecordStatusScopes(c.Status),
  94. IceRaftNameRecordIceLockerIdScopes(c.IceLockerId),
  95. IceRaftNameRecordCoolerBoxIdScopes(c.CoolerBoxId),
  96. ).
  97. Where("ice_raft.status = '2'").
  98. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  99. Preload("IceRaftRecord.IceLocker").
  100. Preload("IceRaftRecord.CoolerBox").
  101. Order("FIELD(ice_raft_record.status,'2','','1','3','4')").
  102. Find(list).Limit(-1).Offset(-1).
  103. Count(count).Error
  104. }
  105. if err != nil {
  106. e.Log.Errorf("db error: %s", err)
  107. return global.GetFailedErr
  108. }
  109. return nil
  110. }
  111. // Get 获取IceRaft对象
  112. func (e *IceRaft) Get(d *dto.IceRaftGetReq, iceRaftModel *model.IceRaft, p *actions.DataPermission) error {
  113. err := e.Orm.
  114. Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  115. First(iceRaftModel, d.GetId()).Error
  116. if err != nil {
  117. e.Log.Errorf("db error: %s", err)
  118. if errors.Is(err, gorm.ErrRecordNotFound) {
  119. return global.GetNotFoundOrNoPermissionErr
  120. }
  121. return global.GetFailedErr
  122. }
  123. return nil
  124. }
  125. func (e *IceRaft) GetByCode(d *dto.IceRaftGetByCodeReq, iceRaftModel *model.IceRaft, p *actions.DataPermission) error {
  126. err := e.Orm.
  127. Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  128. Where("code = ?", d.Code).
  129. First(iceRaftModel).Error
  130. if err != nil {
  131. e.Log.Errorf("db error: %s", err)
  132. if errors.Is(err, gorm.ErrRecordNotFound) {
  133. return errors.New(fmt.Sprintf("冰排编号【%s】不存在!", d.Code))
  134. }
  135. return global.GetFailedErr
  136. }
  137. return nil
  138. }
  139. // Insert 创建IceRaft对象
  140. func (e *IceRaft) Insert(c *dto.IceRaftInsertReq) error {
  141. var err error
  142. var data model.IceRaft
  143. tx := e.Orm.Begin()
  144. defer func() {
  145. if err != nil {
  146. tx.Rollback()
  147. } else {
  148. tx.Commit()
  149. }
  150. }()
  151. for _, code := range c.CodeList {
  152. var k int64
  153. err = tx.Model(&data).Where("code = ? and dept_id = ?", code, c.DeptId).Count(&k).Error
  154. if err != nil {
  155. e.Log.Errorf("db error: %s", err)
  156. return global.CreateFailedErr
  157. }
  158. // 冰排编号已存在
  159. if k > 0 {
  160. continue
  161. }
  162. // 添加冰排
  163. data.Id = 0
  164. c.Generate(&data, code)
  165. err = tx.Create(&data).Error
  166. if err != nil {
  167. e.Log.Errorf("db error: %s", err)
  168. return global.CreateFailedErr
  169. }
  170. }
  171. return nil
  172. }
  173. // Update 修改IceRaft对象
  174. func (e *IceRaft) Update(c *dto.IceRaftUpdateReq, p *actions.DataPermission) error {
  175. var err error
  176. tx := e.Orm.Begin()
  177. defer func() {
  178. if err != nil {
  179. tx.Rollback()
  180. } else {
  181. tx.Commit()
  182. }
  183. }()
  184. var iceRaftModel = model.IceRaft{}
  185. // 查询冰排是否存在
  186. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  187. First(&iceRaftModel, c.GetId()).Error
  188. if err != nil {
  189. e.Log.Errorf("db error: %s", err)
  190. if errors.Is(err, gorm.ErrRecordNotFound) {
  191. return global.UpdateNotFoundOrNoPermissionErr
  192. }
  193. return global.UpdateFailedErr
  194. }
  195. if iceRaftModel.Code != c.Code {
  196. var k int64
  197. var data = model.IceRaft{}
  198. err = tx.Model(&data).Where("code = ? and dept_id = ?", c.Code, iceRaftModel.DeptId).Count(&k).Error
  199. if err != nil {
  200. e.Log.Errorf("db error: %s", err)
  201. return global.CreateFailedErr
  202. }
  203. if k > 0 {
  204. err = errors.New("冰排编号已存在!")
  205. e.Log.Errorf("db error: %s", err)
  206. return err
  207. }
  208. }
  209. c.Generate(&iceRaftModel)
  210. err = tx.Save(&iceRaftModel).Error
  211. if err != nil {
  212. e.Log.Errorf("db error: %s", err)
  213. return global.UpdateFailedErr
  214. }
  215. var iceRaftRecordModel model.IceRaftRecord
  216. iceRaftRecordModel.Id = iceRaftModel.IceRaftRecordId
  217. iceRaftRecordModel.FreezeClaim = iceRaftModel.FreezeClaim
  218. iceRaftRecordModel.Label = iceRaftModel.Label
  219. err = tx.Updates(&iceRaftRecordModel).Error
  220. if err != nil {
  221. e.Log.Errorf("db error: %s", err)
  222. return errors.New("修改冰排记录失败")
  223. }
  224. c.Id = iceRaftModel.Id
  225. return nil
  226. }
  227. // Remove 删除IceRaft
  228. func (e *IceRaft) Remove(c *dto.IceRaftDeleteReq, p *actions.DataPermission) error {
  229. var err error
  230. tx := e.Orm.Begin()
  231. defer func() {
  232. if err != nil {
  233. tx.Rollback()
  234. } else {
  235. tx.Commit()
  236. }
  237. }()
  238. var iceRaftModel model.IceRaft
  239. // 查询冰排是否存在
  240. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  241. First(&iceRaftModel, c.GetId()).Error
  242. if err != nil {
  243. e.Log.Errorf("db error: %s", err)
  244. if errors.Is(err, gorm.ErrRecordNotFound) {
  245. return global.DeleteNotFoundOrNoPermissionErr
  246. }
  247. return global.DeleteFailedErr
  248. }
  249. db := tx.Delete(&iceRaftModel)
  250. if err = db.Error; err != nil {
  251. e.Log.Errorf("db error: %s", err)
  252. return global.DeleteFailedErr
  253. }
  254. if db.RowsAffected == 0 {
  255. return global.DeleteNotFoundOrNoPermissionErr
  256. }
  257. return nil
  258. }
  259. func (e *IceRaft) InStorage(c *dto.IceRaftInStorageReq, p *actions.DataPermission) error {
  260. var err error
  261. tx := e.Orm.Begin()
  262. defer func() {
  263. if err != nil {
  264. tx.Rollback()
  265. } else {
  266. tx.Commit()
  267. }
  268. }()
  269. // 查询冷冻柜信息
  270. var iceLockerModel model.IceLocker
  271. err = e.Orm.Scopes(actions.Permission(iceLockerModel.TableName(), p)).
  272. First(&iceLockerModel, c.IceLockerId).Error
  273. if err != nil {
  274. e.Log.Errorf("db error: %s", err)
  275. return errors.New("获取冷冻柜信息失败")
  276. }
  277. if iceLockerModel.Status != model.IceLockerStatusNormal {
  278. err = errors.New("冷冻柜状态异常")
  279. return err
  280. }
  281. for _, code := range c.Code {
  282. // 查询冰排是否存在
  283. var iceRaftModel model.IceRaft
  284. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  285. Where("code = ?", code).
  286. Preload("IceRaftRecord").
  287. First(&iceRaftModel).Error
  288. if err != nil {
  289. // 冰排编号不存在,添加
  290. //if errors.Is(err, gorm.ErrRecordNotFound) {
  291. // iceRaftModel.Code = code
  292. // iceRaftModel.Status = model.IceRaftStatusNormal
  293. // iceRaftModel.DeptId = p.DeptId
  294. // iceRaftModel.CreateBy = p.UserId
  295. // err = tx.Create(&iceRaftModel).Error
  296. // if err != nil {
  297. // e.Log.Errorf("db error: %s", err)
  298. // return errors.New(fmt.Sprintf("添加冰排【%s】信息失败", code))
  299. // }
  300. //} else {
  301. // e.Log.Errorf("db error: %s", err)
  302. // return errors.New(fmt.Sprintf("获取冰排【%s】信息失败",code))
  303. //}
  304. e.Log.Errorf("db error: %s", err)
  305. return errors.New(fmt.Sprintf("获取冰排【%s】信息失败", code))
  306. }
  307. if iceRaftModel.Status != model.IceRaftStatusNormal {
  308. err = errors.New(fmt.Sprintf("冰排【%s】状态异常", code))
  309. return err
  310. }
  311. if iceRaftModel.IceRaftRecord.Status == model.IceRaftRecordStatusFreezing || iceRaftModel.IceRaftRecord.Status == model.IceRaftRecordStatusWaitUse {
  312. err = errors.New(fmt.Sprintf("冰排【%s】状态为%s,入库失败!", code, model.GetIceRaftRecordStatus(iceRaftModel.IceRaftRecord.Status)))
  313. return err
  314. }
  315. // 更新冰排记录状态 使用中 -> 已完成
  316. err = tx.Model(&model.IceRaftRecord{}).
  317. Where("ice_raft_id = ? and status = ?", iceRaftModel.Id, model.IceRaftRecordStatusUsing).
  318. Updates(map[string]interface{}{
  319. "status": model.IceRaftRecordStatusFinish,
  320. }).Error
  321. // 添加冰排记录
  322. var iceRaftRecordModel = model.IceRaftRecord{
  323. IceLockerId: iceLockerModel.Id,
  324. IceRaftId: iceRaftModel.Id,
  325. Status: model.IceRaftRecordStatusFreezing,
  326. Label: iceRaftModel.Label,
  327. InStorageTime: model2.Time(time.Now()),
  328. FreezeClaim: iceRaftModel.FreezeClaim,
  329. DeptBy: model2.DeptBy{
  330. DeptId: p.DeptId,
  331. },
  332. }
  333. err = tx.Create(&iceRaftRecordModel).Error
  334. if err != nil {
  335. e.Log.Errorf("db error: %s", err)
  336. return errors.New("添加冰排记录失败")
  337. }
  338. // 更新冰排使用记录
  339. err = tx.Model(&iceRaftModel).
  340. Where("id = ?", iceRaftModel.Id).
  341. Updates(map[string]interface{}{
  342. "ice_raft_record_id": iceRaftRecordModel.Id,
  343. }).Error
  344. if err != nil {
  345. e.Log.Errorf("db error: %s", err)
  346. return errors.New("关联冰排记录失败")
  347. }
  348. }
  349. return nil
  350. }
  351. func (e *IceRaft) OutStorage(c *dto.IceRaftOutStorageReq, p *actions.DataPermission) error {
  352. var err error
  353. tx := e.Orm.Begin()
  354. defer func() {
  355. if err != nil {
  356. tx.Rollback()
  357. } else {
  358. tx.Commit()
  359. }
  360. }()
  361. // 查询保温箱信息
  362. var coolerBoxModel model.CoolerBox
  363. err = e.Orm.Scopes(actions.Permission(coolerBoxModel.TableName(), p)).
  364. First(&coolerBoxModel, c.CoolerBoxId).Error
  365. if err != nil {
  366. e.Log.Errorf("db error: %s", err)
  367. return errors.New("获取保温箱信息失败")
  368. }
  369. if coolerBoxModel.Status != model.CoolerBoxStatusNormal {
  370. err = errors.New("保温箱状态异常")
  371. return err
  372. }
  373. for _, code := range c.Code {
  374. // 查询冰排是否存在
  375. var iceRaftModel model.IceRaft
  376. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  377. Where("code = ?", code).
  378. Preload("IceRaftRecord").
  379. First(&iceRaftModel).Error
  380. if err != nil {
  381. e.Log.Errorf("db error: %s", err)
  382. return errors.New("获取冰排信息失败")
  383. }
  384. if iceRaftModel.Status != model.IceRaftStatusNormal {
  385. err = errors.New(fmt.Sprintf("冰排【%s】状态异常", code))
  386. return err
  387. }
  388. // 获取冰排记录
  389. var iceRaftRecordModel model.IceRaftRecord
  390. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  391. Where("ice_raft_id = ?", iceRaftModel.Id).
  392. First(&iceRaftRecordModel, iceRaftModel.IceRaftRecordId).Error
  393. if err != nil {
  394. e.Log.Errorf("db error: %s", err)
  395. return errors.New("获取冰排记录失败,请确认冰排是否入库!")
  396. }
  397. if len(iceRaftRecordModel.OutStorageTime.String()) > 0 {
  398. err = errors.New(fmt.Sprintf("冰排【%s】已出库,禁止出库!", code))
  399. return err
  400. }
  401. inTime := iceRaftRecordModel.InStorageTime.Local()
  402. if inTime.Add(time.Hour * time.Duration(iceRaftRecordModel.FreezeClaim)).After(time.Now()) {
  403. err = errors.New(fmt.Sprintf("冰排【%s】未达到冷冻时长,禁止出库", code))
  404. return err
  405. }
  406. iceRaftRecordModel.Status = model.IceRaftRecordStatusUsing
  407. iceRaftRecordModel.CoolerBoxId = coolerBoxModel.Id
  408. iceRaftRecordModel.OutStorageTime = model2.Time(time.Now())
  409. err = tx.Save(&iceRaftRecordModel).Error
  410. if err != nil {
  411. e.Log.Errorf("db error: %s", err)
  412. return errors.New("保存冰排记录失败")
  413. }
  414. }
  415. return nil
  416. }
  417. // 查询保温箱关联的冰排
  418. func (e *IceRaft) GetPageByCoolerBoxId(coolerBoxId int, list *[]string, p *actions.DataPermission) error {
  419. var data model.IceRaft
  420. err := e.Orm.Model(&data).
  421. Select("code").
  422. Scopes(
  423. actions.Permission(data.TableName(), p),
  424. IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
  425. IceRaftNameRecordCoolerBoxIdScopes(coolerBoxId),
  426. ).
  427. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  428. Find(list).Error
  429. if err != nil {
  430. e.Log.Errorf("db error: %s", err)
  431. return global.GetFailedErr
  432. }
  433. return nil
  434. }
  435. type IceRaftRecord struct {
  436. service.Service
  437. }
  438. // GetPage 获取IceRaft列表
  439. func (e *IceRaftRecord) GetPage(c *dto.IceRaftRecordGetPageReq, list *[]model.IceRaftRecord, count *int64, p *actions.DataPermission) error {
  440. var err error
  441. var data model.IceRaftRecord
  442. if c.PageSize == 9999 {
  443. err = e.Orm.Model(&data).
  444. Scopes(
  445. actions.Permission(data.TableName(), p),
  446. ).
  447. Preload("IceLocker").
  448. Preload("CoolerBox").
  449. Find(list).Limit(-1).Offset(-1).
  450. Count(count).Error
  451. } else {
  452. err = e.Orm.Model(&data).
  453. Scopes(
  454. cDto.MakeCondition(c.GetNeedSearch()),
  455. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  456. actions.Permission(data.TableName(), p),
  457. ).
  458. Preload("IceLocker").
  459. Preload("CoolerBox").
  460. Find(list).Limit(-1).Offset(-1).
  461. Count(count).Error
  462. }
  463. if err != nil {
  464. e.Log.Errorf("db error: %s", err)
  465. return global.GetFailedErr
  466. }
  467. return nil
  468. }
  469. // Update 修改IceRaft对象
  470. func (e *IceRaftRecord) Update(c *dto.IceRaftRecordUpdateReq, p *actions.DataPermission) error {
  471. var err error
  472. tx := e.Orm.Begin()
  473. defer func() {
  474. if err != nil {
  475. tx.Rollback()
  476. } else {
  477. tx.Commit()
  478. }
  479. }()
  480. var iceRaftRecordModel = model.IceRaftRecord{}
  481. // 查询冰排是否存在
  482. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  483. First(&iceRaftRecordModel, c.GetId()).Error
  484. if err != nil {
  485. e.Log.Errorf("db error: %s", err)
  486. if errors.Is(err, gorm.ErrRecordNotFound) {
  487. return global.UpdateNotFoundOrNoPermissionErr
  488. }
  489. return global.UpdateFailedErr
  490. }
  491. iceRaftRecordModel.IceLockerId = c.IceLockerId
  492. iceRaftRecordModel.FreezeClaim = c.FreezeClaim
  493. if len(iceRaftRecordModel.OutStorageTime.String()) > 0 {
  494. iceRaftRecordModel.CoolerBoxId = c.CoolerBoxId
  495. }
  496. err = tx.Save(&iceRaftRecordModel).Error
  497. if err != nil {
  498. e.Log.Errorf("db error: %s", err)
  499. return global.UpdateFailedErr
  500. }
  501. c.Id = iceRaftRecordModel.Id
  502. return nil
  503. }
  504. // Remove 删除IceRaft
  505. func (e *IceRaftRecord) Remove(c *dto.IceRaftRecordDeleteReq, p *actions.DataPermission) error {
  506. var err error
  507. tx := e.Orm.Begin()
  508. defer func() {
  509. if err != nil {
  510. tx.Rollback()
  511. } else {
  512. tx.Commit()
  513. }
  514. }()
  515. var iceRaftRecordModel model.IceRaftRecord
  516. // 查询冰排是否存在
  517. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  518. First(&iceRaftRecordModel, c.GetId()).Error
  519. if err != nil {
  520. e.Log.Errorf("db error: %s", err)
  521. if errors.Is(err, gorm.ErrRecordNotFound) {
  522. return global.DeleteNotFoundOrNoPermissionErr
  523. }
  524. return global.DeleteFailedErr
  525. }
  526. db := tx.Delete(&iceRaftRecordModel)
  527. if err = db.Error; err != nil {
  528. e.Log.Errorf("db error: %s", err)
  529. return global.DeleteFailedErr
  530. }
  531. if db.RowsAffected == 0 {
  532. return global.DeleteNotFoundOrNoPermissionErr
  533. }
  534. // 更改冰排关联状态
  535. var iceRaftModel model.IceRaft
  536. var secondIceRaftRecordModel model.IceRaftRecord
  537. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  538. First(&iceRaftModel, iceRaftRecordModel.IceRaftId).Error
  539. if err != nil {
  540. e.Log.Errorf("db error: %s", err)
  541. return global.DeleteFailedErr
  542. }
  543. err = e.Orm.Scopes(actions.Permission(secondIceRaftRecordModel.TableName(), p)).
  544. Where("ice_raft_id = ? and id != ?", iceRaftRecordModel.IceRaftId, iceRaftRecordModel.Id).
  545. First(&secondIceRaftRecordModel).Error
  546. if err != nil {
  547. if errors.Is(err, gorm.ErrRecordNotFound) {
  548. iceRaftModel.IceRaftRecordId = 0
  549. }
  550. }
  551. iceRaftModel.IceRaftRecordId = secondIceRaftRecordModel.Id
  552. err = tx.Save(&iceRaftModel).Error
  553. if err != nil {
  554. e.Log.Errorf("db error: %s", err)
  555. return global.UpdateFailedErr
  556. }
  557. return nil
  558. }