// 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              : OPCComRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-13 16:59:07
 *
 ******************************************************************************/
var DefaultConditionOfOPCComRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCComRule_OperationId.UrlParameterName:     grmi.NewConditionItem(meta.OPCComRule_OperationId, grmi.Equal, true),
		meta.OPCComRule_RuleId.UrlParameterName:          grmi.NewConditionItem(meta.OPCComRule_RuleId, grmi.Equal, true),
		meta.OPCComRule_WorkPlaceNr.UrlParameterName:     grmi.NewConditionItem(meta.OPCComRule_WorkPlaceNr, grmi.Equal, true),
		meta.OPCComRule_PO.UrlParameterName:              grmi.NewConditionItem(meta.OPCComRule_PO, grmi.Equal, false),
		meta.OPCComRule_StepNo.UrlParameterName:          grmi.NewConditionItem(meta.OPCComRule_StepNo, grmi.Equal, false),
		meta.OPCComRule_StepDesc.UrlParameterName:        grmi.NewConditionItem(meta.OPCComRule_StepDesc, grmi.Equal, false),
		meta.OPCComRule_ProjectId.UrlParameterName:       grmi.NewConditionItem(meta.OPCComRule_ProjectId, grmi.Equal, false),
		meta.OPCComRule_ActionType.UrlParameterName:      grmi.NewConditionItem(meta.OPCComRule_ActionType, grmi.Equal, false),
		meta.OPCComRule_DBlockAddress.UrlParameterName:   grmi.NewConditionItem(meta.OPCComRule_DBlockAddress, grmi.Equal, false),
		meta.OPCComRule_DBlockValueType.UrlParameterName: grmi.NewConditionItem(meta.OPCComRule_DBlockValueType, grmi.Equal, false),
		meta.OPCComRule_DBlockValue.UrlParameterName:     grmi.NewConditionItem(meta.OPCComRule_DBlockValue, grmi.Equal, false),
		meta.OPCComRule_LastModify.UrlParameterName:      grmi.NewConditionItem(meta.OPCComRule_LastModify, grmi.Approximate, false),
		meta.OPCComRule_LastUser.UrlParameterName:        grmi.NewConditionItem(meta.OPCComRule_LastUser, grmi.Equal, false),
		meta.OPCComRule_CreateTime.UrlParameterName:      grmi.NewConditionItem(meta.OPCComRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCComRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	//basicDao := dal.NewOPCBasicComInfoDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.RuleId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	//basic, err := basicDao.SelectOne(entity.PO, entity.StepNo)
	//if err != nil {
	//	return err
	//}
	//if basic == nil {
	//	return grmi.NewBusinessError("不存在对应的基础通信规则")
	//}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) SelectOne(user *models.Usertab, operationId int, ruleId int, workPlaceNr int) (*model.OPCComRule, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(operationId, ruleId, workPlaceNr)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCComRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.OperationId, entity.RuleId, entity.WorkPlaceNr)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCComRuleService.Insert
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCComRule) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.RuleId, 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/OPCComRuleService.Delete
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCComRule) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.OperationId, entity.RuleId, 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/OPCComRuleService.Select
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfOPCComRuleAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCComRuleDAO(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/OPCComRuleService.Update
 *
 ******************************************************************************/
func (impl *OPCComRuleServiceImplement) Update(user *models.Usertab, entities *[]model.OPCComRule) error {

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

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

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

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