package com.ssj.service.conch.parents.v2.service.impl;


import com.ssj.framework.core.security.manager.TokenManager;
import com.ssj.framework.core.util.Constant;
import com.ssj.service.conch.parents.v2.service.ParentV2Service;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ParentV2ServiceImpl implements ParentV2Service {

    private   Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TokenManager tokenManager;

    @Override
    public void updateExitLibRecord(String vipId, String oldLibId, String newLibId) {
        if (StringUtils.isNotBlank(oldLibId)) {
            //已绑定馆，切换时需要保存已离馆记录至redis
            List<String> oldLibExitedStudentIdList = tokenManager.getList(Constant.EXITED_STUDENT_PREFIX + oldLibId, String.class);
            if (oldLibExitedStudentIdList == null) {
                oldLibExitedStudentIdList = new ArrayList<>();
            }
            if (!oldLibExitedStudentIdList.contains(vipId)) {
                oldLibExitedStudentIdList.add(vipId);
                tokenManager.set(Constant.EXITED_STUDENT_PREFIX + oldLibId, oldLibExitedStudentIdList);
            }
        }

        //若新馆存在对应离馆记录，需要清除
        List<String> newLibExitedStudentIdList = tokenManager.getList(Constant.EXITED_STUDENT_PREFIX + newLibId, String.class);
        if (newLibExitedStudentIdList != null && newLibExitedStudentIdList.contains(vipId)) {
            newLibExitedStudentIdList.remove(vipId);
            tokenManager.set(Constant.EXITED_STUDENT_PREFIX + newLibId, newLibExitedStudentIdList);
        }
    }
}
	