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

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/base"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/base"
	model "LAPP_LF_MOM_BACKEND/models/base"
	"LAPP_LF_MOM_BACKEND/web/models"
)

/******************************************************************************
 *
 * @Description              : OPCBasicComInfo的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfOPCBasicComInfo = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCBasicComInfo_OperationId.UrlParameterName: grmi.NewConditionItem(meta.OPCBasicComInfo_OperationId, grmi.Equal, true),
		meta.OPCBasicComInfo_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.OPCBasicComInfo_WorkPlaceNr, grmi.Equal, true),
		meta.OPCBasicComInfo_PO.UrlParameterName:          grmi.NewConditionItem(meta.OPCBasicComInfo_PO, grmi.Equal, false),
		meta.OPCBasicComInfo_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCBasicComInfo_StepNo, grmi.Equal, false),
		meta.OPCBasicComInfo_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCBasicComInfo_StepDesc, grmi.Equal, false),
		meta.OPCBasicComInfo_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.OPCBasicComInfo_ProjectId, grmi.Equal, false),
		meta.OPCBasicComInfo_BrandName.UrlParameterName:   grmi.NewConditionItem(meta.OPCBasicComInfo_BrandName, grmi.Equal, false),
		meta.OPCBasicComInfo_MachineType.UrlParameterName: grmi.NewConditionItem(meta.OPCBasicComInfo_MachineType, grmi.Equal, false),
		meta.OPCBasicComInfo_IpAddress.UrlParameterName:   grmi.NewConditionItem(meta.OPCBasicComInfo_IpAddress, grmi.Equal, false),
		meta.OPCBasicComInfo_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.OPCBasicComInfo_LastModify, grmi.Approximate, false),
		meta.OPCBasicComInfo_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.OPCBasicComInfo_LastUser, grmi.Equal, false),
		meta.OPCBasicComInfo_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.OPCBasicComInfo_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCBasicComInfo的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfOPCBasicComInfoAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCBasicComInfo.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCBasicComInfoServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCBasicComInfo的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
type OPCBasicComInfoServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCBasicComInfoDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCBasicComInfoDAOImplement实例
 *
 * @Return Value             : OPCBasicComInfoDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
func NewOPCBasicComInfoServiceImplement() *OPCBasicComInfoServiceImplement {
	return &OPCBasicComInfoServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	operationDao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	operation, err := operationDao.SelectOne(entity.OperationId)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询工艺数据失败, error:" + err.Error())
	}
	if operation == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工艺")
	}
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workPlace == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工位")
	}
	if operation.PO != entity.PO || operation.StepNo != entity.StepNo || operation.StepDesc != entity.StepDesc || operation.ProjectId != entity.ProjectId {
		_ = session.Rollback()
		return grmi.NewBusinessError("工艺数据错误")
	}
	ruleLi := entity.OPCComRuleLi
	if len(ruleLi) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("未获取到OPC通讯规则数据")
	}
	for index, rule := range ruleLi {
		if rule.RuleId == 0 || rule.ActionType == "" {
			_ = session.Rollback()
			return grmi.NewBusinessError("OPC通讯规则数据不合规")
		}
		rule.PO = entity.PO
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		rule.OperationId = entity.OperationId
		rule.ProjectId = entity.ProjectId
		rule.WorkPlaceNr = entity.WorkPlaceNr
		if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
			if len(rule.OPCComDetailRuleLi) == 0 {
				_ = session.Rollback()
				return grmi.NewBusinessError("未获取到OPC通讯详细规则")
			}
			for index, detail := range rule.OPCComDetailRuleLi {
				if detail.DetailId == 0 || detail.BaseOnType == "" {
					_ = session.Rollback()
					return grmi.NewBusinessError("OPC通讯详细规则数据不合规")
				}
				detail.PO = rule.PO
				detail.StepNo = rule.StepNo
				detail.RuleId = rule.RuleId
				detail.StepDesc = rule.StepDesc
				detail.OperationId = rule.OperationId
				detail.ProjectId = rule.ProjectId
				detail.WorkPlaceNr = rule.WorkPlaceNr
				rule.OPCComDetailRuleLi[index] = detail
			}
			err = detailDao.Insert(&rule.OPCComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("插入详细规则数据失败, error:" + err.Error())
			}
		}
		ruleLi[index] = rule
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("插入通讯规则失败, error:" + err.Error())
	}
	err = dao.InsertOne(entity)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("插入OPC通讯基础规则失败, error:" + err.Error())
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) DeleteOne(user *models.Usertab, operationId int, workPlaceNr int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, operationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = dao.DeleteOne(operationId, workPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) SelectOne(user *models.Usertab, operationId int, workPlaceNr int) (*model.OPCBasicComInfo, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, 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.OPCComRule_OperationId.NewPredicate(grmi.Equal, operationId)}, []grmi.Field{meta.OPCComRule_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.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, operationId), meta.OPCComDetailRule_RuleId.NewPredicate(grmi.Equal, rule.RuleId)}, []grmi.Field{meta.OPCComDetailRule_RuleId})
			if err != nil {
				return nil, err
			}
			rule.OPCComDetailRuleLi = detailLi
			ruleLi[index] = rule
		} else {
			rule.OPCComDetailRuleLi = make([]model.OPCComDetailRule, 0)
			ruleLi[index] = rule
		}
	}
	result.OPCComRuleLi = ruleLi
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if record == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	ruleLi := entity.OPCComRuleLi
	if len(ruleLi) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("未获取到OPC通讯规则数据")
	}
	for index, rule := range ruleLi {
		if rule.RuleId == 0 || rule.ActionType == "" {
			_ = session.Rollback()
			return grmi.NewBusinessError("OPC通讯规则数据不合规")
		}
		rule.PO = entity.PO
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		rule.OperationId = entity.OperationId
		rule.WorkPlaceNr = entity.WorkPlaceNr
		rule.ProjectId = entity.ProjectId
		if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
			if len(rule.OPCComDetailRuleLi) == 0 {
				_ = session.Rollback()
				return grmi.NewBusinessError("未获取到OPC通讯详细规则")
			}
			for index, detail := range rule.OPCComDetailRuleLi {
				if detail.DetailId == 0 || detail.BaseOnType == "" {
					_ = session.Rollback()
					return grmi.NewBusinessError("OPC通讯详细规则数据不合规")
				}
				detail.PO = rule.PO
				detail.StepNo = rule.StepNo
				detail.RuleId = rule.RuleId
				detail.StepDesc = rule.StepDesc
				detail.OperationId = rule.OperationId
				detail.ProjectId = rule.ProjectId
				detail.WorkPlaceNr = rule.WorkPlaceNr
				rule.OPCComDetailRuleLi[index] = detail
			}
			err = detailDao.Insert(&rule.OPCComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("插入详细规则数据失败, error:" + err.Error())
			}
		}
		ruleLi[index] = rule
	}
	err = ruleDao.Insert(&ruleLi)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("插入通讯规则失败, error:" + err.Error())
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	_ = session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Insert
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Delete
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Select
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Update
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Update(user *models.Usertab, entities *[]model.OPCBasicComInfo) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, 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_LF_MOM_BACKEND/services/base/OPCBasicComInfoService.Delete
 *
 ******************************************************************************/
