package com.miaxis.spread.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.common.core.domain.entity.UserInfo;
import com.miaxis.spread.domain.WxSpreadRelation;
import com.miaxis.spread.mapper.WxSpreadRelationMapper;
import com.miaxis.spread.service.IWxSpreadRelationService;
import com.miaxis.spread.vo.WxSpreadRelationVo;
import com.miaxis.user.mapper.UserInfoMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * wx推广关系Service业务层处理
 * @author wwl
 * @version 1.0
 * @date 2021/10/29 9:42
 */
@Service
@AllArgsConstructor
public class WxSpreadRelationServiceImpl extends ServiceImpl<WxSpreadRelationMapper, WxSpreadRelation> implements IWxSpreadRelationService {

    private final WxSpreadRelationMapper wxSpreadRelationMapper;
    private final UserInfoMapper userInfoMapper;

    /**
     * 根据openid查询一二级绑定用户
     * @param openid
     * @return
     */
    @Override
    public ArrayList<WxSpreadRelationVo> spreadRelationList(String openid) {

        ArrayList<WxSpreadRelationVo> relationVos = new ArrayList<>();

        List<UserInfo> userInfos = userInfoMapper.selectUserByParentOpenid(openid);
        WxSpreadRelationVo spreadVo = new WxSpreadRelationVo();
        spreadVo.setTitle("一级");
        spreadVo.setUserList(userInfos);
        relationVos.add(spreadVo);

        List<String> collect = userInfos.stream().map(u -> u.getOpenid()).collect(Collectors.toList());
        List<UserInfo> users = new ArrayList<>();
        if (!collect.isEmpty()){
            users = userInfoMapper.selectUserByList(collect);
        }
        WxSpreadRelationVo spread = new WxSpreadRelationVo();
        spread.setTitle("二级");
        spread.setUserList(users);
        relationVos.add(spread);

        return relationVos;
    }

    @Override
    public List<WxSpreadRelation> getWxSpreadRelationList(String openid) {
        return wxSpreadRelationMapper.getWxSpreadRelationList(openid);
    }

}
