ice_raft.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. "sort"
  14. "time"
  15. )
  16. type IceRaft struct {
  17. service.Service
  18. }
  19. // GetPage 获取IceRaft列表
  20. func (e *IceRaft) GetPage(c *dto.IceRaftGetPageReq, list *[]model.IceRaft, count *int64, p *actions.DataPermission) error {
  21. var err error
  22. var data model.IceRaft
  23. if c.PageSize == 9999 {
  24. err = e.Orm.Model(&data).
  25. Scopes(
  26. actions.Permission(data.TableName(), p),
  27. ).
  28. Find(list).Limit(-1).Offset(-1).
  29. Count(count).Error
  30. } else {
  31. err = e.Orm.Model(&data).
  32. Scopes(
  33. cDto.MakeCondition(c.GetNeedSearch()),
  34. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  35. actions.Permission(data.TableName(), p),
  36. ).
  37. Find(list).Limit(-1).Offset(-1).
  38. Count(count).Error
  39. }
  40. if err != nil {
  41. e.Log.Errorf("db error: %s", err)
  42. return global.GetFailedErr
  43. }
  44. return nil
  45. }
  46. func IceRaftNameRecordStatusScopes(status string) func(db *gorm.DB) *gorm.DB {
  47. return func(db *gorm.DB) *gorm.DB {
  48. if len(status) == 0 {
  49. return db
  50. }
  51. if status == "0" {
  52. return db.Where("ice_raft_record.status is null")
  53. }
  54. return db.Where("ice_raft_record.status = ?", status)
  55. }
  56. }
  57. func IceRaftNameRecordIceLockerScopes(status string) func(db *gorm.DB) *gorm.DB {
  58. return func(db *gorm.DB) *gorm.DB {
  59. if len(status) == 0 {
  60. return db.Where("ice_raft_record.status = 1 or ice_raft_record.status =2")
  61. }
  62. if status == "0" {
  63. return db.Where("ice_raft_record.status is null")
  64. }
  65. return db.Where("ice_raft_record.status = ?", status)
  66. }
  67. }
  68. func IceRaftNameRecordIceLockerIdScopes(iceLockerId int) func(db *gorm.DB) *gorm.DB {
  69. return func(db *gorm.DB) *gorm.DB {
  70. if iceLockerId == 0 {
  71. return db
  72. }
  73. return db.Where("ice_raft_record.ice_locker_id = ? and ice_raft_record.cooler_box_id = 0", iceLockerId)
  74. }
  75. }
  76. func IceRaftNameRecordCoolerBoxIdScopes(coolerBoxId int) func(db *gorm.DB) *gorm.DB {
  77. return func(db *gorm.DB) *gorm.DB {
  78. if coolerBoxId == 0 {
  79. return db
  80. }
  81. return db.Where("ice_raft_record.cooler_box_id = ?", coolerBoxId)
  82. }
  83. }
  84. func IceRaftStartTimeAndEndTimeScopes(startTime, endtime string) func(db *gorm.DB) *gorm.DB {
  85. return func(db *gorm.DB) *gorm.DB {
  86. if len(startTime) == 0 {
  87. return db
  88. }
  89. return db.Where("ice_raft_record.ice_use_time >= ?", startTime).Where("ice_raft_record.ice_use_time <= ?", endtime)
  90. }
  91. }
  92. func CollerStartTimeAndEndTimeScopes(startTime, endtime string) func(db *gorm.DB) *gorm.DB {
  93. return func(db *gorm.DB) *gorm.DB {
  94. if len(startTime) == 0 {
  95. return db
  96. }
  97. return db.Where("cooler_box_record.cooler_box_star_time >= ?", startTime).Where("cooler_box_record.cooler_box_star_time <= ?", endtime)
  98. }
  99. }
  100. func (e *IceRaft) GetRecordPage(c *dto.IceRaftGetNewestRecordPageReq, list *[]model.IceRaft, count *int64, p *actions.DataPermission) error {
  101. var err error
  102. var data model.IceRaft
  103. if c.PageSize == 9999 {
  104. err = e.Orm.Model(&data).
  105. Scopes(
  106. actions.Permission(data.TableName(), p),
  107. ).
  108. Where("ice_raft.status = '2'").
  109. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  110. Preload("IceRaftRecord.IceLocker").
  111. Preload("IceRaftRecord.CoolerBox").
  112. Preload("IceRaftRecord").
  113. Order("FIELD(ice_raft_record.status,'2','','1','3','4')").
  114. Find(list).Limit(-1).Offset(-1).
  115. Count(count).Error
  116. } else {
  117. err = e.Orm.Model(&data).
  118. Scopes(
  119. cDto.MakeCondition(c.GetNeedSearch()),
  120. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  121. actions.Permission(data.TableName(), p),
  122. IceRaftNameRecordStatusScopes(c.Status),
  123. IceRaftNameRecordIceLockerIdScopes(c.IceLockerId),
  124. IceRaftNameRecordCoolerBoxIdScopes(c.CoolerBoxId),
  125. ).
  126. Where("ice_raft.status = '2'").
  127. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  128. Preload("IceRaftRecord.IceLocker").
  129. Preload("IceRaftRecord.CoolerBox").
  130. Preload("IceRaftRecord").
  131. Order("FIELD(ice_raft_record.status,'2','','1','3','4')").
  132. Find(list).Limit(-1).Offset(-1).
  133. Count(count).Error
  134. }
  135. if err != nil {
  136. e.Log.Errorf("db error: %s", err)
  137. return global.GetFailedErr
  138. }
  139. return nil
  140. }
  141. // Get 获取IceRaft对象
  142. func (e *IceRaft) Get(d *dto.IceRaftGetReq, iceRaftModel *model.IceRaft, p *actions.DataPermission) error {
  143. err := e.Orm.
  144. Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  145. First(iceRaftModel, d.GetId()).Error
  146. if err != nil {
  147. e.Log.Errorf("db error: %s", err)
  148. if errors.Is(err, gorm.ErrRecordNotFound) {
  149. return global.GetNotFoundOrNoPermissionErr
  150. }
  151. return global.GetFailedErr
  152. }
  153. return nil
  154. }
  155. func (e *IceRaft) GetByCode(d *dto.IceRaftGetByCodeReq, iceRaftModel *model.IceRaft, p *actions.DataPermission) error {
  156. err := e.Orm.
  157. Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  158. Where("code = ?", d.Code).
  159. First(iceRaftModel).Error
  160. if err != nil {
  161. e.Log.Errorf("db error: %s", err)
  162. if errors.Is(err, gorm.ErrRecordNotFound) {
  163. return errors.New(fmt.Sprintf("冰排编号【%s】不存在!", d.Code))
  164. }
  165. return global.GetFailedErr
  166. }
  167. return nil
  168. }
  169. // Insert 创建IceRaft对象
  170. func (e *IceRaft) Insert(c *dto.IceRaftInsertReq) error {
  171. var err error
  172. var data model.IceRaft
  173. tx := e.Orm.Begin()
  174. defer func() {
  175. if err != nil {
  176. tx.Rollback()
  177. } else {
  178. tx.Commit()
  179. }
  180. }()
  181. for _, code := range c.CodeList {
  182. var k int64
  183. err = tx.Model(&data).Where("code = ? and dept_id = ?", code, c.DeptId).Count(&k).Error
  184. if err != nil {
  185. e.Log.Errorf("db error: %s", err)
  186. return global.CreateFailedErr
  187. }
  188. // 冰排编号已存在
  189. if k > 0 {
  190. err = errors.New("该编号已存在!")
  191. return err
  192. }
  193. // 添加冰排
  194. data.Id = 0
  195. c.Generate(&data, code)
  196. err = tx.Create(&data).Error
  197. if err != nil {
  198. e.Log.Errorf("db error: %s", err)
  199. return global.CreateFailedErr
  200. }
  201. }
  202. return nil
  203. }
  204. // Update 修改IceRaft对象
  205. func (e *IceRaft) Update(c *dto.IceRaftUpdateReq, p *actions.DataPermission) error {
  206. var err error
  207. tx := e.Orm.Begin()
  208. defer func() {
  209. if err != nil {
  210. tx.Rollback()
  211. } else {
  212. tx.Commit()
  213. }
  214. }()
  215. var iceRaftModel = model.IceRaft{}
  216. // 查询冰排是否存在
  217. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  218. First(&iceRaftModel, c.GetId()).Error
  219. if err != nil {
  220. e.Log.Errorf("db error: %s", err)
  221. if errors.Is(err, gorm.ErrRecordNotFound) {
  222. return global.UpdateNotFoundOrNoPermissionErr
  223. }
  224. return global.UpdateFailedErr
  225. }
  226. if iceRaftModel.Code != c.Code {
  227. var k int64
  228. var data = model.IceRaft{}
  229. err = tx.Model(&data).Where("code = ? and dept_id = ?", c.Code, iceRaftModel.DeptId).Count(&k).Error
  230. if err != nil {
  231. e.Log.Errorf("db error: %s", err)
  232. return global.CreateFailedErr
  233. }
  234. if k > 0 {
  235. err = errors.New("冰排编号已存在!")
  236. e.Log.Errorf("db error: %s", err)
  237. return err
  238. }
  239. }
  240. c.Generate(&iceRaftModel)
  241. err = tx.Save(&iceRaftModel).Error
  242. if err != nil {
  243. e.Log.Errorf("db error: %s", err)
  244. return global.UpdateFailedErr
  245. }
  246. // 查询冰排记录是否存在
  247. var iceRaftRecordModel model.IceRaftRecord
  248. var count int64
  249. e.Orm.Table(iceRaftRecordModel.TableName()).Where("ice_raft_id = ?", c.Id).Count(&count)
  250. if count > 0 {
  251. iceRaftRecordModel.Id = iceRaftModel.IceRaftRecordId
  252. iceRaftRecordModel.FreezeClaim = iceRaftModel.FreezeClaim
  253. iceRaftRecordModel.Label = iceRaftModel.Label
  254. err = tx.Updates(&iceRaftRecordModel).Error
  255. if err != nil {
  256. e.Log.Errorf("db error: %s 该冰排未使用", err)
  257. return errors.New("修改冰排记录失败")
  258. }
  259. }
  260. c.Id = iceRaftModel.Id
  261. return nil
  262. }
  263. // Remove 删除IceRaft
  264. func (e *IceRaft) Remove(c *dto.IceRaftDeleteReq, p *actions.DataPermission) error {
  265. var err error
  266. tx := e.Orm.Begin()
  267. defer func() {
  268. if err != nil {
  269. tx.Rollback()
  270. } else {
  271. tx.Commit()
  272. }
  273. }()
  274. var iceRaftModel model.IceRaft
  275. // 查询冰排是否存在
  276. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  277. First(&iceRaftModel, c.GetId()).Error
  278. if err != nil {
  279. e.Log.Errorf("db error: %s", err)
  280. if errors.Is(err, gorm.ErrRecordNotFound) {
  281. return global.DeleteNotFoundOrNoPermissionErr
  282. }
  283. return global.DeleteFailedErr
  284. }
  285. db := tx.Delete(&iceRaftModel)
  286. if err = db.Error; err != nil {
  287. e.Log.Errorf("db error: %s", err)
  288. return global.DeleteFailedErr
  289. }
  290. if db.RowsAffected == 0 {
  291. return global.DeleteNotFoundOrNoPermissionErr
  292. }
  293. return nil
  294. }
  295. func (e *IceRaft) InStorage(c *dto.IceRaftInStorageReq, p *actions.DataPermission) error {
  296. var err error
  297. tx := e.Orm.Begin()
  298. defer func() {
  299. if err != nil {
  300. tx.Rollback()
  301. } else {
  302. tx.Commit()
  303. }
  304. }()
  305. // 查询冷冻柜信息
  306. var iceLockerModel model.IceLocker
  307. err = e.Orm.Scopes(actions.Permission(iceLockerModel.TableName(), p)).
  308. First(&iceLockerModel, c.IceLockerId).Error
  309. if err != nil {
  310. e.Log.Errorf("db error: %s", err)
  311. return errors.New("获取冷冻柜信息失败")
  312. }
  313. if iceLockerModel.Status != model.IceLockerStatusNormal {
  314. err = errors.New("冷冻柜状态异常")
  315. return err
  316. }
  317. for _, code := range c.Code {
  318. // 查询冰排是否存在
  319. var iceRaftModel model.IceRaft
  320. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  321. Where("code = ?", code).
  322. Preload("IceRaftRecord").
  323. First(&iceRaftModel).Error
  324. if err != nil {
  325. // 冰排编号不存在,添加
  326. //if errors.Is(err, gorm.ErrRecordNotFound) {
  327. // iceRaftModel.Code = code
  328. // iceRaftModel.Status = model.IceRaftStatusNormal
  329. // iceRaftModel.DeptId = p.DeptId
  330. // iceRaftModel.CreateBy = p.UserId
  331. // err = tx.Create(&iceRaftModel).Error
  332. // if err != nil {
  333. // e.Log.Errorf("db error: %s", err)
  334. // return errors.New(fmt.Sprintf("添加冰排【%s】信息失败", code))
  335. // }
  336. //} else {
  337. // e.Log.Errorf("db error: %s", err)
  338. // return errors.New(fmt.Sprintf("获取冰排【%s】信息失败",code))
  339. //}
  340. e.Log.Errorf("db error: %s", err)
  341. return errors.New(fmt.Sprintf("获取冰排【%s】信息失败", code))
  342. }
  343. if iceRaftModel.Status != model.IceRaftStatusNormal {
  344. err = errors.New(fmt.Sprintf("冰排【%s】状态异常", code))
  345. return err
  346. }
  347. if iceRaftModel.IceRaftRecord.Status == model.IceRaftRecordStatusFreezing || iceRaftModel.IceRaftRecord.Status == model.IceRaftRecordStatusWaitUse {
  348. err = errors.New(fmt.Sprintf("冰排【%s】状态为%s,入库失败!", code, model.GetIceRaftRecordStatus(iceRaftModel.IceRaftRecord.Status)))
  349. return err
  350. }
  351. // 更新冰排记录状态 使用中 -> 已完成
  352. err = tx.Model(&model.IceRaftRecord{}).
  353. Where("ice_raft_id = ? and status = ?", iceRaftModel.Id, model.IceRaftRecordStatusUsing).
  354. Updates(map[string]interface{}{
  355. "status": model.IceRaftRecordStatusFinish,
  356. }).Error
  357. // 添加冰排记录
  358. var iceRaftRecordModel = model.IceRaftRecord{
  359. IceLockerId: iceLockerModel.Id,
  360. IceRaftId: iceRaftModel.Id,
  361. Status: model.IceRaftRecordStatusFreezing,
  362. Label: iceRaftModel.Label,
  363. Code: code,
  364. InStorageTime: model2.Time(time.Now()),
  365. //FreezeClaim: iceRaftModel.FreezeClaim,
  366. DeptBy: model2.DeptBy{
  367. DeptId: p.DeptId,
  368. },
  369. }
  370. iceRaftRecordModel.FreezeClaim = append(iceRaftRecordModel.FreezeClaim, iceRaftModel.FreezeClaim...)
  371. err = tx.Create(&iceRaftRecordModel).Error
  372. if err != nil {
  373. e.Log.Errorf("db error: %s", err)
  374. return errors.New("添加冰排记录失败")
  375. }
  376. // 更新冰排使用记录
  377. err = tx.Model(&iceRaftModel).
  378. Where("id = ?", iceRaftModel.Id).
  379. Updates(map[string]interface{}{
  380. "ice_raft_record_id": iceRaftRecordModel.Id,
  381. }).Error
  382. if err != nil {
  383. e.Log.Errorf("db error: %s", err)
  384. return errors.New("关联冰排记录失败")
  385. }
  386. }
  387. return nil
  388. }
  389. func (e *IceRaft) OutStorage(c *dto.IceRaftOutStorageReq, p *actions.DataPermission) error {
  390. var err error
  391. tx := e.Orm.Begin()
  392. defer func() {
  393. if err != nil {
  394. tx.Rollback()
  395. } else {
  396. tx.Commit()
  397. }
  398. }()
  399. // 查询保温箱信息
  400. var coolerBoxModel model.CoolerBox
  401. err = e.Orm.Scopes(actions.Permission(coolerBoxModel.TableName(), p)).
  402. First(&coolerBoxModel, c.CoolerBoxId).Error
  403. if err != nil {
  404. e.Log.Errorf("db error: %s", err)
  405. return errors.New("获取保温箱信息失败")
  406. }
  407. if coolerBoxModel.Status != model.CoolerBoxStatusNormal {
  408. err = errors.New("保温箱状态异常")
  409. return err
  410. }
  411. var user model.SysUser
  412. first := e.Orm.Model(&model.SysUser{}).Where("id = ?", p.UserId).First(&user)
  413. if first.Error != nil {
  414. return global.GetFailedErr
  415. }
  416. for _, code := range c.Code {
  417. // 查询冰排是否存在
  418. var iceRaftModel model.IceRaft
  419. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  420. Where("code = ?", code).
  421. Preload("IceRaftRecord").
  422. First(&iceRaftModel).Error
  423. if err != nil {
  424. e.Log.Errorf("db error: %s", err)
  425. return errors.New("获取冰排信息失败")
  426. }
  427. if iceRaftModel.Status != model.IceRaftStatusNormal {
  428. err = errors.New(fmt.Sprintf("冰排【%s】状态异常", code))
  429. return err
  430. }
  431. // 获取冰排记录
  432. var iceRaftRecordModel model.IceRaftRecord
  433. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  434. Where("ice_raft_id = ?", iceRaftModel.Id).
  435. First(&iceRaftRecordModel, iceRaftModel.IceRaftRecordId).Error
  436. if err != nil {
  437. e.Log.Errorf("db error: %s", err)
  438. return errors.New("获取冰排记录失败,请确认冰排是否入库!")
  439. }
  440. if len(iceRaftRecordModel.OutStorageTime.String()) > 0 {
  441. err = errors.New(fmt.Sprintf("冰排【%s】已出库,禁止出库!", code))
  442. return err
  443. }
  444. inTime := iceRaftRecordModel.InStorageTime.Local()
  445. sort.Slice(iceRaftRecordModel.FreezeClaim, func(i, j int) bool {
  446. return iceRaftRecordModel.FreezeClaim[i] < iceRaftRecordModel.FreezeClaim[j]
  447. })
  448. err, sysUser := GetUserProfile(e.Orm, p.UserId)
  449. if err != nil {
  450. err = errors.New("获取用户信息失败")
  451. return err
  452. }
  453. if sysUser.Dept.IsOutStorage {
  454. for _, v := range iceRaftRecordModel.FreezeClaim {
  455. if inTime.Add(time.Hour * time.Duration(v)).After(time.Now()) {
  456. str := fmt.Sprintf("冰排【%s】未达到冷冻时长【%v】,禁止出库", code, v)
  457. err = errors.New(str)
  458. return err
  459. }
  460. }
  461. }
  462. //if inTime.Add(time.Hour * time.Duration(iceRaftRecordModel.FreezeClaim[0])).After(time.Now()) {
  463. // err = errors.New(fmt.Sprintf("冰排【%s】未达到冷冻时长,禁止出库", code))
  464. // return err
  465. //}
  466. iceRaftRecordModel.Status = model.IceRaftRecordStatusUsing
  467. iceRaftRecordModel.CoolerBoxId = coolerBoxModel.Id
  468. iceRaftRecordModel.OutStorageTime = model2.Time(time.Now())
  469. iceRaftRecordModel.IceUseUsers = user.NickName
  470. iceRaftRecordModel.IceUseTime = model2.Time(time.Now())
  471. //data, err := nats_server.Read_DeviceTask_List_By_Condition(coolerBoxModel.Sn)
  472. //if data.T_State != 1 {
  473. // e.Log.Errorf("该保温箱未启动: %s", err)
  474. // return errors.New("该保温箱未启动,请启动保温箱监控")
  475. //}
  476. //if err != nil {
  477. // e.Log.Errorf("获取设备任务列表失败: %s", err)
  478. // return errors.New("该保温箱未绑定平台")
  479. //}
  480. //iceRaftRecordModel.CoolerBoxStarTime = model2.Time(data.T_Ut_start)
  481. err = tx.Save(&iceRaftRecordModel).Error
  482. if err != nil {
  483. e.Log.Errorf("db error: %s", err)
  484. return errors.New("保存冰排记录失败")
  485. }
  486. }
  487. //添加保温箱记录
  488. var cooler model.CoolerBoxRecord
  489. cooler.CoolerBoxId = coolerBoxModel.Id
  490. cooler.CoolerUserTime = model2.Time(time.Now())
  491. cooler.Sn = coolerBoxModel.Sn
  492. cooler.Status = "1" //保温箱使用状态变为使用中
  493. cooler.DeptId = p.DeptId
  494. cooler.Name = coolerBoxModel.Name
  495. cooler.HistoryCode = append(cooler.HistoryCode, c.Code...)
  496. cooler.CoolerUseUsers = user.NickName
  497. //查询是否有正在使用中的保温箱记录
  498. var count int64
  499. err = e.Orm.Model(&model.CoolerBoxRecord{}).Where("id = ?", coolerBoxModel.Id).Where("status = ?", "1").Count(&count).Error
  500. if err != nil {
  501. e.Log.Errorf("获取保温箱信息失败: %s", err)
  502. return errors.New("获取保温箱信息失败")
  503. }
  504. //如果有正在使用中的保温箱记录,就更新
  505. if count > 0 {
  506. err = e.Orm.Updates(&cooler).Error
  507. if err != nil {
  508. e.Log.Errorf("db 更新保温箱记录失败: %s", err)
  509. return errors.New("更新保温箱记录失败")
  510. }
  511. } else {
  512. err = e.Orm.Create(&cooler).Error
  513. if err != nil {
  514. e.Log.Errorf("db 创建保温箱记录失败: %s", err)
  515. return errors.New("创建保温箱记录失败")
  516. }
  517. }
  518. return nil
  519. }
  520. // IsOutStorage 判断冰排是否达到冷冻时长
  521. func (e *IceRaft) IsOutStorage(c *dto.IceOutStorageReq, p *actions.DataPermission) (error, string) {
  522. var err error
  523. var str string
  524. // 获取冰排记录
  525. for _, v := range c.Code {
  526. var iceRaftRecordModel model.IceRaftRecord
  527. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  528. Where("code = ?", v).
  529. Where("status = ?", model.IceRaftRecordStatusFreezing).Or("status = ?", model.IceRaftRecordStatusWaitUse).
  530. First(&iceRaftRecordModel).Error
  531. if err != nil {
  532. e.Log.Errorf("db error: %s", err)
  533. return errors.New("获取冰排记录失败,请确认冰排是否入库!"), ""
  534. }
  535. if len(iceRaftRecordModel.OutStorageTime.String()) > 0 {
  536. err = errors.New(fmt.Sprintf("冰排【%s】已出库,禁止出库!", v))
  537. return err, ""
  538. }
  539. inTime := iceRaftRecordModel.InStorageTime.Local()
  540. sort.Slice(iceRaftRecordModel.FreezeClaim, func(i, j int) bool {
  541. return iceRaftRecordModel.FreezeClaim[i] < iceRaftRecordModel.FreezeClaim[j]
  542. })
  543. for _, vs := range iceRaftRecordModel.FreezeClaim {
  544. if inTime.Add(time.Hour * time.Duration(vs)).After(time.Now()) {
  545. str = fmt.Sprintf("冰排编号【%s】未达到冷冻时长【%v】小时,已达到冷冻时长【%v】,是否继续出库", v, vs, time.Now().Sub(inTime).Hours())
  546. break
  547. }
  548. }
  549. }
  550. return nil, str
  551. }
  552. // 查询保温箱关联的冰排
  553. func (e *IceRaft) GetPageByCoolerBoxId(coolerBoxId int, list *[]string, p *actions.DataPermission) error {
  554. var data model.IceRaft
  555. err := e.Orm.Model(&data).
  556. Select("ice_raft.code").
  557. Scopes(
  558. actions.Permission(data.TableName(), p),
  559. IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
  560. IceRaftNameRecordCoolerBoxIdScopes(coolerBoxId),
  561. ).
  562. Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
  563. Find(list).Error
  564. if err != nil {
  565. e.Log.Errorf("db error: %s", err)
  566. return global.GetFailedErr
  567. }
  568. return nil
  569. }
  570. type IceRaftRecord struct {
  571. service.Service
  572. }
  573. // GetPage 获取IceRaft列表
  574. func (e *IceRaftRecord) GetPage(c *dto.IceRaftRecordGetPageReq, list *[]model.IceRaftRecord, count *int64, p *actions.DataPermission) error {
  575. var err error
  576. var data model.IceRaftRecord
  577. if c.PageSize == 9999 {
  578. err = e.Orm.Model(&data).
  579. Scopes(
  580. actions.Permission(data.TableName(), p),
  581. ).
  582. Preload("IceLocker").
  583. Preload("CoolerBox").
  584. Find(list).Limit(-1).Offset(-1).
  585. Count(count).Error
  586. } else {
  587. err = e.Orm.Model(&data).
  588. Scopes(
  589. cDto.MakeCondition(c.GetNeedSearch()),
  590. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  591. actions.Permission(data.TableName(), p),
  592. ).
  593. Preload("IceLocker").
  594. Preload("CoolerBox").
  595. Find(list).Limit(-1).Offset(-1).
  596. Count(count).Error
  597. }
  598. if err != nil {
  599. e.Log.Errorf("db error: %s", err)
  600. return global.GetFailedErr
  601. }
  602. return nil
  603. }
  604. // Update 修改IceRaft对象
  605. func (e *IceRaftRecord) Update(c *dto.IceRaftRecordUpdateReq, p *actions.DataPermission) error {
  606. var err error
  607. tx := e.Orm.Begin()
  608. defer func() {
  609. if err != nil {
  610. tx.Rollback()
  611. } else {
  612. tx.Commit()
  613. }
  614. }()
  615. var iceRaftRecordModel = model.IceRaftRecord{}
  616. // 查询冰排是否存在
  617. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  618. First(&iceRaftRecordModel, c.GetId()).Error
  619. if err != nil {
  620. e.Log.Errorf("db error: %s", err)
  621. if errors.Is(err, gorm.ErrRecordNotFound) {
  622. return global.UpdateNotFoundOrNoPermissionErr
  623. }
  624. return global.UpdateFailedErr
  625. }
  626. iceRaftRecordModel.IceLockerId = c.IceLockerId
  627. iceRaftRecordModel.FreezeClaim = c.FreezeClaim
  628. if len(iceRaftRecordModel.OutStorageTime.String()) > 0 {
  629. iceRaftRecordModel.CoolerBoxId = c.CoolerBoxId
  630. }
  631. err = tx.Save(&iceRaftRecordModel).Error
  632. if err != nil {
  633. e.Log.Errorf("db error: %s", err)
  634. return global.UpdateFailedErr
  635. }
  636. c.Id = iceRaftRecordModel.Id
  637. return nil
  638. }
  639. // Remove 删除IceRaft
  640. func (e *IceRaftRecord) Remove(c *dto.IceRaftRecordDeleteReq, p *actions.DataPermission) error {
  641. var err error
  642. tx := e.Orm.Begin()
  643. defer func() {
  644. if err != nil {
  645. tx.Rollback()
  646. } else {
  647. tx.Commit()
  648. }
  649. }()
  650. var iceRaftRecordModel model.IceRaftRecord
  651. // 查询冰排是否存在
  652. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  653. First(&iceRaftRecordModel, c.GetId()).Error
  654. if err != nil {
  655. e.Log.Errorf("db error: %s", err)
  656. if errors.Is(err, gorm.ErrRecordNotFound) {
  657. return global.DeleteNotFoundOrNoPermissionErr
  658. }
  659. return global.DeleteFailedErr
  660. }
  661. db := tx.Delete(&iceRaftRecordModel)
  662. if err = db.Error; err != nil {
  663. e.Log.Errorf("db error: %s", err)
  664. return global.DeleteFailedErr
  665. }
  666. if db.RowsAffected == 0 {
  667. return global.DeleteNotFoundOrNoPermissionErr
  668. }
  669. // 更改冰排关联状态
  670. var iceRaftModel model.IceRaft
  671. var secondIceRaftRecordModel model.IceRaftRecord
  672. err = e.Orm.Scopes(actions.Permission(iceRaftModel.TableName(), p)).
  673. First(&iceRaftModel, iceRaftRecordModel.IceRaftId).Error
  674. if err != nil {
  675. e.Log.Errorf("db error: %s", err)
  676. return global.DeleteFailedErr
  677. }
  678. err = e.Orm.Scopes(actions.Permission(secondIceRaftRecordModel.TableName(), p)).
  679. Where("ice_raft_id = ? and id != ?", iceRaftRecordModel.IceRaftId, iceRaftRecordModel.Id).
  680. First(&secondIceRaftRecordModel).Error
  681. if err != nil {
  682. if errors.Is(err, gorm.ErrRecordNotFound) {
  683. iceRaftModel.IceRaftRecordId = 0
  684. }
  685. }
  686. iceRaftModel.IceRaftRecordId = secondIceRaftRecordModel.Id
  687. err = tx.Save(&iceRaftModel).Error
  688. if err != nil {
  689. e.Log.Errorf("db error: %s", err)
  690. return global.UpdateFailedErr
  691. }
  692. return nil
  693. }
  694. func (e *IceRaftRecord) EndForCold(c *dto.IceRaftRecordEedReq, p *actions.DataPermission) error {
  695. var err error
  696. tx := e.Orm.Begin()
  697. defer func() {
  698. if err != nil {
  699. tx.Rollback()
  700. } else {
  701. tx.Commit()
  702. }
  703. }()
  704. // 查询冰排是否存在
  705. for _, v := range c.Id {
  706. var iceRaftRecordModel model.IceRaftRecord
  707. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  708. First(&iceRaftRecordModel, v).Error
  709. if err != nil {
  710. e.Log.Errorf("db error: %s", err)
  711. if errors.Is(err, gorm.ErrRecordNotFound) {
  712. return global.UpdateNotFoundOrNoPermissionErr
  713. }
  714. return global.UpdateFailedErr
  715. }
  716. iceRaftRecordModel.IsSuitableForCold = 1 // 是否适冷
  717. iceRaftRecordModel.EndForColdTime = model2.Time(time.Now())
  718. iceRaftRecordModel.Id = v
  719. updates := tx.Where("id = ?", iceRaftRecordModel.Id).Updates(&iceRaftRecordModel)
  720. if err = updates.Error; err != nil {
  721. e.Log.Errorf("db error: %s", err)
  722. return global.UpdateFailedErr
  723. }
  724. }
  725. return nil
  726. }
  727. // StartForCold 冰排开始释冷
  728. func (e *IceRaftRecord) StartForCold(c *dto.IceStartbleForColfTimReq, p *actions.DataPermission) error {
  729. var err error
  730. tx := e.Orm.Begin()
  731. defer func() {
  732. if err != nil {
  733. tx.Rollback()
  734. } else {
  735. tx.Commit()
  736. }
  737. }()
  738. var user model.SysUser
  739. first := e.Orm.Model(&model.SysUser{}).Where("id = ?", p.UserId).First(&user)
  740. if first.Error != nil {
  741. return global.GetFailedErr
  742. }
  743. //查询冰排是否存在
  744. for _, v := range c.Id {
  745. var iceRaftRecordModel model.IceRaftRecord
  746. var iceRaft model.IceRaft
  747. err = e.Orm.Scopes(actions.Permission(iceRaftRecordModel.TableName(), p)).
  748. First(&iceRaftRecordModel, v).Error
  749. if err != nil {
  750. e.Log.Errorf("db error: %s", err)
  751. if errors.Is(err, gorm.ErrRecordNotFound) {
  752. return global.UpdateNotFoundOrNoPermissionErr
  753. }
  754. return global.UpdateFailedErr
  755. }
  756. err = e.Orm.Model(&iceRaft).Where("id = ?", iceRaftRecordModel.IceRaftId).Error
  757. if err != nil {
  758. return global.GetFailedErr
  759. }
  760. iceRaftRecordModel.StartIceColdTime = model2.Time(time.Now())
  761. iceRaftRecordModel.Status = model.IceRaftRecordReleasedInTheCold //释冷中
  762. iceRaftRecordModel.IceColdUsers = user.NickName
  763. iceRaftRecordModel.IceColdAddress = iceRaft.IceColdAddress
  764. iceRaftRecordModel.SuitableForCold = iceRaft.SuitableForCold
  765. updates := tx.Where("id = ?", iceRaftRecordModel.Id).Updates(&iceRaftRecordModel)
  766. if err = updates.Error; err != nil {
  767. e.Log.Errorf("db error: %s", err)
  768. return global.UpdateFailedErr
  769. }
  770. }
  771. return nil
  772. }
  773. // IceRaftRecordRecording 获取冰排的历史记录
  774. func (e *IceRaft) IceRaftRecordRecording(c *dto.IceRaftRecordRecording, list *[]model.IceRaftRecord, count *int64, p *actions.DataPermission) error {
  775. var err error
  776. var data model.IceRaftRecord
  777. if c.PageSize == 9999 {
  778. err = e.Orm.Model(&data).
  779. Scopes(
  780. cDto.MakeCondition(c.GetNeedSearch()),
  781. actions.Permission(data.TableName(), p),
  782. IceRaftStartTimeAndEndTimeScopes(c.StartTime, c.EndTime),
  783. ).
  784. Where("status =?", model.IceRaftRecordStatusFinish).
  785. Preload("IceLocker").
  786. Preload("CoolerBox").
  787. Find(list).Limit(-1).Offset(-1).
  788. Count(count).Error
  789. } else {
  790. err = e.Orm.Model(&data).
  791. Scopes(
  792. cDto.MakeCondition(c.GetNeedSearch()),
  793. cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
  794. actions.Permission(data.TableName(), p),
  795. IceRaftStartTimeAndEndTimeScopes(c.StartTime, c.EndTime),
  796. ).
  797. Where("status =?", model.IceRaftRecordStatusFinish).
  798. Preload("IceLocker").
  799. Preload("CoolerBox").
  800. Find(list).Limit(-1).Offset(-1).
  801. Count(count).Error
  802. }
  803. if err != nil {
  804. e.Log.Errorf("db error: %s", err)
  805. return global.GetFailedErr
  806. }
  807. return nil
  808. }