package com.zd.base.message.service.impl;

import com.zd.base.message.domain.UserOpenId;
import com.zd.base.message.mapper.UserOpenIdMapper;
import com.zd.base.message.service.IUserOpenIdService;
import com.zd.base.message.service.IWechatMsgSendService;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户和微信openID对应表Service业务层处理
 *
 * @author hanson
 */
@Service
public class UserOpenIdServiceImpl implements IUserOpenIdService {
    @Resource
    private UserOpenIdMapper userOpenIdMapper;
    @Resource
    private IWechatMsgSendService wechatMsgSendService;

    @Resource
    private TokenService tokenService;

    /**
     * 查询用户和微信openID对应表
     *
     * @param id 用户和微信openID对应表主键
     * @return 用户和微信openID对应表
     */
    @Override
    public UserOpenId selectUserOpenIdById(Long id) {
        return userOpenIdMapper.selectUserOpenIdById(id);
    }

    /**
     * 查询用户和微信openID对应表列表
     *
     * @param userOpenId 用户和微信openID对应表
     * @return 用户和微信openID对应表
     */
    @Override
    public List<UserOpenId> selectUserOpenIdList(UserOpenId userOpenId) {
        return userOpenIdMapper.selectUserOpenIdList(userOpenId);
    }

    @Override
    public List<UserOpenId> getListByIds(List<Long> ids) {
        if (ids.isEmpty()) {
            new ArrayList<>();
        }
        return userOpenIdMapper.getListByIds(ids);
    }

    /**
     * 新增用户和微信openID对应表
     *
     * @param userOpenId 用户和微信openID对应表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertUserOpenId(UserOpenId userOpenId) {
        return userOpenIdMapper.insertUserOpenId(userOpenId);
    }

    /**
     * 修改用户和微信openID对应表
     *
     * @param userOpenId 用户和微信openID对应表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUserOpenId(UserOpenId userOpenId) {
        return userOpenIdMapper.updateUserOpenId(userOpenId);
    }

    /**
     * 批量删除用户和微信openID对应表
     *
     * @param ids 需要删除的用户和微信openID对应表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserOpenIdByIds(Long[] ids) {
        return userOpenIdMapper.deleteUserOpenIdByIds(ids);
    }

    /**
     * 删除用户和微信openID对应表信息
     *
     * @param id 用户和微信openID对应表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUserOpenIdById(Long id) {
        return userOpenIdMapper.deleteUserOpenIdById(id);
    }

    @Override
    public UserOpenId getOpenId(String code) {
        UserOpenId userOpenId;
        LoginUser loginUser = tokenService.getLoginUser();
        if (loginUser!=null){
            Long userId = loginUser.getSysUser().getUserId();
            userOpenId=userOpenIdMapper.getByUserId(userId);
            if (userOpenId!=null){
                return userOpenId;
            }
            String openId = wechatMsgSendService.getOpenId(code);
            if (StringUtils.isEmpty(openId)){
                return new UserOpenId();
            }
            userOpenId = new UserOpenId();
            userOpenId.setOpenId(openId);
            userOpenId.setUserId(userId);

            List<UserOpenId> userOpenIds = userOpenIdMapper.selectUserOpenIdList(userOpenId);
            if (userOpenIds.isEmpty()) {
                userOpenIdMapper.insertUserOpenId(userOpenId);
            }
            return userOpenId;
        }else {
            throw new ServiceException("未检测到登录信息");
        }
    }

    @Override
    public UserOpenId getByUserId(Long userId) {
        return userOpenIdMapper.getByUserId(userId);
    }
}
