/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.security.comment.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.event.LevelUpEvent;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserExtension;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.dao.UserExtensionMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.security.comment.dao.AppConnectMapper;
import com.yami.shop.security.comment.model.AppConnect;
import com.yami.shop.security.comment.service.AppConnectService;
import com.yami.shop.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
 *
 * @author lgh on 2018/09/07.
 */
@Service
@AllArgsConstructor
public class AppConnectServiceImpl extends ServiceImpl<AppConnectMapper, AppConnect> implements AppConnectService {

	private final AppConnectMapper appConnectMapper;
	private final ApplicationContext applicationContext;

	private final UserMapper userMapper;
	private final UserService userService;
	private final UserExtensionMapper userExtensionMapper;


	/**
	 * YamiUserServiceImpl#insertUserIfNecessary 将会清除该缓存信息
	 * @param bizUserId
	 * @return
	 */
	@Override
	public AppConnect getByBizUserId(String bizUserId, Integer appId) {
		return appConnectMapper.getByBizUserId(bizUserId, appId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public User registerOrBindUser(User user, AppConnect appConnect, Integer appId) {
		if (StrUtil.isBlank(user.getUserId())) {
			if (userMapper.selectCount(new LambdaQueryWrapper<User>().eq(User::getUserName, user.getUserName())) > 0) {
				throw new GlobalException("用户名已存在");
			}
			String userId = IdUtil.simpleUUID();
			user.setUserId(userId);
			user.setLevel(1);
			user.setLevelType(0);
			userMapper.insert(user);
			UserExtension userExtension = new UserExtension();
			userExtension.setBalance(0.0);
			userExtension.setGrowth(0);
			userExtension.setLevel(1);
			userExtension.setLevelType(0);
			userExtension.setScore(0);
			userExtension.setUpdateTime(new Date());
			userExtension.setVersion(0);
			userExtension.setUserId(userId);
			userExtensionMapper.insert(userExtension);
			//用户注册成功后发送等级提升事件
			applicationContext.publishEvent(new LevelUpEvent(userExtension,1,0));
		} else {
			if (appConnect != null&& StrUtil.isBlank(user.getPic())) {
				User userParam = new User();
				userParam.setUserId(user.getUserId());
				userParam.setModifyTime(new Date());
				userParam.setPic(appConnect.getImageUrl());
				userService.updateById(userParam);
			}
		}
		if (appConnect == null) {
			// 避免重复插入数据
			if (appConnectMapper.getByBizUserId(user.getUserId(), appId) != null) {
				return user;
			}
			appConnect = new AppConnect();
			appConnect.setUserId(user.getUserId());
			appConnect.setNickName(user.getNickName());
			appConnect.setImageUrl(user.getPic());

			// 0表示是系统的用户，不是第三方的
			appConnect.setAppId(appId);
			appConnectMapper.insert(appConnect);
		} else if (StrUtil.isBlank(appConnect.getUserId())) {
			appConnect.setUserId(user.getUserId());
			appConnectMapper.updateById(appConnect);
		}

		return user;
	}

}
