package com.limap.core.basic.service.impl;

import java.util.List;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.limap.common.core.redis.RedisCache;
import com.limap.common.utils.DateUtils;
import com.limap.core.screen.vo.ControlSwitch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.limap.common.utils.StringUtils;
import com.limap.core.basic.mapper.BasicUserBindInfoMapper;
import com.limap.core.basic.domain.BasicUserBindInfo;
import com.limap.core.basic.service.IBasicUserBindInfoService;

import javax.annotation.Resource;

/**
 * 用户设备绑定信息Service业务层处理
 *
 * @author jjb
 * @date 2025-04-11
 */
@Service
public class BasicUserBindInfoServiceImpl extends ServiceImpl<BasicUserBindInfoMapper, BasicUserBindInfo> implements IBasicUserBindInfoService {
    @Autowired
    private BasicUserBindInfoMapper basicUserBindInfoMapper;

    /**
     * 查询用户设备绑定信息列表
     *
     * @param basicUserBindInfo 用户设备绑定信息
     * @return 用户设备绑定信息
     */
    @Override
    public List<BasicUserBindInfo> selectBasicUserBindInfoList(BasicUserBindInfo basicUserBindInfo) {
    	LambdaQueryWrapper<BasicUserBindInfo> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(basicUserBindInfo.getUserId())) {
			queryWrapper.eq(BasicUserBindInfo::getUserId, basicUserBindInfo.getUserId());
		}
        if (StringUtils.isNotEmpty(basicUserBindInfo.getDeviceName())) {
			queryWrapper.like(BasicUserBindInfo::getDeviceName, basicUserBindInfo.getDeviceName());
		}
        if (StringUtils.isNotEmpty(basicUserBindInfo.getProductKey())) {
			queryWrapper.eq(BasicUserBindInfo::getProductKey, basicUserBindInfo.getProductKey());
		}
        if (StringUtils.isNotEmpty(basicUserBindInfo.getType())) {
			queryWrapper.eq(BasicUserBindInfo::getType, basicUserBindInfo.getType());
		}
        if (StringUtils.isNotEmpty(basicUserBindInfo.getUserName())) {
			queryWrapper.likeLeft(BasicUserBindInfo::getUserName, basicUserBindInfo.getUserName());
		}
        queryWrapper.orderByDesc(BasicUserBindInfo::getCreateTime);
        return basicUserBindInfoMapper.selectBasicUserBindInfoList(basicUserBindInfo);
    }

    @Override
    @DS("test")
    public BasicUserBindInfo selectAppUserInfo(BasicUserBindInfo basicUserBindInfo) {
        List<BasicUserBindInfo> list = basicUserBindInfoMapper.selectAppUserInfo(basicUserBindInfo);
        list.forEach(item -> item.setUserId(basicUserBindInfo.getUserId()));
        return list.stream().findFirst().orElse(null);
    }

    @Override
    @Cacheable(value = "userBindInfo", key = "#deviceId", unless = "#result == null")
    public BasicUserBindInfo getInfoByDeviceId(Long deviceId) {
        return this.getById(deviceId);
    }

    @Override
    @DS("test")
    public List<BasicUserBindInfo> selectAppUserInfo() {
        return basicUserBindInfoMapper.selectAppUserInfo(new BasicUserBindInfo());
    }

    @Override
    @DS("test")
    public List<ControlSwitch> getcControlInfo(String deviceName) {
        return basicUserBindInfoMapper.getcControlInfo(deviceName);
    }
}
