package com.salpa.web.controller.model;

import com.salpa.common.constant.UserConstants;
import com.salpa.common.core.controller.BaseController;
import com.salpa.common.core.domain.AjaxResult;
import com.salpa.common.core.page.TableDataInfo;
import com.salpa.model.domain.ProjectModelInfo;
import com.salpa.model.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 模型管理
 *
 * @author zhuff
 */
@RestController
@RequestMapping("/model/management")
public class ModelController extends BaseController {

    @Autowired
    private ModelService modelService;

    /**
     * 获取当前用户项目模型列表
     * @author zhuff
     * @return
     */
    @GetMapping("list")
    public TableDataInfo getList(@RequestParam Integer pageNum,@RequestParam Integer pageSize,@RequestParam String projectName) {
        List<ProjectModelInfo> list = modelService.getList(pageNum,pageSize,projectName);
        return getDataTable(list);
    }

    /**
     * 根据项目id获取项目模型
     * @author zhuff
     * @param projectId
     * @return
     */
    @GetMapping("getById")
    public AjaxResult getById(Integer projectId) {
        return AjaxResult.success(modelService.getById(projectId));
    }

    /**
     * 新增项目模型
     * @author zhuff
     * @param projectModelInfo
     * @return
     */
    @PostMapping("add")
    public AjaxResult addProject(@RequestBody ProjectModelInfo projectModelInfo) throws IOException {
        if (UserConstants.NOT_UNIQUE.equals(modelService.checkProjectNameUnique(projectModelInfo.getProjectName())))
        {
            return AjaxResult.error("新增项目模型'" + projectModelInfo.getProjectName() + "'失败，该项目模型已存在");
        }
        return toAjax(modelService.addProject(projectModelInfo));
    }

    /**
     * 修改项目模型
     * @author zhuff
     * @param projectModelInfo
     * @return
     */
    @PostMapping("update")
    public AjaxResult updateProject(@RequestBody ProjectModelInfo projectModelInfo) {
        ProjectModelInfo project = modelService.getById(projectModelInfo.getProjectId());
        if (!project.getProjectName().equals(projectModelInfo.getProjectName())){
            if (UserConstants.NOT_UNIQUE.equals(modelService.checkProjectNameUnique(projectModelInfo.getProjectName())))
            {
                return AjaxResult.error("修改项目模型'" + projectModelInfo.getProjectName() + "'失败，该项目模型已存在");
            }
        }
        return toAjax(modelService.updateProject(projectModelInfo));
    }

    /**
     * 删除项目模型
     * @author zhuff
     * @param projectId
     * @return
     */
    @DeleteMapping("delete")
    public AjaxResult deleteProject(Integer projectId) {
        return toAjax(modelService.deleteProject(projectId));
    }

    /**
     * 获取除管理员外所有用户
     * @author zhuff
     * @return
     */
    @GetMapping("userList")
    public AjaxResult getUserList() {
        List<Map<String ,Object>> list = modelService.getUserList();
        return AjaxResult.success(list);
    }

    /**
     * 更新jsonFile
     * @param projectModelInfo
     * @return
     */
    @PostMapping("updateJsonFile")
    public AjaxResult updateJsonFile(@RequestBody ProjectModelInfo projectModelInfo){
        return toAjax(modelService.updateJsonFile(projectModelInfo));
    }

    /**
     * 测试连接数据库
     * @param map
     * @return
     */
    @PostMapping("connectDataSource")
    public AjaxResult connectDataSource(@RequestBody Map map) {
        return modelService.connectDataSource(map);
    }

    /**
     * 执行ddl语句
     * @param map
     * @return
     */
    @PostMapping("executeSql")
    public AjaxResult executeSql(@RequestBody Map map) {
        return modelService.executeSql(map);
    }
}
