package com.goer.project.model.dataType.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 lombok.SneakyThrows;
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.dataType.domain.DataType;
import com.goer.project.model.dataType.service.IDataTypeService;
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/dataType")
public class DataTypeController extends BaseController
{
    private String prefix = "model/dataType";

    @Autowired
    private IDataTypeService dataTypeService;

    @Autowired
    private IModelProjectService modelProjectService;

    @RequiresPermissions("model:dataType:view")
    @GetMapping("")
    public String dataType(@RequestParam(name = "projectId", required = false, defaultValue = "0") Long projectId,
                           @RequestParam(name = "versionId", required = false, defaultValue = "0") Long versionId,
                           ModelMap mmap)
    {
        ModelProject modelProject = modelProjectService.selectModelProjectById(projectId);
        if(modelProject == null) {
            modelProject = new ModelProject();
            modelProject.setId(projectId);
            modelProject.setVersionId(0L);
            modelProject.setModelName("GOER");
            modelProject.setModelRemark("系统预置数据类型");
        }
        mmap.put("modelProject", modelProject);
        return prefix + "/dataType";
    }

    /**
     * 查询数据类型列表
     */
    @RequiresPermissions("model:dataType:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DataType dataType)
    {
        startPage();
        List<DataType> list = dataTypeService.selectDataTypeList(dataType);
        return getDataTable(list);
    }

    /**
     * 导出数据类型列表
     */
    @RequiresPermissions("model:dataType:export")
    @Log(title = "数据类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DataType dataType)
    {
        List<DataType> list = dataTypeService.selectDataTypeList(dataType);
        ExcelUtil<DataType> util = new ExcelUtil<DataType>(DataType.class);
        return util.exportExcel(list, "数据类型数据");
    }

    /**
     * 新增数据类型
     */
    @GetMapping("/add/{projectId}")
    public String add(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        ModelProject modelProject = modelProjectService.selectModelProjectById(projectId);
        if(modelProject == null) {
            modelProject = new ModelProject();
            modelProject.setId(projectId);
            modelProject.setModelName("GOER");
            modelProject.setModelRemark("系统预置数据类型");
        }
        modelProject.setVersionId(0L);

        mmap.put("modelProject", modelProject);
        return prefix + "/add";
    }

    /**
     * 新增保存数据类型
     */
    @RequiresPermissions("model:dataType:add")
    @Log(title = "数据类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DataType dataType)
    {
        dataType.setVersionId(0L);
        dataType.setCreateBy(getLoginName());
        return toAjax(dataTypeService.insertDataType(dataType));
    }

    /**
     * 修改数据类型
     */
    @RequiresPermissions("model:dataType:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DataType dataType = dataTypeService.selectDataTypeById(id);
        mmap.put("dataType", dataType);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据类型
     */
    @RequiresPermissions("model:dataType:edit")
    @Log(title = "数据类型", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DataType dataType)
    {
        dataType.setUpdateBy(getLoginName());
        return toAjax(dataTypeService.updateDataType(dataType));
    }

    /**
     * 删除数据类型
     */
    @RequiresPermissions("model:dataType:remove")
    @Log(title = "数据类型", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dataTypeService.deleteDataTypeByIds(ids));
    }

    /**
     * 排序
     */
    @RequiresPermissions("model:dataType:edit")
    @Log(title = "数据类型", businessType = BusinessType.UPDATE)
    @PostMapping("/sort")
    @ResponseBody
    @SneakyThrows
    public AjaxResult sort(DataType dataType)
    {
        dataType.setUpdateBy(getLoginName());
        dataType.setUpdateTime(new Date());
        return toAjax(dataTypeService.sort(dataType));
    }
}
