package dao

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
)

func GetMenu(db *DaoDB, name string, level, menuType int, userID string) (menus []*model.Menu, err error) {
	sqlParams := []interface{}{}
	sql := `
		SELECT DISTINCT a.*
		  FROM menu a
	`
	if userID != "" {
		sql += ` 
				 JOIN menu d ON d.parent_id = a.id
				 JOIN user_role b ON b.user_id = ? AND b.deleted_at = ?
				 JOIN role_menu c ON c.menu_id = d.id AND c.role_id = b.role_id AND c.deleted_at = ?
		`
		sqlParams = append(sqlParams, userID, utils.DefaultTimeValue, utils.DefaultTimeValue)
	}
	sql += " WHERE a.deleted_at = ?"
	sqlParams = append(sqlParams, utils.DefaultTimeValue)
	if name != "" {
		sql += " AND a.name LIKE ?"
		sqlParams = append(sqlParams, "%"+name+"%")
	}
	if level != 0 {
		sql += " AND a.level = ?"
		sqlParams = append(sqlParams, level)
	}
	if menuType != 0 {
		sql += " AND a.type = ?"
		sqlParams = append(sqlParams, menuType)
	}
	sql += " ORDER BY a.id"
	err = GetRows(db, &menus, sql, sqlParams)
	return menus, err
}

func GetMenuWithUser(db *DaoDB, name string, level, menuType int, userID string) (menus []*model.Menu, err error) {
	sql := `
	SELECT DISTINCT b.* FROM (
		SELECT a.id,a.parent_id
		FROM menu a
		JOIN user_role b ON b.user_id = ? AND b.deleted_at = ?
		JOIN role_menu c ON c.menu_id = a.id AND c.role_id = b.role_id AND c.deleted_at = ?
		WHERE a.deleted_at = ?)a 
	JOIN menu b ON (b.id = a.id OR b.id = a.parent_id)
	WHERE b.type = ?
	ORDER BY b.id
	`
	sqlParams := []interface{}{userID, utils.DefaultTimeValue, utils.DefaultTimeValue, utils.DefaultTimeValue, menuType}
	err = GetRows(db, &menus, sql, sqlParams)
	return menus, err
}

func GetRole(db *DaoDB, name, name2 string) (roles []*model.Role, err error) {
	sql := `
		SELECT *
		  FROM role
		 WHERE deleted_at = ? 
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if name != "" {
		sql += " AND name LIKE ?"
		sqlParams = append(sqlParams, "%"+name+"%")
	}
	if name2 != "" {
		sql += " AND name = ?"
		sqlParams = append(sqlParams, name2)
	}
	err = GetRows(db, &roles, sql, sqlParams)
	return roles, err
}

func GetUserRole(db *DaoDB, userIDs []string, roleIDs []int) (userRoles []*model.UserRole, err error) {
	sql := `
		SELECT *
		  FROM user_role
		 WHERE deleted_at = ? 
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(userIDs) > 0 {
		sql += " AND user_id IN (" + GenQuestionMarks(len(userIDs)) + ")"
		sqlParams = append(sqlParams, userIDs)
	}
	if len(roleIDs) > 0 {
		sql += " AND role_id IN (" + GenQuestionMarks(len(roleIDs)) + ")"
		sqlParams = append(sqlParams, roleIDs)
	}
	err = GetRows(db, &userRoles, sql, sqlParams)
	return userRoles, err
}

type GetUserRole2Result struct {
	model.Role
	model.UserRole
}

func GetUserRole2(db *DaoDB, userIDs []string, roleIDs []int) (userRoles []*GetUserRole2Result, err error) {
	sql := `
		SELECT *
		  FROM user_role a
		  JOIN role b ON a.role_id = b.id
		 WHERE a.deleted_at = ? 
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(userIDs) > 0 {
		sql += " AND a.user_id IN (" + GenQuestionMarks(len(userIDs)) + ")"
		sqlParams = append(sqlParams, userIDs)
	}
	if len(roleIDs) > 0 {
		sql += " AND a.role_id IN (" + GenQuestionMarks(len(roleIDs)) + ")"
		sqlParams = append(sqlParams, roleIDs)
	}
	err = GetRows(db, &userRoles, sql, sqlParams)
	return userRoles, err
}

func GetRoleMenu(db *DaoDB, roleIDs, menuIDs []int) (roleMenus []*model.RoleMenu, err error) {
	sql := `
		SELECT *
		  FROM role_menu
		 WHERE deleted_at = ? 
	`
	sqlParams := []interface{}{
		utils.DefaultTimeValue,
	}
	if len(roleIDs) > 0 {
		sql += " AND role_id IN (" + GenQuestionMarks(len(roleIDs)) + ")"
		sqlParams = append(sqlParams, roleIDs)
	}
	if len(menuIDs) > 0 {
		sql += " AND menu_id IN (" + GenQuestionMarks(len(menuIDs)) + ")"
		sqlParams = append(sqlParams, menuIDs)
	}
	err = GetRows(db, &roleMenus, sql, sqlParams)
	return roleMenus, err
}
