package com.dgtis.dorm.manager.admin.web;


import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.Role;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.dgtis.dorm.manager.db.service.RoleService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/role")
public class RoleController {
    private final Log logger = LogFactory.getLog(RoleController.class);


    @Autowired
    private RoleService roleService;



    /**
     * 根据角色名称查询角色
     *
     * @param roleName
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/selectByRoleName")
    public Object selectByRoleName(@LoginAdmin Integer adminId, String roleName, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        Map<String,Object> map = null;

        try {
            map = roleService.selectByRoleName(roleName, page, limit);
        } catch (Exception e){
            e.printStackTrace();
            ResponseUtil.fail();
        }

        return ResponseUtil.ok(map);

    }

    /**
     * 添加角色
     *
     * @param role
     * @return
     */
    @PostMapping("/insertSelective")
    public Object insertSelective(@LoginAdmin Integer adminId, Role role){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        Integer res = null;
        try {
            role.setCreateTime(LocalDateTime.now());
            role.setCreateUserId(adminId+"");
            role.setModifyTime(LocalDateTime.now());
            role.setModifyUserId(adminId+"");
            res = roleService.insertSelective(role);
        } catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok(res);
    }

    /**
     * 跟新角色
     *
     * @param role
     * @return
     */
    @PostMapping("/updateByPrimaryKeySelective")
    public Object updateByPrimaryKeySelective(@LoginAdmin Integer adminId,Role role){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        Integer res = null;
        try {
            role.setModifyTime(LocalDateTime.now());
            role.setModifyUserId(adminId+"");
            res = roleService.updateByPrimaryKeySelective(role);
        } catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok(res);
    }

    /**
     * 添加角色和菜单关联
     * @param roleId
     * @param menuList
     */
    @PostMapping("/insertRoleMenu")
    public Object insertRoleMenu(@LoginAdmin Integer adminId,String roleId,String menuList){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        try {
            roleService.insertRoleMenu(Integer.parseInt(roleId),Arrays.asList(menuList.split(",")));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();

    }


    /**
     *
     * 根据角色id查询菜单
     *
     * @param roleId
     * @return
     */
    @PostMapping("/selectMenuByRoleId")
    public Object selectMenuByRoleId(@LoginAdmin Integer adminId,Integer roleId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        List<String> list = roleService.selectMenuByRoleId(roleId);
        return ResponseUtil.ok(list);
    }



    /**
     * 添加admin用户和角色关联
     * @param adminId
     * @param roleList
     */
    @PostMapping("/insertAdminRole")
    public Object insertAdminRole(@LoginAdmin Integer adminId,String userId,String roleList){

        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        try {
            roleService.insertAdminRole(Integer.parseInt(userId),Arrays.asList(roleList.split(",")));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseUtil.fail();
        }

        return ResponseUtil.ok();
    }

    /**
     *
     * 查询当前用户所拥有的菜单
     *
     * @param adminId
     */
    @PostMapping("selectAdminMenu")
    public Object selectAdminMenu(@LoginAdmin Integer adminId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        List<String> list = null;
        try {
            list = roleService.selectAdminUserMenuByAdminId(adminId);
        } catch (Exception e){
            e.printStackTrace();
            ResponseUtil.fail();
        }

        return ResponseUtil.ok(list);
    }


    /**
     * 查询用户拥有的角色
     * @param adminId
     * @return
     */
    @PostMapping("selectAdminRole")
    public Object selectAdminRole(@LoginAdmin Integer adminId,String userId){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        List<String> list =null;
        try {
            list = roleService.selectAdminUserRoleByAdminId(Integer.parseInt(userId));
        } catch (Exception e){
            e.printStackTrace();
            ResponseUtil.fail();
        }


        return ResponseUtil.ok(list);
    }

}
