// 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              : OPCMonitorComRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorComRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCMonitorComRule_WorkPlaceNr.UrlParameterName:                grmi.NewConditionItem(meta.OPCMonitorComRule_WorkPlaceNr, grmi.Equal, true),
		meta.OPCMonitorComRule_MonitorPalletInPlace.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorComRule_MonitorPalletInPlace, grmi.Equal, true),
		meta.OPCMonitorComRule_MonitorManualRelease.UrlParameterName:       grmi.NewConditionItem(meta.OPCMonitorComRule_MonitorManualRelease, grmi.Equal, true),
		meta.OPCMonitorComRule_MonitorProblemPieceRelease.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorComRule_MonitorProblemPieceRelease, grmi.Equal, true),
		meta.OPCMonitorComRule_RuleId.UrlParameterName:                     grmi.NewConditionItem(meta.OPCMonitorComRule_RuleId, grmi.Equal, true),
		meta.OPCMonitorComRule_ActionType.UrlParameterName:                 grmi.NewConditionItem(meta.OPCMonitorComRule_ActionType, grmi.Equal, false),
		meta.OPCMonitorComRule_DBlockAddress.UrlParameterName:              grmi.NewConditionItem(meta.OPCMonitorComRule_DBlockAddress, grmi.Equal, false),
		meta.OPCMonitorComRule_DBlockValueType.UrlParameterName:            grmi.NewConditionItem(meta.OPCMonitorComRule_DBlockValueType, grmi.Equal, false),
		meta.OPCMonitorComRule_Formula.UrlParameterName:                    grmi.NewConditionItem(meta.OPCMonitorComRule_Formula, grmi.Equal, false),
		meta.OPCMonitorComRule_OperatorSimbol.UrlParameterName:             grmi.NewConditionItem(meta.OPCMonitorComRule_OperatorSimbol, grmi.Equal, false),
		meta.OPCMonitorComRule_DBlockValue.UrlParameterName:                grmi.NewConditionItem(meta.OPCMonitorComRule_DBlockValue, grmi.Equal, false),
		meta.OPCMonitorComRule_OKGotoRuleId.UrlParameterName:               grmi.NewConditionItem(meta.OPCMonitorComRule_OKGotoRuleId, grmi.Equal, false),
		meta.OPCMonitorComRule_NOKGotoRuleId.UrlParameterName:              grmi.NewConditionItem(meta.OPCMonitorComRule_NOKGotoRuleId, grmi.Equal, false),
		meta.OPCMonitorComRule_LastModify.UrlParameterName:                 grmi.NewConditionItem(meta.OPCMonitorComRule_LastModify, grmi.Approximate, false),
		meta.OPCMonitorComRule_LastUser.UrlParameterName:                   grmi.NewConditionItem(meta.OPCMonitorComRule_LastUser, grmi.Equal, false),
		meta.OPCMonitorComRule_CreateTime.UrlParameterName:                 grmi.NewConditionItem(meta.OPCMonitorComRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCMonitorComRule的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorComRuleAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCMonitorComRule.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCMonitorComRuleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCMonitorComRule的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
type OPCMonitorComRuleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCMonitorComRuleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCMonitorComRuleDAOImplement实例
 *
 * @Return Value             : OPCMonitorComRuleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-22 15:49:36
 *
 ******************************************************************************/
func NewOPCMonitorComRuleServiceImplement() *OPCMonitorComRuleServiceImplement {
	return &OPCMonitorComRuleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorComRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) InsertOne(user *models.Usertab, entity *model.OPCMonitorComRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease, entity.RuleId)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorComRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) DeleteOne(user *models.Usertab, workPlaceNr int, monitorPalletInPlace int, monitorManualRelease int, monitorProblemPieceRelease int, ruleId int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(workPlaceNr, monitorPalletInPlace, monitorManualRelease, monitorProblemPieceRelease, ruleId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(workPlaceNr, monitorPalletInPlace, monitorManualRelease, monitorProblemPieceRelease, ruleId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorComRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) SelectOne(user *models.Usertab, workPlaceNr int, monitorPalletInPlace int, monitorManualRelease int, monitorProblemPieceRelease int, ruleId int) (*model.OPCMonitorComRule, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/OPCMonitorComRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) UpdateOne(user *models.Usertab, entity *model.OPCMonitorComRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorComRuleDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease, entity.RuleId)
	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/OPCMonitorComRuleService.Insert
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) Insert(user *models.Usertab, entities *[]model.OPCMonitorComRule) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.WorkPlaceNr, entity.MonitorPalletInPlace, entity.MonitorManualRelease, entity.MonitorProblemPieceRelease, entity.RuleId)
		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/OPCMonitorComRuleService.Delete
 *
 ******************************************************************************/
func (impl *OPCMonitorComRuleServiceImplement) Delete(user *models.Usertab, entities *[]model.OPCMonitorComRule) error {

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

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

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

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

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

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

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

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

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