// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	baseDal "LAPP_ACURA_MOM_BACKEND/dao/base"
	jitDal "LAPP_ACURA_MOM_BACKEND/dao/jit"
	meDal "LAPP_ACURA_MOM_BACKEND/dao/me"
	omDal "LAPP_ACURA_MOM_BACKEND/dao/om"
	dal "LAPP_ACURA_MOM_BACKEND/dao/qm"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	bmeta "LAPP_ACURA_MOM_BACKEND/meta/base"
	jitMeta "LAPP_ACURA_MOM_BACKEND/meta/jit"
	omMeta "LAPP_ACURA_MOM_BACKEND/meta/om"
	meta "LAPP_ACURA_MOM_BACKEND/meta/qm"
	baseModel "LAPP_ACURA_MOM_BACKEND/models/base"
	bmodel "LAPP_ACURA_MOM_BACKEND/models/base"
	jitModel "LAPP_ACURA_MOM_BACKEND/models/jit"
	omModel "LAPP_ACURA_MOM_BACKEND/models/om"
	model "LAPP_ACURA_MOM_BACKEND/models/qm"
	svr "LAPP_ACURA_MOM_BACKEND/services/om"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"strconv"
	"time"
)

/******************************************************************************
 *
 * @Description              : ReclinerReclst的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 14:05:20
 *
 ******************************************************************************/
