// 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"
)

/******************************************************************************
 *
 * @Description              : OPCMonitorComDetailRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-11-25 09:48:01
 *
 ******************************************************************************/
var DefaultConditionOfOPCMonitorComDetailRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCMonitorComDetailRule_OperationId.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorComDetailRule_OperationId, grmi.Equal, true),
		meta.OPCMonitorComDetailRule_WorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorComDetailRule_WorkPlaceNr, grmi.Equal, true),
		meta.OPCMonitorComDetailRule_RuleId.UrlParameterName:      grmi.NewConditionItem(meta.OPCMonitorComDetailRule_RuleId, grmi.Equal, true),
		meta.OPCMonitorComDetailRule_DetailId.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorComDetailRule_DetailId, grmi.Equal, true),
		meta.OPCMonitorComDetailRule_PO.UrlParameterName:          grmi.NewConditionItem(meta.OPCMonitorComDetailRule_PO, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_StepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCMonitorComDetailRule_StepNo, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_StepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorComDetailRule_StepDesc, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_ProjectId.UrlParameterName:   grmi.NewConditionItem(meta.OPCMonitorComDetailRule_ProjectId, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_BaseOnType.UrlParameterName:  grmi.NewConditionItem(meta.OPCMonitorComDetailRule_BaseOnType, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_BaseOnValue.UrlParameterName: grmi.NewConditionItem(meta.OPCMonitorComDetailRule_BaseOnValue, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_DataType.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorComDetailRule_DataType, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_LastModify.UrlParameterName:  grmi.NewConditionItem(meta.OPCMonitorComDetailRule_LastModify, grmi.Approximate, false),
		meta.OPCMonitorComDetailRule_LastUser.UrlParameterName:    grmi.NewConditionItem(meta.OPCMonitorComDetailRule_LastUser, grmi.Equal, false),
		meta.OPCMonitorComDetailRule_CreateTime.UrlParameterName:  grmi.NewConditionItem(meta.OPCMonitorComDetailRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorComDetailRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComDetailRuleServiceImplement) InsertOne(user *global.User, entity *model.OPCMonitorComDetailRule) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorComDetailRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComDetailRuleServiceImplement) DeleteOne(user *global.User, operationId int, workPlaceNr int, ruleId int, detailId int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(operationId, workPlaceNr, ruleId, detailId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(operationId, workPlaceNr, ruleId, detailId)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorComDetailRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComDetailRuleServiceImplement) SelectOne(user *global.User, operationId int, workPlaceNr int, ruleId int, detailId int) (*model.OPCMonitorComDetailRule, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCMonitorComDetailRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCMonitorComDetailRuleServiceImplement) UpdateOne(user *global.User, entity *model.OPCMonitorComDetailRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCMonitorComDetailRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.OperationId, entity.WorkPlaceNr, entity.RuleId, entity.DetailId)
	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/OPCMonitorComDetailRuleService.Insert
 *
 ******************************************************************************/
func (impl *OPCMonitorComDetailRuleServiceImplement) Insert(user *global.User, entities *[]model.OPCMonitorComDetailRule) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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