package com.ankaibei.workFlow.webadmin.api;

import com.ankaibei.workFlow.webadmin.upms.model.SysMenu;
import com.ankaibei.workFlow.webadmin.upms.model.constant.SysUserType;
import com.ankaibei.workFlow.webadmin.upms.service.SysMenuService;
import com.ankaibei.workFlow.webadmin.upms.service.SysPermCodeService;
import com.ankaibei.workFlow.webadmin.upms.service.SysUserService;
import com.ankaibei.workflow.api.SysUserApi;
import com.ankaibei.workflow.vo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/foreign/admin/upms/user")
public class SysUserApiService implements SysUserApi {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysPermCodeService sysPermCodeService;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    @RequestMapping("/getUserInfoByUserName")
    public SysUser getUserInfoByUserName(String userName) {
        com.ankaibei.workFlow.webadmin.upms.model.SysUser user =
                sysUserService.getSysUserByLoginName(userName);
        if(ObjectUtils.isEmpty(user)){
            return  null;
        }
        boolean isAdmin = user.getUserType() == SysUserType.TYPE_ADMIN;

        List<String> allMenuList;
        List<String> permCodeList;
        if (isAdmin) {
            allMenuList = sysMenuService.getAllListByOrder("showOrder")
                    .stream()
                    .map(SysMenu::getMenuName)
                    .collect(Collectors.toList());
            permCodeList = sysPermCodeService.getAllPermCodeList().stream().collect(Collectors.toList());
        } else {
            allMenuList = sysMenuService.getMenuListByUserId(user.getUserId())
                    .stream()
                    .map(SysMenu::getMenuName)
                    .collect(Collectors.toList());;
            permCodeList = sysPermCodeService.getPermCodeListByUserId(user.getUserId()).stream().collect(Collectors.toList());
        }

        return  SysUser.builder()
                .name(user.getShowName())
                .deptName(null)
                .deptId(user.getDeptId())
                .description(null)
                .email(user.getEmail())
                .id(user.getUserId())
                .headUrl(user.getHeadImageUrl())  // 头像地址
                .password(user.getPassword())
                .phone(user.getMobile())
                .username(user.getLoginName())
                .status(user.getUserStatus())
                .userPermsList(permCodeList)
                .isAdmin(isAdmin)
                .menuList(allMenuList)
                .build();
    }

    @Override
    @RequestMapping("/getSysUserListByRoleId")
    public List<String> getSysUserListByRoleId(String roleId) {
        List<com.ankaibei.workFlow.webadmin.upms.model.SysUser> sysUserListByRoleId = sysUserService.getSysUserListByRoleId(1L, null, null);
        List<String> userNameList=new ArrayList<>();
        if (!ObjectUtils.isEmpty(sysUserListByRoleId)){
            for (com.ankaibei.workFlow.webadmin.upms.model.SysUser sysUser : sysUserListByRoleId) {
                userNameList.add(sysUser.getShowName());
            }
        }
        return userNameList;
    }
}
