package easydo.technology.controller;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.model.ApproveInfo;
import easydo.technology.service.GLDMasterService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
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.Map;

@RestController
@RequestMapping("/gldMaster")
public class GLDMasterController {
    @Resource
    GLDMasterService gldMasterService;
    @Value("${GLD.APPROVE.mgtGroupId}")
    String mgtGroupId;
    @Value("${tenantId_gld_master}")
    String tenantId;
    @Value("${GLD.host}")
    String host;
    @Resource
    DataSource dataSource;
    @Resource
    JdbcClient jdbcClient;

    @WebLog
    @RequestMapping("/getToken")
    public Object getToken() throws Exception {
        Map<String, Object> result = new HashMap<>();
        String token = gldMasterService.getGetToken();
        result.put("mgtGroupId", mgtGroupId);
        result.put("tenantId", tenantId);
        result.put("token", token);
        result.put("host", host);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @WebLog
    @RequestMapping("/getFormId")
    public Object getFormId(@RequestBody ApproveInfo approveInfo) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            approveInfo = jdbcClient.getJdbcModel(approveInfo, connection);
            return new ResponseEntity<>(approveInfo.getFormId(), HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }


    @WebLog
    @GetMapping("/removeToken")
    public Object removeToken() throws Exception {
        gldMasterService.removeToken();
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
