
package com.sonic.bear.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sonic.bear.domain.entity.User;
import com.sonic.bear.domain.entity.UserThird;
import com.sonic.bear.domain.input.KickOutInput;
import com.sonic.bear.lib.enums.OptTypeEnum;
import com.sonic.bear.lib.enums.UserTypeEnum;
import com.sonic.bear.lib.input.CompleteUserInfoInput;
import com.sonic.bear.lib.input.EditUserInfoInput;
import com.sonic.bear.service.*;
import com.sonic.pigeon.lib.client.ImUserClient;
import com.sonic.pigeon.lib.enums.ImUserTypeEnum;
import com.sonic.pigeon.lib.input.EditImUserInput;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.baomidou.mybatisplus.core.toolkit.Wrappers.update;

/**
 * @author code
 */
@Slf4j
@Service
public class UserSetServiceImpl implements UserSetService {

    @Autowired
    private UserService userService;
    @Autowired
    private UserThirdService userThirdService;
    @Autowired
    private UserSessionService userSessionService;
    @Autowired
    private UserNicknamePoolService userNicknamePoolService;
    @Autowired
    private ImUserClient imUserClient;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void completeUserInfo(CompleteUserInfoInput input) {
        //查询用户基础信息
        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, input.getUserId()));
        //更新用户基础信息
        userService.update(Wrappers.<User>lambdaUpdate()
                .set(StringUtils.isNotEmpty(input.getHeadImage()), User::getHeadImage, input.getHeadImage())
                .set(StringUtils.isNotEmpty(input.getNickname()), User::getNickname, input.getNickname())
                .set(input.getSex() != null, User::getSex, input.getSex())
                .set(input.getBirthday() != null, User::getBirthday, input.getBirthday())
                .eq(User::getUserId, input.getUserId()));
        if(user == null) {
            //将数据添加到数据池
            userNicknamePoolService.syncPool(input.getUserId(), UserTypeEnum.USER.getCode(), input.getNickname(), OptTypeEnum.ADD.name());
        } else {
            //将数据添加到数据池
            userNicknamePoolService.syncPool(input.getUserId(), UserTypeEnum.USER.getCode(), input.getNickname(), OptTypeEnum.UPD.name());
        }
        //有数据变更则需要更新im用户基础信息
        if((StringUtils.isNotEmpty(input.getNickname()) && !input.getNickname().equals(user.getNickname())) ||
                (StringUtils.isNotEmpty(input.getHeadImage()) && !input.getHeadImage().equals(user.getHeadImage()))) {
            User querUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, input.getUserId()));
            //更新im基础信息
            EditImUserInput editImUserInput = new EditImUserInput();
            editImUserInput.setImUserType(ImUserTypeEnum.u);
            editImUserInput.setUserId(querUser.getUserId());
            editImUserInput.setNickname(querUser.getNickname());
            editImUserInput.setHeadImage(querUser.getHeadImage());
            //编辑im基础信息
            imUserClient.editImUser(editImUserInput);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editUserInfo(EditUserInfoInput input) {
        //查询用户基础信息
        User user = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, input.getUserId()));
        //更新用户基础信息
        userService.update(Wrappers.<User>lambdaUpdate()
                .set(StringUtils.isNotEmpty(input.getHeadImage()), User::getHeadImage, input.getHeadImage())
                .set(StringUtils.isNotEmpty(input.getNickname()), User::getNickname, input.getNickname())
                .set(input.getBirthday() != null, User::getBirthday, input.getBirthday())
                .eq(User::getUserId, input.getUserId()));
        //用户昵称有变更
        if(!user.getNickname().equals(input.getNickname())) {
            //将数据添加到数据池
            userNicknamePoolService.syncPool(input.getUserId(), UserTypeEnum.USER.getCode(), input.getNickname(), OptTypeEnum.UPD.name());
        }
        //有数据变更则需要更新im用户基础信息
        if(!user.getNickname().equals(input.getNickname()) || !user.getHeadImage().equals(input.getHeadImage())) {
            User querUser = userService.getOne(Wrappers.<User>lambdaQuery().eq(User::getUserId, input.getUserId()));
            //更新im基础信息
            EditImUserInput editImUserInput = new EditImUserInput();
            editImUserInput.setImUserType(ImUserTypeEnum.u);
            editImUserInput.setUserId(querUser.getUserId());
            editImUserInput.setNickname(querUser.getNickname());
            editImUserInput.setHeadImage(querUser.getHeadImage());
            //编辑im基础信息
            imUserClient.editImUser(editImUserInput);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delAccount(Long userId) {
        //删除基础账号
        update(Wrappers.<User>lambdaUpdate()
                .set(User::getIsDelete, true)
                .eq(User::getUserId, userId));
        //删除三方账号
        userThirdService.update(Wrappers.<UserThird>lambdaUpdate()
                .set(UserThird::getIsDelete, true)
                .eq(UserThird::getUserId, userId));
        //将用户踢下线
        KickOutInput input = new KickOutInput();
        input.setUserId(userId);
        userSessionService.kickOut(input);
        //将数据从数据池中删除
        userNicknamePoolService.syncPool(input.getUserId(), UserTypeEnum.USER.getCode(), null, OptTypeEnum.DEL.name());
    }

}
