package easydo.technology.controller;

import easydo.technology.components.JdbcClient;
import easydo.technology.model.Warehouse;
import easydo.technology.service.FlowNoService;
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.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warehouse")
public class WarehouseController {

    @Resource
    private JdbcClient jdbcClient;

    @Resource
    private DataSource dataSource;

    @Resource
    private FlowNoService flowNoService;

    @PostMapping(value = "/getPage")
    public Object getPage(@RequestBody(required = false) Map<String, Object> map) {
        try {
            if (map == null) {
                map = new HashMap<>();
            }
            // 分页查询仓库信息
            Map<String, Object> page = jdbcClient.getJdbcPage(map, Warehouse.class);
            if (page.get("records") == null) {
                page.put("records", new java.util.ArrayList<>());
            }
            return new ResponseEntity<>(page, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/getList")
    public Object getList(@RequestBody(required = false) Map<String, Object> map) {
        try {
            if (map == null) {
                map = new HashMap<>();
            }
            // 全量查询仓库信息
            List<Warehouse> list = jdbcClient.getJdbcList(map, Warehouse.class);
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/save")
    public Object save(@RequestBody Warehouse model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false); // 开启事务

            // 1) 调用面向对象的专用方法生成编码 (含细粒度锁、租户隔离、重复检查)
            String code = flowNoService.generateWarehouseCode(model, connection);
            model.setCode(code);

            // 2) 插入仓库记录
            jdbcClient.jdbcInsert(model, connection);

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

    @PostMapping(value = "/update")
    public Object update(@RequestBody Warehouse model) {
        try {
            // 根据 ID 修改仓库信息
            jdbcClient.jdbcUpdateById(model);
            return new ResponseEntity<>(model, HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping(value = "/remove")
    public Object remove(@RequestBody Warehouse model) throws Exception {
        Connection connection = dataSource.getConnection();
        try {
            connection.setAutoCommit(false); // 开启事务 (对齐 Go 版)

            // 根据 ID 删除仓库记录
            jdbcClient.jdbcRemoveById(model, connection);

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

