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

package implments

import (
	dal "LAPP_ACURA_MOM_BACKEND/dao/base"
	"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"
	"strconv"
)

/******************************************************************************
 *
 * @Description              : OPCMonitorBasicComInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 09:48:01
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorBasicComInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCMonitorBasicComInfo_OperationId.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_OperationId, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_WorkPlaceNr.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_WorkPlaceNr, grmi.Equal, true),
		meta.OPCMonitorBasicComInfo_PO.UrlParameterName:             grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_PO, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_StepNo.UrlParameterName:         grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_StepNo, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_StepDesc.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_StepDesc, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_ProjectId.UrlParameterName:      grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_ProjectId, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_BrandName.UrlParameterName:      grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_BrandName, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_MachineType.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_MachineType, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_IpAddress.UrlParameterName:      grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_IpAddress, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_CheckWriteBack.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_CheckWriteBack, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_LastModify.UrlParameterName:     grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_LastModify, grmi.Approximate, false),
		meta.OPCMonitorBasicComInfo_LastUser.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_LastUser, grmi.Equal, false),
		meta.OPCMonitorBasicComInfo_CreateTime.UrlParameterName:     grmi.NewConditionItem(meta.OPCMonitorBasicComInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCMonitorBasicComInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 09:48:01
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorBasicComInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCMonitorBasicComInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCMonitorBasicComInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCMonitorBasicComInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 09:48:01
 *
 ******************************************************************************/
type OPCMonitorBasicComInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCMonitorBasicComInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCMonitorBasicComInfoDAOImplement实例
 *
 * @Return Value             : OPCMonitorBasicComInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 09:48:01
 *
 ******************************************************************************/
