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

     1  package service
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/easysoft/zendata/internal/pkg/domain"
    10  	"github.com/easysoft/zendata/internal/pkg/helper"
    11  	"github.com/easysoft/zendata/pkg/utils/vari"
    12  )
    13  
    14  type LoopService struct {
    15  	FixService    *FixService    `inject:""`
    16  	FormatService *FormatService `inject:""`
    17  }
    18  
    19  func (s *LoopService) LoopAndFixFieldValues(field *domain.DefField, withFix bool) {
    20  	s.ComputerLoopTimes(field)
    21  
    22  	values := make([]interface{}, 0)
    23  
    24  	indexOfRow := 0
    25  	count := 0
    26  	for {
    27  		// 处理格式、前后缀、loop等
    28  		str := s.LoopFieldValueToSingleStr(field, &indexOfRow, count, withFix)
    29  		values = append(values, str)
    30  
    31  		count++
    32  		isRandomAndLoopEnd := (*field).IsRand && (*field).LoopIndex == (*field).LoopEnd
    33  		isNotRandomAndValOver := !(*field).IsRand && indexOfRow >= len(field.Values)
    34  		if count >= vari.GlobalVars.Total || isRandomAndLoopEnd || isNotRandomAndValOver {
    35  			break
    36  		}
    37  
    38  		(*field).LoopIndex = (*field).LoopIndex + 1
    39  		if (*field).LoopIndex > (*field).LoopEnd {
    40  			(*field).LoopIndex = (*field).LoopStart
    41  		}
    42  	}
    43  
    44  	field.Values = values
    45  
    46  	return
    47  }
    48  
    49  func (s *LoopService) LoopFieldValueToSingleStr(field *domain.DefField, indexOfRow *int, count int, withFix bool) (
    50  	ret interface{}) {
    51  
    52  	if (*field).LoopIndex <= 1 && field.Loopfix == "" {
    53  		ret, _ = s.getFieldValByIndex(*field, indexOfRow)
    54  		ret = s.FixService.AddFix(ret, field, count, withFix)
    55  
    56  		*indexOfRow++
    57  		return
    58  	}
    59  
    60  	for j := 0; j < (*field).LoopIndex; j++ {
    61  		if ret != nil && ret != "" {
    62  			ret = fmt.Sprintf("%v", ret) + field.Loopfix
    63  		}
    64  
    65  		str, err := s.getFieldValByIndex(*field, indexOfRow)
    66  		if err != nil {
    67  			str = "N/A"
    68  		}
    69  
    70  		temp := fmt.Sprintf("%v", str)
    71  		if ret == nil || ret == "" {
    72  			ret = temp
    73  		} else {
    74  			ret = fmt.Sprintf("%v", ret) + temp
    75  		}
    76  
    77  		*indexOfRow++
    78  	}
    79  
    80  	ret = s.FixService.AddFix(ret, field, count, withFix)
    81  
    82  	return
    83  }
    84  
    85  func (s *LoopService) ComputerLoopTimes(field *domain.DefField) {
    86  	if (*field).LoopIndex != 0 {
    87  		return
    88  	}
    89  
    90  	arr := strings.Split(field.Loop, "-")
    91  	(*field).LoopStart, _ = strconv.Atoi(arr[0])
    92  	if len(arr) > 1 {
    93  		field.LoopEnd, _ = strconv.Atoi(arr[1])
    94  	}
    95  
    96  	if (*field).LoopStart == 0 {
    97  		(*field).LoopStart = 1
    98  	}
    99  	if (*field).LoopEnd == 0 {
   100  		(*field).LoopEnd = 1
   101  	}
   102  
   103  	(*field).LoopIndex = (*field).LoopStart
   104  }
   105  
   106  func (s *LoopService) getFieldValByIndex(field domain.DefField, index *int) (val interface{}, err error) {
   107  	// 叶节点
   108  	if len(field.Values) == 0 {
   109  		if helper.IsSelectExcelWithExpr(field) {
   110  			// logUtils.PrintToWithColor(i118Utils.I118Prt.Sprintf("fail_to_generate_field", field.Field), color.FgCyan)
   111  			err = errors.New("")
   112  		}
   113  		return
   114  	}
   115  
   116  	idx := *index % len(field.Values)
   117  	str := field.Values[idx]
   118  
   119  	val = s.FormatService.GetFieldValStr(field, str)
   120  
   121  	return
   122  }