package easydo.technology.controller;

import easydo.technology.annotation.WebLog;
import easydo.technology.components.JdbcClient;
import easydo.technology.exception.BizException;
import easydo.technology.model.PurchaseContract;
import easydo.technology.service.GLDCostService;
import easydo.technology.util.HttpClientUtil;
import easydo.technology.utils.StringUtil;

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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplierContract")
public class PurchaseContractController {
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;
    @Resource
    GLDCostService gldCostService;

    @WebLog
    @RequestMapping(value = "/getList")
    public Object getList(@RequestBody Map<String, Object> map) throws Exception {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();

            List<PurchaseContract> list = jdbcClient.getJdbcList(map, PurchaseContract.class, connection);
            return new ResponseEntity<>(list, HttpStatus.OK);
        } catch (Exception e) {
            throw e;
        } finally {
            jdbcClient.finallyExecute(connection);
        }
    }

    @WebLog
    @GetMapping(value = "/download")
    public void test(HttpServletResponse response,HttpServletRequest request, @RequestParam String objectId,
            @RequestParam(required = false) String fileName) throws Exception {
        gldCostService.getContractFile(objectId, fileName,request, response);
    }
}
