package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	model "LAPP_ACURA_MOM_BACKEND/models/om"
	"github.com/go-xorm/xorm"
	"strconv"
)

// ScanCodeCheck 生成扫码校验的工艺数据
func (impl *SerialOrderServiceImplement) ScanCodeCheck(user *global.User, session *xorm.Session, checkRuleMap map[string]baseModel.ArtCheckRule, allocateMap map[string][]baseModel.ArtQtyAllocate, artRelMap map[string]int, scanRule *baseModel.ScanRuleInfo, op *baseModel.MeOperation, serialOrderStep *model.SerialOrderStepLst, bkFlag bool) (resCheckRuleMap *map[string]baseModel.ArtCheckRule, resAllocateMap *map[string][]baseModel.ArtQtyAllocate, opLi []model.SerialOrderOPDetail, err error) {
	opLi = make([]model.SerialOrderOPDetail, 0)
	ruleDao := baseDal.NewArtCheckRuleDAO(session, user.PlantNr, user.UserId)
	allocateDao := baseDal.NewArtQtyAllocateDAO(session, user.PlantNr, user.UserId)
	if scanRule.RuleType == baseModel.SCAN_RULE_TYPE_ARTICLE {
		if scanRule.SRCtrlPara1 == baseModel.SCAN_RULE_CHECK {
			// 获取零件校验规则
			rule, exist := checkRuleMap[op.ArticleAtcodLst.ArtId]
			if !exist {
				ruleLi, err := ruleDao.Select([]grmi.Predicate{baseMeta.ArtCheckRule_AttriCode.NewPredicate(grmi.Equal, op.ArticleAtcodLst.AttrCode), baseMeta.ArtCheckRule_SubObject.NewPredicate(grmi.Equal, serialOrderStep.SubArtId)}, nil)
				if err != nil {
					return nil, nil, nil, grmi.NewBusinessError("获取零件检验规则失败, error:" + err.Error())
				}
				if len(ruleLi) == 0 {
					ruleLi, err = ruleDao.Select([]grmi.Predicate{baseMeta.ArtCheckRule_AttriCode.NewPredicate(grmi.Equal, op.ArticleAtcodLst.AttrCode), baseMeta.ArtCheckRule_SubObject.NewPredicate(grmi.Equal, baseModel.MES_MATCH_ALL)}, nil)
					if err != nil {
						return nil, nil, nil, grmi.NewBusinessError("获取零件检验规则失败, error:" + err.Error())
					}
					if len(ruleLi) == 0 {
						return nil, nil, nil, grmi.NewBusinessError("未获取到零件检验规则, artId:" + serialOrderStep.SubArtId)
					}
				}
				rule = ruleLi[0]
				checkRuleMap[op.ArticleAtcodLst.ArtId] = rule
			}
			// 获取零件分配数量
			var num int
			// 零件BOM中两件组成父级零件的数量
			composeQty := artRelMap[serialOrderStep.SubArtId]
			allocateLi, exist := allocateMap[op.ArticleAtcodLst.ArtId]
			if !exist {
				allocateLi, err = allocateDao.Select([]grmi.Predicate{baseMeta.ArtQtyAllocate_AttriCode.NewPredicate(grmi.Equal, op.ArticleAtcodLst.AttrCode), baseMeta.ArtQtyAllocate_TotalPlanty.NewPredicate(grmi.Equal, composeQty)}, nil)
				if err != nil {
					return nil, nil, nil, grmi.NewBusinessError("获取特殊工位数量分配数据失败，错误:" + err.Error())
				}
				allocateMap[op.ArticleAtcodLst.ArtId] = allocateLi
			}
			// 获取特殊工位零件分配数量
			if len(allocateLi) == 0 {
				num = composeQty
			} else {
				var allNum int
				for _, allocate := range allocateLi {
					if allocate.PO == serialOrderStep.PO {
						if allocate.SubObject == baseModel.MES_MATCH_ALL {
							allNum = allocate.AssignPlanty
						} else if allocate.SubObject == serialOrderStep.SubArtId {
							num = allocate.AssignPlanty
						}
					}
				}
				if num == 0 {
					if allNum != 0 {
						num = allNum
					} else {
						num = composeQty
					}
				}
			}
			// 生成零件校验规则的工艺数据
			pos := 1
			for i := 0; i < num; i++ {
				op := model.SerialOrderOPDetail{
					SerialOrderId: serialOrderStep.SerialOrderId,
					OperationId:   serialOrderStep.OperationId,
					PO:            serialOrderStep.PO,
					SubArtId:      serialOrderStep.SubArtId,
					Pos:           pos,
					StepNo:        serialOrderStep.StepNo,
					StepType:      baseModel.STEP_CHECK_RULE,
					Status:        baseModel.WO_STATUS_RELEASED,
					// 扫码校验 AttriCodeDesc
					CharPara1: rule.AttriCodeDesc,
					// 扫码校验 SubObject
					CharPara2: rule.SubObject,
					// 扫码校验 CodeLength
					CharPara3: rule.CodeLength,
					// 扫码校验 CheckObject
					CharPara4: rule.CheckObject,
					// 条码类型
					CharPara5: rule.CodeType,
					// 条码类型校验的期望值
					CharPara6: rule.CheckValue,
					// 扫码校验 CheckStartPos
					IntPara1: rule.CheckStartPos,
					// 扫码校验 CheckLength
					IntPara2: rule.CheckLength,
					// 扫码校验 OnlyBarCodeFlag
					FlagPara1: rule.OnlyBarCodeFlag,
					// 条码类型的校验开关
					FlagPara2: rule.CodeTypeCheckToggle,
					BKFlag:    bkFlag,
				}
				pos++
				opLi = append(opLi, op)
			}
			return &checkRuleMap, &allocateMap, opLi, nil
		} else if scanRule.SRCtrlPara1 == baseModel.SCAN_RULE_CRWAL {
			op := model.SerialOrderOPDetail{
				SerialOrderId: serialOrderStep.SerialOrderId,
				OperationId:   serialOrderStep.OperationId,
				PO:            serialOrderStep.PO,
				SubArtId:      serialOrderStep.SubArtId,
				Pos:           1,
				StepNo:        serialOrderStep.StepNo,
				StepType:      baseModel.STEP_CHECK_RULE,
				Status:        baseModel.WO_STATUS_LOCKED,
				IntPara5:      -1,
			}
			opLi = append(opLi, op)
			return &checkRuleMap, &allocateMap, opLi, nil
		} else {
			return nil, nil, opLi, grmi.NewBusinessError("不支持的条码校验类型")
		}

	} else if scanRule.RuleType == baseModel.SCAN_RULE_TYPE_OTHER {
		//if scanRule.SRCtrlPara1 == baseModel.SCAN_RULE_SERIALORDER {
		op := model.SerialOrderOPDetail{
			SerialOrderId: serialOrderStep.SerialOrderId,
			OperationId:   serialOrderStep.OperationId,
			PO:            serialOrderStep.PO,
			SubArtId:      serialOrderStep.SubArtId,
			Pos:           1,
			StepNo:        serialOrderStep.StepNo,
			StepType:      baseModel.STEP_CHECK_RULE,
			Status:        baseModel.WO_STATUS_RELEASED,
			// 扫码校验 AttriCodeDesc
			CharPara1: serialOrderStep.SerialOrderId,
		}
		opLi = append(opLi, op)
		return &checkRuleMap, &allocateMap, opLi, nil
		//} else {
		//	return nil, nil, grmi.NewBusinessError("不支持的条码校验规则")
		//}
	} else {
		return nil, nil, opLi, grmi.NewBusinessError("不支持的条码校验类型")
	}
}