var DefaultConditionOfReclinerReclst = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ReclinerReclst_RecNr.UrlParameterName:         grmi.NewConditionItem(meta.ReclinerReclst_RecNr, grmi.Equal, true),
		meta.ReclinerReclst_RecType.UrlParameterName:       grmi.NewConditionItem(meta.ReclinerReclst_RecType, grmi.Equal, true),
		meta.ReclinerReclst_Line.UrlParameterName:          grmi.NewConditionItem(meta.ReclinerReclst_Line, grmi.Equal, true),
		meta.ReclinerReclst_ArtId.UrlParameterName:         grmi.NewConditionItem(meta.ReclinerReclst_ArtId, grmi.Equal, false),
		meta.ReclinerReclst_SerialOrderId.UrlParameterName: grmi.NewConditionItem(meta.ReclinerReclst_SerialOrderId, grmi.Equal, false),
		meta.ReclinerReclst_ReclinerNr.UrlParameterName:    grmi.NewConditionItem(meta.ReclinerReclst_ReclinerNr, grmi.Equal, false),
		meta.ReclinerReclst_ItemPartNr.UrlParameterName:    grmi.NewConditionItem(meta.ReclinerReclst_ItemPartNr, grmi.Equal, false),
		meta.ReclinerReclst_PrinterId.UrlParameterName:     grmi.NewConditionItem(meta.ReclinerReclst_PrinterId, grmi.Equal, false),
		meta.ReclinerReclst_WorkPlaceNr.UrlParameterName:   grmi.NewConditionItem(meta.ReclinerReclst_WorkPlaceNr, grmi.Equal, false),
		meta.ReclinerReclst_Status.UrlParameterName:        grmi.NewConditionItem(meta.ReclinerReclst_Status, grmi.Equal, false),
		meta.ReclinerReclst_LastModify.UrlParameterName:    grmi.NewConditionItem(meta.ReclinerReclst_LastModify, grmi.Approximate, false),
		meta.ReclinerReclst_LastUser.UrlParameterName:      grmi.NewConditionItem(meta.ReclinerReclst_LastUser, grmi.Equal, false),
		meta.ReclinerReclst_CreateTime.UrlParameterName:    grmi.NewConditionItem(meta.ReclinerReclst_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ReclinerReclst的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 14:05:20
 *
 ******************************************************************************/
var DefaultConditionOfReclinerReclstAndPaging = grmi.NewCondition(
	DefaultConditionOfReclinerReclst.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ReclinerReclstServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ReclinerReclst的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 14:05:20
 *
 ******************************************************************************/
type ReclinerReclstServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewReclinerReclstDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ReclinerReclstDAOImplement实例
 *
 * @Return Value             : ReclinerReclstDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-07-12 14:05:20
 *
 ******************************************************************************/
func NewReclinerReclstServiceImplement() *ReclinerReclstServiceImplement {
	return &ReclinerReclstServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.InsertOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) InsertOne(user *global.User, entity *model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.RecNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if seriInfo == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	//查询物料主数据表
	artDao := baseDal.NewArticleDAO(session, user.PlantNr, user.UserId)
	artInfo, err := artDao.SelectOne(seriInfo.ArtId, seriInfo.ProjectId)
	if err != nil {
		return err
	}
	if artInfo == nil {
		return grmi.NewBusinessError("客户订单号没有维护!")
	}
	orderId := entity.SerialOrderId

	entity.ArtId = seriInfo.ArtId
	entity.RecType = model.QM_REC_TYPE
	entity.Status = "N"
	if len(orderId) < 27 {
		return grmi.NewBusinessError("序列订单长度错误!")
	}
	entity.ReclinerNr = orderId[0:19] + "R" + orderId[20:27]
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.AddPrinterInfo
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) AddPrinterInfo(user *global.User, entity *model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	templateId := "TSC-BARCODE"
	dao := baseDal.NewPrinterTemplateDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(templateId)
	if err != nil {
		return err
	}
	lstdao := baseDal.NewPrinterTemplateLstDAO(session, user.PlantNr, user.UserId)
	list, err := lstdao.Select([]grmi.Predicate{
		bmeta.PrinterTemplateLst_TemplateId.NewPredicate(grmi.Equal, templateId),
	}, nil)
	if err != nil {
		return err
	}
	result.PrinterTemplateLst = list
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if seriInfo == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	//查询物料主数据表
	artDao := baseDal.NewArticleDAO(session, user.PlantNr, user.UserId)
	artInfo, err := artDao.SelectOne(entity.ArtId, seriInfo.ArtId)
	if err != nil {
		return err
	}
	if artInfo == nil {
		return grmi.NewBusinessError("客户订单号没有维护!")
	}
	//查询序列订单表
	orderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	orderInfo, err := orderDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if orderInfo == nil {
		return grmi.NewBusinessError("序列订单订单不存在!")
	}
	snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId)
	headId, err := snrDao.GetNextSnr("PrintId")
	if err != nil {
		return grmi.NewBusinessError("打印头部生成错误!, error:" + err.Error())
	}
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	headDao := baseDal.NewPrintHeadDAO(session, user.PlantNr, user.UserId)
	head := new(bmodel.PrintHead)
	head.PlantNr = result.PlantNr
	head.PrintHeadId = headId
	head.Status = "N"
	head.TemplateFile = result.TemplateFile
	head.PrinterId = result.PrinterId
	head.PrintCopies = 1
	head.PrintFileType = result.TemplateTye
	err = headDao.InsertOne(head)
	if err != nil {
		session.Rollback()
		return err
	}

	detailDao := baseDal.NewPrintDetailDAO(session, user.PlantNr, user.UserId)
	for k, v := range list {
		detail := new(bmodel.PrintDetail)
		head.PlantNr = result.PlantNr
		detail.PrintHeadId = headId
		detail.Pos = (k + 1)
		detail.VarName = v.VarName
		//映射值
		if detail.VarName == "$SN$" {
			detail.VarValue = entity.ReclinerNr
		} else if detail.VarName == "$DATE$" {
			dateStr := utils.TimeFormat(orderInfo.PlanStartTime.Restore(), "yyyy/MM/dd")
			detail.VarValue = dateStr
		} else if detail.VarName == "$CUSTID$" {
			detail.VarValue = artInfo.CustArtId
		} else if detail.VarName == "$ORDERID$" {
			detail.VarValue = entity.ReclinerNr
		}

		err = detailDao.InsertOne(detail)
		if err != nil {
			session.Rollback()
			return err
		}
	}

	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.DeleteOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) DeleteOne(user *global.User, recNr int) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "DeleteOneReclinerReclst", "删除一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(recNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(recNr)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.SelectOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SelectOne(user *global.User, recNr int) (*model.ReclinerReclst, error) {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "SelectOneReclinerReclst", "查询一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(recNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.UpdateOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) UpdateOne(user *global.User, entity *model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "UpdateOneReclinerReclst", "修改一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.RecNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.Insert
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) Insert(user *global.User, entities *[]model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertReclinerReclst", "插入多个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RecNr)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	err := dao.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.Delete
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) Delete(user *global.User, entities *[]model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "DeleteReclinerReclst", "删除多个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RecNr)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Delete(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.Select
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "SelectReclinerReclst", "查询ReclinerReclst")

	condition := DefaultConditionOfReclinerReclstAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/qm/ReclinerReclstService.Update
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) Update(user *global.User, entities *[]model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "UpdateReclinerReclst", "修改多个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RecNr)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Update(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/qm/ReclinerReclstService.InsertSeriOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) InsertSeriOne(user *global.User, entity *model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")
	var nextNumberStr string
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	statusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	placeDao := baseDal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if seriInfo == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	serialOrderStatus, err := statusDao.SelectOne(seriInfo.SerialOrderId)

	if err != nil {
		return err
	}
	if serialOrderStatus == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED {
		return grmi.NewBusinessError("条码状态未完成!")
	}

	orderId := entity.SerialOrderId

	if len(orderId) < 11 {
		return grmi.NewBusinessError("订单号长度不合法!")
	}
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, 错误:" + err.Error())
	}
	//获取机器码
	machineCode := orderId[0:4]
	ColorValue := orderId[10:11]
	now := time.Now()
	year := now.Year()
	month := int(now.Month())
	day := now.Day()
	yearStr := bmodel.YEAR_MAP[year]
	monthStr := bmodel.MONTH_MAP[month]
	dayStr := bmodel.DAY_MAP[day]
	temOrderId := machineCode + yearStr + monthStr + dayStr
	//计算map反转
	seriMap := make(map[int]string)
	for k, v := range bmodel.ReworkMap {
		key := v
		val := k
		seriMap[key] = val
	}
	//查询当天的机器码对应的返修流水号
	reworkData, err := dao.SelectOneRework(temOrderId)
	if reworkData != nil {
		lastOrderId := reworkData.ReclinerNr
		serialNumber := lastOrderId[7:9]

		nowNumber, ok := bmodel.ReworkMap[serialNumber]
		nextNumber := utils.ValueToInt((nowNumber + 1), 0)
		if ok {
			if nextNumber <= 102 {
				nextNumberStr, ok = seriMap[nextNumber]
				if !ok {
					session.Rollback()
					return grmi.NewBusinessError("流水号生成超出范围!")
				}
			} else {
				session.Rollback()
				return grmi.NewBusinessError("流水号生成超出范围!")
			}
		}
	} else {
		nextNumberStr = seriMap[1]
	}
	if len(nextNumberStr) != 2 {
		session.Rollback()
		return grmi.NewBusinessError("流水号生成错误!")
	}
	serialOrderId := machineCode + yearStr + monthStr + dayStr + nextNumberStr + "0" + ColorValue

	//根据工位查询打印机Id
	placeInfo, err := placeDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		session.Rollback()
		return err
	}
	if placeInfo == nil {
		session.Rollback()
		return grmi.NewBusinessError("工位信息不存在!")
	}
	entity.SerialOrderId = orderId
	entity.ReclinerNr = serialOrderId
	entity.PrinterId = placeInfo.PrinterId
	entity.ArtId = seriInfo.ArtId
	entity.Line = seriInfo.PlanResourceId
	entity.RecType = model.QM_SERI_TYPE
	entity.Status = "N"
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	articleAtocdDao := baseDal.NewArticleAtcodLstDAO(session, user.PlantNr, user.UserId)
	attributeLi, err := articleAtocdDao.SelectByArtId(seriInfo.ArtId)
	if err != nil {
		session.Rollback()
		return grmi.NewBusinessError("查询总成关联属性失败, error:" + err.Error())
	}
	var needEOL bool
	for _, attributeItem := range attributeLi {
		if attributeItem.Attribute.Descr == "EOL" {
			needEOL = true
			break
		}
	}
	if needEOL {
		serialOrderSvr := svr.NewSerialOrderService()
		eolStatus, _, _, err := serialOrderSvr.ExtractEOL(session, user, seriInfo)
		if err != nil {
			session.Rollback()
			return grmi.NewBusinessError("查询工单电检数据失败, 错误:" + err.Error())
		}
		if eolStatus {
			seriInfo.EOLCheck = omModel.EOL_STATUS_OK
		} else {
			seriInfo.EOLCheck = omModel.EOL_STATUS_NG
		}
	}
	//更新seriorder订单表
	//更新原来的flag标识
	seriInfo.GenerateFlag = true
	err = seriDao.UpdateOne(seriInfo)
	if err != nil {
		session.Rollback()
		return err
	}
	//插入新的seriorder条码
	insertSeriOrder := omModel.SerialOrder{}
	insertSeriOrder = *seriInfo
	insertSeriOrder.SerialOrderId = serialOrderId
	insertSeriOrder.LinkSerialOrderId = seriInfo.SerialOrderId
	if seriInfo.OrderType == bmodel.ODER_TYPE_REODER {
		insertSeriOrder.OriginSerialOrderId = seriInfo.OriginSerialOrderId
	} else {
		insertSeriOrder.OriginSerialOrderId = seriInfo.SerialOrderId
	}

	insertSeriOrder.OrderType = bmodel.ODER_TYPE_REODER
	insertSeriOrder.GenerateFlag = false
	insertSeriOrder.RepairFlag = false
	insertSeriOrder.QualityCheck = bmodel.QualityCheck_STATUS_OK
	insertSeriOrder.QualityCheckTime = grmi.DateTime(time.Now())
	err = seriDao.InsertOne(&insertSeriOrder)
	if err != nil {
		session.Rollback()
		return err
	}
	//插入新的条码状态
	insertSeriOrderStatus := new(omModel.SerialOrderStatus)
	insertSeriOrderStatus.SerialOrderId = insertSeriOrder.SerialOrderId
	insertSeriOrderStatus.Status = bmodel.WO_STATUS_FINISHED
	err = statusDao.InsertOne(insertSeriOrderStatus)
	if err != nil {
		session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/qm/ReclinerReclstService.SelectSeriOne
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SelectSeriOne(user *global.User, orderId string) (*model.ReclinerReclst, error) {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")
	var nextNumberStr string
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	statusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	seriInfo, err := seriDao.SelectBySerialOrder(orderId)
	if err != nil {
		return nil, err
	}
	if seriInfo == nil {
		return nil, grmi.NewBusinessError("不存在指定记录!")
	}
	serialOrderStatus, err := statusDao.SelectOne(seriInfo.SerialOrderId)

	if err != nil {
		return nil, err
	}
	if serialOrderStatus == nil {
		return nil, grmi.NewBusinessError("不存在指定记录!")
	}
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED {
		return nil, grmi.NewBusinessError("条码状态未完成!")
	}
	entity := new(model.ReclinerReclst)
	entity.PlantNr = user.PlantNr
	entity.ArtId = seriInfo.ArtId
	entity.SerialOrderId = orderId
	entity.Line = seriInfo.PlanResourceId
	entity.RecType = model.QM_SERI_TYPE

	if len(orderId) < 11 {
		return nil, grmi.NewBusinessError("订单号长度不合法!")
	}
	//获取机器码
	machineCode := orderId[0:4]
	ColorValue := orderId[10:11]

	now := time.Now()
	year := now.Year()
	month := int(now.Month())
	day := now.Day()
	yearStr := bmodel.YEAR_MAP[year]
	monthStr := bmodel.MONTH_MAP[month]
	dayStr := bmodel.DAY_MAP[day]
	temOrderId := machineCode + yearStr + monthStr + dayStr
	//计算map反转
	seriMap := make(map[int]string)
	for k, v := range bmodel.ReworkMap {
		key := v
		val := k
		seriMap[key] = val
	}
	//查询当天的机器码对应的返修流水号
	reworkData, err := dao.SelectOneRework(temOrderId)
	if reworkData != nil {
		lastOrderId := reworkData.ReclinerNr
		if len(lastOrderId) < 11 {
			return nil, grmi.NewBusinessError("ReclinerNr订单号长度不合法!")
		}
		serialNumber := lastOrderId[7:9]
		nowNumber, ok := bmodel.ReworkMap[serialNumber]
		nextNumber := utils.ValueToInt((nowNumber + 1), 0)
		if nextNumber <= 102 {
			nextNumberStr, ok = seriMap[nextNumber]
			if !ok {
				return nil, grmi.NewBusinessError("流水号生成超出范围!")
			}
		} else {
			return nil, grmi.NewBusinessError("流水号生成超出范围!")
		}
	} else {
		nextNumberStr = seriMap[1]
	}
	if len(nextNumberStr) != 2 {
		return nil, grmi.NewBusinessError("流水号生成错误!")
	}
	serialOrderId := machineCode + yearStr + monthStr + dayStr + nextNumberStr + "0" + ColorValue

	entity.ReclinerNr = serialOrderId
	return entity, nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/qm/ReclinerReclstService.CreatePackOrderPrintInfo
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) CreateRecPrintInfo(entity model.ReclinerReclst) error {

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	plantDao := baseDal.NewPlantDAO(session, "sys")
	plant, err := plantDao.SelectOne(entity.PlantNr)
	if err != nil {
		return grmi.NewBusinessError("查询工厂数据失败, 错误:" + err.Error())
	}
	if plant == nil {
		return grmi.NewBusinessError("用户归属工厂不存在, 工厂ID:" + strconv.Itoa(entity.PlantNr))
	}
	plantProject := plant.PlantProject
	seriDao := omDal.NewSerialOrderDAO(session, entity.PlantNr, "sys")
	seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if seriInfo == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	//查询物料主数据表
	artDao := baseDal.NewArticleDAO(session, entity.PlantNr, "Rec")
	artInfo, err := artDao.SelectOne(entity.ArtId, seriInfo.ProjectId)
	if err != nil {
		return err
	}
	if artInfo == nil {
		return grmi.NewBusinessError("物料数据没有维护!")
	}

	//查询物料主数据表
	proDao := meDal.NewProductDAO(session, entity.PlantNr, "Rec")
	proInfo, err := proDao.SelectOne(entity.ArtId, seriInfo.ProjectId)
	if err != nil {
		return err
	}
	if proInfo == nil {
		return grmi.NewBusinessError("物料总成没有维护!")
	}
	dao := baseDal.NewLabelTemplateHeadDAO(session, entity.PlantNr, "Rec")
	result, err := dao.SelectOne(proInfo.PrintTemplateId)
	if err != nil {
		return err
	}
	lstdao := baseDal.NewLabelTemplateDetailDAO(session, entity.PlantNr, "Rec")
	list, err := lstdao.Select([]grmi.Predicate{
		bmeta.LabelTemplateDetail_LabelTemplateId.NewPredicate(grmi.Equal, proInfo.PrintTemplateId),
	}, nil)
	if err != nil {
		return err
	}
	result.LabelTemplateDetail = list

	//查询序列订单表
	orderDao := omDal.NewSerialOrderDAO(session, entity.PlantNr, "Rec")
	orderInfo, err := orderDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		return err
	}
	if orderInfo == nil {
		return grmi.NewBusinessError("序列订单订单不存在!")
	}

	snrDao := baseDal.NewSnrDAO(session, entity.PlantNr, "Rec")
	headId, err := snrDao.GetNextSnr("PrintId")
	if err != nil {
		return grmi.NewBusinessError("打印头部生成错误!")
	}

	// add Begin() before any action
	if err := session.Begin(); err != nil {
		return err
	}
	headDao := baseDal.NewLabelHeadDAO(session, entity.PlantNr, "Rec")
	head := new(bmodel.LabelHead)
	head.PlantNr = result.PlantNr
	head.LabelId = headId
	head.Status = 0
	head.LabelTemplateId = result.LabelTemplateId
	head.PrinterId = entity.PrinterId
	if plantProject == baseModel.PROJECT_NANSHA {
		head.PrintQty = 1
	} else {
		head.PrintQty = 2
	}
	head.BusinessObjType = "BACK"
	head.BusinessObjId = entity.SerialOrderId
	err = headDao.InsertOne(head)
	if err != nil {
		session.Rollback()
		return err
	}

	detailDao := baseDal.NewLabelDetailDAO(session, entity.PlantNr, "Rec")
	for k, v := range list {
		detail := new(bmodel.LabelDetail)
		head.PlantNr = result.PlantNr
		detail.LabelId = headId
		detail.Pos = (k + 1)
		detail.VariableName = v.LabelItemValue
		//映射值
		if detail.VariableName == "$SerialOrderId$" {
			detail.VariableValue = entity.ReclinerNr
		} else if detail.VariableName == "$date$" {
			dateStr := utils.TimeFormat(orderInfo.PlanStartTime.Restore(), "yyyy/MM/dd")
			detail.VariableValue = dateStr
		} else if detail.VariableName == "$AlternativeArtId$" {
			detail.VariableValue = artInfo.AlternativeArtId
		} else if detail.VariableName == "$ORDERID$" {
			detail.VariableValue = entity.ReclinerNr
		} else if detail.VariableName == "$Descr2$" {
			detail.VariableValue = artInfo.Descr2
		} else if detail.VariableName == "$Descr1$" {
			detail.VariableValue = artInfo.Descr1
		} else if detail.VariableName == "$CustArtId$" {
			detail.VariableValue = artInfo.CustArtId
		} else if detail.VariableName == "*$SerialOrderId$*" {
			detail.VariableValue = "*" + entity.ReclinerNr + "*"
		} else if detail.VariableName == "$ArtId$" {
			if len(artInfo.ArtId) < 7 {
				continue
			}
			detail.VariableValue = artInfo.ArtId[:7]
		}
		err = detailDao.InsertOne(detail)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.SelectUnPrint
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SelectUnPrint(num int) ([]model.ReclinerReclst, error) {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, 0, "Rec")
	if num < 1 {
		num = 50
	}
	result, err := dao.SelectUnPrint(num)
	if err != nil {
		return nil, err
	}
	return result, nil

}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.UpdateUnPrint
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) UpdateUnPrint(recIds []int) error {
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, 0, "Rec")
	err := dao.UpdateUnPrint(recIds)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.FirstPartRepair
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) FirstPartRepair(user *global.User, entity *model.ReclinerReclst) error {
	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "FirstPartRepair", "首件返修")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	statusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId)
	if err != nil {
		session.Rollback()
		return err
	}
	if seriInfo == nil {
		session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}

	serialOrderStatus, err := statusDao.SelectOne(seriInfo.SerialOrderId)

	if err != nil {
		session.Rollback()
		return err
	}
	if serialOrderStatus == nil {
		session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	if serialOrderStatus.Status != bmodel.WO_STATUS_UNCHECKED {
		session.Rollback()
		return grmi.NewBusinessError("条码状态不需要首件校验!")
	}
	//更新seriorder订单表
	serialOrderStatus.Status = bmodel.WO_STATUS_FINISHED
	err = statusDao.UpdateOne(serialOrderStatus)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.FirstPartRepair
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SelectSerialOrderProcess(user *global.User, serialOrderId string) ([]bmodel.Process, error) {
	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "SelectSerialOrderProcess", "查询工单经历的所有工序")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return nil, grmi.NewBusinessError("查询工单数据失败, error:" + err.Error())
	}
	if serialOrder == nil {
		return nil, grmi.NewBusinessError("工单不存在")
	}
	serialOrderPoLstDao := omDal.NewSerialOrderPOLstDAO(session, user.PlantNr, user.UserId)
	poDataLi, err := serialOrderPoLstDao.SelectWithPO(serialOrderId, serialOrder.UsedResourceId)
	if err != nil {
		return nil, grmi.NewBusinessError("查询工序数据失败, error:" + err.Error())
	}
	result := make([]bmodel.Process, 0, len(poDataLi))
	for _, poData := range poDataLi {
		result = append(result, poData.Process)
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.BackOnline
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) BackOnline(user *global.User, serialOrderId string, poLi []string) error {
	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "BackOnline", "重新上线")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	serialOrderPoLstDao := omDal.NewSerialOrderPOLstDAO(session, user.PlantNr, user.UserId)
	serialOrderPOStatusRecDao := omDal.NewSerialOrderPOStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrderStepDao := omDal.NewSerialOrderStepLstDAO(session, user.PlantNr, user.UserId)
	serialOrderStepRecDao := omDal.NewSerialOrderStepStatusRecLstDAO(session, user.PlantNr, user.UserId)
	opDetailDao := omDal.NewSerialOrderOPDetailDAO(session, user.PlantNr, user.UserId)
	opDetailRecDao := omDal.NewSerialOrderOPDetailStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrderStatusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	serialOrderStatusRecDao := omDal.NewSerialOrderStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单数据失败, error:" + err.Error())
	}
	if serialOrder == nil {
		return grmi.NewBusinessError("工单不存在")
	}
	serialOrderStatus, err := serialOrderStatusDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单状态失败, error:" + err.Error())
	}
	if serialOrderStatus == nil {
		return grmi.NewBusinessError("工单状态不存在")
	}
	// 校验工单状态 只有80和75才可以进行线上返修
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED && serialOrderStatus.Status != bmodel.WO_STATUS_UNCHECKED {
		return grmi.NewBusinessError("该工单不允许线上返修")
	}
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
	}
	serialOrder.RepairFlag = true
	err = serialOrderDao.UpdateOne(serialOrder)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("更新工单数据失败, error:" + err.Error())
	}
	serialOrderStatusRec := omModel.SerialOrderStatusRecLst{
		SerialOrderId: serialOrderId,
		PrevStatus:    serialOrderStatus.Status,
		Status:        bmodel.WO_STATUS_RUNNING,
	}
	err = serialOrderStatusRecDao.InsertOne(&serialOrderStatusRec)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("写入工单状态变更记录失败, error:" + err.Error())
	}
	serialOrderStatus.Status = bmodel.WO_STATUS_RUNNING
	err = serialOrderStatusDao.UpdateOne(serialOrderStatus)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("更新工单状态失败, error:" + err.Error())
	}
	// 特殊处理:
	// 如果工单为原始工单 不需要特殊处理 查询自己工单的工艺数据即可
	// 如果工单为后生成工单 需要查询原始工单的工艺数据
	if serialOrder.OriginSerialOrderId != "" {
		// 如果不是原始工单 将需要返修的所有工序数据 和原始工单相差的数据都copy过来
		allPoLi, err := serialOrderPoLstDao.SelectOrderAllPo(serialOrderId)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询当前工单工序数据失败, 错误:" + err.Error())
		}
		originAllPoLi, err := serialOrderPoLstDao.SelectOrderAllPo(serialOrderId)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询原工单工序数据失败, 错误:" + err.Error())
		}
		currentOrderPoMap := make(map[string]interface{})
		needCopyPoLi := make([]string, 0)
		for _, po := range allPoLi {
			currentOrderPoMap[po] = nil
		}
		for _, po := range originAllPoLi {
			_, exist := currentOrderPoMap[po]
			if !exist {
				needCopyPoLi = append(needCopyPoLi, po)
			}
		}
		// copy缺少的工序及工艺数据
		// 复制工序
		poDataLi, err := serialOrderPoLstDao.Select([]grmi.Predicate{
			omMeta.SerialOrderPOLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrder.OriginSerialOrderId),
		}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询原始工单工序数据失败, 错误:" + err.Error())
		}
		needInsertPoLi := make([]omModel.SerialOrderPOLst, 0, 20)
		for _, poData := range poDataLi {
			poData.SerialOrderId = serialOrderId
			needInsertPoLi = append(needInsertPoLi, poData)
			if len(needInsertPoLi)%20 == 0 {
				err = serialOrderPoLstDao.Insert(&needInsertPoLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("复制工序数据失败, 错误:" + err.Error())
				}
				needInsertPoLi = make([]omModel.SerialOrderPOLst, 0, 20)
			}
		}
		if len(needInsertPoLi) != 0 {
			if len(needInsertPoLi)%20 == 0 {
				err = serialOrderPoLstDao.Insert(&needInsertPoLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("复制工序数据失败, 错误:" + err.Error())
				}
			}
		}
		// 复制工步数据
		stepDataLi, err := serialOrderStepDao.Select([]grmi.Predicate{
			omMeta.SerialOrderStepLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrder.OriginSerialOrderId),
		}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询原始工单工步数据失败, 错误:" + err.Error())
		}
		needInsertStepLi := make([]omModel.SerialOrderStepLst, 0)
		for _, stepData := range stepDataLi {
			stepData.SerialOrderId = serialOrderId
			needInsertStepLi = append(needInsertStepLi, stepData)
			if len(needInsertStepLi)%20 == 0 {
				err = serialOrderStepDao.Insert(&needInsertStepLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("复制工步数据失败, 错误:" + err.Error())
				}
				needInsertStepLi = make([]omModel.SerialOrderStepLst, 0)
			}
		}
		if len(needInsertStepLi) != 0 {
			err = serialOrderStepDao.Insert(&needInsertStepLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("复制工步数据失败, 错误:" + err.Error())
			}
		}
		// 复制工艺数据
		opDetailLi, err := opDetailDao.Select([]grmi.Predicate{
			omMeta.SerialOrderOPDetail_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
		}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询原始工单工艺数据失败, 错误:" + err.Error())
		}
		needInsertOpDetailLi := make([]omModel.SerialOrderOPDetail, 0)
		for _, opDetail := range opDetailLi {
			opDetail.SerialOrderId = serialOrderId
			needInsertOpDetailLi = append(needInsertOpDetailLi, opDetail)
			if len(needInsertOpDetailLi)%20 == 0 {
				err = opDetailDao.Insert(&needInsertOpDetailLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("复制工艺数据失败, 错误:" + err.Error())
				}
				needInsertOpDetailLi = make([]omModel.SerialOrderOPDetail, 0)
			}
		}
		if len(needInsertOpDetailLi) != 0 {
			err = opDetailDao.Insert(&needInsertOpDetailLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("复制工艺数据失败, 错误:" + err.Error())
			}
		}
	}

	for _, po := range poLi {
		serialOrderPo, err := serialOrderPoLstDao.SelectOne(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工序数据失败， error:" + err.Error())
		}
		if serialOrderPo == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("工单工序不存在")
		}
		maxPos, err := serialOrderPOStatusRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("获取工单工序状态最后一条状态变更记录失败, error:" + err.Error())
		}
		serialOrderPoRec := omModel.SerialOrderPOStatusRecLst{
			SerialOrderId: serialOrderId,
			PO:            po,
			Pos:           maxPos + 1,
			PrevStatus:    serialOrderPo.Status,
			Status:        bmodel.WO_STATUS_STOP,
		}
		stepLi, err := serialOrderStepDao.Select([]grmi.Predicate{omMeta.SerialOrderStepLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
			omMeta.SerialOrderStepLst_PO.NewPredicate(grmi.Equal, po)}, []grmi.Field{omMeta.SerialOrderStepLst_StepNo})
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工步数据失败， error:" + err.Error())
		}
		if len(stepLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("未获取到工单工步数据")
		}
		opDetailLi, err := opDetailDao.Select([]grmi.Predicate{omMeta.SerialOrderOPDetail_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
			omMeta.SerialOrderOPDetail_PO.NewPredicate(grmi.Equal, po)}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单详细操作数据失败, error:" + err.Error())
		}
		if len(opDetailLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("未获取到工单详细操作")
		}
		stepStatusMaxPos, err := serialOrderStepRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工步状态变更数据失败， error:" + err.Error())
		}
		opDetailMaxPos, err := opDetailRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单详细操作状态变更数据失败， error:" + err.Error())
		}

		// 更新工单工序数据
		err = serialOrderPOStatusRecDao.InsertOne(&serialOrderPoRec)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("写入工单工序状态变更记录失败, error:" + err.Error())
		}
		serialOrderPo.Status = bmodel.WO_STATUS_STOP
		err = serialOrderPoLstDao.UpdateOne(serialOrderPo)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新工单工序数据失败, error:" + err.Error())
		}
		// 更新工单工步数据
		stepStatusRecLi := make([]omModel.SerialOrderStepStatusRecLst, 0, len(stepLi))
		for index, step := range stepLi {
			stepStatusRec := omModel.SerialOrderStepStatusRecLst{
				SerialOrderId: serialOrderId,
				OperationId:   step.OperationId,
				Pos:           stepStatusMaxPos + 1 + index,
				PO:            po,
				StepNo:        step.StepNo,
				PrevStatus:    step.Status,
				Status:        bmodel.WO_STATUS_STOP,
			}
			stepStatusRecLi = append(stepStatusRecLi, stepStatusRec)
			step.Status = bmodel.WO_STATUS_STOP
			err = serialOrderStepDao.UpdateOne(&step)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("更新工单工步状态失败, error:" + err.Error())
			}
		}
		tempStepStatusRecLi := make([]omModel.SerialOrderStepStatusRecLst, 0, 20)
		for index, statusRec := range stepStatusRecLi {
			if index != 0 && index%20 == 0 {
				err = serialOrderStepRecDao.Insert(&tempStepStatusRecLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("写入工单工步状态变更历史失败, error:" + err.Error())
				}
				tempStepStatusRecLi = make([]omModel.SerialOrderStepStatusRecLst, 0, 20)
			}
			tempStepStatusRecLi = append(tempStepStatusRecLi, statusRec)
		}
		if len(tempStepStatusRecLi) != 0 {
			err = serialOrderStepRecDao.Insert(&tempStepStatusRecLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("写入工单工步状态变更历史失败, error:" + err.Error())
			}
		}
		// 更新opdetail
		opDetailStatusRecLi := make([]omModel.SerialOrderOPDetailStatusRecLst, 0, len(opDetailLi))
		for index, opDetail := range opDetailLi {
			opDetailStatusRec := omModel.SerialOrderOPDetailStatusRecLst{
				DetailId:      opDetail.DetailId,
				Pos:           opDetailMaxPos + 1 + index,
				SerialOrderId: serialOrderId,
				PO:            po,
				SubArtId:      opDetail.SubArtId,
				StepNo:        opDetail.StepNo,
				OriginPos:     opDetail.Pos,
				StepType:      opDetail.StepType,
				PrevStatus:    opDetail.Status,
				Status:        bmodel.WO_STATUS_STOP,
			}
			opDetailStatusRecLi = append(opDetailStatusRecLi, opDetailStatusRec)
			opDetail.Status = bmodel.WO_STATUS_STOP
			err = opDetailDao.UpdateOne(&opDetail)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("更新工单详细操作状态失败, error:" + err.Error())
			}
		}
		tempOpdetailStatusRecLi := make([]omModel.SerialOrderOPDetailStatusRecLst, 0, 20)
		for index, statusRec := range opDetailStatusRecLi {
			if index != 0 && index%20 == 0 {
				err = opDetailRecDao.Insert(&tempOpdetailStatusRecLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("写入工单详细操作状态变更历史失败, error:" + err.Error())
				}
				tempOpdetailStatusRecLi = make([]omModel.SerialOrderOPDetailStatusRecLst, 0, 20)
			}
			tempOpdetailStatusRecLi = append(tempOpdetailStatusRecLi, statusRec)
		}
		if len(tempOpdetailStatusRecLi) != 0 {
			err = opDetailRecDao.Insert(&tempOpdetailStatusRecLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("写入工单详细操作状态变更历史失败, error:" + err.Error())
			}
		}
		// 写入repair info
		repairInfo := bmodel.RepairInfo{
			WorkLineId:    serialOrder.UsedResourceId,
			PO:            po,
			SerialOrderId: serialOrderId,
			RepairReason:  "线上返修",
			Status:        bmodel.WO_STATUS_STOP,
			IsActive:      1,
			LastModify:    grmi.DateTime(time.Now()),
			LastUser:      user.UserId,
			CreateTime:    grmi.DateTime(time.Now()),
		}
		_, err = session.Table("RepairInfo").InsertOne(&repairInfo)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("记录返修数据失败, error:" + err.Error())
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.FirstPartRepair
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SplitProduct(user *global.User, serialOrderId string) error {
	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "SelectSerialOrderProcess", "查询工单经历的所有工序")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单数据失败, error:" + err.Error())
	}
	if serialOrder == nil {
		return grmi.NewBusinessError("工单不存在")
	}
	packOrderItemDao := jitDal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId)
	packOrderItemLi, err := packOrderItemDao.Select([]grmi.Predicate{jitMeta.PackOrderItemLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId)}, nil)
	if err != nil {
		return grmi.NewBusinessError("查询包装单子项数据失败, error:" + err.Error())
	}
	if len(packOrderItemLi) == 0 {
		return grmi.NewBusinessError("该工单未进行打包")
	}
	var packOrderId string
	for _, packOrderItem := range packOrderItemLi {
		if packOrderItem.Status == jitModel.PACK_STATUS_CLOSED {
			packOrderId = packOrderItem.PackOrderId
		}
	}
	shipOrderDataLstDao := jitDal.NewShipOrderDataLstDAO(session, user.PlantNr, user.UserId)
	dataLi, err := shipOrderDataLstDao.Select([]grmi.Predicate{jitMeta.ShipOrderDataLst_PackOrderId.NewPredicate(grmi.Equal, packOrderId)}, nil)
	if err != nil {
		return grmi.NewBusinessError("查询发运项数据失败, error:" + err.Error())
	}
	if len(dataLi) == 0 {
		return grmi.NewBusinessError("该工单未发运")
	}
	var status int
	for _, data := range dataLi {
		if data.Status == jitModel.SHIP_STATUS_CLOSED {
			status = data.Status
		}
	}
	if status != jitModel.SHIP_STATUS_CLOSED {
		return grmi.NewBusinessError("该工单的未发运")
	}
	serialOrderStatusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	serialOrderStatus, err := serialOrderStatusDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单状态失败, error:" + err.Error())
	}
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED {
		return grmi.NewBusinessError("工单状态不是已完成")
	}
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
	}
	serialOrderStatusRec := omModel.SerialOrderStatusRecLst{
		SerialOrderId: serialOrderId,
		PrevStatus:    serialOrderStatus.Status,
		Status:        bmodel.WO_STATUS_STOP,
	}
	serialOrderStatusRecDao := omDal.NewSerialOrderStatusRecLstDAO(session, user.PlantNr, user.UserId)
	err = serialOrderStatusRecDao.InsertOne(&serialOrderStatusRec)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("写入工单状态变更历史失败, error:" + err.Error())
	}
	serialOrderStatus.Status = bmodel.WO_STATUS_STOP
	err = serialOrderStatusDao.UpdateOne(serialOrderStatus)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("更新工单状态失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/log/PackOrderService.BackOnline
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) BackOnlineFromShip(user *global.User, serialOrderId string, poLi []string) error {
	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "BackOnline", "重新上线")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	serialOrderPoLstDao := omDal.NewSerialOrderPOLstDAO(session, user.PlantNr, user.UserId)
	serialOrderPOStatusRecDao := omDal.NewSerialOrderPOStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrderStepDao := omDal.NewSerialOrderStepLstDAO(session, user.PlantNr, user.UserId)
	serialOrderStepRecDao := omDal.NewSerialOrderStepStatusRecLstDAO(session, user.PlantNr, user.UserId)
	opDetailDao := omDal.NewSerialOrderOPDetailDAO(session, user.PlantNr, user.UserId)
	opDetailRecDao := omDal.NewSerialOrderOPDetailStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	serialOrderStatusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)
	serialOrderStatusRecDao := omDal.NewSerialOrderStatusRecLstDAO(session, user.PlantNr, user.UserId)
	serialOrder, err := serialOrderDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单数据失败, error:" + err.Error())
	}
	if serialOrder == nil {
		return grmi.NewBusinessError("工单不存在")
	}
	serialOrderStatus, err := serialOrderStatusDao.SelectOne(serialOrderId)
	if err != nil {
		return grmi.NewBusinessError("查询工单状态失败, error:" + err.Error())
	}
	if serialOrderStatus == nil {
		return grmi.NewBusinessError("工单状态不存在")
	}
	packOrderItemDao := jitDal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId)
	packOrderItemLi, err := packOrderItemDao.Select([]grmi.Predicate{jitMeta.PackOrderItemLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId)}, nil)
	if err != nil {
		return grmi.NewBusinessError("查询包装单子项数据失败, error:" + err.Error())
	}
	if len(packOrderItemLi) == 0 {
		return grmi.NewBusinessError("该工单未进行打包")
	}
	var packOrderId string
	for _, packOrderItem := range packOrderItemLi {
		if packOrderItem.Status == jitModel.PACK_STATUS_CLOSED {
			packOrderId = packOrderItem.PackOrderId
		}
	}
	shipOrderDataLstDao := jitDal.NewShipOrderDataLstDAO(session, user.PlantNr, user.UserId)
	dataLi, err := shipOrderDataLstDao.Select([]grmi.Predicate{jitMeta.ShipOrderDataLst_PackOrderId.NewPredicate(grmi.Equal, packOrderId)}, nil)
	if err != nil {
		return grmi.NewBusinessError("查询发运项数据失败, error:" + err.Error())
	}
	if len(dataLi) == 0 {
		return grmi.NewBusinessError("该工单未发运")
	}
	var status int
	for _, data := range dataLi {
		if data.Status == jitModel.SHIP_STATUS_CLOSED {
			status = data.Status
		}
	}
	if status != jitModel.SHIP_STATUS_CLOSED {
		return grmi.NewBusinessError("该工单的未发运")
	}
	// 校验工单状态 只有80和75才可以进行线上返修
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED && serialOrderStatus.Status != bmodel.WO_STATUS_UNCHECKED {
		return grmi.NewBusinessError("该工单不允许线上返修")
	}
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
	}
	serialOrder.RepairFlag = true
	err = serialOrderDao.UpdateOne(serialOrder)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("更新工单数据失败, error:" + err.Error())
	}
	serialOrderStatusRec := omModel.SerialOrderStatusRecLst{
		SerialOrderId: serialOrderId,
		PrevStatus:    serialOrderStatus.Status,
		Status:        bmodel.WO_STATUS_RUNNING,
	}
	err = serialOrderStatusRecDao.InsertOne(&serialOrderStatusRec)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("写入工单状态变更记录失败, error:" + err.Error())
	}
	serialOrderStatus.Status = bmodel.WO_STATUS_RUNNING
	err = serialOrderStatusDao.UpdateOne(serialOrderStatus)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("更新工单状态失败, error:" + err.Error())
	}
	for _, po := range poLi {
		serialOrderPo, err := serialOrderPoLstDao.SelectOne(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工序数据失败， error:" + err.Error())
		}
		if serialOrderPo == nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("工单工序不存在")
		}
		maxPos, err := serialOrderPOStatusRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("获取工单工序状态最后一条状态变更记录失败, error:" + err.Error())
		}
		serialOrderPoRec := omModel.SerialOrderPOStatusRecLst{
			SerialOrderId: serialOrderId,
			PO:            po,
			Pos:           maxPos + 1,
			PrevStatus:    serialOrderPo.Status,
			Status:        bmodel.WO_STATUS_STOP,
		}
		stepLi, err := serialOrderStepDao.Select([]grmi.Predicate{omMeta.SerialOrderStepLst_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
			omMeta.SerialOrderStepLst_PO.NewPredicate(grmi.Equal, po)}, []grmi.Field{omMeta.SerialOrderStepLst_StepNo})
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工步数据失败， error:" + err.Error())
		}
		if len(stepLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("未获取到工单工步数据")
		}
		opDetailLi, err := opDetailDao.Select([]grmi.Predicate{omMeta.SerialOrderOPDetail_SerialOrderId.NewPredicate(grmi.Equal, serialOrderId),
			omMeta.SerialOrderOPDetail_PO.NewPredicate(grmi.Equal, po)}, nil)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单详细操作数据失败, error:" + err.Error())
		}
		if len(opDetailLi) == 0 {
			_ = session.Rollback()
			return grmi.NewBusinessError("未获取到工单详细操作")
		}
		stepStatusMaxPos, err := serialOrderStepRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单工步状态变更数据失败， error:" + err.Error())
		}
		opDetailMaxPos, err := opDetailRecDao.GetMaxPos(serialOrderId, po)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("查询工单详细操作状态变更数据失败， error:" + err.Error())
		}

		// 更新工单工序数据
		err = serialOrderPOStatusRecDao.InsertOne(&serialOrderPoRec)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("写入工单工序状态变更记录失败, error:" + err.Error())
		}
		serialOrderPo.Status = bmodel.WO_STATUS_STOP
		err = serialOrderPoLstDao.UpdateOne(serialOrderPo)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新工单工序数据失败, error:" + err.Error())
		}
		// 更新工单工步数据
		stepStatusRecLi := make([]omModel.SerialOrderStepStatusRecLst, 0, len(stepLi))
		for index, step := range stepLi {
			stepStatusRec := omModel.SerialOrderStepStatusRecLst{
				SerialOrderId: serialOrderId,
				OperationId:   step.OperationId,
				Pos:           stepStatusMaxPos + 1 + index,
				PO:            po,
				StepNo:        step.StepNo,
				PrevStatus:    step.Status,
				Status:        bmodel.WO_STATUS_STOP,
			}
			stepStatusRecLi = append(stepStatusRecLi, stepStatusRec)
			step.Status = bmodel.WO_STATUS_STOP
			err = serialOrderStepDao.UpdateOne(&step)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("更新工单工步状态失败, error:" + err.Error())
			}
		}
		tempStepStatusRecLi := make([]omModel.SerialOrderStepStatusRecLst, 0, 20)
		for index, statusRec := range stepStatusRecLi {
			if index != 0 && index%20 == 0 {
				err = serialOrderStepRecDao.Insert(&tempStepStatusRecLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("写入工单工步状态变更历史失败, error:" + err.Error())
				}
				tempStepStatusRecLi = make([]omModel.SerialOrderStepStatusRecLst, 0, 20)
			}
			tempStepStatusRecLi = append(tempStepStatusRecLi, statusRec)
		}
		if len(tempStepStatusRecLi) != 0 {
			err = serialOrderStepRecDao.Insert(&tempStepStatusRecLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("写入工单工步状态变更历史失败, error:" + err.Error())
			}
		}
		// 更新opdetail
		opDetailStatusRecLi := make([]omModel.SerialOrderOPDetailStatusRecLst, 0, len(opDetailLi))
		for index, opDetail := range opDetailLi {
			opDetailStatusRec := omModel.SerialOrderOPDetailStatusRecLst{
				DetailId:      opDetail.DetailId,
				Pos:           opDetailMaxPos + 1 + index,
				SerialOrderId: serialOrderId,
				PO:            po,
				SubArtId:      opDetail.SubArtId,
				StepNo:        opDetail.StepNo,
				OriginPos:     opDetail.Pos,
				StepType:      opDetail.StepType,
				PrevStatus:    opDetail.Status,
				Status:        bmodel.WO_STATUS_STOP,
			}
			opDetailStatusRecLi = append(opDetailStatusRecLi, opDetailStatusRec)
			opDetail.Status = bmodel.WO_STATUS_STOP
			err = opDetailDao.UpdateOne(&opDetail)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("更新工单详细操作状态失败, error:" + err.Error())
			}
		}
		tempOpdetailStatusRecLi := make([]omModel.SerialOrderOPDetailStatusRecLst, 0, 20)
		for index, statusRec := range opDetailStatusRecLi {
			if index != 0 && index%20 == 0 {
				err = opDetailRecDao.Insert(&tempOpdetailStatusRecLi)
				if err != nil {
					_ = session.Rollback()
					return grmi.NewBusinessError("写入工单详细操作状态变更历史失败, error:" + err.Error())
				}
				tempOpdetailStatusRecLi = make([]omModel.SerialOrderOPDetailStatusRecLst, 0, 20)
			}
			tempOpdetailStatusRecLi = append(tempOpdetailStatusRecLi, statusRec)
		}
		if len(tempOpdetailStatusRecLi) != 0 {
			err = opDetailRecDao.Insert(&tempOpdetailStatusRecLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("写入工单详细操作状态变更历史失败, error:" + err.Error())
			}
		}
		// 写入repair info
		repairInfo := bmodel.RepairInfo{
			WorkLineId:    serialOrder.UsedResourceId,
			PO:            po,
			SerialOrderId: serialOrderId,
			RepairReason:  "线上返修",
			Status:        bmodel.WO_STATUS_STOP,
			IsActive:      1,
			LastModify:    grmi.DateTime(time.Now()),
			LastUser:      user.UserId,
			CreateTime:    grmi.DateTime(time.Now()),
		}
		_, err = session.Table("RepairInfo").InsertOne(&repairInfo)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("记录返修数据失败, error:" + err.Error())
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/qm/ReclinerReclstService.SelectSeriByAssembly
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) SelectSeriByAssembly(user *global.User, orderId string) (*model.ReclinerReclst, error) {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	seriDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId)
	statusDao := omDal.NewSerialOrderStatusDAO(session, user.PlantNr, user.UserId)

	assemblyDao := omDal.NewSerialOrderOPDetailRecvDataLstDAO(session, user.PlantNr, user.UserId)

	seriInfo, err := seriDao.SelectBySerialOrder(orderId)
	if err != nil {
		return nil, err
	}
	if seriInfo == nil {
		//查询mes数据表，找到总成条码
		assemblyInfo, err := assemblyDao.SelectOneByAssembly(orderId)

		if err != nil {
			return nil, err
		}
		if assemblyInfo == nil {
			return nil, grmi.NewBusinessError("没有找到总成条码信息!")
		}
		seriInfo, err = seriDao.SelectBySerialOrder(assemblyInfo.SerialOrderId)
		if err != nil {
			return nil, err
		}
		if seriInfo == nil {
			return nil, grmi.NewBusinessError("条码信息不存在!")
		}
	}
	if seriInfo.GenerateFlag {
		//查询最近返修记录，
		newSerialOrder, err := seriDao.SelectByOriginSerialOrderId(seriInfo.SerialOrderId)
		if err != nil {
			return nil, err
		}
		if newSerialOrder != nil {
			seriInfo.SerialOrderId = newSerialOrder.SerialOrderId
		}

	}
	serialOrderStatus, err := statusDao.SelectOne(seriInfo.SerialOrderId)

	if err != nil {
		return nil, err
	}
	if serialOrderStatus == nil {
		return nil, grmi.NewBusinessError("不存在指定记录!")
	}
	if serialOrderStatus.Status != bmodel.WO_STATUS_FINISHED {
		return nil, grmi.NewBusinessError("条码状态未完成!")
	}
	entity := new(model.ReclinerReclst)
	entity.PlantNr = user.PlantNr
	entity.ArtId = seriInfo.ArtId
	entity.SerialOrderId = seriInfo.SerialOrderId
	entity.Line = seriInfo.PlanResourceId
	entity.RecType = model.QM_REPIR_TYPE
	entity.ReclinerNr = seriInfo.SerialOrderId
	return entity, nil
}

/******************************************************************************
 *
 * @Reference LAPP_GAAS_GFrame_BACKEND/services/qm/ReclinerReclstService.InsertSeriByAssembly
 *
 ******************************************************************************/
func (impl *ReclinerReclstServiceImplement) InsertSeriByAssembly(user *global.User, entity *model.ReclinerReclst) error {

	grmi.Log(user, "/services/qm/implments/ReclinerReclst.service.impl.go", "InsertOneReclinerReclst", "插入一个ReclinerReclst")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewReclinerReclstDAO(session, user.PlantNr, user.UserId)
	placeDao := baseDal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)

	//根据工位查询打印机Id
	placeInfo, err := placeDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		session.Rollback()
		return err
	}
	if placeInfo == nil {
		session.Rollback()
		return grmi.NewBusinessError("工位信息不存在!")
	}

	entity.ReclinerNr = entity.SerialOrderId
	entity.RecType = model.QM_REPIR_TYPE
	entity.PrinterId = placeInfo.PrinterId
	entity.Status = "N"
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}
