package com.bjtdba.userCenter.service.impl;

import com.bjtdba.entity.userCenter.UnionUsers;
import com.bjtdba.entity.userCenter.User;
import com.bjtdba.entity.userCenter.UserAccount;
import com.bjtdba.userCenter.dao.UnionUsersDao;
import com.bjtdba.userCenter.dao.UserAccountDao;
import com.bjtdba.userCenter.dao.UserDao;
import com.bjtdba.userCenter.service.UnionUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UnionUsersServiceImpl implements UnionUsersService {

    @Autowired
    private UnionUsersDao unionUsersDao;

    @Autowired
    private UserAccountDao userAccountDao;

    @Autowired
    private UserDao userDao;

    public void getAllNotBind(String wechat_user_id,String phone){
        //获取UnionUsers 对象  微信id 和 uid=0
        UnionUsers unionUsers = unionUsersDao.selectUnionUsersByWechatUserid(wechat_user_id,0);
        User userPhone = userDao.selectUserByPhone(phone);
        if(unionUsers!=null){
            int mer_id = unionUsers.getLock_mer_id();
            //修稿user表
            userDao.updateUserMerIdByPhone(mer_id,userPhone.getUid());//绑定首次消费商户id
            //修改unionUsers uid = user.getUid();
            unionUsersDao.updateUid(userPhone.getUid(),unionUsers.getId());
        }
        //User userPhone = userDao.selectUserByPhone(phonne);
        //查询rrx_user_account
        UserAccount userAccount = userAccountDao.selectUserAccountByUidAndType(userPhone.getUid(),1);//type 1 微信    2 支付宝
        UserAccount userAccountSave = new UserAccount();
        userAccountSave.setType(1);//1 微信  2 支付宝
        userAccountSave.setUid(userPhone.getUid());
        userAccountSave.setNickname(userPhone.getNickname());
        userAccountSave.setAvater(userPhone.getAvatar());
        if(userAccount!=null){
            userAccountSave.setId(userAccount.getId());
            //修改 rrx_user_account
            userAccountDao.updateUserAcction(userAccountSave);
        }else{
            //inster rrx_user_account
            userAccountDao.inster(userAccountSave);
        }
    }
}