func (impl *OPCBasicComInfoServiceImplement) Save(user *models.Usertab, entity *model.OPCBasicComInfo) error {

	grmi.Log(user, "/services/base/implments/OPCBasicComInfo.service.impl.go", "Save", "保存OPC协议")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	if err := session.Begin(); err != nil {
		return err
	}
	dao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	ruleDao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	detailDao := dal.NewOPCComDetailRuleDAO(session, user.Pid, user.Userid)
	operationDao := dal.NewOperationDAO(session, user.Pid, user.Userid)
	workPlaceDao := dal.NewWorkPlaceDAO(session, user.Pid, user.Userid)
	operation, err := operationDao.SelectOne(entity.OperationId)
	if err != nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("查询工艺数据失败, error:" + err.Error())
	}
	if operation == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工艺")
	}
	workPlace, err := workPlaceDao.SelectOne(entity.WorkPlaceNr)
	if err != nil {
		_ = session.Rollback()
		return err
	}
	if workPlace == nil {
		_ = session.Rollback()
		return grmi.NewBusinessError("不存在对应的工位")
	}
	if operation.PO != entity.PO || operation.StepNo != entity.StepNo || operation.StepDesc != entity.StepDesc || operation.ProjectId != entity.ProjectId {
		_ = session.Rollback()
		return grmi.NewBusinessError("工艺数据错误")
	}
	err = dao.DeleteWhere([]grmi.Predicate{meta.OPCBasicComInfo_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = ruleDao.DeleteWhere([]grmi.Predicate{meta.OPCComRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	err = detailDao.DeleteWhere([]grmi.Predicate{meta.OPCComDetailRule_OperationId.NewPredicate(grmi.Equal, entity.OperationId)})
	if err != nil {
		_ = session.Rollback()
		return err
	}
	ruleLi := entity.OPCComRuleLi
	if len(ruleLi) == 0 {
		_ = session.Rollback()
		return grmi.NewBusinessError("未获取到OPC通讯规则数据")
	}
	for index, rule := range ruleLi {
		if rule.RuleId == 0 || rule.ActionType == "" {
			_ = session.Rollback()
			return grmi.NewBusinessError("OPC通讯规则数据不合规")
		}
		rule.PO = entity.PO
		rule.StepNo = entity.StepNo
		rule.StepDesc = entity.StepDesc
		rule.OperationId = entity.OperationId
		if rule.DBlockValueType == model.DBlockValueType_VALUE_MAP {
			if len(rule.OPCComDetailRuleLi) == 0 {
				_ = session.Rollback()
				return grmi.NewBusinessError("未获取到OPC通讯详细规则")
			}
			for index, detail := range rule.OPCComDetailRuleLi {
				if detail.DetailId == 0 || detail.BaseOnType == "" {
					_ = session.Rollback()
					return grmi.NewBusinessError("OPC通讯详细规则数据不合规")
				}
				detail.PO = rule.PO
				detail.StepNo = rule.StepNo
				detail.RuleId = rule.RuleId
				detail.StepDesc = rule.StepDesc
				detail.OperationId = rule.OperationId
				rule.OPCComDetailRuleLi[index] = detail
			}
			err = detailDao.Insert(&rule.OPCComDetailRuleLi)
			if err != nil {
				_ = session.Rollback()
				return grmi.NewBusinessError("插入详细规则数据失败, error:" + err.Error())
			}
		}
		ruleLi[index] = rule
	}
	err = ruleDao.Insert(&ruleLi)
	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
}