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

import java.util.Date;
import java.util.List;

import com.goer.project.model.modelProject.domain.ModelProject;
import com.goer.project.model.modelProject.service.IModelProjectService;
import com.goer.project.model.modelVersion.domain.VersionScript;
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.modelVersion.domain.ModelVersion;
import com.goer.project.model.modelVersion.service.IModelVersionService;
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/modelVersion")
public class ModelVersionController extends BaseController
{
    private String prefix = "model/modelVersion";

    @Autowired
    private IModelVersionService modelVersionService;

    @Autowired
    private IModelProjectService modelProjectService;

    @RequiresPermissions("model:modelVersion:view")
    @GetMapping("")
    public String modelVersion()
    {
        return prefix + "/modelVersion";
    }

    @RequiresPermissions("model:modelVersion:view")
    @GetMapping("/versionLine/{projectId}")
    public String versionLine(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        ModelVersion modelVersion = new ModelVersion();
        modelVersion.setProjectId(projectId);
        List<ModelVersion> list = modelVersionService.selectModelVersionList(modelVersion);

        modelVersion.setId(0L);
        modelVersion.setCreateTime(new Date());
        if(list != null && list.size() > 0) {
            modelVersion.setVersionNo("最新内容");
            modelVersion.setVersionRemark("最新变化内容请点击脚本详情按钮");
        } else {
            modelVersion.setVersionNo("初版内容");
            modelVersion.setVersionRemark("未保存版本信息，详细内容请点击脚本详情按钮");
        }
        mmap.put("modelVersion", modelVersion);
        mmap.put("versionList", list);

        return prefix + "/versionLine";
    }

    /**
     * 查询模型对象版本号列表
     */
    @RequiresPermissions("model:modelVersion:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ModelVersion modelVersion)
    {
        startPage();
        List<ModelVersion> list = modelVersionService.selectModelVersionList(modelVersion);
        return getDataTable(list);
    }

    /**
     * 导出模型对象版本号列表
     */
    @RequiresPermissions("model:modelVersion:export")
    @Log(title = "模型对象版本号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ModelVersion modelVersion)
    {
        List<ModelVersion> list = modelVersionService.selectModelVersionList(modelVersion);
        ExcelUtil<ModelVersion> util = new ExcelUtil<ModelVersion>(ModelVersion.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:modelVersion:add")
    @Log(title = "模型对象版本号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ModelVersion modelVersion)
    {
        modelVersion.setSyncFlag("0");
        modelVersion.setCreateBy(getLoginName());
        modelVersion.setCreateTime(new Date());
//        return toAjax(1);
        return toAjax(modelVersionService.insertModelVersion(modelVersion));
    }

    /**
     * 修改模型对象版本号
     */
    @RequiresPermissions("model:modelVersion:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ModelVersion modelVersion = modelVersionService.selectModelVersionById(id);
        mmap.put("modelVersion", modelVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存模型对象版本号
     */
    @RequiresPermissions("model:modelVersion:edit")
    @Log(title = "模型对象版本号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ModelVersion modelVersion)
    {
        return toAjax(modelVersionService.updateModelVersion(modelVersion));
    }

    /**
     * 删除模型对象版本号
     */
    @RequiresPermissions("model:modelVersion:remove")
    @Log(title = "模型对象版本号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(modelVersionService.deleteModelVersionByIds(ids));
    }

    /**
     * 脚本信息
     */
    @GetMapping("/sqlScript")
    public String sqlScript(@RequestParam(name = "projectId", required = false, defaultValue = "0") Long projectId,
                            @RequestParam(name = "versionId", required = false, defaultValue = "0") Long versionId,
                            ModelMap mmap)
    {
        ModelVersion modelVersion = new ModelVersion();
        modelVersion.setProjectId(projectId);
        modelVersion.setId(versionId);
        modelVersion = modelVersionService.buildVersionScript(modelVersion);
        mmap.put("modelVersion", modelVersion);
        return prefix + "/sqlScript";
    }

    /**
     * 脚本信息
     */
    @GetMapping("/getSqlScript")
    @ResponseBody
    public AjaxResult getSqlScript(ModelVersion modelVersion)
    {
        modelVersion = modelVersionService.buildVersionScript(modelVersion);
//        mmap.put("modelVersion", modelVersion);
//        return prefix + "/sqlScript::fragment-sqlscript";

        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", modelVersion);
        return ajax;
    }

    /**
     * 模型对象版本同步标识
     */
    @Log(title = "模型对象版本同步标识", businessType = BusinessType.UPDATE)
    @RequiresPermissions("model:modelVersion:sync")
    @PostMapping("/setSyncFlag")
    @ResponseBody
    public AjaxResult setSyncFlag(ModelVersion modelVersion) {
        modelVersion.setUpdateBy(getLoginName());
        return toAjax(modelVersionService.updateModelVersion(modelVersion));
    }
}
