package com.goer.project.model.modelDbs.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.goer.common.command.AbstractDBCommand;
import com.goer.common.command.ExecResult;
import com.goer.common.command.impl.DBReverseGetAllTablesListImpl;
import com.goer.common.command.impl.PingLoadDriverClassImpl;
import com.goer.project.model.modelProject.domain.ModelProject;
import com.goer.project.model.modelProject.service.IModelProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.goer.framework.aspectj.lang.annotation.Log;
import com.goer.framework.aspectj.lang.enums.BusinessType;
import com.goer.project.model.modelDbs.domain.ModelDbs;
import com.goer.project.model.modelDbs.service.IModelDbsService;
import com.goer.framework.web.controller.BaseController;
import com.goer.framework.web.domain.AjaxResult;
import com.goer.common.utils.poi.ExcelUtil;
import com.goer.framework.web.page.TableDataInfo;

/**
 * 模型对象数据库配置Controller
 * 
 * @author goer
 * @date 2022-01-17
 */
@Controller
@RequestMapping("/model/modelDbs")
public class ModelDbsController extends BaseController
{
    private String prefix = "model/modelDbs";

    @Autowired
    private IModelProjectService modelProjectService;

    @Autowired
    private IModelDbsService modelDbsService;

    @RequiresPermissions("model:modelDbs:view")
    @GetMapping()
    public String modelDbs(@RequestParam(name = "projectId", required = false, defaultValue = "0") Long projectId,
                           ModelMap mmap)
    {
        ModelProject modelProject = modelProjectService.selectModelProjectById(projectId);
        mmap.put("modelProject", modelProject);
        return prefix + "/modelDbs";
    }

    /**
     * 查询模型对象数据库配置列表
     */
    @RequiresPermissions("model:modelDbs:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ModelDbs modelDbs)
    {
        startPage();
        List<ModelDbs> list = modelDbsService.selectModelDbsList(modelDbs);
        return getDataTable(list);
    }

    /**
     * 导出模型对象数据库配置列表
     */
    @RequiresPermissions("model:modelDbs:export")
    @Log(title = "模型对象数据库配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ModelDbs modelDbs)
    {
        List<ModelDbs> list = modelDbsService.selectModelDbsList(modelDbs);
        ExcelUtil<ModelDbs> util = new ExcelUtil<ModelDbs>(ModelDbs.class);
        return util.exportExcel(list, "模型对象数据库配置数据");
    }

    /**
     * 新增模型对象数据库配置
     */
    @GetMapping("/add/{projectId}")
    public String add(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        ModelProject modelProject = modelProjectService.selectModelProjectById(projectId);
        mmap.put("modelProject", modelProject);
        return prefix + "/add";
    }

    /**
     * 新增保存模型对象数据库配置
     */
    @RequiresPermissions("model:modelDbs:add")
    @Log(title = "模型对象数据库配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ModelDbs modelDbs)
    {
        modelDbs.setDefaultDb("N");
        modelDbs.setCreateBy(getLoginName());
        return toAjax(modelDbsService.insertModelDbs(modelDbs));
    }

    /**
     * 修改模型对象数据库配置
     */
    @RequiresPermissions("model:modelDbs:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ModelDbs modelDbs = modelDbsService.selectModelDbsById(id);
        mmap.put("modelDbs", modelDbs);
        return prefix + "/edit";
    }

    /**
     * 修改保存模型对象数据库配置
     */
    @RequiresPermissions("model:modelDbs:edit")
    @Log(title = "模型对象数据库配置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ModelDbs modelDbs)
    {
        modelDbs.setUpdateBy(getLoginName());
        return toAjax(modelDbsService.updateModelDbs(modelDbs));
    }

    /**
     * 删除模型对象数据库配置
     */
    @RequiresPermissions("model:modelDbs:remove")
    @Log(title = "模型对象数据库配置", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(modelDbsService.deleteModelDbsByIds(ids));
    }

    /**
     * 模型对象数据库配置默认修改
     */
    @Log(title = "模型对象数据库配置", businessType = BusinessType.UPDATE)
    @RequiresPermissions("model:modelDbs:edit")
    @PostMapping("/setDefaultDb")
    @ResponseBody
    public AjaxResult setDefaultDb(ModelDbs modelDbs)
    {
        modelDbs.setUpdateBy(getLoginName());
        return toAjax(modelDbsService.setDefaultDb(modelDbs));
    }

    /**
     * 测试
     */
    @Log(title = "模型对象数据库测试", businessType = BusinessType.UPDATE)
    @RequiresPermissions("model:modelDbs:edit")
    @PostMapping("/pingLoadDriver")
    @ResponseBody
    public AjaxResult pingLoadDriver(ModelDbs modelDbs)
    {
        if(modelDbs.getId() != null && modelDbs.getId() != 0L) {
            modelDbs = modelDbsService.selectModelDbsById(modelDbs.getId());
        }
        ExecResult ret = new ExecResult(ExecResult.FAILED,"未知错误");
        PingLoadDriverClassImpl ping = new PingLoadDriverClassImpl();
        Map<String, String> argsMap = new HashMap<String, String>();
        argsMap.put(AbstractDBCommand.KEY_DRIVER_CLASS_NAME,modelDbs.getDriverClassName());
        argsMap.put(AbstractDBCommand.KEY_URL,modelDbs.getUrl());
        argsMap.put(AbstractDBCommand.KEY_USERNAME,modelDbs.getUserName());
        argsMap.put(AbstractDBCommand.KEY_PASSWORD,modelDbs.getPassword());
        ret = ping.exec(argsMap);

        modelDbs.setUpdateBy(getLoginName());
        if(ExecResult.SUCCESS.equals(ret.getStatus())) {
            return AjaxResult.success(ret.getBody().toString());
        } else {
            return AjaxResult.error(ret.getBody().toString());
        }
    }
}
