120 lines
3.5 KiB
Go
120 lines
3.5 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"management/internal/erpserver/model/dto"
|
|
"management/internal/erpserver/model/system"
|
|
"management/internal/erpserver/repository"
|
|
)
|
|
|
|
type departmentRepository struct {
|
|
repo *repository.Repository
|
|
}
|
|
|
|
func NewDepartmentRepository(repo *repository.Repository) system.DepartmentRepository {
|
|
return &departmentRepository{
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (r *departmentRepository) Initialize(ctx context.Context) error {
|
|
var count int64
|
|
if err := r.repo.DB(ctx).Model(&system.Department{}).Count(&count).Error; err != nil {
|
|
return err
|
|
}
|
|
if count == 0 {
|
|
obj := system.Department{
|
|
Name: "公司",
|
|
ParentID: 0,
|
|
ParentPath: ",0,",
|
|
Status: 0,
|
|
Sort: 6666,
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
}
|
|
return r.Create(ctx, &obj)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *departmentRepository) Create(ctx context.Context, obj *system.Department) error {
|
|
return r.repo.DB(ctx).Create(obj).Error
|
|
}
|
|
|
|
func (r *departmentRepository) Update(ctx context.Context, obj *system.Department) error {
|
|
return r.repo.DB(ctx).Save(obj).Error
|
|
}
|
|
|
|
func (r *departmentRepository) Get(ctx context.Context, id int32) (*system.Department, error) {
|
|
var obj system.Department
|
|
err := r.repo.DB(ctx).First(&obj, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &obj, nil
|
|
}
|
|
|
|
func (r *departmentRepository) All(ctx context.Context) ([]*system.Department, error) {
|
|
var departs []*system.Department
|
|
err := r.repo.DB(ctx).Find(&departs).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return departs, nil
|
|
}
|
|
|
|
func (r *departmentRepository) List(ctx context.Context, q dto.SearchDto) ([]*system.Department, int64, error) {
|
|
query := r.repo.DB(ctx).
|
|
Model(&system.Department{}).
|
|
Where("created_at BETWEEN ? AND ?", q.SearchTimeBegin, q.SearchTimeEnd)
|
|
if q.SearchID != 0 {
|
|
query = query.Where("id = ?", q.SearchID)
|
|
}
|
|
if q.SearchParentID != 0 && q.SearchParentID != 1 {
|
|
query = query.Where("parent_id = ?", q.SearchParentID)
|
|
}
|
|
if q.SearchName != "" {
|
|
query = query.Where("name LIKE ?", "%"+q.SearchName+"%")
|
|
}
|
|
if q.SearchStatus != 9999 {
|
|
query = query.Where("status = ?", q.SearchStatus)
|
|
}
|
|
|
|
var count int64
|
|
err := query.Count(&count).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
var departs []*system.Department
|
|
err = query.
|
|
Order("id DESC").
|
|
Offset((q.Page - 1) * q.Rows).
|
|
Limit(q.Rows).
|
|
Find(&departs).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return departs, count, nil
|
|
}
|
|
|
|
func (r *departmentRepository) RebuildParentPath(ctx context.Context) error {
|
|
query := `UPDATE sys_department AS tm
|
|
SET parent_path = (SELECT ',' || string_agg(cast(t.parent_id AS VARCHAR), ',') || ','
|
|
FROM (WITH RECURSIVE temp (id, parent_id) AS (SELECT id, tm.parent_id
|
|
FROM sys_department
|
|
WHERE id = tm.id
|
|
UNION ALL
|
|
SELECT sys_department.id, sys_department.parent_id
|
|
FROM sys_department,
|
|
temp
|
|
WHERE sys_department.id = temp.parent_id)
|
|
SELECT id, parent_id
|
|
FROM temp
|
|
ORDER BY id) AS t)
|
|
WHERE tm.status = 0;`
|
|
return r.repo.DB(ctx).Exec(query).Error
|
|
}
|