// 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              : Menu的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-04-13 15:04:54
 *
 ******************************************************************************/
var DefaultConditionOfMenu = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Menu_MenuId.UrlParameterName:     grmi.NewConditionItem(meta.Menu_MenuId, grmi.Equal, true),
		meta.Menu_MenuName.UrlParameterName:   grmi.NewConditionItem(meta.Menu_MenuName, grmi.Equal, false),
		meta.Menu_Title.UrlParameterName:      grmi.NewConditionItem(meta.Menu_Title, grmi.Equal, false),
		meta.Menu_Icon.UrlParameterName:       grmi.NewConditionItem(meta.Menu_Icon, grmi.Equal, false),
		meta.Menu_Path.UrlParameterName:       grmi.NewConditionItem(meta.Menu_Path, grmi.Equal, false),
		meta.Menu_Paths.UrlParameterName:      grmi.NewConditionItem(meta.Menu_Paths, grmi.Equal, false),
		meta.Menu_MenuType.UrlParameterName:   grmi.NewConditionItem(meta.Menu_MenuType, grmi.Equal, false),
		meta.Menu_Action.UrlParameterName:     grmi.NewConditionItem(meta.Menu_Action, grmi.Equal, false),
		meta.Menu_Permission.UrlParameterName: grmi.NewConditionItem(meta.Menu_Permission, grmi.Equal, false),
		meta.Menu_ParentId.UrlParameterName:   grmi.NewConditionItem(meta.Menu_ParentId, grmi.Equal, false),
		meta.Menu_NoCache.UrlParameterName:    grmi.NewConditionItem(meta.Menu_NoCache, grmi.Equal, false),
		meta.Menu_BreadCrumb.UrlParameterName: grmi.NewConditionItem(meta.Menu_BreadCrumb, grmi.Equal, false),
		meta.Menu_Component.UrlParameterName:  grmi.NewConditionItem(meta.Menu_Component, grmi.Equal, false),
		meta.Menu_Sort.UrlParameterName:       grmi.NewConditionItem(meta.Menu_Sort, grmi.Equal, false),
		meta.Menu_Visible.UrlParameterName:    grmi.NewConditionItem(meta.Menu_Visible, grmi.Equal, false),
		meta.Menu_IsFrame.UrlParameterName:    grmi.NewConditionItem(meta.Menu_IsFrame, grmi.Equal, false),
		meta.Menu_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Menu_LastModify, grmi.Approximate, false),
		meta.Menu_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.Menu_LastUser, grmi.Equal, false),
		meta.Menu_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Menu_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/MenuService.InsertOne
 *
 ******************************************************************************/
func (impl *MenuServiceImplement) InsertOne(user *models.Usertab, entity *model.Menu) error {

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/MenuService.SelectOne
 *
 ******************************************************************************/
func (impl *MenuServiceImplement) SelectOne(user *models.Usertab, menuId int) (*model.Menu, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/base/MenuService.UpdateOne
 *
 ******************************************************************************/
func (impl *MenuServiceImplement) UpdateOne(user *models.Usertab, entity *model.Menu) error {

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

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

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

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

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

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

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

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

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

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

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

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

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

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