package com.mx.cneeds.server.user.web;

import com.mx.cneeds.common.dto.DepartmentDto;
import com.mx.cneeds.common.dto.RoleDto;
import com.mx.cneeds.common.pager.PageUtils;
import com.mx.cneeds.common.result.R;
import com.mx.cneeds.common.validator.ValidatorUtils;
import com.mx.cneeds.server.entity.SysDeptEntity;
import com.mx.cneeds.server.entity.SysRoleEntity;
import com.mx.cneeds.server.user.service.SysDeptService;
import com.mx.cneeds.server.user.service.SysRoleDeptService;
import com.mx.cneeds.server.user.service.SysRoleFuncService;
import com.mx.cneeds.server.user.service.SysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//import org.apache.shiro.authz.annotation.RequiresPermissions;


/**
 * 角色表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-01-09 15:09:01
 */
@RestController
@RequestMapping("sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysRoleFuncService sysRoleFuncService;

    @Autowired
    private SysRoleDeptService sysRoleDeptService;
    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:sysrole:list")
    public PageUtils list(@RequestParam Map<String, Object> params){
        PageUtils page = sysRoleService.queryPage(params);

        Set<Long> set = new HashSet<>();

        page.getList().forEach(e -> {
            SysRoleEntity entity = (SysRoleEntity) e;
            set.add(entity.getDeptId());
        });

        List<SysDeptEntity> sysDeptEntityList = sysDeptService.queryDeptIdsBydeptName(set);

        List<RoleDto> roleDtos = new ArrayList<>();
        page.getList().forEach(e -> {
            SysRoleEntity entity = (SysRoleEntity) e;
            for(SysDeptEntity deptEntity :sysDeptEntityList) {
                if (deptEntity.getDeptId().equals(entity.getDeptId())){
                    RoleDto roleDto = new RoleDto();
                    BeanUtils.copyProperties(entity,roleDto);
                    roleDto.setDeptName(deptEntity.getName());
                    roleDtos.add(roleDto);
                }
            }
        });
        page.setList(roleDtos);
        return page;
    }

    @RequestMapping("/listAll")
    public List<RoleDto> listAll(@RequestParam Map<String, Object> params){
        List<SysRoleEntity> sysRoleEntityList = sysRoleService.queryList(params);
        List<RoleDto> roleDtoList = new ArrayList<>();
        sysRoleEntityList.forEach(sysRoleEntity -> {
            RoleDto roleDto = new RoleDto();
            BeanUtils.copyProperties(sysRoleEntity,roleDto);
            roleDtoList.add(roleDto);
        });
        return roleDtoList;
    }


    /**
     * 信息
     */
    @RequestMapping("/info")
    public RoleDto info(Long roleId){
        SysRoleEntity role = sysRoleService.getById(roleId);

        RoleDto roleDto = new RoleDto();

        BeanUtils.copyProperties(role,roleDto);


        //查询角色对应的菜单
        List<Long> funcIdList = sysRoleFuncService.queryFuncId(roleId);
        roleDto.setFuncIdList(funcIdList);

        //查询角色对应的部门
        List<Long> deptIdList = sysRoleDeptService.queryDeptId(roleId);
        roleDto.setDeptIdList(deptIdList);

        return roleDto;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody RoleDto dto){
        SysRoleEntity sysRole = new SysRoleEntity();
        BeanUtils.copyProperties(dto,sysRole);
        sysRoleService.saveRole(sysRole,dto.getFuncIdList(),dto.getDeptIdList());

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody RoleDto dto){
        ValidatorUtils.validateEntity(dto);
        SysRoleEntity sysRole = new SysRoleEntity();
        BeanUtils.copyProperties(dto,sysRole);
        sysRoleService.update(sysRole, dto.getFuncIdList(), dto.getDeptIdList());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] roleIds){
        sysRoleService.deleteBatch(roleIds);

        return R.ok();
    }

}