// OPCCommunication 生成OPC通信的工艺数据
func (impl *SerialOrderServiceImplement) OPCCommunication(user *global.User, session *xorm.Session, opcBasicMap map[int][]baseModel.OPCBasicComInfo, serialOrderStep *model.SerialOrderStepLst, attributeMap map[int][]baseModel.ArticleWithAttrValue, subArtMap map[string]int, bkFlag bool) (*map[int][]baseModel.OPCBasicComInfo, []model.SerialOrderOPDetail, error) {
	var err error
	opLi := make([]model.SerialOrderOPDetail, 0)
	opcBasicDao := baseDal.NewOPCBasicComInfoDAO(session, user.PlantNr, user.UserId)
	opcComRuleDao := baseDal.NewOPCComRuleDAO(session, user.PlantNr, user.UserId)
	opcComDetailDao := baseDal.NewOPCComDetailRuleDAO(session, user.PlantNr, user.UserId)
	opcBasicLi, exist := opcBasicMap[serialOrderStep.OperationId]
	if !exist {
		opcBasicLi, err = opcBasicDao.Select([]grmi.Predicate{baseMeta.OPCBasicComInfo_OperationId.NewPredicate(grmi.Equal, serialOrderStep.OperationId)}, nil)
		if err != nil {
			return nil, nil, grmi.NewBusinessError("查询OPC基础通信规则失败，error:" + err.Error())
		}
		if len(opcBasicLi) == 0 {
			return nil, nil, grmi.NewBusinessError("未获取到OPC基础通信规则")
		}
		for index, opcBasic := range opcBasicLi {
			opcComLi, err := opcComRuleDao.Select([]grmi.Predicate{baseMeta.OPCComRule_OperationId.NewPredicate(grmi.Equal, opcBasic.OperationId), baseMeta.OPCComRule_WorkPlaceNr.NewPredicate(grmi.Equal, opcBasic.WorkPlaceNr)}, nil)
			if err != nil {
				return nil, nil, grmi.NewBusinessError("查询OPC通信规则失败, error:" + err.Error())
			}
			if len(opcComLi) == 0 {
				return nil, nil, grmi.NewBusinessError("未获取到OPC通信规则")
			}
			opcDetailLi, err := opcComDetailDao.Select([]grmi.Predicate{baseMeta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, opcBasic.OperationId), baseMeta.OPCComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, opcBasic.WorkPlaceNr)}, nil)
			if err != nil {
				return nil, nil, grmi.NewBusinessError("查询OPC通信详细规则失败, error:" + err.Error())
			}
			for index, opcCom := range opcComLi {
				opcCom.OPCComDetailRuleLi = make([]baseModel.OPCComDetailRule, 0)
				for _, detail := range opcDetailLi {
					if detail.OperationId == opcCom.OperationId && detail.RuleId == opcCom.RuleId {
						opcCom.OPCComDetailRuleLi = append(opcCom.OPCComDetailRuleLi, detail)
					}
				}
				opcComLi[index] = opcCom
			}
			opcBasic.OPCComRuleLi = opcComLi
			opcBasicLi[index] = opcBasic
		}
		opcBasicMap[serialOrderStep.OperationId] = make([]baseModel.OPCBasicComInfo, 0)
		opcBasicMap[serialOrderStep.OperationId] = opcBasicLi
	}
	pos := 1
	for _, opcBasic := range opcBasicLi {
		for _, opc := range opcBasic.OPCComRuleLi {
			var value string
			if opc.DBlockValueType == baseModel.OPC_DBBLOCK_VALUE_TYPE_MAP {
				for _, detail := range opc.OPCComDetailRuleLi {
					if detail.BaseOnType == baseModel.BASE_ON_TYPE_ATTRNAME {
						attrCode, err := strconv.Atoi(detail.BaseOnValue)
						if err != nil {
							return nil, nil, grmi.NewBusinessError("OPC通讯规则映射值转换ATTRCODE失败, error:" + err.Error())
						}
						attrLi, exist := attributeMap[attrCode]
						if !exist {
							return nil, nil, grmi.NewBusinessError("未获取到OPC通信细则中的零件属性描述对应的shortCode, 属性描述:" + detail.BaseOnValue)
						}
						if detail.DataType == baseModel.DATA_TYPE_ATTRCODE {

							value += strconv.Itoa(attrLi[0].AttributeValst.AttrCode)
						} else if detail.DataType == baseModel.DATA_TYPE_SHORTCODE {
							var sign bool
							for _, data := range attrLi {
								_, exist := subArtMap[data.ArticleAtcodLst.ArtId]
								if !exist {
									continue
								} else {
									if sign {
										return nil, nil, grmi.NewBusinessError("OPC通讯规则映射值获取ATTRCODE下的零件多于一个")
									}
									sign = true
									value += data.AttributeValst.ShortCode
								}
							}
							if !sign {
								return nil, nil, grmi.NewBusinessError("未获取到OPC通信细则中的零件属性描述对应的shortCode, 属性描述:" + detail.BaseOnValue)
							}
						} else if detail.DataType == baseModel.DATA_TYPE_ATTRVALUE {
							var sign bool
							for _, data := range attrLi {
								_, exist := subArtMap[data.ArticleAtcodLst.ArtId]
								if !exist {
									continue
								} else {
									if sign {
										return nil, nil, grmi.NewBusinessError("OPC通讯规则映射值获取ATTRCODE下的零件多于一个")
									}
									sign = true
									value += data.AttributeValst.AttrValue
								}
							}
						} else {
							return nil, nil, grmi.NewBusinessError("不支持的OPC通信细则中的DATA TYPE, DATA TYPE:" + detail.DataType)
						}
					} else if detail.BaseOnType == baseModel.BASE_ON_TYPE_ORDERINFO {
						if detail.DataType == baseModel.DATA_TYPE_PARTNO {
							value += serialOrderStep.SubArtId
						} else {
							return nil, nil, grmi.NewBusinessError("不支持的OPC通信细则中的DATA TYPE, DATA TYPE:" + detail.DataType)
						}
					} else if detail.BaseOnType == baseModel.BASE_ON_TYPE_MAINBARCODE {
						if detail.BaseOnValue == baseModel.BASE_ON_VALUE_UNITSN {
							value += serialOrderStep.SerialOrderId
						} else {
							return nil, nil, grmi.NewBusinessError("不支持的OPC通信细则中的BASE ON VALUE, BASE ON VALUE:" + detail.BaseOnValue)
						}
					}
				}
			} else {
				value = opc.DBlockValue
			}
			op := model.SerialOrderOPDetail{
				SerialOrderId: serialOrderStep.SerialOrderId,
				OperationId:   serialOrderStep.OperationId,
				PO:            serialOrderStep.PO,
				SubArtId:      serialOrderStep.SubArtId,
				Pos:           pos,
				StepType:      baseModel.STEP_OPC_RULE,
				Status:        baseModel.WO_STATUS_RELEASED,
				StepNo:        serialOrderStep.StepNo,
				// OPC BRAND NAME
				CharPara1: opcBasic.BrandName,
				// OPC MACHINE TYPE
				CharPara2: opcBasic.MachineType,
				// OPC IP ADDRESS
				CharPara3: opcBasic.IpAddress,
				// OPC ACTION TYPE
				CharPara4: opc.ActionType,
				// OPC DBLOCK ADDRESS
				CharPara5: opc.DBlockAddress,
				// OPC DBLOCK VALUE TYPE
				CharPara6: opc.DBlockValueType,
				CharPara7: opc.Formula,
				CharPara8: opc.OperatorSimbol,
				CharPara9: value,
				// OPC WorkPlaceNr
				IntPara1: opcBasic.WorkPlaceNr,
				IntPara5: opc.RuleId,
				BKFlag:   bkFlag,
			}
			opLi = append(opLi, op)
			pos++

		}
	}
	return &opcBasicMap, opLi, nil
}

