CarsController.java 3.07 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.entity.Cars;
import com.bsth.service.schedule.utils.DataImportExportService;
import com.bsth.service.schedule.utils.DataToolsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xu on 16/5/31.
 */
@RestController
@RequestMapping("cars")
@EnableConfigurationProperties(DataToolsProperties.class)
public class CarsController extends BaseController2<Cars, Integer> {

    @Autowired
    private DataToolsProperties dataToolsProperties;
    @Autowired
    private DataImportExportService dataImportExportService;

    /**
     * 验证。
     * @param map
     * @return
     */
    @RequestMapping(value = "/validate/equale", method = RequestMethod.GET)
    public Map<String, Object> validateData(@RequestParam Map<String, Object> map) {
        // 一般比较自编号是否重复
        return baseService.validateEquale(map);
    }

    // uploadFile post

    // 验证excel sheet
    @RequestMapping(value = "/validate/sheet", method = RequestMethod.GET)
    public Map<String, Object> validateSheet() throws Exception {
        Map<String, Object> rtn = new HashMap<>();

        // TODO:

        rtn.put("status", ResponseCode.SUCCESS);
        return rtn;
    }

    @RequestMapping(value = "/importfile", method = RequestMethod.POST)
    public Map<String, Object> importData(
            @RequestParam Map<String, Object> form)
            throws Exception {
        Map<String, Object> rtn = new HashMap<>();

        // TODO:
        String filename = (String) form.get("filename");


        try {
            // 获取ktr转换文件绝对路径
            File ktrfile = new File(this.getClass().getResource(getDataImportKtrClasspath()).toURI());
            System.out.println(ktrfile.getAbsolutePath());
            // 导入数据
            dataImportExportService.fileDataImport(new File(filename), ktrfile);

            rtn.put("status", ResponseCode.SUCCESS);
            rtn.put("msg", "导入成功");
        } catch (Exception exp) {
            exp.printStackTrace();
            rtn.put("status", ResponseCode.ERROR);
            rtn.put("msg", exp.getLocalizedMessage());
        }

        return rtn;
    }


    @Override
    protected String getDataImportKtrClasspath() {
        return dataToolsProperties.getCarsDatainputktr();
    }

    @Override
    protected String getDataExportKtrClasspath() {
        return dataToolsProperties.getCarsDataoutputktr();
    }

    @Override
    protected String getDataExportFilename() {
        return "车辆基础数据";
    }
}