package com.jsc.oscm.controller;

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

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

@RestController
public class MetaDictController {

    @Resource
    MetaDictService metaDictService;

    @GetMapping("/metadict/byTableName/{tableName}")
    public String queryMetaDictByTableName(@PathVariable(name = "tableName") String tableName) {
        ReturnT<List<MetaDict>> returnT;
        try {
            MetaDict metaDict = new MetaDict();
            metaDict.setTableName(tableName);
            returnT = new ReturnT<>(200, "", metaDictService.queryAll(metaDict));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }


    @GetMapping("/metadict/byColName/{colName}")
    public String queryMetaDictByColName(@PathVariable(name = "colName") String colName) {
        ReturnT<List<MetaDict>> returnT;
        try {
            MetaDict metaDict = new MetaDict();
            metaDict.setColumnName(colName);
            returnT = new ReturnT<>(200, "", metaDictService.queryAll(metaDict));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PostMapping("/metadict/add")
    public String addMetaDict(@RequestBody MetaDict metaDict) {
        ReturnT<MetaDict> returnT;
        try {
            returnT = new ReturnT<>(200, "", metaDictService.save(metaDict));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @PutMapping("/metadict/update")
    public String updateMetaDict(@RequestBody MetaDict metaDict) {
        ReturnT<MetaDict> returnT;
        try {
            returnT = new ReturnT<>(200, "", metaDictService.save(metaDict));
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/metadict/delete")
    public String deleteMetaDict(@RequestBody MetaDict metaDict) {
        ReturnT<MetaDict> returnT;
        try {
            metaDictService.delete(metaDict);
            returnT = new ReturnT<>(200, "", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

    @DeleteMapping("/metadict/deleteAll")
    public String deleteMetaDict(@RequestBody List<MetaDict> metaDictList) {
        ReturnT<MetaDict> returnT;
        try {
            metaDictService.deleteAll(metaDictList);
            returnT = new ReturnT<>(200, "", null);
        } catch (Exception e) {
            returnT = new ReturnT<>(500, e.getMessage(), null);
        }
        return JSON.toJSONString(returnT);
    }

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

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

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

}
