package com.gyee.frame.controller.StandardPointTable;


import com.gyee.frame.service.StandardPointTable.StandardPointTableService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 标准点表
 * @author 马晓霞
 * @email maxiaoxia@gyee-china.com
 * @date 2021-1-9 17:00:04
 */

@RestController
@RequestMapping("/UnionTable")
public class StandardPointTableController {

    List<Map<String,Object>> unionData= new ArrayList<>();

    @Resource
    private StandardPointTableService standardPointTableService;

    /**
     * 数据库表名
     *
     * @param
     * @return 返回数据库表名
     */
    @GetMapping("/tableNames")
    public List<String> getTableName() {
        return standardPointTableService.getTableName();
    }

    /**
     * 查询列名
     * @param tableName 表名
     * @return
     */
    @CrossOrigin(origins  = "*",maxAge = 3600)
    @PostMapping("/columns")
    @ResponseBody
    public  List<String> getAllColumns(@RequestBody String tableName){

        return standardPointTableService.getAllColumns(tableName.substring(0,tableName.length()-1 ));
    }

    /**
     * 联合表
     * @param map 表名,列名,excel数据
     * @return 联合表
     */
    @CrossOrigin(origins  = "*",maxAge = 3600)
    @ResponseBody
    @PostMapping("/tableData")
    public List<Map<String, Object>> getDataByColumns(  @RequestBody Map<String, Object> map) {

        String tableName = (String) map.get("tableName");
        List columnsName  = (List)map.get("columnsName");
        List<Map<String, Object>> excelData  = (List) map.get("excelData");

        String stringColumnsName="";
        for (int i=0; i<columnsName.size();i++) {
            stringColumnsName +=columnsName.get(i)+(i==columnsName.size()-1 ? "":",");
        }
        unionData = standardPointTableService.getDataByColumns(tableName,stringColumnsName,excelData);
        return unionData;
    }

    /**
     * 将上面展示出来的数据保存到数据库
     *
     * @param tableName 数据库表名
     */
    @PostMapping("/saveData")
    @ResponseBody
    @CrossOrigin(origins  = "*",maxAge = 3600)
    public String saveTableData(@RequestBody String tableName) {

        standardPointTableService.saveData(unionData,tableName.substring(0,tableName.length()-1 ));

        return "保存成功";
    }

}


