package com.manage.interfaces.webservice.impl;

import com.alibaba.fastjson.JSON;
import com.manage.dao.Power_MenuMapper;
import com.manage.encrypt.Base64;
import com.manage.encrypt.MD5;
import com.manage.entity.Power_Menu;
import com.manage.entity.Power_User;
import com.manage.service.cache.Cache;
import com.manage.service.cache.CacheManager;
import com.manage.interfaces.webservice.PowerWebService;
import com.manage.service.User_Dept_MenuService;
import com.manage.util.Constant;
import com.manage.vo.Power_UserVo;
import com.manage.vo.Power_UserWebServiceVo;
import com.manage.vo.User_Dept_Menu;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jws.WebService;
import java.util.*;

@Component
@WebService(serviceName = "PowerWebService",
        targetNamespace = "http://webservice.interfaces.manage.com/",
        endpointInterface = "com.manage.interfaces.webservice.PowerWebService"
)
public class PowerWebServiceImpl implements PowerWebService {
    @Autowired
    Power_MenuMapper powerMenuMapper;
    @Autowired
    User_Dept_MenuService userDeptMenuService;

    @Override
    public String getInfosByUserId(String token,String sysFlag) {
         Power_UserWebServiceVo userWebServiceVo = new Power_UserWebServiceVo();
        if(StringUtils.isNotBlank(token)){
            //归档系统的特殊登录token=1462903487866169011
            if("1462903487866169011".equals(token)){

            }
            token = MD5.JM(Base64.decode(token));
            Cache cache = CacheManager.getCacheInfo(token);
            if(cache != null){
                Power_UserVo user = (Power_UserVo) cache.getValue();
                //设置名字
                user.setUserPosition(user.getName());
                List<User_Dept_Menu> menuList = user.getMenuList();
                Set<String> menus = new TreeSet();
                try {
                    for(User_Dept_Menu menu : menuList){
                        if(StringUtils.isNotBlank(menu.getMenuUrl()) && StringUtils.isNotBlank(menu.getSysFlag()) && sysFlag.equals(menu.getSysFlag()) && StringUtils.isNotBlank(menu.getMethod())){
                            menus.add(menu.getMenuUrl());
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
                BeanUtils.copyProperties(user,userWebServiceVo);
                //可预览信息设置在手机号字段
                userWebServiceVo.setUserTel(user.getShowRecord()+"");
                //可下载信息设置在邮箱字段
                userWebServiceVo.setUserEmail(user.getDownloadRecord()+"");
                //查看打印简要设置在职位字段
                userWebServiceVo.setUserAge(user.getShowPrint());
                userWebServiceVo.setMenus(menus);
            }
        }
        return JSON.toJSONString(userWebServiceVo);
    }

    @Override
    public String tempTest() {
        return "aaaa~~~~~~~~~~~~~~~~~~~~~~~";
    }

    private List<Power_Menu> getPowerMenuMethods(Power_User powerUser, String sysId){
        // 根据用户ID以及系统Id查出所有的用户菜单
        List<Power_Menu> powerUserMenus = powerMenuMapper.selectUserMenuByUserIdAndDictId(powerUser.getUserId(), Integer.parseInt(sysId));
        // 根据用户ID以及系统Id查出所有的角色菜单
        List<Power_Menu> powerRoleMenus = powerMenuMapper.selectRoleMenuByUserIdAndDictId(powerUser.getUserId(), Integer.parseInt(sysId));
        // 当前用户的所有菜单集合
        List<Power_Menu> tempPowerMenus = new ArrayList<>(powerRoleMenus);
        for (Power_Menu powerMenu:powerUserMenus) {
            if(powerMenu.getFlag().equals(Constant.EFFECTIVE_YES)){
                tempPowerMenus.add(powerMenu);
            }else if(powerMenu.getFlag().equals(Constant.EFFECTIVE_NO)){
                tempPowerMenus.remove(powerMenu);
            }
        }

        List<Power_Menu> powerMenus = new ArrayList<>(tempPowerMenus);
        for (Power_Menu powerMenu:tempPowerMenus) {
            for (int i = 0;i<tempPowerMenus.size();i++) {
                if(powerMenu.getParentId().equals(tempPowerMenus.get(i).getMenuId()) && powerMenu.getMethod() != null){
                    powerMenus.get(i).getMethods().add(powerMenu);
                    powerMenus.remove(powerMenu);
                }
            }
        }
        return powerMenus;
    }
}
