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

package implments

import (
	apiDal "LAPP_LF_MOM_BACKEND/dao/api"
	baseDal "LAPP_LF_MOM_BACKEND/dao/base"
	meDal "LAPP_LF_MOM_BACKEND/dao/me"
	apiMeta "LAPP_LF_MOM_BACKEND/meta/api"
	baseMeta "LAPP_LF_MOM_BACKEND/meta/base"
	"strings"

	//logDal "LAPP_LF_MOM_BACKEND/dao/log"
	dal "LAPP_LF_MOM_BACKEND/dao/om"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	"LAPP_LF_MOM_BACKEND/infra/logger"
	//logMeta "LAPP_LF_MOM_BACKEND/meta/log"
	meta "LAPP_LF_MOM_BACKEND/meta/om"
	baseModel "LAPP_LF_MOM_BACKEND/models/base"
	//logModel "LAPP_LF_MOM_BACKEND/models/log"
	model "LAPP_LF_MOM_BACKEND/models/om"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"strconv"
	"time"
)

/******************************************************************************
 *
 * @Description              : WorkOrder的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-22 16:45:05
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrder = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.WorkOrder_WorkOrderId.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrder_WorkOrderId, grmi.Equal, true),
		meta.WorkOrder_ArtId.UrlParameterName:                grmi.NewConditionItem(meta.WorkOrder_ArtId, grmi.Equal, false),
		meta.WorkOrder_CustArtId.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_CustArtId, grmi.Equal, false),
		meta.WorkOrder_RouteTemplateId.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrder_RouteTemplateId, grmi.Equal, false),
		meta.WorkOrder_OrderType.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_OrderType, grmi.Equal, false),
		meta.WorkOrder_OrderType1.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_OrderType1, grmi.Equal, false),
		meta.WorkOrder_OrderType2.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_OrderType2, grmi.Equal, false),
		meta.WorkOrder_OrderInfo.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_OrderInfo, grmi.Equal, false),
		meta.WorkOrder_CustOrderId.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrder_CustOrderId, grmi.Equal, false),
		meta.WorkOrder_ErpOrderId.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_ErpOrderId, grmi.Equal, false),
		meta.WorkOrder_ParentOrderId.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_ParentOrderId, grmi.Equal, false),
		meta.WorkOrder_Priority.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrder_Priority, grmi.Equal, false),
		meta.WorkOrder_PlanResourceGroupId.UrlParameterName:  grmi.NewConditionItem(meta.WorkOrder_PlanResourceGroupId, grmi.Equal, false),
		meta.WorkOrder_PlanResourceId.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_PlanResourceId, grmi.Equal, false),
		meta.WorkOrder_UsedResourceId.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_UsedResourceId, grmi.Equal, false),
		meta.WorkOrder_PlanQty.UrlParameterName:              grmi.NewConditionItem(meta.WorkOrder_PlanQty, grmi.Equal, false),
		meta.WorkOrder_QtyUomId.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrder_QtyUomId, grmi.Equal, false),
		meta.WorkOrder_RatePerHourToggle.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrder_RatePerHourToggle, grmi.Equal, false),
		meta.WorkOrder_TimePerItemToggle.UrlParameterName:    grmi.NewConditionItem(meta.WorkOrder_TimePerItemToggle, grmi.Equal, false),
		meta.WorkOrder_TimePerBatchToggle.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrder_TimePerBatchToggle, grmi.Equal, false),
		meta.WorkOrder_BatchTimeFieldToggle.UrlParameterName: grmi.NewConditionItem(meta.WorkOrder_BatchTimeFieldToggle, grmi.Equal, false),
		meta.WorkOrder_OpTimePerItem.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_OpTimePerItem, grmi.Equal, false),
		meta.WorkOrder_BatchTime.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_BatchTime, grmi.Equal, false),
		meta.WorkOrder_QuantityPerHour.UrlParameterName:      grmi.NewConditionItem(meta.WorkOrder_QuantityPerHour, grmi.Equal, false),
		meta.WorkOrder_BatchQuantity.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_BatchQuantity, grmi.Equal, false),
		meta.WorkOrder_TimeUomId.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_TimeUomId, grmi.Equal, false),
		meta.WorkOrder_BatchingMethod.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_BatchingMethod, grmi.Equal, false),
		meta.WorkOrder_PlanStartDate.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_PlanStartDate, grmi.Approximate, false),
		meta.WorkOrder_PlanEndDate.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrder_PlanEndDate, grmi.Approximate, false),
		meta.WorkOrder_SetupStartTime.UrlParameterName:       grmi.NewConditionItem(meta.WorkOrder_SetupStartTime, grmi.Approximate, false),
		meta.WorkOrder_SetupEndTime.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_SetupEndTime, grmi.Approximate, false),
		meta.WorkOrder_PlanStartTime.UrlParameterName:        grmi.NewConditionItem(meta.WorkOrder_PlanStartTime, grmi.Approximate, false),
		meta.WorkOrder_PlanEndTime.UrlParameterName:          grmi.NewConditionItem(meta.WorkOrder_PlanEndTime, grmi.Approximate, false),
		meta.WorkOrder_ActStartTime.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_ActStartTime, grmi.Approximate, false),
		meta.WorkOrder_ActEndTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_ActEndTime, grmi.Approximate, false),
		meta.WorkOrder_ProjectId.UrlParameterName:            grmi.NewConditionItem(meta.WorkOrder_ProjectId, grmi.Equal, false),
		meta.WorkOrder_CustomerId.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_CustomerId, grmi.Equal, false),
		meta.WorkOrder_CustomerName.UrlParameterName:         grmi.NewConditionItem(meta.WorkOrder_CustomerName, grmi.Equal, false),
		meta.WorkOrder_FixStartTimeToggle.UrlParameterName:   grmi.NewConditionItem(meta.WorkOrder_FixStartTimeToggle, grmi.Equal, false),
		meta.WorkOrder_LastModify.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_LastModify, grmi.Approximate, false),
		meta.WorkOrder_LastUser.UrlParameterName:             grmi.NewConditionItem(meta.WorkOrder_LastUser, grmi.Equal, false),
		meta.WorkOrder_CreateTime.UrlParameterName:           grmi.NewConditionItem(meta.WorkOrder_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : WorkOrder的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
var DefaultConditionOfWorkOrderAndPaging = grmi.NewCondition(
	DefaultConditionOfWorkOrder.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : WorkOrderServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : WorkOrder的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
type WorkOrderServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewWorkOrderDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个WorkOrderDAOImplement实例
 *
 * @Return Value             : WorkOrderDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-19 14:04:47
 *
 ******************************************************************************/
