// 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              : OPCComValueCheckRuleDetail的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-09-09 10:59:52
 *
 ******************************************************************************/
var DefaultConditionOfOPCComValueCheckRuleDetail = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.OPCComValueCheckRuleDetail_Id.UrlParameterName:               grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_Id, grmi.Equal, true),
		meta.OPCComValueCheckRuleDetail_DetailId.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_DetailId, grmi.Equal, true),
		meta.OPCComValueCheckRuleDetail_CheckOperationId.UrlParameterName: grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckOperationId, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckPO.UrlParameterName:          grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckPO, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckStepNo.UrlParameterName:      grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckStepNo, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckStepDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckStepDesc, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckPos.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckPos, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckProductId.UrlParameterName:   grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckProductId, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckWorkPlaceNr.UrlParameterName: grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckWorkPlaceNr, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_ObjectStepNo.UrlParameterName:     grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_ObjectStepNo, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_ObjectPos.UrlParameterName:        grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_ObjectPos, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_ObjectOPCDesc.UrlParameterName:    grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_ObjectOPCDesc, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_OperatorSimbol.UrlParameterName:   grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_OperatorSimbol, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CheckValue.UrlParameterName:       grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CheckValue, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_DoWhenOK.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_DoWhenOK, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_DoWhenNOK.UrlParameterName:        grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_DoWhenNOK, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_LastModify, grmi.Approximate, false),
		meta.OPCComValueCheckRuleDetail_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_LastUser, grmi.Equal, false),
		meta.OPCComValueCheckRuleDetail_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.OPCComValueCheckRuleDetail_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleDetailService.InsertOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleDetailServiceImplement) InsertOne(user *global.User, entity *model.OPCComValueCheckRuleDetail) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleDetailService.SelectOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleDetailServiceImplement) SelectOne(user *global.User, id int, detailId int) (*model.OPCComValueCheckRuleDetail, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/OPCComValueCheckRuleDetailService.UpdateOne
 *
 ******************************************************************************/
func (impl *OPCComValueCheckRuleDetailServiceImplement) UpdateOne(user *global.User, entity *model.OPCComValueCheckRuleDetail) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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