package cn.ezhizao.project.system.controller;


import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;

import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.system.domain.*;

import cn.ezhizao.project.system.service.ISysRoleInItService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 角色信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/inItRole")
public class SysInItRoleController extends BaseController
{
    @Resource
    private ISysRoleInItService sysRoleInitService;

    /**
     * 查询roleInit列表
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRoleInIt sysRoleInit) throws NoSuchFieldException, IllegalAccessException
    {
        startPage();
        List<SysRoleInIt> list = sysRoleInitService.getList(sysRoleInit);
        return getDataTable(list);
    }

    /**
     * 导出roleInit列表
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:export')")
    @Log(title = "roleInit", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleInIt sysRoleInit) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(sysRoleInit);
        List<SysRoleInIt> list = sysRoleInitService.getList(sysRoleInit);
        ExcelUtil<SysRoleInIt> util = new ExcelUtil<SysRoleInIt>(SysRoleInIt.class);
        util.exportExcel(response, list, "roleInit数据");
    }

    /**
     * 获取roleInit详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysRoleInitService.getById(id));
    }

    /**
     * 新增roleInit
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:add')")
    @Log(title = "roleInit", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRoleInIt sysRoleInit) throws NoSuchFieldException, IllegalAccessException
    {
        //判断角色是否存在
        List<SysRoleInIt> list = sysRoleInitService.getList(sysRoleInit);
        if(list.size()>0){
            return error("新增角色'" + sysRoleInit.getRoleName() + "'失败，角色名称已存在");
        }
        return toAjax(sysRoleInitService.insertRoleInIt(sysRoleInit));
    }

    /**
     * 修改roleInit
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:edit')")
    @Log(title = "roleInit", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRoleInIt sysRoleInit) throws NoSuchFieldException, IllegalAccessException
    {
        //判断角色是否存在
        List<SysRoleInIt> list = sysRoleInitService.getList(sysRoleInit);
        if(list.size()==1&&list.get(0).getId().equals(sysRoleInit.getId())){
            return toAjax(sysRoleInitService.updateRoleInIt(sysRoleInit));
        }
        return error("修改角色'" + sysRoleInit.getRoleName() + "'失败，角色名称已存在");
    }

    /**
     * 删除roleInit
     */
    @PreAuthorize("@ss.hasPermi('business:inItRole:remove')")
    @Log(title = "roleInit", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(sysRoleInitService.deletedRoleInIt(ids));
    }
}
