/*
*  Copyright 2019-2025 Tz
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.security.service.UserCacheManager;
import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.domain.BusDevice;
import me.zhengjie.modules.system.domain.BusUser;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.dto.BusTreeVo;
import me.zhengjie.modules.system.domain.dto.BusUserQueryCriteria;
import me.zhengjie.modules.system.mapper.BusUserMapper;
import me.zhengjie.modules.system.service.BusUserService;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author tz
* @date 2025-05-19
**/
@Service
@RequiredArgsConstructor
public class BusUserServiceImpl extends ServiceImpl<BusUserMapper, BusUser> implements BusUserService {

    @Autowired private BusUserMapper busUserMapper;
    @Autowired private UserCacheManager userCacheManager;

    @Override
    public PageResult<BusUser> queryAll(BusUserQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(busUserMapper.findAll(criteria, page));
    }

    @Override
    public List<BusUser> queryAll(BusUserQueryCriteria criteria){
        return busUserMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(BusUser resources) {
        busUserMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusUser resources) {
        BusUser busUser = getById(resources.getId());
        busUser.copy(resources);
        busUserMapper.updateById(busUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        busUserMapper.deleteBatchIds(ids);
    }

    @Override
    public List<BusTreeVo> getUserCommands(Long id) {
        return busUserMapper.getUserCommands(id);
    }

    @Override
    public JwtUserDto addUserCache(BusUser BusUser) {
        String openId = BusUser.getToken();
        JwtUserDto jwtUserDto = userCacheManager.getUserCache(openId);
        if (null == jwtUserDto) {
            User user = new User();
            user.setId(BusUser.getId());
            user.setUsername(BusUser.getToken());
            user.setNickName(BusUser.getNickname());
            user.setAvatarPath(BusUser.getAvatar());
            user.setPhone(BusUser.getPhone());
            user.setFlag(2);
            jwtUserDto = new JwtUserDto(user, new ArrayList<>(), new ArrayList<>());
            userCacheManager.addUserCache(BusUser.getToken(), jwtUserDto);
        }
        return jwtUserDto;
    }

}