123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- package service
- import (
- "cold-delivery/app/admin/model"
- "cold-delivery/app/admin/service/dto"
- "cold-delivery/common/actions"
- cDto "cold-delivery/common/dto"
- "cold-delivery/common/global"
- "cold-delivery/common/nats/nats_server"
- "errors"
- "gogs.baozhida.cn/zoie/OAuth-core/service"
- "gorm.io/gorm"
- )
- type CoolerBox struct {
- service.Service
- }
- // GetPage 获取CoolerBox列表
- func (e *CoolerBox) GetPage(c *dto.CoolerBoxGetPageReq, list *[]model.CoolerBox, count *int64, p *actions.DataPermission) error {
- var err error
- var data model.CoolerBox
- if c.PageSize == 9999 {
- err = e.Orm.Model(&data).
- Scopes(
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- } else {
- err = e.Orm.Model(&data).
- Scopes(
- cDto.MakeCondition(c.GetNeedSearch()),
- cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
- actions.Permission(data.TableName(), p),
- ).
- Find(list).Limit(-1).Offset(-1).
- Count(count).Error
- }
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.GetFailedErr
- }
- if c.ShowTemp {
- // 获取公司秘钥
- var company model.SysDept
- company, err = model.GetCompanyById(p.DeptId)
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return model.GetCompanyKeyErr
- }
- for i := 0; i < len(*list); i++ {
- deviceData, _, _ := nats_server.Cold_CompanyDeviceSensor_List_ByKey((*list)[i].Sn, company.ColdKey)
- if len(deviceData) > 0 {
- (*list)[i].DeviceData = deviceData[0].T_DeviceSensorData
- }
- }
- }
- return nil
- }
- // Get 获取CoolerBox对象
- func (e *CoolerBox) Get(d *dto.CoolerBoxGetReq, CoolerBoxModel *model.CoolerBox, p *actions.DataPermission) error {
- err := e.Orm.
- Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
- First(CoolerBoxModel, d.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.GetNotFoundOrNoPermissionErr
- }
- return global.GetFailedErr
- }
- return nil
- }
- // Insert 创建CoolerBox对象
- func (e *CoolerBox) Insert(c *dto.CoolerBoxInsertReq, p *actions.DataPermission) error {
- var err error
- var data model.CoolerBox
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var k int64
- err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- err = errors.New("该Sn已绑定!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- //var device nats_server.Device
- //var company model.SysDept
- //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
- //if err != nil {
- // err = errors.New("获取SN信息失败,请检查SN是否正确!")
- // e.Log.Errorf("db error: %s", err)
- // return err
- //}
- //company, err = model.GetCompanyById(p.DeptId)
- //if err != nil {
- // e.Log.Errorf("db error: %s", err)
- // return err
- //}
- //if device.T_pid != company.Id {
- // err = errors.New("获取SN信息失败,请检查SN是否正确!")
- // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
- // return err
- //}
- // 添加保温箱
- c.Generate(&data)
- data.HistorySn = []string{data.Sn}
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- c.Id = data.Id
- return nil
- }
- func (e *CoolerBox) BatchInsert(c *dto.CoolerBoxBatchInsertReq) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- for _, coolerBox := range c.List {
- var data model.CoolerBox
- var k int64
- err = tx.Model(&data).Where("sn = ?", coolerBox.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- //err = errors.New("该Sn已绑定!")
- continue
- }
- // 添加保温箱
- coolerBox.CreateBy = c.CreateBy
- coolerBox.DeptId = c.DeptId
- coolerBox.Status = c.Status
- coolerBox.Generate(&data)
- data.HistorySn = []string{data.Sn}
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- }
- return nil
- }
- // Update 修改CoolerBox对象
- func (e *CoolerBox) Update(c *dto.CoolerBoxUpdateReq, p *actions.DataPermission) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var CoolerBoxModel = model.CoolerBox{}
- // 查询保温箱是否存在
- err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
- First(&CoolerBoxModel, c.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.UpdateNotFoundOrNoPermissionErr
- }
- return global.UpdateFailedErr
- }
- if CoolerBoxModel.Sn != c.Sn && len(c.Sn) > 0 {
- var k int64
- var data = model.CoolerBox{}
- err = tx.Model(&data).Where("sn = ?", c.Sn).Count(&k).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.CreateFailedErr
- }
- if k > 0 {
- err = errors.New("该Sn已绑定!")
- e.Log.Errorf("db error: %s", err)
- return err
- }
- //var device nats_server.Device
- //var company model.SysDept
- //device, err = nats_server.Cold_ReadDeviceByT_sn(c.Sn)
- //if err != nil {
- // err = errors.New("获取SN信息失败,请检查SN是否正确!")
- // e.Log.Errorf("db error: %s", err)
- // return err
- //}
- //company, err = model.GetCompanyById(p.DeptId)
- //if err != nil {
- // e.Log.Errorf("db error: %s", err)
- // return err
- //}
- //if device.T_pid != company.Id {
- // err = errors.New("获取SN信息失败,请检查SN是否正确!")
- // e.Log.Errorf("获取SN信息失败,device.T_pid != company.Id")
- // return err
- //}
- CoolerBoxModel.HistorySn = append(CoolerBoxModel.HistorySn, c.Sn)
- }
- c.Generate(&CoolerBoxModel)
- err = tx.Save(&CoolerBoxModel).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.UpdateFailedErr
- }
- c.Id = CoolerBoxModel.Id
- return nil
- }
- // Remove 删除CoolerBox
- func (e *CoolerBox) Remove(c *dto.CoolerBoxDeleteReq, p *actions.DataPermission) error {
- var err error
- tx := e.Orm.Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- var iceRaftModel model.IceRaft
- var count int64
- err = e.Orm.Model(&iceRaftModel).
- Scopes(
- actions.Permission(iceRaftModel.TableName(), p),
- IceRaftNameRecordStatusScopes(model.IceRaftRecordStatusUsing),
- IceRaftNameRecordCoolerBoxIdScopes(c.Id),
- ).
- Where("ice_raft.status = '2'").
- Joins("left join ice_raft_record on ice_raft.ice_raft_record_id = ice_raft_record.id").
- Count(&count).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.DeleteFailedErr
- }
- if count > 0 {
- err = errors.New("该保温箱下有冰排正在使用中,无法删除!")
- return err
- }
- var CoolerBoxModel model.CoolerBox
- // 查询保温箱是否存在
- err = e.Orm.Scopes(actions.Permission(CoolerBoxModel.TableName(), p)).
- First(&CoolerBoxModel, c.GetId()).Error
- if err != nil {
- e.Log.Errorf("db error: %s", err)
- if errors.Is(err, gorm.ErrRecordNotFound) {
- return global.DeleteNotFoundOrNoPermissionErr
- }
- return global.DeleteFailedErr
- }
- db := tx.Delete(&CoolerBoxModel)
- if err = db.Error; err != nil {
- e.Log.Errorf("db error: %s", err)
- return global.DeleteFailedErr
- }
- if db.RowsAffected == 0 {
- return global.DeleteNotFoundOrNoPermissionErr
- }
- return nil
- }
|