package com.manage.interfaces.webservice.impl;

import com.alibaba.fastjson.JSON;
import com.manage.dao.Power_MenuMapper;
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.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)){
            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);
    }
}
