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

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

import com.goer.project.model.dataType.domain.DataType;
import com.goer.project.model.dataType.service.IDataTypeService;
import com.goer.project.model.modelEntity.domain.ModelEntity;
import com.goer.project.model.modelEntity.service.IModelEntityService;
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.modelField.domain.ModelField;
import com.goer.project.model.modelField.service.IModelFieldService;
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/modelField")
public class ModelFieldController extends BaseController
{
    private String prefix = "model/modelField";

    @Autowired
    private IModelProjectService modelProjectService;

    @Autowired
    private IModelEntityService modelEntityService;

    @Autowired
    private IModelFieldService modelFieldService;

    @Autowired
    private IDataTypeService dataTypeService;

    @RequiresPermissions("model:modelField:view")
    @GetMapping("")
    public String modelField(@RequestParam(name = "projectId", required = false, defaultValue = "0") Long projectId,
                             @RequestParam(name = "entityId", required = false, defaultValue = "0") Long entityId,
                             @RequestParam(name = "versionId", required = false, defaultValue = "0") Long versionId,
                             ModelMap mmap)
    {
        ModelProject modelProject = null;
        if(projectId != null && projectId != 0L) {
            modelProject = modelProjectService.selectModelProjectById(projectId);
        } else {
            modelProject = new ModelProject();
            modelProject.setId(projectId);
            modelProject.setVersionId(0L);
            modelProject.setModelName("GOER");
            modelProject.setModelRemark("系统预置数据字段");
        }

        mmap.put("modelProject", modelProject);

        ModelEntity modelEntity = null;
        if(entityId != null && entityId != 0L) {
            modelEntity = modelEntityService.selectModelEntityById(entityId);
        } else {
            modelEntity = new ModelEntity();
            modelEntity.setId(0L);
            modelEntity.setVersionId(0L);
        }
        mmap.put("modelEntity", modelEntity);

        DataType dataType = new DataType();
        dataType.setProjectId(modelProject.getId());
        dataType.setVersionId(0L);
        List<DataType> dataTypeList = dataTypeService.selectDataTypeList(dataType);
        mmap.put("dataTypeList", dataTypeList);

        return prefix + "/modelField";
    }

    /**
     * 查询数据字段列表
     */
    @RequiresPermissions("model:modelField:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ModelField modelField)
    {
        startPage();
        List<ModelField> list = modelFieldService.selectModelFieldList(modelField);
        return getDataTable(list);
    }

    /**
     * 导出数据字段列表
     */
    @RequiresPermissions("model:modelField:export")
    @Log(title = "数据字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ModelField modelField)
    {
        List<ModelField> list = modelFieldService.selectModelFieldList(modelField);
        ExcelUtil<ModelField> util = new ExcelUtil<ModelField>(ModelField.class);
        return util.exportExcel(list, "数据字段数据");
    }

    /**
     * 新增数据字段
     */
    @GetMapping("/add")
    public String add(@RequestParam(name = "projectId", required = false, defaultValue = "0") Long projectId,
                      @RequestParam(name = "entityId", required = false, defaultValue = "0") Long entityId,
                      @RequestParam(name = "versionId", required = false, defaultValue = "0") Long versionId,
                      ModelMap mmap)
    {
        DataType dataType = new DataType();
        dataType.setProjectId(projectId);
        dataType.setVersionId(0L);
        List<DataType> dataTypeList = dataTypeService.selectDataTypeList(dataType);
        mmap.put("dataTypeList", dataTypeList);

        mmap.put("projectId", projectId);
        mmap.put("entityId", entityId);
        mmap.put("versionId", versionId);
        return prefix + "/add";
    }

    /**
     * 新增保存数据字段
     */
    @RequiresPermissions("model:modelField:add")
    @Log(title = "数据字段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ModelField modelField)
    {
        modelField.setCreateBy(getLoginName());
        return toAjax(modelFieldService.insertModelField(modelField));
    }

    /**
     * 修改数据字段
     */
    @RequiresPermissions("model:modelField:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {

        ModelField modelField = modelFieldService.selectModelFieldById(id);
        DataType dataType = new DataType();
        dataType.setProjectId(modelField.getProjectId());
        dataType.setVersionId(0L);
        List<DataType> dataTypeList = dataTypeService.selectDataTypeList(dataType);
        mmap.put("dataTypeList", dataTypeList);
        mmap.put("modelField", modelField);
        return prefix + "/edit";
    }

    /**
     * 修改保存数据字段
     */
    @RequiresPermissions("model:modelField:edit")
    @Log(title = "数据字段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ModelField modelField)
    {
        modelField.setUpdateBy(getLoginName());
        return toAjax(modelFieldService.updateModelField(modelField));
    }

    /**
     * 删除数据字段
     */
    @RequiresPermissions("model:modelField:remove")
    @Log(title = "数据字段", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(modelFieldService.deleteModelFieldByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "数据字段", businessType = BusinessType.UPDATE)
    @RequiresPermissions("model:modelField:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(ModelField modelField)
    {
        modelField.setUpdateBy(getLoginName());
        return toAjax(modelFieldService.updateModelField(modelField));
    }

    /**
     * 排序
     */
    @RequiresPermissions("model:modelField:edit")
    @Log(title = "数据字段", businessType = BusinessType.UPDATE)
    @PostMapping("/sort")
    @ResponseBody
    @SneakyThrows
    public AjaxResult sort(ModelField modelField)
    {
        modelField.setUpdateBy(getLoginName());
        modelField.setUpdateTime(new Date());
        return toAjax(modelFieldService.sort(modelField));
    }

    /**
     * 校验表字段名唯一性
     */
    @PostMapping("/checkFieldCodeUnique")
    @ResponseBody
    public String checkFieldCodeUnique(ModelField modelField)
    {
        return modelFieldService.checkFieldCodeUnique(modelField);
    }
}
