package com.bofeng.service;

import com.bofeng.dao.UserMapper;
import com.bofeng.dao.UserOpenMapper;
import com.bofeng.dao.UserPlaceMapper;
import com.bofeng.entity.User;
import com.bofeng.entity.UserOpen;
import com.bofeng.entity.UserPlace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class UserOpenService {

    @Autowired
    private UserOpenMapper userOpenMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserPlaceMapper userPlaceMapper;

    public UserOpen getUserByOpenId(String openId) {
        return userOpenMapper.selectById(openId);
    }

    public UserOpen getUserByUserId(Long userId) {
        return userOpenMapper.selectByUserId(userId);
    }

    @Transactional
    public int bindUserOpen(UserOpen userOpen, User user) {
        return userOpenMapper.insert(userOpen) + userMapper.insert(user);
    }

    @Transactional
    public int bindUser(UserOpen userOpen, User user, UserPlace userPlace) {

        if (userOpenMapper.selectById(userOpen.getOpenId())==null) {
            userOpenMapper.insert(userOpen);
        }
        if (userMapper.selectById(user.getUserId())==null) {

            userMapper.insert(user);
        }
        return userPlaceMapper.insert(userPlace);

    }
}
