package com.app.admin.framework.system.permission.role.controller;

import com.app.admin.framework.system.permission.role.model.SysRole;
import com.app.admin.framework.system.permission.role.service.SysRoleService;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.controller.CrudBaseControllerSupport;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.grid.Paging;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色管理
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController extends CrudBaseControllerSupport<SysRole> {

    @Autowired
    SysRoleService sysRoleService;

    /**
     * 查询列表的具体实现
     *
     * @param request HttpServletRequest
     * @param view    查询条件
     * @param paging  分页条件
     * @return 查询结果
     */
    @Override
    public List<SysRole> list(HttpServletRequest request, SysRole view, Paging paging) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        view.setOrganizationId(loginUser.getOrganizationId());
        return sysRoleService.getPageList(view, paging);
    }

    /**
     * 保存记录的具体实现
     *
     * @param request HttpServletRequest
     * @param entity  实体
     * @return View对象
     */
    @Override
    public AjaxResult saveEntity(HttpServletRequest request, SysRole entity) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        entity.setOrganizationId(loginUser.getOrganizationId());
        entity.setCreateUserId(loginUser.getId());
        boolean resultCount = sysRoleService.saveSysRole(entity);
        return AjaxResult.wrap(resultCount);
    }

    /**
     * 删除记录,根据Id 具体实现
     *
     * @param request HttpServletRequest
     * @param oids    实体
     */
    @Override
    public AjaxResult batchDelete(HttpServletRequest request, Long oids[]) {
        boolean b = sysRoleService.removeByIds(Arrays.stream(oids).collect(Collectors.toList()));
        return AjaxResult.wrap(b);
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request, SysRole entity) {
        if (entity.getOid() != null) {
            entity = sysRoleService.getById(entity.getOid());
        }
        return new ModelAndView("admin/framework/system/role/edit.html", "entity", entity);
    }

    @RequestMapping(value = "/updateStatus")
    public @ResponseBody AjaxResult updateStatus(Long oid, Integer state) {
        SysRole record = new SysRole();
        record.setOid(oid);
        record.setState(state);
        boolean resultCount = sysRoleService.updateById(record);
        return AjaxResult.wrap(resultCount);
    }
}
