package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.utils.R;
import cn.wisenergy.common.utils.ShareCodeUtil;
import cn.wisenergy.mapper.RecommendUserMapper;
import cn.wisenergy.mapper.TeamUserInfoMapper;
import cn.wisenergy.mapper.UsersMapper;
import cn.wisenergy.model.app.RecommendUser;
import cn.wisenergy.model.app.TeamUserInfo;
import cn.wisenergy.model.app.User;
import cn.wisenergy.service.app.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86187
 * @ Description: 用户接口实现
 * @ Author     : 86187
 * @ Date       : 2021/1/6 16:11
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UsersMapper, User> implements UserService {

    @Autowired
    private UsersMapper usersMapper;

    //直推表userId
    @Autowired
    private RecommendUserMapper recommendUserMapper;

    //团队表
    @Autowired
    private TeamUserInfoMapper teamUserInfoMapper;

    @Override
    public User getById(String userId) {
        return null;
    }

    @Override
    public R<Integer> getById1(String userId) {

        return  R.ok(usersMapper.ByUserId(userId));
    }

    @Override
    public User getByUserId(String userId) {
        return usersMapper.getByUserId(userId);
    }


    @Override
    public User queryUsersByPhone(String phone) {
        Map<String,Object> param=new HashMap<String, Object>();
        param.put("phone",phone);
        List<User> usersList=usersMapper.getUsersListByMap(param);
        if(!CollectionUtils.isEmpty(usersList)){
            return usersList.get(0);
        }
        return null;
    }

    @Override
    public User queryUsersByOpenId(String id) {
        return null;
    }

    @Override
    public R userByZx(String userId, String beInvitedCode) {
        /**
         * 判断用户推荐人的邀请码是否为空，空的话填写1
         */
        if (null == beInvitedCode || "" == beInvitedCode) {
            beInvitedCode = String.valueOf(1);
        }
        //用户的被邀请码，查询到推荐人用户，根据推荐人用户的邀请码查询/修改
        User user=usersMapper.getByBeInvitedCode(beInvitedCode);
        int ida = user.getId();
//        邀请人的被邀请码
        String byqm= user.getBeInvitedCode();
//        通过被邀请码，比对本人邀请码查询本人id
        int idb=usersMapper.beInvitedCode1(beInvitedCode);
        //判断被邀请用户的创建时间是否比推荐人的用户时间晚
        if(idb<ida){
            return R.error(1,"邀请码无效,请重新填写！");
        }
//        插入用户手机号与推荐人邀请码
        usersMapper.insertbyint(userId, beInvitedCode);
        //根据插入的用户手机号，查询用户唯一ID
        Integer yqm = usersMapper.ByUserId(userId);
        //用户唯一ID调用生成6位邀请码
        String inviteCode = ShareCodeUtil.idToCode(yqm);
        //根据用户手机号，更新用户信息
        user = new User();
        user.setInviteCode(inviteCode);
        user.setUserLevel(0);
        usersMapper.updateById(user);

        //插入直推用户表
        RecommendUser recommendUser = new RecommendUser();
        recommendUser.setUserId(userId);
        recommendUserMapper.insert(recommendUser);
        //插入用户团队表
        TeamUserInfo teamUserInfo = new TeamUserInfo();
        teamUserInfo.setUserId(userId);
        teamUserInfoMapper.insert(teamUserInfo);
        //根据用户的推荐人邀请码比对推荐人的本人邀请码，查询推荐人的用户ID=userid
        String userid = String.valueOf(usersMapper.inviteCodeBeInvitedCode(Integer.valueOf(beInvitedCode)));
        //根据用户推荐人的userid,查询数据库，看推荐人用户是否存在
        Integer a = recommendUserMapper.zcByUserId(userid);

        //判断从数据库中是否存在该用户    如果存在，普通用户+1
        if (null != a && 0 != a) {
            Integer normalUserNum = recommendUser.getNormalUserNum();
            //更新直推表用户中的普通用户字段+1
            recommendUser.setNormalUserNum(normalUserNum + 1);
            //更新直推表用户数据
            recommendUserMapper.updateById(recommendUser);
        } else {
            return R.error(1, "验证码无效");
        }
        //递归向上修改团队用户信息表
        teamgg(beInvitedCode);
        return R.ok("直推表普通用户数量+1成功！",0);

    }
    //私有构造方法-传入被邀请码
    private R teamgg(String beInvitedCode) {
        //用户的被邀请码，查询到推荐人用户，根据推荐人用户的邀请码查询/修改
        User user=usersMapper.getByBeInvitedCode(beInvitedCode);
        //根据用户的推荐人邀请码比对推荐人的本人邀请码，查询推荐人的用户ID=userid
        String userid = String.valueOf(usersMapper.inviteCodeBeInvitedCode(Integer.valueOf(beInvitedCode)));
        //根据用户推荐人的userid,查询数据库，看推荐人用户是否存在
        Integer c = teamUserInfoMapper.czByUserId(userid);
        if (null != c && 0 != c) {
            TeamUserInfo teamUserInfo = new TeamUserInfo();
            int normalUserNum1 = teamUserInfo.getNormalUserNum();
            //更新团队表用户中的普通用户字段+1
            teamUserInfo.setNormalUserNum(normalUserNum1 + 1);
            //更新团队表用户数据
            teamUserInfoMapper.updateById(teamUserInfo);
        } else {
            return R.error(0, "验证码无效");
        }
        beInvitedCode=user.getBeInvitedCode();
        //递归向上修改团队用户信息表
       teamgg(beInvitedCode);
        return R.ok("团队表普通用户数量+1成功！",0);
    }
}
