package com.dxhy.admin.sys.controller;

import com.dxhy.admin.sys.config.SysConfig;
import com.dxhy.admin.sys.entity.SysRoleEntity;
import com.dxhy.admin.sys.service.SysRoleMenuService;
import com.dxhy.admin.sys.service.SysRoleService;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.Constant;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.utils.ValidatorUtils;
import com.dxhy.common.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author jiaohongyang
 */
@RestController
@RequestMapping("/sys/role")
@Slf4j
public class SysRoleController extends AbstractController {
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 角色列表
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        UserInfo userInfo = getUserInfo();
        if(Long.parseLong(userInfo.getId()) == Constant.SUPER_ADMIN){
            //如果是超管，可以查出所有的角色
            params.put("company", "");
        }
        if (Long.parseLong(userInfo.getId()) != Constant.SUPER_ADMIN
            && !userInfo.getCompany().contains(SysConfig.ZXQY)) {
            params.put("company", userInfo.getCompany());
        }
        PageUtils page = sysRoleService.queryPage(params);
        return ResponseEntity.ok(R.ok().put("page", page));
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
    public ResponseEntity<?> select(String company) {
        List<SysRoleEntity> list;
        list = sysRoleService.selectCompany(company);
        return ResponseEntity.ok(R.ok().put("list", list));
    }

    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
    public ResponseEntity<?> info(@PathVariable("roleId") Long roleId) {
        SysRoleEntity role = sysRoleService.queryRoleById(roleId);

        // 查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        if (menuIdList != null && menuIdList.size() > 0) {
            role.setMenuIdList(menuIdList);
        }

        return ResponseEntity.ok(R.ok().put("role", role));
    }

    /**
     * 保存角色
     */
    @RequestMapping("/save")
    public ResponseEntity<?> save(@RequestBody SysRoleEntity role) {
        try {

            ValidatorUtils.validateEntity(role);

            sysRoleService.saveRole(role);

            return ResponseEntity.ok(R.ok("保存成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 修改角色
     */
    @RequestMapping("/update")
    public ResponseEntity<?> update(@RequestBody SysRoleEntity role) {
        try {
            ValidatorUtils.validateEntity(role);

            sysRoleService.update(role);

            return ResponseEntity.ok(R.ok("保存成功"));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("保存失败"));

        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("/delete")
    public ResponseEntity<?> delete(Long roleId) {
        try {
            sysRoleService.deleteBatch(roleId);
            return ResponseEntity.ok(R.ok("删除成功"));

        } catch (Exception e) {
            return ResponseEntity.ok(R.ok("删除失败"));

        }
    }
}
