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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	meDal "LAPP_ACURA_MOM_BACKEND/dao/me"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/global"
	"LAPP_ACURA_MOM_BACKEND/grmi"
	meta "LAPP_ACURA_MOM_BACKEND/meta/base"
	model "LAPP_ACURA_MOM_BACKEND/models/base"
)

/******************************************************************************
 *
 * @Description              : Process的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcess = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Process_PO.UrlParameterName:                    grmi.NewConditionItem(meta.Process_PO, grmi.Equal, true),
		meta.Process_Name.UrlParameterName:                  grmi.NewConditionItem(meta.Process_Name, grmi.Equal, false),
		meta.Process_Descr.UrlParameterName:                 grmi.NewConditionItem(meta.Process_Descr, grmi.Equal, false),
		meta.Process_PrePO.UrlParameterName:                 grmi.NewConditionItem(meta.Process_PrePO, grmi.Equal, false),
		meta.Process_NextPO.UrlParameterName:                grmi.NewConditionItem(meta.Process_NextPO, grmi.Equal, false),
		meta.Process_IsFirstPO.UrlParameterName:             grmi.NewConditionItem(meta.Process_IsFirstPO, grmi.Equal, false),
		meta.Process_IsLastPO.UrlParameterName:              grmi.NewConditionItem(meta.Process_IsLastPO, grmi.Equal, false),
		meta.Process_PrePOForBK.UrlParameterName:            grmi.NewConditionItem(meta.Process_PrePOForBK, grmi.Equal, false),
		meta.Process_NextPOForBK.UrlParameterName:           grmi.NewConditionItem(meta.Process_NextPOForBK, grmi.Equal, false),
		meta.Process_IsFirstPOForBK.UrlParameterName:        grmi.NewConditionItem(meta.Process_IsFirstPOForBK, grmi.Equal, false),
		meta.Process_IsLastPOForBK.UrlParameterName:         grmi.NewConditionItem(meta.Process_IsLastPOForBK, grmi.Equal, false),
		meta.Process_HasDependOn.UrlParameterName:           grmi.NewConditionItem(meta.Process_HasDependOn, grmi.Equal, false),
		meta.Process_EnableMonitor.UrlParameterName:         grmi.NewConditionItem(meta.Process_EnableMonitor, grmi.Equal, false),
		meta.Process_AllowPrePOStatus.UrlParameterName:      grmi.NewConditionItem(meta.Process_AllowPrePOStatus, grmi.Equal, false),
		meta.Process_AllowPrePOStatusForBK.UrlParameterName: grmi.NewConditionItem(meta.Process_AllowPrePOStatusForBK, grmi.Equal, false),
		meta.Process_LastModify.UrlParameterName:            grmi.NewConditionItem(meta.Process_LastModify, grmi.Approximate, false),
		meta.Process_LastUser.UrlParameterName:              grmi.NewConditionItem(meta.Process_LastUser, grmi.Equal, false),
		meta.Process_CreateTime.UrlParameterName:            grmi.NewConditionItem(meta.Process_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Process的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
var DefaultConditionOfProcessAndPaging = grmi.NewCondition(
	DefaultConditionOfProcess.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ProcessServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Process的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
type ProcessServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewProcessDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ProcessDAOImplement实例
 *
 * @Return Value             : ProcessDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-22 17:33:27
 *
 ******************************************************************************/
