package com.younike.admin.controller.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.younike.admin.utils.ShiroUtils;
import com.younike.common.bean.APIResult;
import com.younike.common.bean.ResultGenerator;
import com.younike.common.constant.UserConstants;
import com.younike.common.core.controller.BaseController;
import com.younike.common.core.page.TableResult;
import com.younike.service.pojo.SysRole;
import com.younike.service.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色信息
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 获取角色列表
     */
    @RequiresPermissions("system:role:list")
    @GetMapping("/list")
    public TableResult list(SysRole role) {
        startPage();
        List<SysRole> roles = sysRoleService.selectRoleList(role);
        return getDataTable(roles);
    }

    /**
     * 新增角色
     */
    @PostMapping("")

    public APIResult addRole(@RequestBody Map<String, Object> map) {
        ObjectMapper objectMapper = new ObjectMapper();
        SysRole role = objectMapper.convertValue(map, SysRole.class);
        //检查唯一性
        if (!sysRoleService.checkRoleNameUnique(role).equals(UserConstants.ROLE_NAME_UNIQUE)) {
            return ResultGenerator.getFailResult("该角色名称已存在");
        }
//        if (!sysRoleService.checkRoleKeyUnique(role).equals(UserConstants.ROLE_KEY_UNIQUE)) {
//            return ResultGenerator.getFailResult("此机构中该权限字符已存在");
//        }
        role.setCreateBy(ShiroUtils.getLoginName());
        role.setUpdateBy(ShiroUtils.getLoginName());
        sysRoleService.insertRole(role);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 更新角色
     */
    @PutMapping("")

    public APIResult updateRole(@RequestBody Map<String, Object> map) {
        SysRole role = new SysRole();
        role.setRoleId(Long.parseLong(map.get("roleId").toString()));
        role.setRoleName((String) map.get("roleName"));
        role.setRoleKey((String) map.get("roleKey"));
        role.setStatus((String) map.get("status"));
        role.setRoleSort(map.get("roleSort").toString());
        List<Object> menuIdsL = (List<Object>) map.get("menuIds");
        if (menuIdsL != null) {
            role.setMenuIds(menuIdsL.stream().map(s -> Long.parseLong(s.toString())).toArray(Long[] :: new));
        }
        role.setRemark((String) map.get("remark"));
        try {
            sysRoleService.checkRoleAllowed(role);
        } catch (Exception e) {
            return ResultGenerator.getFailResult(e.getMessage());
        }
        //检查唯一性
        if (!sysRoleService.checkRoleNameUnique(role).equals(UserConstants.ROLE_NAME_UNIQUE)) {
            return ResultGenerator.getFailResult("该角色名称已存在");
        }
//        if (!sysRoleService.checkRoleKeyUnique(role).equals(UserConstants.ROLE_KEY_UNIQUE)) {
//            return ResultGenerator.getFailResult("该权限字符已存在");
//        }
        role.setUpdateBy(ShiroUtils.getLoginName());
        sysRoleService.updateRole(role);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 删除角色
     */
    @DeleteMapping(value = "/{roleId}")

    public APIResult deleteRoles(@PathVariable String roleId) {
        try {
            sysRoleService.deleteRoleByIds(roleId);
        } catch (Exception e) {
            return ResultGenerator.getFailResult(e.getMessage());
        }
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 更改状态
     */
    @PutMapping("/changeStatus")
    public APIResult changeStatus(@RequestBody Map<String, Object> data) {
        Long roleId = Long.parseLong(String.valueOf(data.get("roleId")));
        String status = String.valueOf(data.get("status"));

        SysRole role = new SysRole();
        role.setRoleId(roleId);
        role.setStatus(status);
        try {
            sysRoleService.checkRoleAllowed(role);
        } catch (Exception e) {
            return ResultGenerator.getFailResult(e.getMessage());
        }
        role.setUpdateBy(ShiroUtils.getLoginName());
        sysRoleService.changeStatus(role);
        return ResultGenerator.getSuccessResult();
    }

    /**
     * 根据id查找角色
     */
    @GetMapping(value = "/{roleId}")
    public SysRole getRole(@PathVariable Long roleId) {
        return sysRoleService.selectRoleById(roleId);
    }


}