// TightenWrenchInfo 生成开放协议的工艺数据
func (impl *SerialOrderServiceImplement) TightenWrenchInfo(user *global.User, session *xorm.Session, twMap map[int][]baseModel.TightenWrenchInfo, serialOrderStep *model.SerialOrderStepLst, bkFlag bool, article *baseModel.Article) (*map[int][]baseModel.TightenWrenchInfo, []model.SerialOrderOPDetail, error) {

	var err error
	twDao := baseDal.NewTightenWrenchInfoDAO(session, user.PlantNr, user.UserId)
	twrDao := baseDal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
	opLi := make([]model.SerialOrderOPDetail, 0)
	twLi, exist := twMap[serialOrderStep.OperationId]
	if !exist {
		twLi, err = twDao.Select([]grmi.Predicate{baseMeta.TightenWrenchInfo_OperationId.NewPredicate(grmi.Equal, serialOrderStep.OperationId)}, nil)
		if err != nil {
			return nil, nil, grmi.NewBusinessError("查询开放协议通信规则失败，error:" + err.Error())
		}
		if len(twLi) == 0 {
			return nil, nil, grmi.NewBusinessError("未获取到开放协议通信规则, 工艺ID:" + strconv.Itoa(serialOrderStep.OperationId))
		}
		for index, tw := range twLi {
			twrLi, err := twrDao.Select([]grmi.Predicate{baseMeta.TightenWrenchRuleInfo_OperationId.NewPredicate(grmi.Equal, tw.OperationId), baseMeta.TightenWrenchRuleInfo_WorkPlaceNr.NewPredicate(grmi.Equal, tw.WorkPlaceNr)}, nil)
			if err != nil {
				return nil, nil, grmi.NewBusinessError("查询开放协议通信细则失败, error:" + err.Error())
			}
			if len(twrLi) == 0 {
				return nil, nil, grmi.NewBusinessError("未获取到开放协议通信细则, 工艺ID:" + strconv.Itoa(serialOrderStep.OperationId))
			}
			tw.TightenWrenchRuleInfoLi = twrLi
			twLi[index] = tw
		}
		twMap[serialOrderStep.OperationId] = twLi
	}
	pos := 1
	for _, tw := range twLi {
		for _, twr := range tw.TightenWrenchRuleInfoLi {
			controlId := serialOrderStep.SerialOrderId
			if twr.MainControlAction == baseModel.CONTROL_ACTION_DOWNLOAD && twr.ControlId == baseModel.CONTROL_ID_PREFIX {
				controlId = article.Prefix + serialOrderStep.SerialOrderId
			}
			for i := 0; i < twr.ControlTime; i++ {
				op := model.SerialOrderOPDetail{
					SerialOrderId: serialOrderStep.SerialOrderId,
					OperationId:   serialOrderStep.OperationId,
					PO:            serialOrderStep.PO,
					SubArtId:      serialOrderStep.SubArtId,
					Pos:           pos,
					StepType:      baseModel.STEP_TW_RULE,
					Status:        baseModel.WO_STATUS_RELEASED,
					StepNo:        serialOrderStep.StepNo,
					// TightenWrenchInfo BRAND NAME
					CharPara1: tw.BrandName,
					// TightenWrenchInfo 	MACHINE TYPE
					CharPara2: tw.MachineType,
					// TightenWrenchInfo IP ADDRESS
					CharPara3: tw.IpAddress,
					// TightenWrenchInfo MainControlAction
					CharPara4: twr.MainControlAction,
					// TightenWrenchInfo ControlTime
					CharPara5: controlId,
					// TightenWrenchInfo WorkPlaceNr
					IntPara2: tw.WorkPlaceNr,
					IntPara5: twr.RuleId,
					BKFlag:   bkFlag,
				}
				pos++
				opLi = append(opLi, op)
			}
		}
	}
	return &twMap, opLi, nil
}

