package com.qryord.project.master.field.controller;

import com.qryord.common.enums.EDelete;
import com.qryord.common.utils.DateUtils;
import com.qryord.common.utils.poi.ExcelUtil;
import com.qryord.common.utils.security.ShiroUtils;
import com.qryord.framework.aspectj.lang.annotation.Log;
import com.qryord.framework.aspectj.lang.enums.BusinessType;
import com.qryord.framework.web.controller.BaseController;
import com.qryord.framework.web.domain.AjaxResult;
import com.qryord.framework.web.page.TableDataInfo;
import com.qryord.project.master.field.domain.MField;
import com.qryord.project.master.field.service.IMFieldService;
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 java.util.Date;
import java.util.List;

/**
 * 字段Controller
 * 
 * @author ruoyi
 * @date 2022-08-06
 */
@Controller
@RequestMapping("/master/field")
public class MFieldController extends BaseController
{
    private String prefix = "master/field";

    @Autowired
    private IMFieldService mFieldService;

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

    /**
     * 查询字段列表
     */
    @RequiresPermissions("master:field:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MField mField)
    {
        mField.setDelFlag(EDelete.FALSE.getVal());
        startPage();
        List<MField> list = mFieldService.selectMFieldList(mField);
        return getDataTable(list);
    }

    /**
     * 导出字段列表
     */
    @RequiresPermissions("master:field:export")
    @Log(title = "字段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MField mField)
    {
        List<MField> list = mFieldService.selectMFieldList(mField);
        ExcelUtil<MField> util = new ExcelUtil<MField>(MField.class);
        return util.exportExcel(list, "字段数据");
    }

    /**
     * 新增字段
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存字段
     */
    @RequiresPermissions("master:field:add")
    @Log(title = "字段", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MField mField)
    {
        Date nowDate = DateUtils.getNowDate();
        mField.setDelFlag(EDelete.FALSE.getVal());
        mField.setCreateBy(ShiroUtils.getLoginName());
        mField.setCreateTime(nowDate);
        mField.setUpdateBy(ShiroUtils.getLoginName());
        mField.setUpdateTime(nowDate);
        return toAjax(mFieldService.insertMField(mField));
    }

    /**
     * 修改字段
     */
    @RequiresPermissions("master:field:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MField mField = mFieldService.selectMFieldById(id);
        mmap.put("mField", mField);
        return prefix + "/edit";
    }

    /**
     * 修改保存字段
     */
    @RequiresPermissions("master:field:edit")
    @Log(title = "字段", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MField mField)
    {
        mField.setUpdateBy(ShiroUtils.getLoginName());
        mField.setUpdateTime(DateUtils.getNowDate());
        return toAjax(mFieldService.updateMField(mField));
    }

    /**
     * 删除字段
     */
    @RequiresPermissions("master:field:remove")
    @Log(title = "字段", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mFieldService.deleteMFieldByIds(ids));
    }


    @RequiresPermissions("master:field:edit")
    @Log(title = "字段拖动排序", businessType = BusinessType.UPDATE)
    @PostMapping("/sorted")
    @ResponseBody
    public AjaxResult saveSorted(MField mField){
        return toAjax(mFieldService.dragSorted(mField));
    }

}
