package com.qryord.project.master.roleField.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.roleField.domain.RoleField;
import com.qryord.project.master.roleField.service.IRoleFieldService;
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/roleField")
public class RoleFieldController extends BaseController
{
    private String prefix = "master/roleField";

    @Autowired
    private IRoleFieldService roleFieldService;

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

    /**
     * 查询角色字段控制列表
     */
    @RequiresPermissions("master:roleField:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RoleField roleField)
    {
        roleField.setDelFlag(EDelete.FALSE.getVal());
        startPage();
        List<RoleField> list = roleFieldService.selectRoleList(roleField);
        return getDataTable(list);
    }

    /**
     * 查询角色字段控制列表
     */
    @RequiresPermissions("master:roleField:list")
    @PostMapping("/listDetail")
    @ResponseBody
    public TableDataInfo listDetail(RoleField roleField)
    {
        roleField.setDelFlag(EDelete.FALSE.getVal());
        startPage();
        List<RoleField> list = roleFieldService.selectFieldList(roleField);
        for (RoleField rf : list) {
            rf.setRoleKey(roleField.getRoleKey());
        }
        return getDataTable(list);
    }

    /**
     * 导出角色字段控制列表
     */
    @RequiresPermissions("master:roleField:export")
    @Log(title = "角色字段控制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RoleField roleField)
    {
        List<RoleField> list = roleFieldService.selectRoleFieldList(roleField);
        ExcelUtil<RoleField> util = new ExcelUtil<RoleField>(RoleField.class);
        return util.exportExcel(list, "角色字段控制数据");
    }

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

    /**
     * 新增保存角色字段控制
     */
    @RequiresPermissions("master:roleField:add")
    @Log(title = "角色字段控制", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RoleField roleField)
    {
        Date nowDate = DateUtils.getNowDate();
        roleField.setDelFlag(EDelete.FALSE.getVal());
        roleField.setCreateBy(ShiroUtils.getLoginName());
        roleField.setCreateTime(nowDate);
        roleField.setUpdateBy(ShiroUtils.getLoginName());
        roleField.setUpdateTime(nowDate);
        return toAjax(roleFieldService.insertRoleField(roleField));
    }

    /**
     * 修改角色字段控制
     */
    @RequiresPermissions("master:roleField:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RoleField roleField = roleFieldService.selectRoleFieldById(id);
        mmap.put("roleField", roleField);
        return prefix + "/edit";
    }

    /**
     * 修改保存角色字段控制
     */
    @RequiresPermissions("master:roleField:edit")
    @Log(title = "角色字段控制", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RoleField roleField)
    {
        roleField.setUpdateBy(ShiroUtils.getLoginName());
        roleField.setUpdateTime(DateUtils.getNowDate());
        return toAjax(roleFieldService.updateRoleField(roleField));
    }

    /**
     * 删除角色字段控制
     */
    @RequiresPermissions("master:roleField:remove")
    @Log(title = "角色字段控制", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(roleFieldService.deleteRoleFieldByIds(ids));
    }

    @Log(title = "角色字段控制;", businessType = BusinessType.UPDATE)
    @RequiresPermissions("master:roleField:edit")
    @PostMapping("/changeShowFlag")
    @ResponseBody
    public AjaxResult changeShowFlag(RoleField roleField) {

        roleField.setUpdateBy(ShiroUtils.getLoginName());
        roleField.setUpdateTime(DateUtils.getNowDate());
        return toAjax(roleFieldService.changeShowFlag(roleField));
    }
}