// GuideRuleInfo 生成指导数据
func (impl *SerialOrderServiceImplement) GuideRuleInfo(guideRule *baseModel.GuideRuleInfo, serialOrderStep *model.SerialOrderStepLst, bkFlag bool) model.SerialOrderOPDetail {

	op := model.SerialOrderOPDetail{
		SerialOrderId: serialOrderStep.SerialOrderId,
		OperationId:   serialOrderStep.OperationId,
		PO:            serialOrderStep.PO,
		SubArtId:      serialOrderStep.SubArtId,
		Pos:           1,
		StepType:      baseModel.STEP_GUIDE_RULE,
		Status:        baseModel.WO_STATUS_RELEASED,
		StepNo:        serialOrderStep.StepNo,
		IntPara1:      guideRule.DisplayTime,
		BKFlag:        bkFlag,
	}
	return op
}

// PrintRuleInfo 生成单据打印数据
func (impl *SerialOrderServiceImplement) PrintRuleInfo(serialOrderStep *model.SerialOrderStepLst, rule *baseModel.PrintBasicRuleInfo, printTemplate *baseModel.LabelTemplateHead, article *baseModel.Article, bkFlag bool, planDate string) (op model.SerialOrderOPDetail, labelHeadLi []baseModel.LabelHead, labelDetailLi []baseModel.LabelDetail) {
	var qty = 1
	var collectScanBarcode = false
	var labelDetailPos string
	var scanOperationId = 0
	var scanPo string
	if rule != nil {
		qty = rule.Qty
		collectScanBarcode = rule.CollectScanBarcode
		labelDetailPos = rule.LabelDetailPos
		scanOperationId = rule.ScanOperationId
		scanPo = rule.ScanPo

	}
	//  FlagPara1 标识是不是根据扫码更新打印数据 1为需要
	// CharPara2 插入打印模板子项索引 多个用;分割
	// IntPara2 扫码的步骤
	// 生成打印规则
	op = model.SerialOrderOPDetail{
		SerialOrderId: serialOrderStep.SerialOrderId,
		OperationId:   serialOrderStep.OperationId,
		PO:            serialOrderStep.PO,
		SubArtId:      serialOrderStep.SubArtId,
		Pos:           1,
		StepType:      baseModel.STEP_PRINT_RULE,
		Status:        baseModel.WO_STATUS_RELEASED,
		StepNo:        serialOrderStep.StepNo,
		BKFlag:        bkFlag,
		IntPara1:      qty,
		IntPara2:      scanOperationId,
		CharPara1:     scanPo,
		CharPara2:     labelDetailPos,
		FlagPara1:     collectScanBarcode,
	}

	// 生成打印模板数据
	if rule != nil {
		labelId := serialOrderStep.SerialOrderId + "_" + serialOrderStep.PO + strconv.Itoa(serialOrderStep.StepNo)
		labelHead := baseModel.LabelHead{
			LabelId:         labelId,
			LabelTemplateId: printTemplate.LabelTemplateId,
			PrinterId:       rule.PrinterId,
			BusinessObjType: "SerialOrderId",
			BusinessObjId:   serialOrderStep.SerialOrderId,
			Status:          -1,
			PrintQty:        1,
			OperationId:     rule.OperationId,
		}
		labelHeadLi = append(labelHeadLi, labelHead)
		labelDetailLi = make([]baseModel.LabelDetail, 0, len(printTemplate.LabelTemplateDetail))
		var pos = 1
		for _, templateDetail := range printTemplate.LabelTemplateDetail {
			var labelDetail baseModel.LabelDetail
			switch templateDetail.LabelItemValue {
			case "$Descr2$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: article.Descr2,
				}
				pos++
			case "$Descr1$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: article.Descr1,
				}
				pos++
			case "$AlternativeArtId$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: article.AlternativeArtId,
				}
				pos++
			case "$date$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: planDate,
				}
				pos++
			case "$SerialOrderId$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: serialOrderStep.SerialOrderId,
				}
				pos++
			case "*$SerialOrderId$*":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: "*" + serialOrderStep.SerialOrderId + "*",
				}
				pos++
			case "$ArtId$":
				if rule.MainCodeName == "Pre" {
					labelDetail = baseModel.LabelDetail{
						LabelId:       labelId,
						Pos:           pos,
						VariableName:  templateDetail.LabelItemValue,
						VariableValue: article.ArtId,
					}
				} else {
					if len(article.ArtId) < 7 {
						continue
					}
					labelDetail = baseModel.LabelDetail{
						LabelId:       labelId,
						Pos:           pos,
						VariableName:  templateDetail.LabelItemValue,
						VariableValue: article.ArtId[:7],
					}
				}
				pos++
			case "$CustArtId$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: article.CustArtId,
				}
				pos++
			case "$OrderId$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: serialOrderStep.SerialOrderId,
				}
				pos++
			case "$Barcode$":
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: serialOrderStep.SerialOrderId,
				}
				pos++
			default:
				labelDetail = baseModel.LabelDetail{
					LabelId:       labelId,
					Pos:           pos,
					VariableName:  templateDetail.LabelItemValue,
					VariableValue: templateDetail.LabelItemValue,
				}
				pos++
			}
			labelDetailLi = append(labelDetailLi, labelDetail)
		}
	}
	return op, labelHeadLi, labelDetailLi
}

