// 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              : Stdef的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfStdef = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Stdef_StdefTyp.UrlParameterName:   grmi.NewConditionItem(meta.Stdef_StdefTyp, grmi.Equal, true),
		meta.Stdef_StatId.UrlParameterName:     grmi.NewConditionItem(meta.Stdef_StatId, grmi.Equal, true),
		meta.Stdef_Lang.UrlParameterName:       grmi.NewConditionItem(meta.Stdef_Lang, grmi.Equal, true),
		meta.Stdef_Bez.UrlParameterName:        grmi.NewConditionItem(meta.Stdef_Bez, grmi.Equal, false),
		meta.Stdef_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Stdef_LastModify, grmi.Approximate, false),
		meta.Stdef_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.Stdef_LastUser, grmi.Equal, false),
		meta.Stdef_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Stdef_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : Stdef的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfStdefAndPaging = grmi.NewCondition(
	DefaultConditionOfStdef.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : StdefServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : Stdef的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
type StdefServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewStdefDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个StdefDAOImplement实例
 *
 * @Return Value             : StdefDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
func NewStdefServiceImplement() *StdefServiceImplement {
	return &StdefServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StdefService.InsertOne
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) InsertOne(user *global.User, entity *model.Stdef) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStdefDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.StdefTyp, entity.StatId, entity.Lang)
	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/StdefService.DeleteOne
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) DeleteOne(user *global.User, stdefTyp string, statId string, lang string) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StdefService.SelectOne
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) SelectOne(user *global.User, stdefTyp string, statId string, lang string) (*model.Stdef, error) {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStdefDAO(session, user.UserId)
	result, err := dao.SelectOne(stdefTyp, statId, lang)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/StdefService.UpdateOne
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) UpdateOne(user *global.User, entity *model.Stdef) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewStdefDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.StdefTyp, entity.StatId, entity.Lang)
	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/StdefService.Insert
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) Insert(user *global.User, entities *[]model.Stdef) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StdefTyp, entity.StatId, entity.Lang)
		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/StdefService.Delete
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) Delete(user *global.User, entities *[]model.Stdef) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.StdefTyp, entity.StatId, entity.Lang)
		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/StdefService.Select
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.Stdef, error) {

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

	condition := DefaultConditionOfStdefAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()

	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewStdefDAO(session, 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/StdefService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *StdefServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

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

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

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

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

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