// 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              : Role的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-08-20 11:11:05
 *
 ******************************************************************************/
var DefaultConditionOfRole = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.Role_RoleId.UrlParameterName:     grmi.NewConditionItem(meta.Role_RoleId, grmi.Equal, true),
		meta.Role_RoleName.UrlParameterName:   grmi.NewConditionItem(meta.Role_RoleName, grmi.Equal, false),
		meta.Role_RoleSort.UrlParameterName:   grmi.NewConditionItem(meta.Role_RoleSort, grmi.Equal, false),
		meta.Role_Status.UrlParameterName:     grmi.NewConditionItem(meta.Role_Status, grmi.Equal, false),
		meta.Role_Remark.UrlParameterName:     grmi.NewConditionItem(meta.Role_Remark, grmi.Equal, false),
		meta.Role_RoleKey.UrlParameterName:    grmi.NewConditionItem(meta.Role_RoleKey, grmi.Equal, false),
		meta.Role_DataScope.UrlParameterName:  grmi.NewConditionItem(meta.Role_DataScope, grmi.Equal, false),
		meta.Role_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Role_LastModify, grmi.Approximate, false),
		meta.Role_LastUser.UrlParameterName:   grmi.NewConditionItem(meta.Role_LastUser, grmi.Equal, false),
		meta.Role_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Role_CreateTime, grmi.Approximate, false),
	},
	nil,
)

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

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.InsertOne
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) InsertOne(user *global.User, entity *model.Role) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()

	dao := dal.NewRoleDAO(session, user.UserId)
	record, err := dao.SelectByRoleName(entity.RoleKey)
	if err != nil {
		session.Rollback()
		return err
	}
	if record != nil {
		session.Rollback()
		return grmi.NewBusinessError("已经存在相同RoleName的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	result, err := dao.SelectByRoleName(entity.RoleKey)
	if err != nil {
		session.Rollback()
		return err
	}
	rdao := dal.NewRoleMenuDAO(session, user.UserId)
	_, err = rdao.InsertRoleMenu(result.RoleId, entity.MenuIds)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.DeleteOne
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) DeleteOne(user *global.User, roleId int) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()

	dao := dal.NewRoleDAO(session, user.UserId)
	record, err := dao.SelectOne(roleId)
	if err != nil {
		session.Rollback()
		return err
	}
	if record == nil {
		session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	//删除角色
	err = dao.DeleteOne(roleId)
	if err != nil {
		session.Rollback()
		return err
	}
	//删除对应关系
	rdao := dal.NewRoleMenuDAO(session, user.UserId)
	_, err = rdao.DeleteRoleMenu(roleId, record.RoleKey)
	if err != nil {
		session.Rollback()
		return err
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.SelectOne
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) SelectOne(user *global.User, roleId int) (*model.Role, error) {

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

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

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.UpdateOne
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) UpdateOne(user *global.User, entity *model.Role) error {

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

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err := session.Begin()
	dao := dal.NewRoleDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.RoleId)
	if err != nil {
		session.Rollback()
		return err
	}
	if record == nil {
		session.Rollback()
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		session.Rollback()
		return err
	}
	//删除对应关系
	rdao := dal.NewRoleMenuDAO(session, user.UserId)
	_, err = rdao.DeleteRoleMenu(entity.RoleId, record.RoleKey)
	if err != nil {
		session.Rollback()
		return err
	}
	if len(entity.MenuIds) > 0 {
		_, err = rdao.InsertRoleMenu(entity.RoleId, entity.MenuIds)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.UpdateStatus
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) UpdateStatus(user *global.User, entity *model.Role) error {

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

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

	dao := dal.NewRoleDAO(session, user.UserId)
	record, err := dao.SelectOne(entity.RoleId)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateStatus(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_ACURA_MOM_BACKEND/services/base/RoleService.Insert
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) Insert(user *global.User, entities *[]model.Role) error {

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

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

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

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

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

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

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

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

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

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

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

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

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.RoleId)
		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/RoleService.GetList
 *
 ******************************************************************************/
func (impl *RoleServiceImplement) GetList(user *global.User) ([]model.Role, error) {

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

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