github.com/easysoft/zendata@v0.0.0-20240513203326-705bd5a7fd67/internal/server/service/excel.go (about)

     1  package serverService
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/easysoft/zendata/internal/pkg/domain"
     7  
     8  	consts "github.com/easysoft/zendata/internal/pkg/const"
     9  	"github.com/easysoft/zendata/internal/pkg/helper"
    10  	"github.com/easysoft/zendata/internal/pkg/model"
    11  	serverRepo "github.com/easysoft/zendata/internal/server/repo"
    12  	serverUtils "github.com/easysoft/zendata/internal/server/utils"
    13  	fileUtils "github.com/easysoft/zendata/pkg/utils/file"
    14  	"github.com/easysoft/zendata/pkg/utils/vari"
    15  	"gorm.io/gorm"
    16  )
    17  
    18  type ExcelService struct {
    19  	ExcelRepo  *serverRepo.ExcelRepo `inject:""`
    20  	ResService *ResService           `inject:""`
    21  }
    22  
    23  func (s *ExcelService) List(keywords string, page int) (list []*model.ZdExcel, total int) {
    24  	list, total, _ = s.ExcelRepo.List(strings.TrimSpace(keywords), page)
    25  	return
    26  }
    27  
    28  func (s *ExcelService) Get(id int) (excel model.ZdExcel, dirs []domain.Dir) {
    29  	excel, _ = s.ExcelRepo.Get(uint(id))
    30  
    31  	serverUtils.GetDirs(consts.ResDirData, &dirs)
    32  
    33  	return
    34  }
    35  
    36  func (s *ExcelService) Save(excel *model.ZdExcel) (err error) {
    37  	excel.Folder = serverUtils.DealWithPathSepRight(excel.Folder)
    38  	excel.Path = vari.WorkDir + excel.Folder + serverUtils.AddExt(excel.FileName, ".xlsx")
    39  	excel.ReferName = helper.PathToName(excel.Path, consts.ResDirData, consts.ResTypeExcel)
    40  
    41  	if excel.ID == 0 {
    42  		// excel should not be create on webpage
    43  	} else {
    44  		err = s.Update(excel)
    45  	}
    46  
    47  	return
    48  }
    49  func (s *ExcelService) Update(excel *model.ZdExcel) (err error) {
    50  	var old model.ZdExcel
    51  	old, err = s.ExcelRepo.Get(excel.ID)
    52  	if err == gorm.ErrRecordNotFound {
    53  		return
    54  	}
    55  	if excel.Path != old.Path {
    56  		fileUtils.RemoveExist(old.Path)
    57  	}
    58  
    59  	err = s.ExcelRepo.Update(excel)
    60  
    61  	return
    62  }
    63  
    64  func (s *ExcelService) Remove(id int) (err error) {
    65  	var old model.ZdExcel
    66  	old, err = s.ExcelRepo.Get(uint(id))
    67  	if err == gorm.ErrRecordNotFound {
    68  		return
    69  	}
    70  
    71  	fileUtils.RemoveExist(old.Path)
    72  	err = s.ExcelRepo.Remove(uint(id))
    73  	return
    74  }
    75  
    76  func (s *ExcelService) Sync(files []domain.ResFile) (err error) {
    77  	list := s.ExcelRepo.ListAll()
    78  
    79  	mp := map[string]*model.ZdExcel{}
    80  	for _, item := range list {
    81  		mp[item.Path] = item
    82  	}
    83  
    84  	for _, fi := range files {
    85  		_, found := mp[fi.Path]
    86  		//logUtils.PrintTo(fi.UpdatedAt.OpenApiDataTypeString() + ", " + mp[fi.Path].UpdatedAt.OpenApiDataTypeString())
    87  		if !found { // no record
    88  			s.SyncToDB(fi)
    89  		} else if fi.UpdatedAt.Unix() > mp[fi.Path].UpdatedAt.Unix() { // db is old
    90  			s.ExcelRepo.Remove(mp[fi.Path].ID)
    91  			s.SyncToDB(fi)
    92  		} else { // db is new
    93  
    94  		}
    95  	}
    96  
    97  	return
    98  }
    99  func (s *ExcelService) SyncToDB(file domain.ResFile) (err error) {
   100  	excel := model.ZdExcel{
   101  		Title:     file.Title,
   102  		Sheet:     file.Title,
   103  		Path:      file.Path,
   104  		Folder:    serverUtils.GetRelativePath(file.Path),
   105  		ReferName: helper.PathToName(file.Path, consts.ResDirData, consts.ResTypeExcel),
   106  		FileName:  fileUtils.GetFileName(file.Path),
   107  	}
   108  	s.ExcelRepo.Create(&excel)
   109  
   110  	return
   111  }
   112  
   113  func NewExcelService(excelRepo *serverRepo.ExcelRepo) *ExcelService {
   114  	return &ExcelService{ExcelRepo: excelRepo}
   115  }