// 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              : OPCComValueCheckRule的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-09 10:59:52
 *
 ******************************************************************************/
var DefaultConditionOfOPCComValueCheckRule = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCComValueCheckRule_Id.UrlParameterName:               grmi.NewConditionItem(meta.OPCComValueCheckRule_Id, grmi.Equal, true),
		meta.OPCComValueCheckRule_CheckOperationId.UrlParameterName: grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckOperationId, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckPO.UrlParameterName:          grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckPO, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckStepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckStepNo, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckStepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckStepDesc, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckPos.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckPos, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckProductId.UrlParameterName:   grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckProductId, grmi.Equal, false),
		meta.OPCComValueCheckRule_CheckWorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.OPCComValueCheckRule_CheckWorkPlaceNr, grmi.Equal, false),
		meta.OPCComValueCheckRule_MaxCheckTime.UrlParameterName:     grmi.NewConditionItem(meta.OPCComValueCheckRule_MaxCheckTime, grmi.Equal, false),
		meta.OPCComValueCheckRule_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.OPCComValueCheckRule_LastModify, grmi.Approximate, false),
		meta.OPCComValueCheckRule_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRule_LastUser, grmi.Equal, false),
		meta.OPCComValueCheckRule_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.OPCComValueCheckRule_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : OPCComValueCheckRule的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-09 10:59:52
 *
 ******************************************************************************/
var DefaultConditionOfOPCComValueCheckRuleAndPaging = grmi.NewCondition(
	DefaultConditionOfOPCComValueCheckRule.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : OPCComValueCheckRuleServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : OPCComValueCheckRule的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-09 10:59:52
 *
 ******************************************************************************/
type OPCComValueCheckRuleServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewOPCComValueCheckRuleDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个OPCComValueCheckRuleDAOImplement实例
 *
 * @Return Value             : OPCComValueCheckRuleDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-09 10:59:52
 *
 ******************************************************************************/
func NewOPCComValueCheckRuleServiceImplement() *OPCComValueCheckRuleServiceImplement {
	return &OPCComValueCheckRuleServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) InsertOne(user *global.User, entity *model.OPCComValueCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComValueCheckRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.Id)
	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/OPCComValueCheckRuleService.DeleteOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) DeleteOne(user *global.User, id int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) SelectOne(user *global.User, id int) (*model.OPCComValueCheckRule, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) UpdateOne(user *global.User, entity *model.OPCComValueCheckRule) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewOPCComValueCheckRuleDAO(session, user.PlantNr, user.UserId)
	record, err := dao.SelectOne(entity.Id)
	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/OPCComValueCheckRuleService.Insert
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) Insert(user *global.User, entities *[]model.OPCComValueCheckRule) error {

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.Id)
		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/OPCComValueCheckRuleService.Select
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) Select(user *global.User, urlParameters map[string]string) (interface{}, error) {

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

	condition := DefaultConditionOfOPCComValueCheckRuleAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewOPCComValueCheckRuleDAO(session, user.PlantNr, 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_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleService.Update
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleServiceImplement) Update(user *global.User, entities *[]model.OPCComValueCheckRule) error {

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

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

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

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