package ink.wgink.mongo.login.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.user.mongo.IMongoLoginUserService;
import ink.wgink.login.base.service.user.UserLoginService;
import ink.wgink.pojo.bos.LoginUser;
import ink.wgink.service.department.service.IDepartmentUserService;
import ink.wgink.service.user.enums.UserTypeEnum;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.service.user.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName: MongoLoginServiceImpl
 * @Description: mongo登录
 * @Author: wanggeng
 * @Date: 2022/5/19 11:31
 * @Version: 1.0
 */
@Service
public class MongoLoginUserServiceImpl extends DefaultBaseService implements IMongoLoginUserService {

    private static final String COLLECTION_NAME = "loginUser";
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private IDepartmentUserService departmentUserService;

    @Override
    public void updateByUserId(String userId) {
        UserPO userPO = userService.getPO(userId);
        LoginUser loginUser = UserUtil.createLoginUser(userPO);
        update(loginUser);
    }

    @Override
    public void updateByUserIds(List<String> userIds) {
        userService.listPOByUserIds(userIds).forEach(userPO -> {
            LoginUser loginUser = UserUtil.createLoginUser(userPO);
            update(loginUser);
        });

    }

    @Override
    public void update(List<LoginUser> loginUsers) {
        loginUsers.forEach(this::update);
    }

    @Override
    public void update(LoginUser loginUser) {
        // 系统用户、普通用户加载逻辑，公共用户不处理逻辑
        if (loginUser.getUserType().equals(UserTypeEnum.SYSTEM.getValue()) ||
                loginUser.getUserType().equals(UserTypeEnum.NORMAL.getValue())) {
            userLoginService.setLoginUserInfo(loginUser);
        }
        // 删除原有
        delete(Arrays.asList(loginUser.getUserId()));
        // 新增更新
        mongoTemplate.save(loginUser, COLLECTION_NAME);
    }

    @Override
    public void delete(List<String> userIds) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").in(userIds)), COLLECTION_NAME);
    }

    @Override
    public LoginUser get(String userId) {
        return mongoTemplate.findOne(new Query().addCriteria(Criteria.where("userId").is(userId)), LoginUser.class, COLLECTION_NAME);
    }

    @Override
    public LoginUser getByUsername(String username) {
        return mongoTemplate.findOne(new Query().addCriteria(Criteria.where("username").is(username)), LoginUser.class, COLLECTION_NAME);
    }

}
