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

     1  package serverService
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/360EntSecGroup-Skylar/excelize/v2"
     7  	consts "github.com/easysoft/zendata/internal/pkg/const"
     8  	"github.com/easysoft/zendata/internal/pkg/domain"
     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  )
    13  
    14  type ResService struct {
    15  	RangesRepo    *serverRepo.RangesRepo    `inject:""`
    16  	InstancesRepo *serverRepo.InstancesRepo `inject:""`
    17  	ConfigRepo    *serverRepo.ConfigRepo    `inject:""`
    18  	ExcelRepo     *serverRepo.ExcelRepo     `inject:""`
    19  	TextRepo      *serverRepo.TextRepo      `inject:""`
    20  	DefRepo       *serverRepo.DefRepo       `inject:""`
    21  }
    22  
    23  func (s *ResService) LoadRes(resType string) (ret []domain.ResFile) {
    24  	res, _, _ := helper.GetRes()
    25  
    26  	for _, key := range consts.ResKeys {
    27  		for _, res := range res[key] {
    28  			if res.ResType == consts.ResTypeExcel && strings.Index(res.Title, "|") > -1 {
    29  				// more than 1 sheet
    30  				arr := strings.Split(res.Title, "|")
    31  
    32  				res.Title = arr[0]
    33  				ret = append(ret, res)
    34  
    35  				var temp interface{} = res // clone
    36  				res1 := temp.(domain.ResFile)
    37  				res1.Title = arr[1]
    38  				ret = append(ret, res1)
    39  			} else {
    40  				ret = append(ret, res)
    41  			}
    42  		}
    43  	}
    44  
    45  	return
    46  }
    47  
    48  func (s *ResService) ListReferFileForSelection(resType string) (ret interface{}) {
    49  	if resType == "ranges" {
    50  		ret = s.RangesRepo.ListAll()
    51  	} else if resType == "instances" {
    52  		ret = s.InstancesRepo.ListAll()
    53  	} else if resType == "config" {
    54  		ret = s.ConfigRepo.ListAll()
    55  	} else if resType == "yaml" {
    56  		ret = s.DefRepo.ListAll()
    57  	} else if resType == "excel" {
    58  		ret = s.ExcelRepo.ListFiles()
    59  	} else if resType == "text" {
    60  		ret = s.TextRepo.ListAll()
    61  	}
    62  
    63  	return
    64  }
    65  
    66  func (s *ResService) ListReferSheetForSelection(referName string) (ret []*model.ZdExcel) {
    67  	index := strings.LastIndex(referName, ".")
    68  	file := referName[:index]
    69  
    70  	ret = s.ExcelRepo.ListSheets(file)
    71  
    72  	return
    73  }
    74  func (s *ResService) ListReferExcelColForSelection(referName string) (ret []domain.ResField) {
    75  	index := strings.LastIndex(referName, ".")
    76  	file := referName[:index]
    77  	sheet := referName[index+1:]
    78  
    79  	res, _ := s.ExcelRepo.GetBySheet(file, sheet)
    80  	excel, err := excelize.OpenFile(res.Path)
    81  	if err != nil {
    82  		return
    83  	}
    84  
    85  	for _, sheet := range excel.GetSheetList() {
    86  		if res.Sheet != sheet {
    87  			continue
    88  		}
    89  
    90  		rows, _ := excel.GetRows(sheet)
    91  
    92  		for index, row := range rows {
    93  			if index > 0 {
    94  				break
    95  			}
    96  			for i, col := range row {
    97  				val := strings.TrimSpace(col)
    98  				if val == "" {
    99  					break
   100  				}
   101  
   102  				field := domain.ResField{Name: val, Index: i + 1}
   103  				ret = append(ret, field)
   104  			}
   105  		}
   106  	}
   107  
   108  	return
   109  }
   110  
   111  func (s *ResService) ListReferResFieldForSelection(resId int, resType string) (ret []domain.ResField) {
   112  	if resType == "instances" {
   113  		items, _ := s.InstancesRepo.GetItems(uint(resId))
   114  		for i, item := range items {
   115  			if item.ParentID != 0 {
   116  				return
   117  			} // ignore sub nodes
   118  			field := domain.ResField{Name: item.Instance, Index: i + 1}
   119  			ret = append(ret, field)
   120  		}
   121  	} else if resType == "ranges" {
   122  		items, _ := s.RangesRepo.GetItems(resId)
   123  		for i, item := range items {
   124  			if item.ParentID != 0 {
   125  				return
   126  			} // ignore sub nodes
   127  			field := domain.ResField{Name: item.Field, Index: i + 1}
   128  			ret = append(ret, field)
   129  		}
   130  	}
   131  
   132  	return
   133  }
   134  
   135  func NewResService(rangesRepo *serverRepo.RangesRepo,
   136  	instancesRepo *serverRepo.InstancesRepo,
   137  	configRepo *serverRepo.ConfigRepo,
   138  	excelRepo *serverRepo.ExcelRepo,
   139  	textRepo *serverRepo.TextRepo,
   140  	defRepo *serverRepo.DefRepo) *ResService {
   141  	return &ResService{RangesRepo: rangesRepo, InstancesRepo: instancesRepo,
   142  		ConfigRepo: configRepo, ExcelRepo: excelRepo,
   143  		TextRepo: textRepo, DefRepo: defRepo}
   144  }