Search in sources :

Example 31 with AgentBean

use of com.code.server.constant.game.AgentBean in project summer by foxsugar.

the class WechatAction method becomeAgent.

@RequestMapping("/becomeAgent")
@ResponseBody
public AgentResponse becomeAgent(long userId) {
    AgentResponse agentResponse = new AgentResponse();
    AgentBean agentBean = agentRedisService.getAgentBean(userId);
    // todo 之前是代理
    if (agentBean != null) {
        return new AgentResponse().setCode(ErrorCode.ALREADY_AGENT);
    }
    // 之前不是代理
    GameAgent gameAgent = new GameAgent();
    gameAgent.setId(userId);
    // 推荐
    String unionId = userService.getUserDao().getOpenIdById(userId);
    Recommend recommend = recommendService.getRecommendDao().findOne(unionId);
    GameAgentWx gameAgentWx = gameAgentWxService.getGameAgentWxDao().findOne(unionId);
    if (gameAgentWx == null) {
        return new AgentResponse().setCode(ErrorCode.NOT_WX_USER);
    }
    String openId = gameAgentWx.getOpenId();
    gameAgent.setOpenId(openId);
    gameAgent.setId(userId);
    gameAgent.setUnionId(unionId);
    // 有推荐
    if (recommend != null) {
        long parentId = recommend.getAgentId();
        AgentBean parent = agentRedisService.getAgentBean(parentId);
        // 上级代理存在
        if (parent != null) {
            // 和上级的partner是同一个
            gameAgent.setPartnerId(parent.getPartnerId());
            gameAgent.setParentId(parentId);
            gameAgent.setIsPartner(0);
            // 上级代理加一个child
            parent.getChildList().add(userId);
        }
    }
    // 保存到数据库
    gameAgentService.getGameAgentDao().save(gameAgent);
    agentBean = AgentService.gameAgent2AgentBean(gameAgent);
    // 保存的reids
    agentRedisService.setAgent2Redis(agentBean);
    return agentResponse;
}
Also used : AgentBean(com.code.server.constant.game.AgentBean) GameAgent(com.code.server.db.model.GameAgent) GameAgentWx(com.code.server.db.model.GameAgentWx) Recommend(com.code.server.db.model.Recommend) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 32 with AgentBean

use of com.code.server.constant.game.AgentBean in project summer by foxsugar.

the class WechatAction method handle_link_redirect.

private void handle_link_redirect(long agentId, HttpServletResponse response) throws IOException {
    // 二维码 和 图片 放到cookie里
    AgentBean agentBean = RedisManager.getAgentRedisService().getAgentBean(agentId);
    // cookie 里的值
    Map<String, Object> info = new HashMap<>();
    info.put("icon", agentBean.getImage());
    info.put("qr", agentBean.getQrTicket());
    String sid = "" + System.currentTimeMillis() + "_" + new Random().nextInt(999999);
    String json = JsonUtil.toJson(info);
    json = URLEncoder.encode(json);
    Cookie cookie = new Cookie("info" + sid, json);
    cookie.setDomain(serverConfig.getDomain());
    cookie.setPath("/");
    // 过期时间 30s
    cookie.setMaxAge(300);
    response.addCookie(cookie);
    System.out.println("cookie = " + json);
    String url = MessageFormat.format("http://" + serverConfig.getDomain() + "/agent/#/sharelink?id={0}&sid={1}", "" + agentId, sid);
    System.out.println("sharelink = " + url);
    response.sendRedirect(url);
}
Also used : AgentBean(com.code.server.constant.game.AgentBean) Cookie(javax.servlet.http.Cookie) Random(java.util.Random) HashMap(java.util.HashMap)

Example 33 with AgentBean

use of com.code.server.constant.game.AgentBean in project summer by foxsugar.

the class AgentService method gameAgent2AgentBean.

public static AgentBean gameAgent2AgentBean(GameAgent gameAgent) {
    AgentBean agentBean = new AgentBean();
    BeanUtils.copyProperties(gameAgent, agentBean);
    agentBean.setChildList(gameAgent.getAgentChild().getChildren());
    return agentBean;
}
Also used : AgentBean(com.code.server.constant.game.AgentBean)

Example 34 with AgentBean

use of com.code.server.constant.game.AgentBean in project summer by foxsugar.

the class AgentService method player2Agnet.

/**
 * 玩家成为代理
 *
 * @param userId
 * @param unionId
 */
