package com.salpa.web.controller.subject;

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.subject.domain.MonitorDataSource;
import com.salpa.subject.domain.MonitorOperationLog;
import com.salpa.subject.domain.MonitorProject;
import com.salpa.subject.domain.vo.MonitorDataSourceVo;
import com.salpa.subject.domain.vo.OperationLogVo;
import com.salpa.subject.service.ProjectService;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 项目管理
 *
 * @author zhuff
 */
@RestController
@RequestMapping("/subject/project")
public class ProjectController extends BaseController {

    @Autowired
    private ProjectService projectService;

    /**
     * 获取项目列表
     * @author zhuff
     * @param monitorDataSourceVo
     * @return
     */
    @PostMapping("list")
    public TableDataInfo getList(@RequestBody MonitorDataSourceVo monitorDataSourceVo) {
        List<Map<String , Object>> list = projectService.getList(monitorDataSourceVo);
        return getDataTable(list);
    }

    /**
     * 创建项目
     * @author zhuff
     * @param monitorProject
     * @return
     */
    @PostMapping("createProject")
    public AjaxResult createProject(@RequestBody MonitorProject monitorProject) {
        if (monitorProject.getProjectSyncRule().isAutoSync()){
            try {
                new CronExpression(monitorProject.getProjectSyncRule().getAutoSyncCron());
            } catch (ParseException e) {
                e.printStackTrace();
                return AjaxResult.error("新增项目模型'" + monitorProject.getProjectName() + "'失败，错误的Cron表达式");
            }
        }
        if (UserConstants.NOT_UNIQUE.equals(projectService.checkProjectNameUnique(monitorProject.getProjectName())))
        {
            return AjaxResult.error("新增项目模型'" + monitorProject.getProjectName() + "'失败，该项目模型已存在");
        }
        if (UserConstants.NOT_UNIQUE.equals(projectService.checkDatabaseNameUnique(monitorProject.getMonitorDataSource().getDatabaseName())))
        {
            return AjaxResult.error("新增数据库'" + monitorProject.getMonitorDataSource().getDatabaseName() + "'失败，该数据库已存在");
        }
        return toAjax(projectService.addProject(monitorProject));
    }

    /**
     * 修改项目
     * @author zhuff
     * @param monitorProject
     * @return
     */
    @PostMapping("updateProject")
    public AjaxResult updateProject(@RequestBody MonitorProject monitorProject) {
        if (monitorProject.getProjectSyncRule().isAutoSync()){
            try {
                new CronExpression(monitorProject.getProjectSyncRule().getAutoSyncCron());
            } catch (ParseException e) {
                e.printStackTrace();
                return AjaxResult.error("修改项目模型'" + monitorProject.getProjectName() + "'失败，错误的Cron表达式");
            }
        }
        if (!projectService.getById(monitorProject.getId()).getProjectName().equals(monitorProject.getProjectName())) {
            if (UserConstants.NOT_UNIQUE.equals(projectService.checkProjectNameUnique(monitorProject.getProjectName())))
            {
                return AjaxResult.error("修改项目模型'" + monitorProject.getProjectName() + "'失败，该项目模型已存在");
            }
        }
        if (!projectService.getDataSource(monitorProject.getId()).getDatabaseName().equals(monitorProject.getMonitorDataSource().getDatabaseName())) {
            if (UserConstants.NOT_UNIQUE.equals(projectService.checkDatabaseNameUnique(monitorProject.getMonitorDataSource().getDatabaseName())))
            {
                return AjaxResult.error("修改数据库'" + monitorProject.getMonitorDataSource().getDatabaseName() + "'失败，该数据库已存在");
            }
        }
        return toAjax(projectService.updateProject(monitorProject));
    }

    /**
     * 根据id获取项目
     * @author zhuff
     * @param id
     * @return
     */
    @GetMapping("getProjectById")
    public AjaxResult getProjectById(@RequestParam Integer id) {
        return AjaxResult.success(projectService.getProjectById(id));
    }

    /**
     * 删除项目
     * @param id
     * @return
     */
    @DeleteMapping("deleteProject")
    public AjaxResult deleteProject(Integer id){
        return toAjax(projectService.deleteProject(id));
    }


    /**
     * 测试连接
     * @param monitorProject
     * @return
     */
    @PostMapping("testConnection")
    public AjaxResult testConnection(@RequestBody MonitorProject monitorProject){
        int i = projectService.testConnection(monitorProject.getMonitorDataSource());
        if (i == 1) {
            return AjaxResult.error("加载驱动失败,请检查后重试");
        } else if (i == 2) {
            return AjaxResult.error("数据库连接失败,请检查后重试");
        } else if (i == 3) {
            return AjaxResult.success("连接数据库成功");
        } else {
            return AjaxResult.error("连接失败，请检查后重试");
        }
    }

    /**
     * 获取日志列表
     * @param operationLogVo
     * @return
     */
    @PostMapping("getLogList")
    public TableDataInfo getLogList(@RequestBody OperationLogVo operationLogVo) {
        List<MonitorOperationLog> list = projectService.getLogList(operationLogVo);
        return getDataTable(list);
    }

    @PostMapping("createDataSource")
    public AjaxResult createDataSource(@RequestBody MonitorDataSource monitorDataSource) {
        if (UserConstants.NOT_UNIQUE.equals(projectService.checkDatabaseNameUnique(monitorDataSource.getDatabaseName())))
        {
            return AjaxResult.error("新增数据库'" + monitorDataSource.getDatabaseName() + "'失败，该数据库已存在");
        }
        projectService.addDataSource(monitorDataSource);
        return AjaxResult.success();
    }

}
