// 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"
	"LAPP_ACURA_MOM_BACKEND/utils"
)

/******************************************************************************
 *
 * @Description              : RoleMenu的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 16:11:14
 *
 ******************************************************************************/
var DefaultConditionOfRoleMenu = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.RoleMenu_RoleId.UrlParameterName:     grmi.NewConditionItem(meta.RoleMenu_RoleId, grmi.Equal, true),
		meta.RoleMenu_MenuId.UrlParameterName:     grmi.NewConditionItem(meta.RoleMenu_MenuId, grmi.Equal, true),
		meta.RoleMenu_RoleName.UrlParameterName:   grmi.NewConditionItem(meta.RoleMenu_RoleName, grmi.Equal, false),
		meta.RoleMenu_LastModify.UrlParameterName: grmi.NewConditionItem(meta.RoleMenu_LastModify, grmi.Approximate, false),
		meta.RoleMenu_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.RoleMenu_LastUser, grmi.Equal, false),
		meta.RoleMenu_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.RoleMenu_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : RoleMenu的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 16:11:14
 *
 ******************************************************************************/
var DefaultConditionOfRoleMenuAndPaging = grmi.NewCondition(
	DefaultConditionOfRoleMenu.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : RoleMenuServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : RoleMenu的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 16:11:14
 *
 ******************************************************************************/
type RoleMenuServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewRoleMenuDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个RoleMenuDAOImplement实例
 *
 * @Return Value             : RoleMenuDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-16 16:11:14
 *
 ******************************************************************************/
func NewRoleMenuServiceImplement() *RoleMenuServiceImplement {
	return &RoleMenuServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleMenuService.InsertOne
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) InsertOne(user *global.User, entity *model.RoleMenu) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleMenuDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.RoleId, 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_ACURA_MOM_BACKEND/services/base/RoleMenuService.DeleteOne
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) DeleteOne(user *global.User, roleId int, menuId int) error {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleMenuService.SelectOne
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) SelectOne(user *global.User, roleId int, menuId int) (*model.RoleMenu, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleMenuService.UpdateOne
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) UpdateOne(user *global.User, entity *model.RoleMenu) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewRoleMenuDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.RoleId, 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_ACURA_MOM_BACKEND/services/base/RoleMenuService.Insert
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) Insert(user *global.User, entities *[]model.RoleMenu) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RoleId, 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_ACURA_MOM_BACKEND/services/base/RoleMenuService.Delete
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) Delete(user *global.User, entities *[]model.RoleMenu) error {

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RoleId, 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_ACURA_MOM_BACKEND/services/base/RoleMenuService.Select
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) Select(user *global.User, urlParameters map[string]string) ([]model.RoleMenu, error) {

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

	condition := DefaultConditionOfRoleMenuAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewRoleMenuDAO(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/RoleMenuService.SelectAndPaging
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) SelectAndPaging(user *global.User, urlParameters map[string]string) (grmi.PagingResult, error) {

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

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

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

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleMenuService.GetMenuRole
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) GetMenuRole(user *global.User) ([]model.Menu, error) {

	grmi.Log(user, "/services/base/implments/RoleMenu.service.impl.go", "GetMenuRole", "联动获取左侧动态菜单")

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

	record, err := dao.GetByRoleId(user.RoleId)
	if err != nil {
		return record, err
	}
	menuList := make([]model.Menu, 0)
	for i := 0; i < len(record); i++ {
		if record[i].ParentId != 0 {
			continue
		}
		menusInfo := impl.DiguiMenu(&record, record[i])

		menuList = append(menuList, menusInfo)
	}
	return menuList, nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleMenuService.DiguiMenu
 *
 ******************************************************************************/
func (impl *RoleMenuServiceImplement) DiguiMenu(menulist *[]model.Menu, menu model.Menu) model.Menu {

	list := *menulist

	min := make([]model.Menu, 0)
	for j := 0; j < len(list); j++ {
		if menu.MenuId != list[j].ParentId {
			continue
		}
		mi := model.Menu{}
		mi.MenuId = list[j].MenuId
		mi.MenuName = list[j].MenuName
		mi.Title = list[j].Title
		mi.Icon = list[j].Icon
		mi.Path = list[j].Path
		mi.MenuType = list[j].MenuType
		mi.Action = list[j].Action
		mi.Permission = list[j].Permission
		mi.ParentId = list[j].ParentId
		mi.NoCache = list[j].NoCache
		mi.BreadCrumb = list[j].BreadCrumb
		mi.Component = list[j].Component
		mi.Sort = list[j].Sort
		mi.Visible = list[j].Visible
		mi.CreateTime = list[j].CreateTime
		mi.LastUser = list[j].LastUser
		mi.LastModify = list[j].LastModify
		mi.Children = []model.Menu{}
		utils.TrimStruct(&mi, mi)
		if mi.MenuType != "F" {
			ms := impl.DiguiMenu(menulist, mi)
			utils.TrimStruct(&ms, ms)
			min = append(min, ms)

		} else {
			min = append(min, mi)
		}

	}
	menu.Children = min
	return menu
}