func NewWorkOrderServiceImplement() *WorkOrderServiceImplement {
	return &WorkOrderServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.InsertOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) InsertOne(user *models.Usertab, entity *model.WorkOrderJoin) error {
	log, _ := logger.NewLogger(user.Userid, "OM")
	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "InsertOneWorkOrder", "插入一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	err := session.Begin()
	if err != nil {
		return err
	}
	defer session.Close()

	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	statusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	qtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	//artDemandHeadDao := logDal.NewArtDemandHeadDAO(session, user.Pid, user.Userid)
	//artDemandLstDao := logDal.NewArtDemandLstDAO(session, user.Pid, user.Userid)
	workOrder := entity.WorkOrder
	workQty := entity.WorkOrderQty
	workStatus := entity.WorkOrderStatus
	if workStatus.Status != baseModel.WO_STATUS_UNPLANNED {
		return grmi.NewBusinessError("创建的订单状态不合规,status:" + strconv.Itoa(entity.Status))
	}
	endTime := workOrder.PlanEndTime.Restore()
	startTime := workOrder.PlanStartTime.Restore()
	if endTime.Before(startTime) || endTime.Equal(startTime) {
		return grmi.NewBusinessError("创建的订单状态计划时间不合规")
	}
	startDate := workOrder.PlanStartDate.Restore()
	endDate := workOrder.PlanEndDate.Restore()
	if endDate.Before(startDate) {
		return grmi.NewBusinessError("创建的订单状态计划时间不合规")
	}
	if workOrder.WorkOrderId == "" {
		snr := new(models.Snrtab)
		snr.Finr = user.Pid
		workOrderId, err := snr.GetNextSnrWithSession(session, "WorkOrder")
		if err != nil {
			_ = session.Rollback()
			log.Error("workOrder InsertOne get work order id failed, error: " + err.Error())

			return err
		}
		workOrder.WorkOrderId = workOrderId
	}

	workQty.WorkOrderId = workOrder.WorkOrderId
	workStatus.WorkOrderId = workOrder.WorkOrderId
	err = dao.InsertOne(workOrder)
	if err != nil {
		log.Error("workOrder InsertOne insert workOrder failed, error: " + err.Error())
		session.Rollback()
		return err
	}
	err = statusDao.InsertOne(workStatus)
	if err != nil {
		log.Error("workOrder InsertOne insert workOrder status failed, error: " + err.Error())
		session.Rollback()
		return err
	}
	err = qtyDao.InsertOne(workQty)
	if err != nil {
		log.Error("workOrder InsertOne insert workOrder qty failed, error: " + err.Error())
		session.Rollback()
		return err
	}
	// 联动处理供应商门户
	//artDemandHeadLi, err := artDemandHeadDao.SelectByEndDate(workOrder.ArtId, workOrder.PlanEndDate.Restore().Format("2006-01-02"))
	//if err != nil {
	//	session.Rollback()
	//	log.Error("WorkOrder InsertOne get art demand head, error: " + err.Error())
	//
	//	return err
	//}
	//var demandId string
	//demandTime := workOrder.PlanEndTime.Restore()
	//weekIndex := utils.ParseWeekIndex(demandTime)
	//if len(artDemandHeadLi) == 0 {
	//	snr := new(models.Snrtab)
	//	snr.Finr = user.Pid
	//	demandId, err = snr.GetNextSnr("ArtDemand")
	//	if err != nil {
	//		session.Rollback()
	//		log.Error("WorkOrder InsertOne generate art demand id failed, error: " + err.Error())
	//
	//		return err
	//	}
	//	artDemandHead := logModel.ArtDemandHead{
	//		Source: logModel.DemandSourceInterval,
	//		UnproducedQty: workQty.PlanQty,
	//		DemandId:    demandId,
	//		ArtId:       workOrder.ArtId,
	//		DemandType:  "byDay",
	//		DemandYear:  demandTime.Year(),
	//		DemandMonth: int(demandTime.Month()),
	//		DemandWeek:  weekIndex,
	//		DemandTime:  workOrder.PlanEndTime,
	//		DemandDate:  grmi.Date(demandTime),
	//		DemandQty:   workQty.PlanQty,
	//		Status:      logModel.WaitReceivingStatus,
	//		OrderStatus: workStatus.Status,
	//		QtyUomId:    "",
	//	}
	//	err = artDemandHeadDao.InsertOne(&artDemandHead)
	//	if err != nil {
	//		session.Rollback()
	//		log.Error("WorkOrder InsertOne art demand failed, error: " + err.Error())
	//		return err
	//	}
	//
	//} else {
	//	artDemandHead := artDemandHeadLi[0]
	//	demandId = artDemandHead.DemandId
	//	artDemandHead.OrderStatus = workStatus.Status
	//	artDemandHead.DemandQty = workQty.PlanQty
	//	artDemandHead.UnproducedQty = workQty.PlanQty
	//	err = artDemandHeadDao.UpdateOne(&artDemandHead)
	//	if err != nil {
	//		session.Rollback()
	//		log.Error("WorkOrder InsertOne art demand failed, error: " + err.Error())
	//		return err
	//	}
	//	err = artDemandLstDao.DeleteWhere([]grmi.Predicate{logMeta.ArtDemandLst_DemandId.NewPredicate(grmi.Equal, demandId)})
	//	if err != nil {
	//		session.Rollback()
	//		log.Error("WorkOrder InsertOne art demandLst failed, error: " + err.Error())
	//
	//		return err
	//	}
	//}

	//artDemandLstLi := make([]logModel.ArtDemandLst, 0, workQty.PlanQty)
	//for i := 0; i < int(workQty.ActQty); i++ {
	//	artDemandLst := logModel.ArtDemandLst{
	//		DemandId:    demandId,
	//		ArtId:       workOrder.ArtId,
	//		DemandType:  "byDay",
	//		DemandYear:  demandTime.Year(),
	//		DemandMonth: int(demandTime.Month()),
	//		DemandWeek:  weekIndex,
	//		DemandTime:  workOrder.PlanEndTime,
	//		DemandDate:  grmi.Date(demandTime),
	//		DemandQty:   1,
	//		Status:     logModel.WaitReceivingStatus,
	//		OrderStatus:  workStatus.Status,
	//		QtyUomId:    "",
	//	}
	//	artDemandLstLi = append(artDemandLstLi, artDemandLst)
	//}
	//// 批量插入时 一次最多插入50条
	//demandLstTempLi := make([]logModel.ArtDemandLst, 0, 50)
	//for index, demandLst := range artDemandLstLi {
	//	if index%50 == 0 {
	//		err = artDemandLstDao.Insert(&demandLstTempLi)
	//		if err != nil {
	//			session.Rollback()
	//			log.Error("WorkOrder InsertOne art demandLst failed, error: " + err.Error())
	//			return err
	//		}
	//		demandLstTempLi = make([]logModel.ArtDemandLst, 0, 50)
	//	}
	//	demandLstTempLi = append(demandLstTempLi, demandLst)
	//}
	//if len(demandLstTempLi) != 0 {
	//	err = artDemandLstDao.Insert(&demandLstTempLi)
	//	if err != nil {
	//		session.Rollback()
	//		log.Error("WorkOrder InsertOne art demandLst failed, error: " + err.Error())
	//		return err
	//	}
	//}

	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.DeleteOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) DeleteOne(user *models.Usertab, workOrderId string) error {
	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "DeleteOneWorkOrder", "删除一个WorkOrder")
	log, _ := logger.NewLogger(user.Userid, "OM")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	err := session.Begin()
	if err != nil {
		return err
	}
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	statusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	qtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	statusRecDao := dal.NewWorkOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	//artDemandHeadDao := logDal.NewArtDemandHeadDAO(session, user.Pid, user.Userid)
	//artDemandLstDao := logDal.NewArtDemandLstDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(workOrderId)
	if err != nil {
		log.Error("WorkOrder DeleteOne select workOrder failed, error: " + err.Error())
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定workOrder记录!")
	}
	statusRecord, err := statusDao.SelectOne(workOrderId)
	if err != nil {
		log.Error("WorkOrder DeleteOne select workOrder status failed, error: " + err.Error())
		return err
	}
	if statusRecord == nil {
		return grmi.NewBusinessError("不存在指定workOrderStatus记录!")
	}
	qtyRecord, err := qtyDao.SelectOne(workOrderId)
	if err != nil {
		log.Error("WorkOrder DeleteOne select workOrder qty failed, error: " + err.Error())
		return err
	}
	if qtyRecord == nil {
		return grmi.NewBusinessError("不存在指定workOrderQty记录!")
	}
	if statusRecord.Status != baseModel.WO_STATUS_UNPLANNED {
		return grmi.NewBusinessError("该workOrder不可以删除, status:" + strconv.Itoa(statusRecord.Status))
	}
	//maxPos, err := statusRecDao.GetMaxPos(record.WorkOrderId)
	//if err != nil {
	//	log.Error("WorkOrder DeleteOne get status rec max pos failed, error: " + err.Error())
	//	session.Rollback()
	//	return err
	//}
	var workOrderStatusRec model.WorkOrderStatusRecLst
	workOrderStatusRec.WorkOrderId = record.WorkOrderId
	workOrderStatusRec.PrevStatus = statusRecord.Status
	workOrderStatusRec.Status = baseModel.WO_STATUS_CANCELED
	//workOrderStatusRec.Pos = maxPos + 1
	err = statusRecDao.InsertOne(&workOrderStatusRec)
	if err != nil {
		log.Error("WorkOrder DeleteOne insert status rec failed, error: " + err.Error())
		session.Rollback()
		return err
	}
	statusRecord.Status = baseModel.WO_STATUS_CANCELED
	err = statusDao.UpdateOne(statusRecord)
	if err != nil {
		log.Error("WorkOrder DeleteOne update work order status failed, error: " + err.Error())
		session.Rollback()
		return err
	}
	// 从供应商门户处处理对应的需求数据
	//artDemandHeadLi, err := artDemandHeadDao.SelectByEndDate(record.ArtId, record.PlanEndDate.Restore().Format("2006-01-02"))
	//if err != nil {
	//	log.Error("WorkOrder DeleteOne get art demand head failed, error: " + err.Error())
	//	session.Rollback()
	//	return err
	//}
	//if len(artDemandHeadLi) != 0 {
	//	artDemandHead := artDemandHeadLi[0]
	//	artDemandHead.OrderStatus = statusRecord.Status
	//	err = artDemandHeadDao.UpdateOne(&artDemandHead)
	//	if err != nil {
	//		log.Error("WorkOrder DeleteOne update art demand head failed, error: " + err.Error())
	//		session.Rollback()
	//		return err
	//	}
	//	//err = artDemandLstDao.DeleteWhere([]grmi.Predicate{logMeta.ArtDemandLst_DemandId.NewPredicate(grmi.Equal, artDemandHead.DemandId)})
	//	//if err != nil {
	//	//	log.Error("WorkOrder DeleteOne delete art demand lst failed, error: " + err.Error())
	//	//	session.Rollback()
	//	//	return err
	//	//}
	//}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.SelectOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) SelectOne(user *models.Usertab, workOrderId string) (*model.WorkOrderJoin, error) {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "SelectOneWorkOrder", "查询一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	statusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	qtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	workOrder, err := dao.SelectOne(workOrderId)
	if err != nil {
		return nil, err
	}
	if workOrder == nil {
		return nil, grmi.NewBusinessError("workOrder不存在")
	}
	workOrderStatus, err := statusDao.SelectOne(workOrderId)
	if err != nil {
		return nil, err
	}
	workOrderQty, err := qtyDao.SelectOne(workOrderId)
	if err != nil {
		return nil, err
	}
	data := &model.WorkOrderJoin{
		WorkOrder:       workOrder,
		WorkOrderQty:    workOrderQty,
		WorkOrderStatus: workOrderStatus,
	}

	return data, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.UpdateOne
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) UpdateOne(user *models.Usertab, entity *model.WorkOrderJoin) error {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "UpdateOneWorkOrder", "修改一个WorkOrder")
	//log, _ := logger.NewLogger(user.Userid, "OM")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	if err := session.Begin(); err != nil {
		return err
	}
	defer session.Close()
	workOrder := entity.WorkOrder
	workOrderStatus := entity.WorkOrderStatus
	workOrderQty := entity.WorkOrderQty
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	statusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	qtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	//artDemandHeadDao := logDal.NewArtDemandHeadDAO(session, user.Pid, user.Userid)
	workOrderRecord, err := dao.SelectOne(workOrder.WorkOrderId)
	if err != nil {
		return err
	}
	if workOrderRecord == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	statusRecord, err := statusDao.SelectOne(workOrder.WorkOrderId)
	if err != nil {
		return err
	}
	if statusRecord == nil {
		return grmi.NewBusinessError("不存在指定的WorkOrderStatus")
	}
	qtyRecord, err := qtyDao.SelectOne(workOrder.WorkOrderId)
	if err != nil {
		return err
	}
	if qtyRecord == nil {
		return grmi.NewBusinessError("不存在指定的WorkOrderQty")
	}
	if workOrderStatus.Status != statusRecord.Status {
		return grmi.NewBusinessError("workOrderStatus不允许修改")
	}
	err = dao.UpdateOne(workOrder)
	if err != nil {
		session.Rollback()
		return err
	}
	workOrderQty.WorkOrderId = qtyRecord.WorkOrderId
	err = qtyDao.UpdateOne(workOrderQty)
	if err != nil {
		session.Rollback()
		return err
	}
	// 从供应商门户处处理对应的需求数据
	//artDemandHeadLi, err := artDemandHeadDao.SelectByEndDate(workOrderRecord.ArtId, workOrderRecord.PlanEndDate.Restore().Format("2006-01-02"))
	//if err != nil {
	//	log.Error("WorkOrder UpdateOne get art demand head failed, error: " + err.Error())
	//	session.Rollback()
	//	return err
	//}
	//if len(artDemandHeadLi) != 0 {
	//	artDemandHead := artDemandHeadLi[0]
	//	artDemandHead.OrderStatus = statusRecord.Status
	//	artDemandHead.DemandQty = qtyRecord.PlanQty
	//	artDemandHead.UnproducedQty = qtyRecord.PlanQty
	//	err = artDemandHeadDao.UpdateOne(&artDemandHead)
	//	if err != nil {
	//		log.Error("WorkOrder UpdateOne update art demand head failed, error: " + err.Error())
	//		session.Rollback()
	//		return err
	//	}
	//	//err = artDemandLstDao.DeleteWhere([]grmi.Predicate{logMeta.ArtDemandLst_DemandId.NewPredicate(grmi.Equal, artDemandHead.DemandId)})
	//	//if err != nil {
	//	//	log.Error("WorkOrder DeleteOne delete art demand lst failed, error: " + err.Error())
	//	//	session.Rollback()
	//	//	return err
	//	//}
	//}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.Insert
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Insert(user *models.Usertab, entities *[]model.WorkOrder) error {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "InsertWorkOrder", "插入多个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.Delete
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Delete(user *models.Usertab, entities *[]model.WorkOrder) error {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "DeleteWorkOrder", "删除多个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.Select
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "SelectWorkOrder", "查询WorkOrder")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	var projectId string
	var artId string
	var timeStart string
	var timeEnd string
	var workOrderId string
	var orderType string
	var statusStr string
	var pageNumberStr string
	var pageSizeStr string
	projectId, _ = urlParameters["ProjectId"]
	artId, _ = urlParameters["ArtId"]
	workOrderId, _ = urlParameters["WorkOrderId"]
	timeStart, _ = urlParameters["start"]
	timeEnd, _ = urlParameters["end"]
	orderType, _ = urlParameters["OrderType"]
	statusStr, _ = urlParameters["status"]
	pageNumberStr, _ = urlParameters["pageNumber"]
	pageSizeStr, _ = urlParameters["pageSize"]
	status, err := strconv.Atoi(statusStr)
	if err != nil {
		status = 0
	}
	if timeStart != "" {
		_, err = time.ParseInLocation("2006-01-02 15:04:05", timeStart, utils.TimezoneLocation)
		if err != nil {
			return nil, err
		}
	}
	if timeEnd != "" {
		_, err = time.ParseInLocation("2006-01-02 15:04:05", timeEnd, utils.TimezoneLocation)
		if err != nil {
			return nil, err
		}
	}
	pageNumber, err := strconv.Atoi(pageNumberStr)
	if err != nil {
		pageNumber = 1
	}
	pageSize, err := strconv.Atoi(pageSizeStr)
	if err != nil {
		pageSize = 10
	}
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectByPage(projectId, artId, timeStart, timeEnd, workOrderId, orderType, status, pageNumber, pageSize)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.Update
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Update(user *models.Usertab, entities *[]model.WorkOrder) error {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "UpdateWorkOrder", "修改多个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.Revocation
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) Revocation(user *models.Usertab, entities []string) error {

	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "UpdateOneWorkOrder", "修改一个WorkOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return err
	}
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	statusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	qtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	for _, workOrderId := range entities {
		workOrderRecord, err := dao.SelectOne(workOrderId)
		if err != nil {
			return err
		}
		if workOrderRecord == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
		statusRecord, err := statusDao.SelectOne(workOrderId)
		if err != nil {
			return err
		}
		if statusRecord == nil {
			return grmi.NewBusinessError("不存在指定的WorkOrderStatus")
		}
		qtyRecord, err := qtyDao.SelectOne(workOrderId)
		if err != nil {
			return err
		}
		if qtyRecord == nil {
			return grmi.NewBusinessError("不存在指定的WorkOrderQty")
		}
		if statusRecord.Status > baseModel.WO_STATUS_PLANNED {
			return grmi.NewBusinessError("该订单状态不支持撤销,status:" + strconv.Itoa(statusRecord.Status))
		}
		statusRecord.Status = baseModel.WO_STATUS_UNPLANNED
		err = statusDao.UpdateOne(statusRecord)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.CancelWorkOrder
 *
 ******************************************************************************/
func (impl *WorkOrderServiceImplement) CancelWorkOrder(user *models.Usertab, workOrderId string, qty float64) (err error) {
	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "CancelWorkOrder", "取消workOrder中指定数量的serialOrder")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err = session.Begin(); err != nil {
		return err
	}
	dao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	workOrderQtyDao := dal.NewWorkOrderQtyDAO(session, user.Pid, user.Userid)
	workOrderStatusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	workOrderStatusRecDao := dal.NewWorkOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	serialOrderDao := dal.NewSerialOrderDAO(session, user.Pid, user.Userid)
	serialOrderStatusDao := dal.NewSerialOrderStatusDAO(session, user.Pid, user.Userid)
	serialOrderStatusRecDao := dal.NewSerialOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	workOrder, err := dao.SelectOneJoin(workOrderId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workOrder == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("workOrder不存在")
	}
	if workOrder.WorkOrderStatus.Status != baseModel.WO_STATUS_LOCKED && workOrder.WorkOrderStatus.Status != baseModel.WO_STATUS_RELEASED && workOrder.WorkOrderStatus.Status != baseModel.WO_STATUS_RUNNING {
		_ = session.Rollback()
		return grmi.NewBusinessError("workOrder目前的状态不支持该操作")
	}
	if qty > (workOrder.WorkOrderQty.PlanQty - workOrder.WorkOrderQty.ReleasedQty) {
		_ = session.Rollback()
		return grmi.NewBusinessError("workOrder未派工数量不足")
	}
	workOrder.WorkOrderQty.ReleasedQty += qty
	workOrder.WorkOrderQty.ActQty3 += qty
	workOrder.WorkOrderQty.ActQty += qty
	err = workOrderQtyDao.UpdateOne(&workOrder.WorkOrderQty)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workOrder.WorkOrderQty.PlanQty == workOrder.WorkOrderQty.ReleasedQty && workOrder.WorkOrderQty.ReleasedQty == workOrder.WorkOrderQty.ActQty {
		var status int
		if workOrder.WorkOrderQty.PlanQty == workOrder.WorkOrderQty.ActQty3 {
			status = baseModel.WO_STATUS_CANCELED
		} else {
			status = baseModel.WO_STATUS_FINISHED
		}
		workOrderStatusRec := model.WorkOrderStatusRecLst{
			WorkOrderId: workOrderId,
			PrevStatus:  workOrder.WorkOrderStatus.Status,
			Status:      status,
		}
		err = workOrderStatusRecDao.InsertOne(&workOrderStatusRec)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		workOrder.WorkOrderStatus.Status = status
		err = workOrderStatusDao.UpdateOne(&workOrder.WorkOrderStatus)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	serialOrderLi, err := serialOrderDao.SelectByWorkOrderId(workOrderId)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if len(serialOrderLi) < int(qty) {
		_ = session.Rollback()
		return grmi.NewBusinessError("未派工的序列工单不足")
	}
	for i := 0; i < int(qty); i++ {
		serialOrder := serialOrderLi[i]
		serialOrderStatusRec := model.SerialOrderStatusRecLst{
			SerialOrderId: serialOrder.SerialOrderId,
			PrevStatus:    serialOrder.SerialOrderStatus.Status,
			Status:        baseModel.WO_STATUS_CANCELED,
		}
		err = serialOrderStatusRecDao.InsertOne(&serialOrderStatusRec)
		if err != nil {
			_ = session.Rollback()
			return err
		}
		serialOrder.SerialOrderStatus.Status = baseModel.WO_STATUS_CANCELED
		err = serialOrderStatusDao.UpdateForStatus(&serialOrder.SerialOrderStatus, baseModel.WO_STATUS_LOCKED)
		if err != nil {
			_ = session.Rollback()
			return err
		}
	}
	slaves := db.Eloquent.Slaves()
	if len(slaves) == 0 {
		return grmi.NewBusinessError("未获取到mes的数据库")
	}
	mesEngine := slaves[0]
	mesSession := mesEngine.NewSession()
	defer mesSession.Close()
	if err = mesSession.Begin(); err != nil {
		_ = session.Rollback()
		_ = mesSession.Rollback()
		return err
	}
	headerDao := apiDal.NewACCWOWORKORDERHEADERDAO(mesSession, user.Userid)
	mesWorkOrderLi, err := headerDao.Select([]grmi.Predicate{apiMeta.ACCWOWORKORDERHEADER_WONO.NewPredicate(grmi.Equal, workOrderId)}, nil, 1)
	if err != nil {
		_ = mesSession.Rollback()
		_ = session.Rollback()
		return err
	}
	if len(mesWorkOrderLi) == 0 {
		_ = mesSession.Rollback()
		_ = session.Rollback()
		return grmi.NewBusinessError("未从mes系统获取到workOrder")
	}
	mesWorkOrder := mesWorkOrderLi[0]
	preQty := mesWorkOrder.QUANTITY
	mesWorkOrder.QUANTITY += int(qty)
	if mesWorkOrder.QUANTITY == mesWorkOrder.PLANQUANTITY {
		mesWorkOrder.STATUS = model.MesOrderStatusAccomplished
		mesWorkOrder.FINISHTIME = grmi.DateTime(time.Now())
	}
	err = headerDao.UpdateForQty(&mesWorkOrder, preQty)
	if err != nil {
		_ = mesSession.Rollback()
		_ = session.Rollback()
		return err
	}
	_ = mesSession.Commit()
	_ = session.Commit()
	return nil

}

/******************************************************************************
*
* @Reference LAPP_LF_MOM_BACKEND/services/om/WorkOrderService.LockWorkOrder
*
******************************************************************************/
func (impl *WorkOrderServiceImplement) LockWorkOrder(user *models.Usertab, workOrderIds []string) ([]model.LockedResponseWorkOrder, error) {
	grmi.Log(user, "/services/om/implments/WorkOrder.service.impl.go", "LockWorkOrder", "锁定workOrder")
	result := make([]model.LockedResponseWorkOrder, 0, len(workOrderIds))
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	if err != nil {
		return nil, err
	}
	dao := dal.NewSerialOrderDAO(session, user.Pid, user.Userid)
	workOrderDao := dal.NewWorkOrderDAO(session, user.Pid, user.Userid)
	workOrderStatusDao := dal.NewWorkOrderStatusDAO(session, user.Pid, user.Userid)
	workOrderStatusRecDao := dal.NewWorkOrderStatusRecLstDAO(session, user.Pid, user.Userid)
	serialOrderStatusDao := dal.NewSerialOrderStatusDAO(session, user.Pid, user.Userid)
	projectDao := meDal.NewProjectDAO(session, user.Pid, user.Userid)
	operationDao := baseDal.NewOperationDAO(session, user.Pid, user.Userid)
	PODao := dal.NewSerialOrderPOLstDAO(session, user.Pid, user.Userid)
	stepDao := dal.NewSerialOrderStepLstDAO(session, user.Pid, user.Userid)
	artRelDao := baseDal.NewArticleRelLstDAO(session, user.Pid, user.Userid)
	ruleDao := baseDal.NewArtCheckRuleDAO(session, user.Pid, user.Userid)
	allocateDao := baseDal.NewArtQtyAllocateDAO(session, user.Pid, user.Userid)
	opcComRuleDao := baseDal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	//opcComDetailDao := baseDal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	serialOrderDetailDao := dal.NewSerialOrderOPDetailDAO(session, user.Pid, user.Userid)
	// 获取项目数据 用于获取流水号
	project, err := projectDao.SelectOne(baseModel.ProjectId)
	if err != nil {
		_ = session.Rollback()
		return nil, grmi.NewBusinessError("获取项目数据失败, error:" + err.Error())
	}
	if project == nil {
		_ = session.Rollback()
		return nil, grmi.NewBusinessError("不存在该项目数据")
	}
	// 进入业务处理逻辑
	for _, workOrderId := range workOrderIds {
		// 进行对应的批次工单数据校验
		vomWorkOrder, err := workOrderDao.SelectOneJoin(workOrderId)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("获取批次工单数据失败,error:" + err.Error())
		}
		if vomWorkOrder == nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("不存在对应的workOrder, workOrderId:" + workOrderId)
		}

		if vomWorkOrder.WorkOrderStatus.Status != baseModel.WO_STATUS_PLANNED {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("对应的workOrderStatus 不是 WO_STATUS_PLANNED，workOrderId:" + workOrderId)
		}
		// 检测在系统中对用的批次工单是否存在对应的序列工单
		serialOrderLi, err := dao.Select([]grmi.Predicate{meta.SerialOrder_WorkOrderId.NewPredicate(grmi.Equal, workOrderId)}, nil)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("查询原有序列工单数据失败, error:" + err.Error())
		}
		if len(serialOrderLi) > 0 {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("已存在对应的SerialOrder, workOrderId:" + workOrderId)
		}
		serialOrderLi = make([]model.SerialOrder, 0, int(vomWorkOrder.WorkOrder.PlanQty))
		serialOrderStatusLi := make([]model.SerialOrderStatus, 0, int(vomWorkOrder.WorkOrder.PlanQty))
		if vomWorkOrder.WorkOrder.PlanQty == 0 {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("订单的计划数量为0,workOrderID:" + workOrderId)
		}
		// 进行批次工单的时间拆解到序列工单的计划时间上
		endTime := vomWorkOrder.WorkOrder.PlanEndTime.Restore()
		now := time.Now()
		var perDuration float64
		if !now.Before(endTime) {
			perDuration = 0
		} else {
			perDuration = endTime.Sub(now).Seconds() / vomWorkOrder.WorkOrder.PlanQty
		}
		// 生成序列工单
		for i := 0; i < int(vomWorkOrder.WorkOrder.PlanQty); i++ {
			snr := new(models.Snrtab)
			snr.Finr = user.Pid
			serialNum, err := snr.GetNextSnrWithSession(session, project.SerialOrderSnr)
			if err != nil {
				_ = session.Rollback()
				return nil, grmi.NewBusinessError("获取序列工单流水号失败, error:" + err.Error())
			}
			planStartDate := vomWorkOrder.WorkOrder.PlanStartDate.Restore().Format("20060102")
			prefix := vomWorkOrder.WorkOrder.ArtId + baseModel.PlantName + planStartDate
			serialOrderId := strings.Replace(serialNum, "SO-", prefix, 1)
			var planEndTime time.Time
			if perDuration != 0 {
				planEndTime = now.Add(time.Duration(perDuration*float64(i+1)) * time.Second)
			} else {
				planEndTime = vomWorkOrder.WorkOrder.PlanEndTime.Restore()
			}
			serialOrder := model.SerialOrder{
				WorkOrderId:    workOrderId,
				SerialOrderId:  serialOrderId,
				ArtId:          vomWorkOrder.WorkOrder.ArtId,
				OrderType1:     vomWorkOrder.WorkOrder.OrderType1,
				OrderType2:     vomWorkOrder.WorkOrder.OrderType2,
				PlanResourceId: vomWorkOrder.WorkOrder.PlanResourceId,
				UsedResourceId: vomWorkOrder.WorkOrder.UsedResourceId,
				PlanQty:        1,
				QtyUomId:       vomWorkOrder.WorkOrder.QtyUomId,
				OpTimePerItem:  vomWorkOrder.WorkOrder.OpTimePerItem,
				TimeUomId:      vomWorkOrder.WorkOrder.TimeUomId,
				PlanStartDate:  vomWorkOrder.WorkOrder.PlanStartDate,
				PlanEndDate:    vomWorkOrder.WorkOrder.PlanEndDate,
				SetupStartTime: vomWorkOrder.WorkOrder.SetupStartTime,
				SetupEndTime:   vomWorkOrder.WorkOrder.SetupEndTime,
				PlanStartTime:  vomWorkOrder.WorkOrder.PlanStartTime,
				PlanEndTime:    grmi.DateTime(planEndTime),
				ActStartTime:   vomWorkOrder.WorkOrder.ActStartTime,
				ActEndTime:     vomWorkOrder.WorkOrder.ActEndTime,
				ProjectId:      vomWorkOrder.WorkOrder.ProjectId,
			}
			serialOrderLi = append(serialOrderLi, serialOrder)
			serialStatus := model.SerialOrderStatus{
				Status:        baseModel.WO_STATUS_LOCKED,
				SerialOrderId: serialOrderId,
			}
			serialOrderStatusLi = append(serialOrderStatusLi, serialStatus)
		}
		for _, serialOrder := range serialOrderLi {
			err = dao.InsertOne(&serialOrder)
			if err != nil {
				_ = session.Rollback()
				return nil, grmi.NewBusinessError("插入序列工单数据失败,error:" + err.Error())
			}
		}
		for _, serialOrderStatus := range serialOrderStatusLi {
			err = serialOrderStatusDao.InsertOne(&serialOrderStatus)
			if err != nil {
				_ = session.Rollback()
				return nil, grmi.NewBusinessError("插入序列工单状态失败,error:" + err.Error())
			}
		}
		// 修改批次工单状态 并记录状态变化
		workOrderStatusRec := model.WorkOrderStatusRecLst{
			WorkOrderId:        workOrderId,
			PrevStatus:         vomWorkOrder.WorkOrderStatus.Status,
			Status:             baseModel.WO_STATUS_LOCKED,
			StartTime:          vomWorkOrder.WorkOrder.PlanStartTime,
			EndTime:            vomWorkOrder.WorkOrder.PlanEndTime,
			OutputEvent:        baseModel.BATORD_EVENT_LOCK,
			OutputStatus:       baseModel.OUTPUT_TRIGGER_UNHANDLED,
			TriggerObjectId:    vomWorkOrder.WorkOrderStatus.TriggerObjectId,
			TriggerSubObjectId: vomWorkOrder.WorkOrderStatus.TriggerSubObjectId,
		}
		err = workOrderStatusRecDao.InsertOne(&workOrderStatusRec)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("插入批次工单状态数据失败,error:" + err.Error())
		}
		vomWorkOrder.WorkOrderStatus.Status = baseModel.WO_STATUS_LOCKED
		err = workOrderStatusDao.UpdateOne(&vomWorkOrder.WorkOrderStatus)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("更新批次工单状态失败,error:" + err.Error())
		}
		artId := vomWorkOrder.WorkOrder.ArtId
		// 生成序列工单对应的工艺数据
		articleLi, err := artRelDao.Select([]grmi.Predicate{baseMeta.ArticleRelLst_PID.NewPredicate(grmi.Equal, artId)}, nil)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("获取物料关联关系失败, error:" + err.Error())
		}
		if len(articleLi) == 0 {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("未获取到零件关联关系")
		}
		artIdLi := make([]string, 0, len(articleLi))
		artRelMap := make(map[string]baseModel.ArticleRelLst)
		for _, article := range articleLi {
			artIdLi = append(artIdLi, article.ArtId)
			artRelMap[article.ArtId] = article
		}
		opLi, err := operationDao.GetOperationRelData(artIdLi, vomWorkOrder.WorkOrder.PlanResourceId)
		if err != nil {
			_ = session.Rollback()
			return nil, grmi.NewBusinessError("获取工艺操作失败,error:" + err.Error())
		}
		allocateMap := make(map[string][]baseModel.ArtQtyAllocate)
		checkRuleMap := make(map[string]baseModel.ArtCheckRule)
		for _, serialOrderStatus := range serialOrderStatusLi {
			var PO int
			processPos := 1
			opPos := 1
			for _, op := range opLi {
				if PO != op.Process.PO {
					PO = op.Process.PO
					serialOrderWorkPlace := model.SerialOrderPOLst{
						SerialOrderId: serialOrderStatus.SerialOrderId,
						PO:            PO,
						Pos:           processPos,
						Status: baseModel.WO_STATUS_LOCKED,
					}
					err = PODao.InsertOne(&serialOrderWorkPlace)
					if err != nil {
						_ = session.Rollback()
						return nil, grmi.NewBusinessError("插入serialOrder工序数据失败,error:" + err.Error())
					}
					processPos++

				}
				serialOrderStep := model.SerialOrderStepLst{
					PO:            op.Process.PO,
					SerialOrderId: serialOrderStatus.SerialOrderId,
					StepNo:        op.Operation.StepNo,
					Pos:           opPos,
					SubArtId:      op.ArticleAtcodLst.ArtId,
					StepDesc:      op.StepType.StepTypeDesc,
					AttrCode:      op.ArticleAtcodLst.AttrCode,
					AttrValue:     op.ArticleAtcodLst.AttrValue,
					StepType:      op.StepType.StepTypeName,
					Status: baseModel.WO_STATUS_LOCKED,
				}
				opPos++
				err = stepDao.InsertOne(&serialOrderStep)
				if err != nil {
					_ = session.Rollback()
					return nil, grmi.NewBusinessError("插入序列工单工艺数据失败, error:" + err.Error())
				}
				if serialOrderStep.StepType == model.STEP_CHECK_RULE {
					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 {
							_ = session.Rollback()
							return 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, model.MES_MATCH_ALL)}, nil)
							if err != nil {
								_ = session.Rollback()
								return nil, grmi.NewBusinessError("获取零件检验规则失败, error:" + err.Error())
							}
							if len(ruleLi) == 0 {
								_ = session.Rollback()
								return nil, grmi.NewBusinessError("未获取到零件检验规则, artId:" + serialOrderStep.SubArtId)
							}
						}
						rule = ruleLi[0]
					}
					var num int
					allocateLi, exist := allocateMap[op.ArticleAtcodLst.ArtId]
					composeQty := int(artRelMap[serialOrderStep.SubArtId].ComposeQty)
					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 {
							_ = session.Rollback()
							return nil, grmi.NewBusinessError("获取特殊工位数量分配数据失败，错误:" + err.Error())
						}
					}
					if len(allocateLi) == 0 {
						num = composeQty
					} else {
						var allNum int
						for _, allocate := range allocateLi {
							if allocate.PO == serialOrderStep.PO {
								if allocate.SubObject == model.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,
							PO:   serialOrderStep.PO,
							SubArtId:      serialOrderStep.SubArtId,
							Pos:           pos,
							StepNo: serialOrderStep.StepNo,
							StepType:      model.STEP_CHECK_RULE,
							Status: baseModel.WO_STATUS_LOCKED,
							// 扫码校验 AttriCodeDesc
							CharPara1: rule.AttriCodeDesc,
							// 扫码校验 SubObject
							CharPara2: rule.SubObject,
							// 扫码校验 CodeLength
							CharPara3: rule.CodeLength,
							CharPara4: rule.CheckObject,
							IntPara1:  rule.AttriCode,
							// 扫码校验 AttriCode
							IntPara2:  rule.CheckStartPos,
							IntPara3:  rule.CheckLength,
							FlagPara1: rule.OnlyBarCodeFlag,
						}
						pos++
						err = serialOrderDetailDao.InsertOne(&op)
						if err != nil {
							_ = session.Rollback()
							return nil, grmi.NewBusinessError("插入操作明细记录失败，error:" + err.Error())
						}
					}

				} else if serialOrderStep.StepType == model.STEP_OPC_RULE {
					opcDataLi, err := opcComRuleDao.SelectOPCJoinData(serialOrderStep.PO, serialOrderStep.StepNo)
					if err != nil {
						_ = session.Rollback()
						return nil, grmi.NewBusinessError("获取OPC通信规则数据失败，error:" + err.Error())
					}
					pos := 1
					for _, opc := range opcDataLi {
						op := model.SerialOrderOPDetail{
							SerialOrderId: serialOrderStep.SerialOrderId,
							PO:   serialOrderStep.PO,
							SubArtId:      serialOrderStep.SubArtId,
							Pos:           pos,
							StepType:      model.STEP_OPC_RULE,
							Status: baseModel.WO_STATUS_LOCKED,
							StepNo: serialOrderStep.StepNo,
							// OPC STEP DESC
							CharPara1: opc.OPCComRule.StepDesc,
							// OPC BRAND NAME
							CharPara2: opc.OPCBasicComInfo.BrandName,
							// OPC MACHINE TYPE
							CharPara3: opc.OPCBasicComInfo.MachineType,
							// OPC IP ADDRESS
							CharPara4: opc.OPCBasicComInfo.IpAddress,
							// OPC ACTION TYPE
							CharPara5: opc.OPCComRule.ActionType,
							// OPC DBLOCK ADDRESS
							CharPara6: opc.OPCComRule.DBlockAddress,
							// OPC DBLOCK VALUE TYPE
							CharPara7: opc.OPCComRule.DBlockValueType,
							// OPC DBLOCK VALUE
							CharPara8: opc.OPCComRule.DBlockValue,
							// OPC STEP NO
							IntPara1: opc.OPCComRule.StepNo,
							// OPC RULE ID
							IntPara2: opc.OPCComRule.RuleId,
						}
						pos++
						err = serialOrderDetailDao.InsertOne(&op)
						if err != nil {
							_ = session.Rollback()
							return nil, grmi.NewBusinessError("插入操作明细记录失败，error:" + err.Error())
						}
					}

				}
			}
		}

		lockedWorkOrder := model.LockedResponseWorkOrder{WorkOrderId: workOrderId, Status: baseModel.WO_STATUS_LOCKED}
		result = append(result, lockedWorkOrder)
	}
	_ = session.Commit()
	return result, nil
}
