package easydo.technology.system.rest;

import easydo.technology.components.JdbcClient;
import easydo.technology.exception.BizException;
import easydo.technology.system.model.SysMenu;
import easydo.technology.system.model.SysRolesMenus;
import easydo.technology.system.model.SysUsersRoles;
import easydo.technology.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;

    @RequestMapping(value = "/getList")
    public Object getList(@RequestBody Map<String, Object> map) throws Exception {
        List<SysMenu> list = jdbcClient.getJdbcList(map, SysMenu.class);
        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    @RequestMapping(value = "/build")
    public ResponseEntity<Object> buildMenus() throws Exception {
        Map<String, Object> paramMap = new HashMap<>();
        SysUsersRoles usersRoles = new SysUsersRoles();
        usersRoles.setUserId(SecurityUtils.getCurrentUserId());
        List<SysUsersRoles> rolesList = jdbcClient.getJdbcList(usersRoles);
        List<Long> roleIdList = rolesList.stream().map(SysUsersRoles::getRoleId).collect(Collectors.toList());

        paramMap.put("roleIdIn", roleIdList);
        List<SysRolesMenus> rolesMenusList = jdbcClient.getJdbcList(paramMap, SysRolesMenus.class);
        List<Long> menuIdList = rolesMenusList.stream().map(SysRolesMenus::getMenuId).distinct().collect(Collectors.toList());

        List<SysMenu> menuList = new ArrayList<>();
        if (!menuIdList.isEmpty()) {
            paramMap.clear();
            paramMap.put("idIn", menuIdList);
            menuList = jdbcClient.getJdbcList(paramMap, SysMenu.class);
        }
        return new ResponseEntity<>(menuList, HttpStatus.OK);
    }

    @RequestMapping(value = "/save")
    public Object add(@RequestBody SysMenu menu) throws Exception {
        jdbcClient.jdbcInsert(menu);
        return new ResponseEntity<>(menu, HttpStatus.OK);
    }


    @RequestMapping(value = "/update")
    public Object update(@RequestBody SysMenu menu) throws Exception {
        jdbcClient.jdbcUpdateById(menu);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(value = "/remove")
    public Object deleteProgram(@RequestBody SysMenu menu) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false);
            SysMenu childrenMenu = new SysMenu();
            childrenMenu.setPid(menu.getId());
            int count = jdbcClient.getJdbcCount(childrenMenu, connection);
            if (count > 0) {
                throw new BizException("存在下级子菜单，无法删除");
            }

            SysRolesMenus rolesMenus = new SysRolesMenus();
            rolesMenus.setMenuId(menu.getId());
            jdbcClient.jdbcRemove(rolesMenus, connection);

            jdbcClient.jdbcRemoveById(menu, connection);
            connection.commit();
            return new ResponseEntity<>(HttpStatus.OK);
        } catch (Exception e) {
            connection.rollback();
            throw new BizException(e.getMessage());
        } finally {
            connection.close();
        }

    }

}
