// 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              : Snr的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfSnr = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Snr_SnrId.UrlParameterName:            grmi.NewConditionItem(meta.Snr_SnrId, grmi.Equal, true),
		meta.Snr_Descr.UrlParameterName:            grmi.NewConditionItem(meta.Snr_Descr, grmi.Equal, false),
		meta.Snr_ValidFrom.UrlParameterName:        grmi.NewConditionItem(meta.Snr_ValidFrom, grmi.Equal, false),
		meta.Snr_ValidTo.UrlParameterName:          grmi.NewConditionItem(meta.Snr_ValidTo, grmi.Equal, false),
		meta.Snr_Prefix.UrlParameterName:           grmi.NewConditionItem(meta.Snr_Prefix, grmi.Equal, false),
		meta.Snr_Infix.UrlParameterName:            grmi.NewConditionItem(meta.Snr_Infix, grmi.Equal, false),
		meta.Snr_Postfix.UrlParameterName:          grmi.NewConditionItem(meta.Snr_Postfix, grmi.Equal, false),
		meta.Snr_StartNr.UrlParameterName:          grmi.NewConditionItem(meta.Snr_StartNr, grmi.Equal, false),
		meta.Snr_EndNr.UrlParameterName:            grmi.NewConditionItem(meta.Snr_EndNr, grmi.Equal, false),
		meta.Snr_Step.UrlParameterName:             grmi.NewConditionItem(meta.Snr_Step, grmi.Equal, false),
		meta.Snr_Length.UrlParameterName:           grmi.NewConditionItem(meta.Snr_Length, grmi.Equal, false),
		meta.Snr_NextNr.UrlParameterName:           grmi.NewConditionItem(meta.Snr_NextNr, grmi.Equal, false),
		meta.Snr_LastNrCreate.UrlParameterName:     grmi.NewConditionItem(meta.Snr_LastNrCreate, grmi.Equal, false),
		meta.Snr_ResetNrRule.UrlParameterName:      grmi.NewConditionItem(meta.Snr_ResetNrRule, grmi.Equal, false),
		meta.Snr_OverflowHandling.UrlParameterName: grmi.NewConditionItem(meta.Snr_OverflowHandling, grmi.Equal, false),
		meta.Snr_IdentifierFormat.UrlParameterName: grmi.NewConditionItem(meta.Snr_IdentifierFormat, grmi.Equal, false),
		meta.Snr_DateVariable.UrlParameterName:     grmi.NewConditionItem(meta.Snr_DateVariable, grmi.Equal, false),
		meta.Snr_DateFormat.UrlParameterName:       grmi.NewConditionItem(meta.Snr_DateFormat, grmi.Equal, false),
		meta.Snr_Identifierlayout.UrlParameterName: grmi.NewConditionItem(meta.Snr_Identifierlayout, grmi.Equal, false),
		meta.Snr_LastModify.UrlParameterName:       grmi.NewConditionItem(meta.Snr_LastModify, grmi.Approximate, false),
		meta.Snr_LastUser.UrlParameterName:         grmi.NewConditionItem(meta.Snr_LastUser, grmi.Equal, false),
		meta.Snr_CreateTime.UrlParameterName:       grmi.NewConditionItem(meta.Snr_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Snr的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
var DefaultConditionOfSnrAndPaging = grmi.NewCondition(
	DefaultConditionOfSnr.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : SnrServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Snr的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
type SnrServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewSnrDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个SnrDAOImplement实例
 *
 * @Return Value             : SnrDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 13:08:46
 *
 ******************************************************************************/
func NewSnrServiceImplement() *SnrServiceImplement {
	return &SnrServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SnrService.InsertOne
 *
 ******************************************************************************/
func (impl *SnrServiceImplement) InsertOne(user *models.Usertab, entity *model.Snr) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSnrDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.SnrId)
	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/SnrService.DeleteOne
 *
 ******************************************************************************/
func (impl *SnrServiceImplement) DeleteOne(user *models.Usertab, snrId string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SnrService.SelectOne
 *
 ******************************************************************************/
func (impl *SnrServiceImplement) SelectOne(user *models.Usertab, snrId string) (*model.Snr, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/SnrService.UpdateOne
 *
 ******************************************************************************/
func (impl *SnrServiceImplement) UpdateOne(user *models.Usertab, entity *model.Snr) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewSnrDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.SnrId)
	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/SnrService.Insert
 *
 ******************************************************************************/
func (impl *SnrServiceImplement) Insert(user *models.Usertab, entities *[]model.Snr) error {

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

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

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

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

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

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

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

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

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

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

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

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