package com.jsc.oscm.controller;

import com.alibaba.fastjson.JSON;
import com.jsc.oscm.entity.MetaData;
import com.jsc.oscm.model.ReturnT;
import com.jsc.oscm.service.MetaDataService;
import org.springframework.web.bind.annotation.*;

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

@RestController
public class MetaDataController {

    @Resource
    MetaDataService metaDataService;

    @GetMapping("/metadata/tablename")
    public String getAllMetaByTableName(@RequestParam String tablename) {
        ReturnT<List<MetaData>> returnT;
        List<MetaData> metaDataList = metaDataService.queryByTableName(tablename);
        returnT = new ReturnT<>(200, "", metaDataList);
        return JSON.toJSONString(returnT);
    }

    @GetMapping("/metadata")
    public String getAllMeta() {
        ReturnT<List<MetaData>> returnT;
        List<MetaData> metaDataList = metaDataService.queryAll();
        returnT = new ReturnT<>(200, String.valueOf(metaDataList.size()), metaDataList);
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/metadata/add")
    public String addMetaData(@RequestBody MetaData metaData) {
        ReturnT<MetaData> returnT;
        try {
            returnT = new ReturnT<>(200, "", metaDataService.save(metaData));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PutMapping("/metadata/update")
    public String updateMetaData(@RequestBody MetaData metaData) {
        ReturnT<MetaData> returnT;
        try {
            returnT = new ReturnT<>(200, "", metaDataService.save(metaData));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/metadata/delete")
    public String deleteMetaData(@RequestBody MetaData metaData) {
        ReturnT<MetaData> returnT;
        try {
            metaDataService.delete(metaData);
            returnT = new ReturnT<>(200, "", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/metadata/deleteAll")
    public String deleteMetaData(@RequestBody List<MetaData> metaDataList) {
        ReturnT<MetaData> returnT;
        try {
            metaDataService.deleteAll(metaDataList);
            returnT = new ReturnT<>(200, "", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/metadata/queryPageable/{pageIndex}/{pageSize}")
    public String queryPageable(@RequestBody MetaData metaData,
                                @PathVariable(value = "pageIndex") int pageIndex,
                                @PathVariable(value = "pageSize") int pageSize) {
        ReturnT<List<MetaData>> returnT;
        try {
            returnT = new ReturnT<>(200,
                    String.valueOf(metaDataService.queryCount(metaData)),
                    metaDataService.queryPageable(metaData, pageIndex, pageSize));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }


    @GetMapping("/metadata/getTableNameList")
    public String getTableNameList() {
        ReturnT<List<Object>> returnT;
        try {
            returnT = new ReturnT<>(200, "", metaDataService.getTableNameList());
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }


}