func NewOPCMonitorBasicComInfoServiceImplement() *OPCMonitorBasicComInfoServiceImplement {
	return &OPCMonitorBasicComInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) InsertOne(user *global.User, entity *model.OPCMonitorBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	operationDao := dal.NewMonitorOperationDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	operation, err := operationDao.SelectOne(entity.OperationId)
	if err != nil {
		return grmi.NewBusinessError("查询监控工步失败, error:" + err.Error())
	}
	if operation == nil {
		return grmi.NewBusinessError("监控工步不存在, 工步ID:" + strconv.Itoa(entity.OperationId))
	}
	entity.PO = operation.PO
	entity.ProjectId = operation.ProjectId
	entity.StepNo = operation.StepNo
	entity.StepDesc = operation.StepDesc
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		return grmi.NewBusinessError("查询工位失败, error:" + err.Error())
	}
	if workPlace == nil {
		return grmi.NewBusinessError("不存在对应的工位, 工位ID:" + strconv.Itoa(entity.WorkPlaceNr))
	}
	if err := session.Begin(); err != nil {
		return err
	}
	ruleLi := entity.OPCMonitorComRuleLi
	for index, rule := range ruleLi {
		rule.PO = entity.PO
		rule.WorkPlaceNr = entity.WorkPlaceNr
		rule.OperationId = entity.OperationId
		rule.ProjectId = entity.ProjectId
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		for detailIndex, detail := range rule.OPCMonitorComDetailRuleLi {
			detail.PO = rule.PO
			detail.OperationId = rule.OperationId
			detail.ProjectId = rule.ProjectId
			detail.WorkPlaceNr = rule.WorkPlaceNr
			detail.StepNo = rule.StepNo
			detail.StepDesc = rule.StepDesc
			detail.RuleId = rule.RuleId
			rule.OPCMonitorComDetailRuleLi[detailIndex] = detail
		}
		ruleLi[index] = rule
	}
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	for _, rule := range ruleLi {
		if len(rule.OPCMonitorComDetailRuleLi) > 0 {
			err = detailDao.Insert(&rule.OPCMonitorComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return err
			}
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) DeleteOne(user *global.User, operationId int, workPlaceNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, workPlaceNr),
		meta.OPCMonitorComRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, workPlaceNr),
		meta.OPCMonitorComDetailRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) SelectOne(user *global.User, operationId int, workPlaceNr int) (*model.OPCMonitorBasicComInfo, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	result, err := dao.SelectOne(operationId, workPlaceNr)
	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, grmi.NewBusinessError("数据不存在")
	}
	ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, result.WorkPlaceNr),
		meta.OPCMonitorComRule_OperationId.NewPredicate(grmi.Equal, operationId)},
		[]grmi.Field{meta.OPCMonitorComRule_RuleId})
	if err != nil {
		return nil, err
	}
	for index, rule := range ruleLi {
		if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
			detailLi, err := detailDao.Select([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, result.WorkPlaceNr),
				meta.OPCMonitorComDetailRule_OperationId.NewPredicate(grmi.Equal, operationId),
				meta.OPCMonitorComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId),
			}, []grmi.Field{meta.OPCMonitorComDetailRule_DetailId})
			if err != nil {
				return nil, err
			}
			rule.OPCMonitorComDetailRuleLi = detailLi
		} else {
			rule.OPCMonitorComDetailRuleLi = make([]model.OPCMonitorComDetailRule, 0)
		}
		ruleLi[index] = rule
	}
	result.OPCMonitorComRuleLi = ruleLi
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) UpdateOne(user *global.User, entity *model.OPCMonitorBasicComInfo) error {

	grmi.Log(user, "/services/base/implments/OPCMonitorBasicComInfo.service.impl.go", "UpdateOneOPCMonitorBasicComInfo", "修改一个OPCMonitorBasicComInfo")

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

	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
	operationDao := dal.NewMonitorOperationDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		return grmi.NewBusinessError("查询工位失败, error:" + err.Error())
	}
	if workPlace == nil {
		return grmi.NewBusinessError("不存在对应的工位, 工位ID:" + strconv.Itoa(entity.WorkPlaceNr))
	}
	operation, err := operationDao.SelectOne(entity.OperationId)
	if err != nil {
		return grmi.NewBusinessError("查询监控工步失败, error:" + err.Error())
	}
	if operation == nil {
		return grmi.NewBusinessError("监控工步不存在")
	}
	entity.PO = operation.PO
	entity.ProjectId = operation.ProjectId
	entity.StepNo = operation.StepNo
	entity.StepDesc = operation.StepDesc
	if err := session.Begin(); err != nil {
		return err
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr),
		meta.OPCMonitorComRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr),
		meta.OPCMonitorComDetailRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	ruleLi := entity.OPCMonitorComRuleLi
	for index, rule := range ruleLi {
		rule.PO = entity.PO
		rule.WorkPlaceNr = entity.WorkPlaceNr
		rule.OperationId = entity.OperationId
		rule.ProjectId = entity.ProjectId
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		for detailIndex, detail := range rule.OPCMonitorComDetailRuleLi {
			detail.PO = rule.PO
			detail.OperationId = rule.OperationId
			detail.ProjectId = rule.ProjectId
			detail.WorkPlaceNr = rule.WorkPlaceNr
			detail.StepNo = rule.StepNo
			detail.StepDesc = rule.StepDesc
			detail.RuleId = rule.RuleId
			rule.OPCMonitorComDetailRuleLi[detailIndex] = detail
		}
		ruleLi[index] = rule
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	for _, rule := range ruleLi {
		if len(rule.OPCMonitorComDetailRuleLi) > 0 {
			err = detailDao.Insert(&rule.OPCMonitorComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return err
			}
		}
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.Insert
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Insert(user *global.User, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
		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/OPCMonitorBasicComInfoService.Delete
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Delete(user *global.User, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
		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/OPCMonitorBasicComInfoService.Select
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.OPCMonitorBasicComInfo, error) {

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

	condition := DefaultConditionOfOPCMonitorBasicComInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(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/OPCMonitorBasicComInfoService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

	condition := DefaultConditionOfOPCMonitorBasicComInfoAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return grmi.EmptyPagingResult, err
	}
	dao := dal.NewOPCMonitorBasicComInfoDAO(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/OPCMonitorBasicComInfoService.Update
 *
 ******************************************************************************/
func (impl *OPCMonitorBasicComInfoServiceImplement) Update(user *global.User, entities *[]model.OPCMonitorBasicComInfo) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
		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/OPCMonitorBasicComInfoService.InsertOne
 *
 ******************************************************************************/
//func (impl *OPCMonitorBasicComInfoServiceImplement) Save(user *global.User, entity *model.OPCMonitorBasicComInfo) error {
//
//	grmi.Log(user, "/services/base/implments/OPCMonitorBasicComInfo.service.impl.go", "InsertOneOPCMonitorBasicComInfo", "插入一个OPCMonitorBasicComInfo")
//
//	engine := db.Eloquent.Master()
//	session := engine.NewSession()
//	defer session.Close()
//	if err := session.Begin(); err != nil {
//		return err
//	}
//	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
//	workPlaceDao := dal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId)
//	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
//	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
//	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	if workPlace == nil {
//		_ = session.Rollback()
//		return grmi.NewBusinessError("不存在对应的工位")
//	}
//	ruleLi := entity.OPCMonitorComRuleLi
//	for index, rule := range ruleLi {
//		rule.PO = entity.PO
//		rule.WorkPlaceNr = entity.WorkPlaceNr
//		rule.OperationId = entity.OperationId
//		rule.ProjectId = entity.ProjectId
//		rule.StepNo = entity.StepNo
//		rule.StepDesc = entity.StepDesc
//		for detailIndex, detail := range rule.OPCMonitorComDetailRuleLi {
//			detail.PO = rule.PO
//			detail.OperationId = rule.OperationId
//			detail.ProjectId = rule.ProjectId
//			detail.WorkPlaceNr = rule.WorkPlaceNr
//			detail.StepNo = rule.StepNo
//			detail.StepDesc = rule.StepDesc
//			detail.RuleId = rule.RuleId
//			rule.OPCMonitorComDetailRuleLi[detailIndex] = detail
//		}
//		ruleLi[index] = rule
//	}
//	err = dao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorBasicComInfo_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, entity.WorkPlaceNr)})
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	err = dao.InsertOne(entity)
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	err = ruleDao.Insert(&ruleLi)
//	if err != nil {
//		_ = session.Rollback()
//		return err
//	}
//	for _, rule := range ruleLi {
//		if len(rule.OPCMonitorComDetailRuleLi) > 0 {
//			err = detailDao.Insert(&rule.OPCMonitorComDetailRuleLi)
//			if err != nil {
//				_ = session.Rollback()
//				return err
//			}
//		}
//	}
//	_ = session.Commit()
//	return nil
//}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorBasicComInfoService.GetRule
 *
 ******************************************************************************/
//func (impl *OPCMonitorBasicComInfoServiceImplement) GetRule(user *global.User, urlParameters map[string]string) (interface{}, error) {
//
//	grmi.Log(user, "/services/base/implments/OPCMonitorBasicComInfo.service.impl.go", "SelectOPCMonitorBasicComInfo", "GetRule")
//
//	condition := DefaultConditionOfOPCMonitorBasicComInfoAndPaging
//	engine := db.Eloquent.Master()
//	session := engine.NewSession()
//	defer session.Close()
//	predicates, err := condition.BuildPredicates(urlParameters)
//	if err != nil {
//		return nil, err
//	}
//	dao := dal.NewOPCMonitorBasicComInfoDAO(session, user.PlantNr, user.UserId)
//	ruleDao := dal.NewOPCMonitorComRuleDAO(session, user.PlantNr, user.UserId)
//	detailDao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
//	result, err := dao.Select(predicates, condition.OrderByFields)
//	if err != nil {
//		return nil, err
//	}
//	if len(result) > 1 {
//		return nil, grmi.NewBusinessError("规则数据过多")
//	} else if len(result) == 0 {
//		return nil, nil
//	}
//	for index, opc := range result {
//		ruleLi, err := ruleDao.Select([]grmi.Predicate{meta.OPCMonitorComRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr)}, []grmi.Field{meta.OPCMonitorComRule_RuleId})
//		if err != nil {
//			return nil, err
//		}
//		for index, rule := range ruleLi {
//			if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
//				detailLi, err := detailDao.Select([]grmi.Predicate{meta.OPCMonitorComDetailRule_WorkPlaceNr.NewPredicate(grmi.Equal, opc.WorkPlaceNr), meta.OPCMonitorComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId)}, []grmi.Field{meta.OPCMonitorComDetailRule_DetailId})
//				if err != nil {
//					return nil, err
//				}
//				rule.OPCMonitorComDetailRuleLi = detailLi
//
//			} else {
//				rule.OPCMonitorComDetailRuleLi = make([]model.OPCMonitorComDetailRule, 0)
//			}
//			ruleLi[index] = rule
//		}
//		opc.OPCMonitorComRuleLi = ruleLi
//		result[index] = opc
//	}
//
//	return result[0], nil
//}