func NewProcessServiceImplement() *ProcessServiceImplement {
	return &ProcessServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.InsertOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) InsertOne(user *global.User, entity *model.Process) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "InsertOneProcess", "插入一个Process")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	source := entity.Source
	if source == "" {
		return grmi.NewBusinessError("未获取到创建来源")
	}
	workLineDao := dal.NewWorkLineDAO(session, user.PlantNr, user.UserId)
	workLine, err := workLineDao.SelectOne(source)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询产线数据失败, error:" + err.Error())
	}
	if workLine == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("产线不存在, 产线ID:" + source)
	}
	dao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.PO)
	if err != nil {
		return grmi.NewBusinessError("查询原有数据失败, error:" + err.Error())
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	workLineProcessDao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	if err = session.Begin(); err != nil {
		return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
	}
	maxPos, err := workLineProcessDao.GetMaxPost(source)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("获取产线工序关联关系位置数据失败, error:" + err.Error())
	}
	workLineProcess := model.WorkLineProcessLst{
		WorkLineId: source,
		PO:         entity.PO,
		PODescr:    entity.Descr,
		Pos:        maxPos + 1,
	}
	err = workLineProcessDao.InsertOne(&workLineProcess)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("写入产线工序关联关系失败, error:" + err.Error())
	}
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.DeleteOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) DeleteOne(user *global.User, pO string) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "DeleteOneProcess", "删除一个Process")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	dependDao := dal.NewProcessDependOnDAO(session, user.PlantNr, user.UserId)
	operationDao := dal.NewOperationDAO(session, user.PlantNr, user.UserId)
	opcBasicDao := dal.NewOPCBasicComInfoDAO(session, user.PlantNr, user.UserId)
	opcComDao := dal.NewOPCComRuleDAO(session, user.PlantNr, user.UserId)
	opcDetailDao := dal.NewOPCComDetailRuleDAO(session, user.PlantNr, user.UserId)
	opcCheckDao := dal.NewOPCComValueCheckRuleDAO(session, user.PlantNr, user.UserId)
	opcCheckDetailDao := dal.NewOPCComValueCheckRuleDetailDAO(session, user.PlantNr, user.UserId)
	twDao := dal.NewTightenWrenchInfoDAO(session, user.PlantNr, user.UserId)
	twrDao := dal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
	scanDao := dal.NewScanRuleInfoDAO(session, user.PlantNr, user.UserId)
	printDao := dal.NewPrintBasicRuleInfoDAO(session, user.PlantNr, user.UserId)
	guideDao := dal.NewGuideRuleInfoDAO(session, user.PlantNr, user.UserId)
	processWorkPlaceDao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
	workLineProcessDao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(pO)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询原有记录失败, error:" + err.Error())
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dependDao.DeleteWhere([]grmi.Predicate{meta.ProcessDependOn_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除工序依赖数据失败, error:" + err.Error())
	}
	err = operationDao.DeleteWhere([]grmi.Predicate{meta.Operation_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除工步数据失败, error:" + err.Error())
	}
	err = opcBasicDao.DeleteWhere([]grmi.Predicate{meta.OPCBasicComInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除OPC基础通讯规则数据失败, error:" + err.Error())
	}
	err = opcComDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除OPC通讯规则数据失败, error:" + err.Error())
	}
	err = opcDetailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除OPC通讯详细规则数据失败, error:" + err.Error())
	}

	err = opcCheckDao.DeleteWhere([]grmi.Predicate{meta.OPCComValueCheckRule_CheckPO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除OPC问题项规则数据失败, error:" + err.Error())
	}

	err = opcCheckDetailDao.DeleteWhere([]grmi.Predicate{meta.OPCComValueCheckRuleDetail_CheckPO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除OPC问题项规则细则数据失败, error:" + err.Error())
	}
	err = twDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除开放协议规则数据失败, error:" + err.Error())
	}
	err = twrDao.DeleteWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除开放协议规则细则数据失败, error:" + err.Error())
	}
	err = scanDao.DeleteWhere([]grmi.Predicate{meta.ScanRuleInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除扫码校验规则数据失败, error:" + err.Error())
	}
	err = printDao.DeleteWhere([]grmi.Predicate{meta.PrintBasicRuleInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除单据打印规则数据失败, error:" + err.Error())
	}
	err = guideDao.DeleteWhere([]grmi.Predicate{meta.GuideRuleInfo_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除指导操作数据失败, error:" + err.Error())
	}
	err = processWorkPlaceDao.DeleteWhere([]grmi.Predicate{meta.ProcessWorkPlaceLst_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除工序工位关联关系失败, error:" + err.Error())
	}
	err = workLineProcessDao.DeleteWhere([]grmi.Predicate{meta.WorkLineProcessLst_PO.NewPredicate(grmi.Equal, pO)})
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除产线工序关联关系失败, error:" + err.Error())
	}
	err = dao.DeleteOne(pO)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("删除工序数据失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.SelectOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectOne(user *global.User, pO string) (*model.Process, error) {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectOneProcess", "查询一个Process")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	workLineProcessDao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(pO)
	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, nil
	}
	li, err := workLineProcessDao.Select([]grmi.Predicate{meta.WorkLineProcessLst_PO.NewPredicate(grmi.Equal, pO)}, []grmi.Field{meta.WorkLineProcessLst_Pos})
	if err != nil {
		return nil, grmi.NewBusinessError("查询产线工序关联关系失败, error:" + err.Error())
	}
	result.WorkLineProcessLstLi = li
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.UpdateOne
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) UpdateOne(user *global.User, entity *model.Process) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "UpdateOneProcess", "修改一个Process")
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewProcessDAO(session, user.PlantNr, user.UserId)
	if entity.OriginPO != entity.PO {
		record, err := dao.SelectOne(entity.OriginPO)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
		if err = session.Begin(); err != nil {
			return grmi.NewBusinessError("开启事务失败, error:" + err.Error())
		}
		err = dao.DeleteOne(entity.OriginPO)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("删除原有数据失败, error:" + err.Error())
		}
		err = dao.InsertOne(entity)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("写入新数据失败, error:" + err.Error())
		}
		prePoUpdate := model.Process{PrePO: entity.PO}
		err = dao.UpdateWhere([]grmi.Predicate{meta.Process_PrePO.NewPredicate(grmi.Equal, entity.OriginPO)}, &prePoUpdate, meta.Process_PrePO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新前序po数据失败, error:" + err.Error())
		}
		nextPoUpdate := model.Process{NextPO: entity.PO}
		err = dao.UpdateWhere([]grmi.Predicate{meta.Process_NextPO.NewPredicate(grmi.Equal, entity.OriginPO)}, &nextPoUpdate, meta.Process_NextPO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新后序po数据失败, error:" + err.Error())
		}
		prePoForBKUpdate := model.Process{PrePOForBK: entity.PO}
		err = dao.UpdateWhere([]grmi.Predicate{meta.Process_PrePOForBK.NewPredicate(grmi.Equal, entity.OriginPO)}, &prePoForBKUpdate, meta.Process_PrePOForBK.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新前序备份po数据失败, error:" + err.Error())
		}
		nextPoForBKUpdate := model.Process{NextPOForBK: entity.PO}
		err = dao.UpdateWhere([]grmi.Predicate{meta.Process_NextPOForBK.NewPredicate(grmi.Equal, entity.OriginPO)}, &nextPoForBKUpdate, meta.Process_NextPOForBK.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新后序备份po数据失败, error:" + err.Error())
		}
		workLineProcessDao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
		workPlaceProcessDao := dal.NewProcessWorkPlaceLstDAO(session, user.PlantNr, user.UserId)
		operationDao := dal.NewOperationDAO(session, user.PlantNr, user.UserId)
		scanRuleDao := dal.NewScanRuleInfoDAO(session, user.PlantNr, user.UserId)
		guideRuleDao := dal.NewGuideRuleInfoDAO(session, user.PlantNr, user.UserId)
		opcBasicDao := dal.NewOPCBasicComInfoDAO(session, user.PlantNr, user.UserId)
		opcComDao := dal.NewOPCComRuleDAO(session, user.PlantNr, user.UserId)
		opcDetailDao := dal.NewOPCComDetailRuleDAO(session, user.PlantNr, user.UserId)
		printRuleDao := dal.NewPrintBasicRuleInfoDAO(session, user.PlantNr, user.UserId)
		monitorOpcBasicDao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
		monitorOPCComDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
		monitorOPCDetailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
		twDao := dal.NewTightenWrenchInfoDAO(session, user.PlantNr, user.UserId)
		twDetailDao := dal.NewTightenWrenchRuleInfoDAO(session, user.PlantNr, user.UserId)
		OperationUpdate := model.Operation{PO: entity.PO}
		err = operationDao.UpdateWhere([]grmi.Predicate{meta.Operation_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &OperationUpdate, meta.Operation_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新工艺中的工序数据失败, error:" + err.Error())
		}
		scanRuleUpdate := model.ScanRuleInfo{PO: entity.PO}
		err = scanRuleDao.UpdateWhere([]grmi.Predicate{meta.ScanRuleInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &scanRuleUpdate, meta.ScanRuleInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新扫码校验中的工序数据失败, error:" + err.Error())
		}
		guideRuleUpdate := model.GuideRuleInfo{PO: entity.PO}
		err = guideRuleDao.UpdateWhere([]grmi.Predicate{meta.GuideRuleInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &guideRuleUpdate, meta.GuideRuleInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新指导中的工序数据失败, error:" + err.Error())
		}
		opcBasicRuleUpdate := model.OPCBasicComInfo{PO: entity.PO}
		err = opcBasicDao.UpdateWhere([]grmi.Predicate{meta.OPCBasicComInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &opcBasicRuleUpdate, meta.OPCBasicComInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc基础通讯规则中的工序数据失败, error:" + err.Error())
		}
		opcComRuleUpdate := model.OPCComRule{PO: entity.PO}
		err = opcComDao.UpdateWhere([]grmi.Predicate{meta.OPCComRule_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &opcComRuleUpdate, meta.OPCComRule_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc通讯规则中的工序数据失败, error:" + err.Error())
		}
		OPCDetailUpdate := model.OPCComDetailRule{PO: entity.PO}
		err = opcDetailDao.UpdateWhere([]grmi.Predicate{meta.OPCComDetailRule_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &OPCDetailUpdate, meta.OPCComDetailRule_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc通讯详细规则中的工序数据失败, error:" + err.Error())
		}
		printRuleUpdate := model.PrintBasicRuleInfo{PO: entity.PO}
		err = printRuleDao.UpdateWhere([]grmi.Predicate{meta.PrintBasicRuleInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &printRuleUpdate, meta.PrintBasicRuleInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新单据打印中的工序数据失败, error:" + err.Error())
		}
		monitorOPCBasicRuleUpdate := model.OPCMonitorBasicComInfo{PO: entity.PO}
		err = monitorOpcBasicDao.UpdateWhere([]grmi.Predicate{meta.OPCMonitorBasicComInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &monitorOPCBasicRuleUpdate, meta.OPCMonitorBasicComInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc监控通讯基础规则中的工序数据失败, error:" + err.Error())
		}
		monitorOPCComRuleUpdate := model.OPCMonitorComRule{PO: entity.PO}
		err = monitorOPCComDao.UpdateWhere([]grmi.Predicate{meta.OPCMonitorComRule_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &monitorOPCComRuleUpdate, meta.OPCMonitorComRule_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc监控通讯规则中的工序数据失败, error:" + err.Error())
		}
		monitorOPCDetailRuleUpdate := model.OPCMonitorComDetailRule{PO: entity.PO}
		err = monitorOPCDetailDao.UpdateWhere([]grmi.Predicate{meta.OPCMonitorComDetailRule_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &monitorOPCDetailRuleUpdate, meta.OPCMonitorComDetailRule_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新opc监控通讯详细规则中的工序数据失败, error:" + err.Error())
		}
		workLinePoUpdate := model.WorkLineProcessLst{
			PO: entity.PO,
		}
		err = workLineProcessDao.UpdateWhere([]grmi.Predicate{meta.WorkLineProcessLst_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &workLinePoUpdate, meta.WorkLineProcessLst_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新产线工序关联的工序数据失败, error:" + err.Error())
		}
		workplacePoUpdate := model.ProcessWorkPlaceLst{
			PO: entity.PO,
		}
		err = workPlaceProcessDao.UpdateWhere([]grmi.Predicate{meta.ProcessWorkPlaceLst_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &workplacePoUpdate, meta.ProcessWorkPlaceLst_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新工位工序关联的工序数据失败, error:" + err.Error())
		}
		twUpdate := model.TightenWrenchInfo{PO: entity.PO}
		err = twDao.UpdateWhere([]grmi.Predicate{meta.TightenWrenchInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &twUpdate, meta.TightenWrenchInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新开放协议中的工序数据失败, error:" + err.Error())
		}
		twDetailUpdate := model.TightenWrenchRuleInfo{PO: entity.PO}
		err = twDetailDao.UpdateWhere([]grmi.Predicate{meta.TightenWrenchRuleInfo_PO.NewPredicate(grmi.Equal, entity.OriginPO)}, &twDetailUpdate, meta.TightenWrenchRuleInfo_PO.Name)
		if err != nil {
			_ = session.Rollback()
			return grmi.NewBusinessError("更新开放协议详细规则中的工序数据失败, error:" + err.Error())
		}
		_ = session.Commit()
		return nil
	} else {
		record, err := dao.SelectOne(entity.PO)
		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/base/ProcessService.Insert
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Insert(user *global.User, entities *[]model.Process) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "InsertProcess", "插入多个Process")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO)
		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/base/ProcessService.Delete
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Delete(user *global.User, entities *[]model.Process) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "DeleteProcess", "删除多个Process")

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.PO)
		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/base/ProcessService.Select
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.Process, error) {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectProcess", "查询Process")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectProcess", "查询Process")

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.Update
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) Update(user *global.User, entities *[]model.Process) error {

	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "UpdateProcess", "修改多个Process")

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.SelectByWorkLine
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectByWorkLine(user *global.User, workLineId string) ([]model.Process, error) {
	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectByWorkLine", "通过产线插线工序")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	workLineProcessDao := dal.NewWorkLineProcessLstDAO(session, user.PlantNr, user.UserId)
	processLi, err := workLineProcessDao.SelectProcessByWorkLine(workLineId)
	return processLi, err
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.SelectByWorkLine
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectOperation(user *global.User, po string) ([]model.OperationDataTree, error) {
	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectOperation", "查询工序下的工步")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	operationDao := dal.NewOperationDAO(session, user.PlantNr, user.UserId)
	projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId)
	operationLi, err := operationDao.Select([]grmi.Predicate{meta.Operation_PO.NewPredicate(grmi.Equal, po)}, []grmi.Field{
		meta.Operation_ProjectId,
		meta.Operation_ActivateInBKStation,
		meta.Operation_StepNo,
	})
	if err != nil {
		return nil, grmi.NewBusinessError("查询工步数据失败, error:" + err.Error())
	}
	if len(operationLi) == 0 {
		return make([]model.OperationDataTree, 0, 0), nil
	}
	var projectId string
	var bk string
	result := make([]model.OperationDataTree, 0)
	var projectTree model.OperationDataTree
	var bkTree model.OperationDataTree
	for _, operation := range operationLi {
		if projectId == "" {
			projectId = operation.ProjectId
			project, err := projectDao.SelectOne(projectId)
			if err != nil {
				return nil, grmi.NewBusinessError("查询车型项目数据失败, error:" + err.Error())
			}
			if project == nil {
				return nil, grmi.NewBusinessError("工步绑定的车型项目不存在, 车型项目ID:" + projectId)
			}
			projectTree.ItemId = projectId
			projectTree.ItemDesc = project.Descr
			projectTree.IsCopy = true
			projectTree.Children = make([]model.OperationDataTree, 0)
		}
		if projectId != operation.ProjectId {
			projectTree.Children = append(projectTree.Children, bkTree)
			result = append(result, projectTree)
			projectId = operation.ProjectId
			project, err := projectDao.SelectOne(projectId)
			if err != nil {
				return nil, grmi.NewBusinessError("查询车型项目数据失败, error:" + err.Error())
			}
			if project == nil {
				return nil, grmi.NewBusinessError("工步绑定的车型项目不存在, 车型项目ID:" + projectId)
			}
			projectTree.ItemId = projectId
			projectTree.ItemDesc = project.Descr
			projectTree.IsCopy = true
			projectTree.Children = make([]model.OperationDataTree, 0)
			bkTree = model.OperationDataTree{
				ItemId:   operation.ActivateInBKStation,
				ItemDesc: bk,
				Children: make([]model.OperationDataTree, 0),
			}
		}
		if bk == "" {
			if operation.ActivateInBKStation {
				bk = "备份生产选定"
			} else {
				bk = "正式成产选定"
			}
			bkTree = model.OperationDataTree{
				ItemId:   operation.ActivateInBKStation,
				ItemDesc: bk,
				Children: make([]model.OperationDataTree, 0),
			}
		}
		if !((bk == "备份生产选定" && operation.ActivateInBKStation) || (bk == "正式成产选定" && !operation.ActivateInBKStation)) {
			projectTree.Children = append(projectTree.Children, bkTree)
			if operation.ActivateInBKStation {
				bk = "备份生产选定"
			} else {
				bk = "正式成产选定"
			}
			bkTree = model.OperationDataTree{
				ItemId:   operation.ActivateInBKStation,
				ItemDesc: bk,
				Children: make([]model.OperationDataTree, 0),
			}
		}
		operationData := model.OperationDataTree{
			ItemId:        operation.OperationId,
			ItemDesc:      operation.StepDesc,
			IsEnd:         true,
			OperationType: operation.StepType,
			ItemData:      operation,
		}
		bkTree.Children = append(bkTree.Children, operationData)
	}
	projectTree.Children = append(projectTree.Children, bkTree)
	result = append(result, projectTree)
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/ProcessService.SelectMonitorOperation
 *
 ******************************************************************************/
func (impl *ProcessServiceImplement) SelectMonitorOperation(user *global.User, po string) ([]model.OperationDataTree, error) {
	grmi.Log(user, "/services/base/implments/Process.service.impl.go", "SelectMonitorOperation", "查询工序下的监控工步")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	operationDao := dal.NewMonitorOperationDAO(session, user.PlantNr, user.UserId)
	projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId)
	operationLi, err := operationDao.Select([]grmi.Predicate{meta.MonitorOperation_PO.NewPredicate(grmi.Equal, po)}, []grmi.Field{
		meta.MonitorOperation_ProjectId,
		meta.MonitorOperation_StepNo,
	})
	if err != nil {
		return nil, grmi.NewBusinessError("查询监控工步数据失败, error:" + err.Error())
	}
	if len(operationLi) == 0 {
		return make([]model.OperationDataTree, 0, 0), nil
	}
	var projectId string
	result := make([]model.OperationDataTree, 0)
	var projectTree model.OperationDataTree
	for _, operation := range operationLi {
		if projectId == "" {
			projectId = operation.ProjectId
			project, err := projectDao.SelectOne(projectId)
			if err != nil {
				return nil, grmi.NewBusinessError("查询车型项目数据失败, error:" + err.Error())
			}
			if project == nil {
				return nil, grmi.NewBusinessError("工步绑定的车型项目不存在, 车型项目ID:" + projectId)
			}
			projectTree.ItemId = projectId
			projectTree.ItemDesc = project.Descr
			projectTree.IsCopy = true
			projectTree.Children = make([]model.OperationDataTree, 0)
		}
		if projectId != operation.ProjectId {
			result = append(result, projectTree)
			projectId = operation.ProjectId
			project, err := projectDao.SelectOne(projectId)
			if err != nil {
				return nil, grmi.NewBusinessError("查询车型项目数据失败, error:" + err.Error())
			}
			if project == nil {
				return nil, grmi.NewBusinessError("工步绑定的车型项目不存在, 车型项目ID:" + projectId)
			}
			projectTree.ItemId = projectId
			projectTree.ItemDesc = project.Descr
			projectTree.IsCopy = true
			projectTree.Children = make([]model.OperationDataTree, 0)
		}
		operationData := model.OperationDataTree{
			ItemId:        operation.OperationId,
			ItemDesc:      operation.StepDesc,
			IsEnd:         true,
			OperationType: operation.StepType,
			ItemData:      operation,
		}
		projectTree.Children = append(projectTree.Children, operationData)
	}
	result = append(result, projectTree)
	return result, nil
}
