// 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              : Etcd的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
var DefaultConditionOfEtcd = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Etcd_EtcdId.UrlParameterName:     grmi.NewConditionItem(meta.Etcd_EtcdId, grmi.Equal, true),
		meta.Etcd_EtcdName.UrlParameterName:   grmi.NewConditionItem(meta.Etcd_EtcdName, grmi.Equal, false),
		meta.Etcd_Descr.UrlParameterName:      grmi.NewConditionItem(meta.Etcd_Descr, grmi.Equal, false),
		meta.Etcd_Health.UrlParameterName:     grmi.NewConditionItem(meta.Etcd_Health, grmi.Equal, false),
		meta.Etcd_State.UrlParameterName:      grmi.NewConditionItem(meta.Etcd_State, grmi.Equal, false),
		meta.Etcd_Path.UrlParameterName:       grmi.NewConditionItem(meta.Etcd_Path, grmi.Equal, false),
		meta.Etcd_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Etcd_LastModify, grmi.Approximate, false),
		meta.Etcd_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.Etcd_LastUser, grmi.Equal, false),
		meta.Etcd_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Etcd_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Etcd的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
var DefaultConditionOfEtcdAndPaging = grmi.NewCondition(
	DefaultConditionOfEtcd.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : EtcdServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Etcd的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
type EtcdServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewEtcdDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个EtcdDAOImplement实例
 *
 * @Return Value             : EtcdDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
func NewEtcdServiceImplement() *EtcdServiceImplement {
	return &EtcdServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtcdService.InsertOne
 *
 ******************************************************************************/
func (impl *EtcdServiceImplement) InsertOne(user *models.Usertab, entity *model.Etcd) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewEtcdDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.EtcdId)
	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/EtcdService.DeleteOne
 *
 ******************************************************************************/
func (impl *EtcdServiceImplement) DeleteOne(user *models.Usertab, etcdId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtcdService.SelectOne
 *
 ******************************************************************************/
func (impl *EtcdServiceImplement) SelectOne(user *models.Usertab, etcdId int) (*model.Etcd, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewEtcdDAO(session, user.Userid)
	result, err := dao.SelectOne(etcdId)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/EtcdService.UpdateOne
 *
 ******************************************************************************/
func (impl *EtcdServiceImplement) UpdateOne(user *models.Usertab, entity *model.Etcd) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewEtcdDAO(session, user.Userid)
	record, err := dao.SelectOne(entity.EtcdId)
	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/EtcdService.Insert
 *
 ******************************************************************************/
func (impl *EtcdServiceImplement) Insert(user *models.Usertab, entities *[]model.Etcd) error {

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

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

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

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

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

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

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

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

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

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

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

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