package easydo.technology.system.rest;

import easydo.technology.components.JdbcClient;
import easydo.technology.system.model.SysRole;
import easydo.technology.system.model.SysRolesMenus;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/sysRole")
public class SysRoleController {
    @Resource
    JdbcClient jdbcClient;

    @Resource
    DataSource dataSource;

    @PostMapping(value = "/getList")
    public Object getList(@RequestBody Map<String, Object> map) {
        try {
            List<SysRole> list = jdbcClient.getJdbcList(map, SysRole.class);
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/getPage")
    public Object getPage(@RequestBody Map<String, Object> map) {
        try {
            Map<String, Object> recordsPage = jdbcClient.getJdbcPage(map, SysRole.class);
            return new ResponseEntity<>(recordsPage, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @PostMapping(value = "/save")
    public Object add(@RequestBody SysRole model) {
        try {
            jdbcClient.jdbcInsert(model);
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }


    @PostMapping(value = "/update")
    public Object update(@RequestBody SysRole model) {
        try {
            jdbcClient.jdbcUpdateById(model);
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/remove")
    public Object deleteProgram(@RequestBody SysRole model) {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(false); // 开启事务

            // 1) 同步删除角色与菜单的关联关系 (对齐 Go 版本 roleRemove)
            SysRolesMenus roleMenuParam = new SysRolesMenus();
            roleMenuParam.setRoleId(model.getId());
            jdbcClient.jdbcRemove(roleMenuParam, connection);

            // 2) 删除角色本身
            jdbcClient.jdbcRemoveById(model, connection);

            connection.commit(); // 提交事务
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            if (connection != null) {
                try {
                    connection.rollback(); // 发生异常回滚事务
                } catch (Exception ignored) {
                }
            }
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception ignored) {
                }
            }
        }
    }

}
