package com.roma.romaapi.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import com.roma.romaapi.utils.CommonUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class PageDao {
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private CommonUtil commonUtil;

    // 获取页面json
    public Map dbQueryForMap(String pageCode) {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("page_code", pageCode);
            Map<String, Object> resMap2 = namedParameterJdbcTemplate.queryForMap(
                    "SELECT `page_json` FROM `page_details` where `page_code` = :page_code", paramMap);

            return resMap2;
        } catch (EmptyResultDataAccessException e) {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("sysErrorCode", "500");
            resultMap.put("sysErrorMessage", "operation failed");

            return resultMap;
        }
    }

    // 获取全部菜单项
    public List<Map<String, Object>> getMenuForList(){
        Map<String, Object> paramMap = new HashMap<>();
          String sql= " SELECT  `rp`.`is_visible`,`rp`.`id`,`rp`.`parent_id`,`rp`.`permissions_icon` as `icon`,`rp`.`object_type`," +
                  "`rp`.`object_id`,`pd`.`page_code` AS `path`,`pd`.`front_path` AS `url`,`rp`.`menu_name` AS `label` " +
                  "FROM `roma_permissions` AS `rp` LEFT JOIN `page_details` AS `pd` ON `pd`.`id`=`rp`.`object_id` " +
                  "WHERE `rp`.`is_enable`=1   and `rp`.`object_type`='page'    ORDER BY `rp`.`permission_sort` ASC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取指定用户的全部菜单项
    public List<Map<String, Object>> getAdminMenuByIdForList(String userId){
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userId", 4);
        String userPermissionSql = "SELECT `permission_id` FROM `roma_permission_role` AS `pr` " +
                "JOIN  `roma_role_user` AS `ru` ON `ru`.`role_id`=`pr`.`role_id` " +
                "WHERE `ru`.`user_id`=:userId GROUP BY `pr`.permission_id ";
        List<Map<String, Object>> permissIdList = namedParameterJdbcTemplate.queryForList(userPermissionSql, paramMap);
        System.out.println("全部的权限id==============="+permissIdList);
        if(permissIdList != null && !permissIdList.isEmpty()){
            String permissionIdString = "";
            for (int i = 0; i < permissIdList.size(); i++) {
                Map map = permissIdList.get(i);
                String permissionId = map.get("permission_id").toString();
                permissionIdString+=permissionId+',';
            }
            permissionIdString = commonUtil.trimFirstAndLastChar(permissionIdString, ",");
            // 获取用户权限下的所有菜单相关
            String sql= " SELECT  `rp`.`is_visible`,`rp`.`id`,`rp`.`parent_id`,`rp`.`permissions_icon` as `icon`,`rp`.`object_type`," +
                    "`rp`.`object_id`,`pd`.`page_code` AS `path`,`rp`.`menu_name` AS `label` " +
                    "FROM `roma_permissions` AS `rp` LEFT JOIN `page_details` AS `pd` ON `pd`.`id`=`rp`.`object_id` " +
                    "WHERE `rp`.`is_enable`=1   and `rp`.`object_type`='page'  " +
                    " AND `` ORDER BY `rp`.`permission_sort` ASC ";
            List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

            return resMap2;
        } else {
            return new ArrayList<>();
        }

    }

    // 获取全部权限
    public List<Map<String, Object>> getAllMenuForList(){
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  `rp`.`is_enable`,`rp`.`id`,`rp`.`parent_id`,`rp`.`permissions_icon`,`rp`.`object_type`," +
                "`rp`.`object_id`,`rp`.`is_dir`,`rp`.`permission_sort`,`rp`.`menu_name` FROM `roma_permissions` AS `rp` " +
                "WHERE  1=1  ORDER BY `rp`.`permission_sort` ASC";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取Page全部权限
    public List<Map<String, Object>> getPageMenuForList(){
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  `pd`.`page_code`,`pd`.`page_name`,`rp`.`is_enable`,`rp`.`id`,`rp`.`parent_id`,`rp`.`permissions_icon`,`rp`.`object_type`," +
                "`rp`.`object_id`,`rp`.`is_dir`,`rp`.`permission_sort`,`rp`.`menu_name` FROM `roma_permissions` AS `rp` " +
                " LEFT JOIN `page_details` as `pd` ON `pd`.`id`=`rp`.`object_id` " +
                "WHERE 1=1 ORDER BY `rp`.`permission_sort` ASC";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取全部的页面
    public List<Map<String, Object>> getAllPageList() {
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  *  FROM `page_details` WHERE 1=1 ORDER BY `id` DESC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // Page页面管理-获取带分页的列表数据
    public List<Map<String, Object>> getAllPageListHasPaging(Map bindValue, String page, String perPage, Integer count) {
        // 计算分页
        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
        String listSql = "SELECT  *  FROM `page_details` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;

        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);

        return resMap2;
    }

    // Page页面管理-获取总数
    public Integer getAllPageListHasPagingCount(Map bindValue) {
        // 获取总数
        String countInnerSql = " SELECT * FROM `page_details` WHERE 1=1 ";
        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);

        return count;
    }

    // Sql页面管理-获取总数
    public Integer getAllSqlListHasPagingCount(Map bindValue) {
        // 获取总数
        String countInnerSql = " SELECT * FROM `sql_details` WHERE 1=1 ";
        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);

        return count;
    }

    // Sql页面管理-获取带分页的列表数据
    public List<Map<String, Object>> getAllSqlListHasPaging(Map bindValue, String page, String perPage, Integer count) {
        // 计算分页
        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
        String listSql = "SELECT  *  FROM `sql_details` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;

        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);

        return resMap2;
    }

    // Api页面管理-获取总数
    public Integer getAllApiListHasPagingCount(Map bindValue) {
        // 获取总数
        String countInnerSql = " SELECT * FROM `api_details` WHERE 1=1 ";
        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);

        return count;
    }

    // Api页面管理-获取带分页的列表数据
    public List<Map<String, Object>> getAllApiListHasPaging(Map bindValue, String page, String perPage, Integer count) {
        // 计算分页
        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
        String listSql = "SELECT  *  FROM `api_details` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;

        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);

        return resMap2;
    }

    // item页面管理-获取总数
    public Integer getAllItemListHasPagingCount(Map bindValue) {
        // 获取总数
        String countInnerSql = " SELECT * FROM `item_details` WHERE 1=1 ";
        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);

        return count;
    }

    // item页面管理-获取带分页的列表数据
    public List<Map<String, Object>> getAllItemListHasPaging(Map bindValue, String page, String perPage, Integer count) {
        // 计算分页
        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
        String listSql = "SELECT  *  FROM `item_details` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;

        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);

        return resMap2;
    }

    // 获取全部的sql
    public List<Map<String, Object>> getAllSqlList() {
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  *  FROM `sql_details` WHERE 1=1 ORDER BY `id` DESC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取全部的api
    public List<Map<String, Object>> getAllApiList() {
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  *  FROM `api_details` WHERE 1=1 ORDER BY `id` DESC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取全部的item
    public List<Map<String, Object>> getAllItemList() {
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  *  FROM `item_details` WHERE 1=1 ORDER BY `id` DESC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 获取全部的role,用户分配角色使用
    public List<Map<String, Object>> getAllRoleList() {
        Map<String, Object> paramMap = new HashMap<>();
        String sql = " SELECT  *  FROM `roma_roles` WHERE `is_enable`=1 ORDER BY `id` DESC ";
        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(sql, paramMap);

        return resMap2;
    }

    // 用户管理-获取总数
    public Integer getAllUserListHasPagingCount(Map bindValue) {
        // 获取总数
        String countInnerSql = " SELECT * FROM `admin_user` WHERE 1=1 ";
        String countSql = "SELECT COUNT(*) AS `totalData` FROM ( " + countInnerSql + " ) AS ROMA";
        Integer count = namedParameterJdbcTemplate.queryForObject(countSql, bindValue, Integer.class);

        return count;
    }

    // 用户管理-获取带分页的列表数据
    public List<Map<String, Object>> getAllUserListHasPaging(Map bindValue, String page, String perPage, Integer count) {
        // 计算分页
        String limitAfter = commonUtil.calcPagingString(count, page, perPage);
        String listSql = "SELECT  *  FROM `admin_user` WHERE 1=1 ORDER BY `id` DESC " + " LIMIT " + limitAfter;

        List<Map<String, Object>> resMap2 = namedParameterJdbcTemplate.queryForList(listSql, bindValue);

        return resMap2;
    }
}