private AgentBean player2Agnet(long userId, String unionId) {
    // fixme 修复如果成为代理前就已经是下级的情况
    GameAgent gameAgent = new GameAgent();
    gameAgent.setCreateDate(new Date());
    gameAgent.setId(userId);
    gameAgent.setUnionId(unionId);
    gameAgent.setIsPartner(0);
    gameAgent.setParentId(0);
    User user = userService.getUserByUserId(userId);
    if (user != null) {
        gameAgent.setParentId(user.getReferee());
    }
    AgentBean agentBean = gameAgent2AgentBean(gameAgent);
    // 保存到redis
    agentRedisService.setAgent2Redis(agentBean);
    // 保存到数据库
    gameAgentService.getGameAgentDao().save(gameAgent);
    return agentBean;
}
Also used : AgentBean(com.code.server.constant.game.AgentBean) GameAgent(com.code.server.db.model.GameAgent) User(com.code.server.db.model.User) AgentUser(com.code.server.db.model.AgentUser) LocalDate(java.time.LocalDate)

Example 35 with AgentBean

use of com.code.server.constant.game.AgentBean in project summer by foxsugar.

the class AgentService method changeAgent.

/**
 * 换代理
 *
 * @param userId
 * @param newAgentId
 */
public void changeAgent(long userId, long newAgentId) {
    // 自己换自己 推出
    if (userId == newAgentId)
        return;
    AgentBean newAgentBean = RedisManager.getAgentRedisService().getAgentBean(newAgentId);
    if (newAgentBean == null)
        return;
    AgentBean agentBean = RedisManager.getAgentRedisService().getAgentBean(userId);
    // 玩家
    if (agentBean == null) {
        UserBean userBean = RedisManager.getUserRedisService().getUserBean(userId);
        long parentId = 0;
        if (userBean != null) {
            parentId = userBean.getReferee();
            userBean.setReferee((int) newAgentId);
            RedisManager.getUserRedisService().updateUserBean(userBean.getId(), userBean);
        } else {
            User user = userService.getUserByUserId(userId);
            if (user != null) {
                parentId = user.getReferee();
                user.setReferee((int) newAgentId);
                userService.save(user);
            }
        }
        // 以前的代理 删掉一个玩家
        AgentBean oldAgent = RedisManager.getAgentRedisService().getAgentBean(parentId);
        if (oldAgent != null) {
            oldAgent.getChildList().remove(userId);
            RedisManager.getAgentRedisService().updateAgentBean(oldAgent);
        }
        newAgentBean.getChildList().add(userId);
        RedisManager.getAgentRedisService().updateAgentBean(newAgentBean);
    // changePlayerReferee(userId, (int)newAgentId);
    } else {
        // 如果是合伙人的处理
        if (agentBean.getIsPartner() == 1) {
        // do nothing
        // 合伙人 换不了代理
        } else {
            // 原来的上级代理
            AgentBean parentBean = RedisManager.getAgentRedisService().getAgentBean(agentBean.getParentId());
            // 自己的合伙人 和 新上级的合伙人是否是同一人
            long newPartnerId = newAgentBean.getPartnerId();
            // 如果合伙人id换了
            if (agentBean.getPartnerId() != newPartnerId) {
                Set<AgentBean> subAgents = new HashSet<>();
                findAllClildAgent(agentBean, subAgents);
                for (AgentBean subAgent : subAgents) {
                    // 新的合伙人
                    subAgent.setPartnerId(newPartnerId);
                    RedisManager.getAgentRedisService().updateAgentBean(subAgent);
                }
                // 自己换合伙人id
                agentBean.setPartnerId(newPartnerId);
            }
            // 自己换上级
            agentBean.setParentId(newAgentId);
            // 原来的上级代理 删掉一个下级
            if (parentBean != null) {
                parentBean.getChildList().remove(userId);
                RedisManager.getAgentRedisService().updateAgentBean(parentBean);
            }
            // 新上级 加一个代理
            newAgentBean.getChildList().add(userId);
            RedisManager.getAgentRedisService().updateAgentBean(newAgentBean);
            // 保存自己
            RedisManager.getAgentRedisService().updateAgentBean(agentBean);
        }
        // 换player的上级
        changePlayerReferee(userId, (int) newAgentId);
    }
}
Also used : AgentBean(com.code.server.constant.game.AgentBean) User(com.code.server.db.model.User) AgentUser(com.code.server.db.model.AgentUser) UserBean(com.code.server.constant.game.UserBean)

Aggregations

AgentBean (com.code.server.constant.game.AgentBean)37 User (com.code.server.db.model.User)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 UserBean (com.code.server.constant.game.UserBean)6 WxErrorException (me.chanjar.weixin.common.exception.WxErrorException)6 Recommend (com.code.server.db.model.Recommend)5 WxMpUser (me.chanjar.weixin.mp.bean.result.WxMpUser)5 AgentUser (com.code.server.db.model.AgentUser)4 GameAgent (com.code.server.db.model.GameAgent)4 ChildCost (com.code.server.constant.db.ChildCost)3 LocalDate (java.time.LocalDate)3 ArrayList (java.util.ArrayList)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 AgentInfo (com.code.server.constant.db.AgentInfo)2 Charge (com.code.server.db.model.Charge)2 DemoChecker (com.code.server.login.anotation.DemoChecker)2 TextBuilder (com.code.server.login.wechat.builder.TextBuilder)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2