|
@@ -12,6 +12,7 @@ import (
|
|
|
"gogs.baozhida.cn/zoie/OAuth-core/service"
|
|
|
"gorm.io/gorm"
|
|
|
"gorm.io/gorm/utils"
|
|
|
+ "strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
@@ -19,6 +20,25 @@ type GasCylinderStatus struct {
|
|
|
service.Service
|
|
|
}
|
|
|
|
|
|
+func GasCylinderStatusScopes(status string) func(db *gorm.DB) *gorm.DB {
|
|
|
+
|
|
|
+ return func(db *gorm.DB) *gorm.DB {
|
|
|
+ if len(status) == 0 {
|
|
|
+ return db
|
|
|
+ }
|
|
|
+ if status == model.GasCylinderStatusUnqualified {
|
|
|
+ return db.Where("status = ? or status = ?", model.GasCylinderStatusScrap, model.GasCylinderStatusExtended)
|
|
|
+ }
|
|
|
+ if strings.Contains(status, model.GasCylinderStatusScrap) {
|
|
|
+ return db.Where("status = ?", model.GasCylinderStatusScrap)
|
|
|
+ }
|
|
|
+ if strings.Contains(status, model.GasCylinderStatusExtended) {
|
|
|
+ return db.Where("status = ?", model.GasCylinderStatusExtended)
|
|
|
+ }
|
|
|
+ return db.Where("status = ? ", status)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// GetPage 获取GasCylinderStatus列表
|
|
|
func (e *GasCylinderStatus) GetPage(c *dto.GasCylinderStatusGetPageReq, list *[]model.GasCylinderStatus, count *int64, p *actions.DataPermission) error {
|
|
|
var err error
|
|
@@ -29,6 +49,7 @@ func (e *GasCylinderStatus) GetPage(c *dto.GasCylinderStatusGetPageReq, list *[]
|
|
|
cDto.MakeCondition(c.GetNeedSearch()),
|
|
|
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
|
|
//actions.Permission(data.TableName(), p),
|
|
|
+ GasCylinderStatusScopes(c.Status),
|
|
|
).
|
|
|
Where("company_id = ? AND user_id = 0", p.DeptId).
|
|
|
Find(list).Limit(-1).Offset(-1).
|
|
@@ -50,6 +71,7 @@ func (e *GasCylinderStatus) GetUserPage(c *dto.GasCylinderStatusGetPageReq, list
|
|
|
cDto.MakeCondition(c.GetNeedSearch()),
|
|
|
cDto.Paginate(c.GetPageSize(), c.GetPageIndex()),
|
|
|
//actions.Permission(data.TableName(), p),
|
|
|
+ GasCylinderStatusScopes(c.Status),
|
|
|
).
|
|
|
Where("user_id = ?", p.UserId).
|
|
|
Find(list).Limit(-1).Offset(-1).
|
|
@@ -62,10 +84,10 @@ func (e *GasCylinderStatus) GetUserPage(c *dto.GasCylinderStatusGetPageReq, list
|
|
|
}
|
|
|
|
|
|
// Get 获取GasCylinderStatus对象
|
|
|
-func (e *GasCylinderStatus) Get(d *dto.GasCylinderStatusGetReq, carInfoModel *model.GasCylinderStatus, p *actions.DataPermission) error {
|
|
|
+func (e *GasCylinderStatus) Get(d *dto.GasCylinderStatusGetReq, gasCylinderStatusModel *model.GasCylinderStatus, p *actions.DataPermission) error {
|
|
|
err := e.Orm.
|
|
|
- Scopes(actions.Permission(carInfoModel.TableName(), p)).
|
|
|
- First(carInfoModel, d.GetId()).Error
|
|
|
+ Scopes(actions.Permission(gasCylinderStatusModel.TableName(), p)).
|
|
|
+ First(gasCylinderStatusModel, d.GetId()).Error
|
|
|
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
@@ -91,12 +113,29 @@ func (e *GasCylinderStatus) Insert(c *dto.GasCylinderStatusInsertReq, p *actions
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
+ inDeptIdScopes := InRootCompanyIdsScopes(p.DeptId)
|
|
|
for _, innerCode := range c.InnerCodeList {
|
|
|
+ // 查询钢瓶档案
|
|
|
+ var gasCylinder = model.GasCylinder{}
|
|
|
+ // 查询订单是否存在
|
|
|
+ err = e.Orm.
|
|
|
+ Scopes(inDeptIdScopes).
|
|
|
+ Where("inner_code = ?", innerCode).
|
|
|
+ First(&gasCylinder).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return errors.New(fmt.Sprintf("钢瓶%s不存在,请先在钢瓶档案添加!", innerCode))
|
|
|
+ }
|
|
|
+ return errors.New(fmt.Sprintf("查询钢瓶%s失败,请检查!", innerCode))
|
|
|
+ }
|
|
|
+
|
|
|
var data model.GasCylinderStatus
|
|
|
|
|
|
err = e.Orm.
|
|
|
Where("inner_code = ? ", innerCode).
|
|
|
Preload("Company").
|
|
|
+ Preload("User").
|
|
|
First(&data).Error
|
|
|
|
|
|
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
@@ -104,16 +143,17 @@ func (e *GasCylinderStatus) Insert(c *dto.GasCylinderStatusInsertReq, p *actions
|
|
|
return global.GetFailedErr
|
|
|
}
|
|
|
if data.Id > 0 {
|
|
|
+ if data.UserId > 0 && data.UserId != p.UserId {
|
|
|
+ err = errors.New(fmt.Sprintf("钢瓶%s被【%s】关联,请检查!", innerCode, data.User.NickName))
|
|
|
+ return err
|
|
|
+ }
|
|
|
if data.CompanyId == p.DeptId {
|
|
|
if data.Status != c.Status {
|
|
|
- data.Status = c.Status
|
|
|
- err = tx.Save(&data).Error
|
|
|
- if err != nil {
|
|
|
- e.Log.Errorf("db error: %s", err)
|
|
|
- return global.CreateFailedErr
|
|
|
+ if data.Status != c.Status {
|
|
|
+ err = errors.New(fmt.Sprintf("钢瓶%s状态为【%s】,请检查!", innerCode, model.GasCylinderStatusState2Map[data.Status]))
|
|
|
+ return err
|
|
|
}
|
|
|
}
|
|
|
- continue
|
|
|
} else {
|
|
|
err = errors.New(fmt.Sprintf("钢瓶%s被【%s】关联,请检查!", innerCode, data.Company.Name))
|
|
|
return err
|
|
@@ -146,7 +186,23 @@ func (e *GasCylinderStatus) UserInsert(c *dto.GasCylinderStatusInsertReq, p *act
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
+ inDeptIdScopes := InRootCompanyIdsScopes(p.DeptId)
|
|
|
for _, innerCode := range c.InnerCodeList {
|
|
|
+ // 查询钢瓶档案
|
|
|
+ var gasCylinder = model.GasCylinder{}
|
|
|
+ // 查询订单是否存在
|
|
|
+ err = e.Orm.
|
|
|
+ Scopes(inDeptIdScopes).
|
|
|
+ Where("inner_code = ?", innerCode).
|
|
|
+ First(&gasCylinder).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ return errors.New(fmt.Sprintf("钢瓶%s不存在,请先在钢瓶档案添加!", innerCode))
|
|
|
+ }
|
|
|
+ return errors.New(fmt.Sprintf("查询钢瓶%s失败,请检查!", innerCode))
|
|
|
+ }
|
|
|
+
|
|
|
var data model.GasCylinderStatus
|
|
|
|
|
|
err = e.Orm.
|
|
@@ -162,16 +218,11 @@ func (e *GasCylinderStatus) UserInsert(c *dto.GasCylinderStatusInsertReq, p *act
|
|
|
if data.Id > 0 {
|
|
|
if data.UserId == p.UserId {
|
|
|
if data.Status != c.Status {
|
|
|
- data.Status = c.Status
|
|
|
- err = tx.Save(&data).Error
|
|
|
- if err != nil {
|
|
|
- e.Log.Errorf("db error: %s", err)
|
|
|
- return global.CreateFailedErr
|
|
|
- }
|
|
|
+ err = errors.New(fmt.Sprintf("钢瓶%s状态为【%s】,请检查!", innerCode, model.GasCylinderStatusState2Map[data.Status]))
|
|
|
+ return err
|
|
|
}
|
|
|
- continue
|
|
|
} else {
|
|
|
- err = errors.New(fmt.Sprintf("钢瓶被【%s】%s关联,请检查!", data.Company.Name, data.User.NickName))
|
|
|
+ err = errors.New(fmt.Sprintf("钢瓶%s被【%s】%s关联,请检查!", innerCode, data.Company.Name, data.User.NickName))
|
|
|
return err
|
|
|
}
|
|
|
|
|
@@ -187,7 +238,7 @@ func (e *GasCylinderStatus) UserInsert(c *dto.GasCylinderStatusInsertReq, p *act
|
|
|
return global.CreateFailedErr
|
|
|
}
|
|
|
|
|
|
- if isorders && c.Status == 2 {
|
|
|
+ if isorders && c.Status == model.GasCylinderStatusWeighty {
|
|
|
// 27 送气员回收空瓶
|
|
|
log := make([]model.OperationLog, 0)
|
|
|
var user model.SysUser
|
|
@@ -265,11 +316,11 @@ func (e *GasCylinderStatus) Remove(c *dto.GasCylinderStatusDeleteReq, p *actions
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- var carInfoModel model.GasCylinderStatus
|
|
|
+ var gasCylinderStatusModel model.GasCylinderStatus
|
|
|
|
|
|
// 查询钢瓶是否存在
|
|
|
- err = e.Orm.Scopes(actions.Permission(carInfoModel.TableName(), p)).
|
|
|
- First(&carInfoModel, c.GetId()).Error
|
|
|
+ err = e.Orm.Scopes(actions.Permission(gasCylinderStatusModel.TableName(), p)).
|
|
|
+ First(&gasCylinderStatusModel, c.GetId()).Error
|
|
|
if err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
@@ -278,7 +329,7 @@ func (e *GasCylinderStatus) Remove(c *dto.GasCylinderStatusDeleteReq, p *actions
|
|
|
return global.DeleteFailedErr
|
|
|
}
|
|
|
|
|
|
- db := tx.Delete(&carInfoModel)
|
|
|
+ db := tx.Delete(&gasCylinderStatusModel)
|
|
|
|
|
|
if err = db.Error; err != nil {
|
|
|
e.Log.Errorf("db error: %s", err)
|
|
@@ -290,3 +341,41 @@ func (e *GasCylinderStatus) Remove(c *dto.GasCylinderStatusDeleteReq, p *actions
|
|
|
|
|
|
return nil
|
|
|
}
|
|
|
+
|
|
|
+// Update 创建GasCylinderStatus对象
|
|
|
+func (e *GasCylinderStatus) Update(c *dto.GasCylinderStatusUpdateReq, p *actions.DataPermission) error {
|
|
|
+ var err error
|
|
|
+
|
|
|
+ tx := e.Orm.Begin()
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ tx.Rollback()
|
|
|
+ } else {
|
|
|
+ tx.Commit()
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ for _, innerCode := range c.InnerCodeList {
|
|
|
+ var data model.GasCylinderStatus
|
|
|
+
|
|
|
+ err = e.Orm.
|
|
|
+ Where("inner_code = ? ", innerCode).
|
|
|
+ First(&data).Error
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return global.GetFailedErr
|
|
|
+ }
|
|
|
+
|
|
|
+ if data.Status != c.Status {
|
|
|
+ data.Status = c.Status
|
|
|
+ data.UpdateBy = c.UpdateBy
|
|
|
+ err = tx.Save(&data).Error
|
|
|
+ if err != nil {
|
|
|
+ e.Log.Errorf("db error: %s", err)
|
|
|
+ return global.CreateFailedErr
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|