package com.usky.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.datascope.context.DataScopeContextHolder;
import com.usky.common.log.annotation.Log;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.iot.domain.PmReceive;
import com.usky.iot.mapper.PmReceiveMapper;
import com.usky.iot.mapper.SysUserMapper;
import com.usky.iot.service.PmReceiveService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.system.domain.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 报告接收表 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-07-09
 */
@Slf4j
@Service
public class PmReceiveServiceImpl extends AbstractCrudService<PmReceiveMapper, PmReceive> implements PmReceiveService {

    @Autowired
    private PmReceiveMapper pmReceiveMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public void add(PmReceive receive) {

    }

    @Override
    public void updateReadFlag(Integer reportId) {
        Long userId = SecurityUtils.getUserId();
        LambdaUpdateWrapper<PmReceive> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper
                .set(PmReceive::getReadFlag, 1)
                .set(PmReceive::getUpdateBy, SecurityUtils.getUsername())
                .set(PmReceive::getUpdateTime, LocalDateTime.now())
                .eq(PmReceive::getReportId, reportId)
                .eq(PmReceive::getReceiverId, userId);
        pmReceiveMapper.update(null, updateWrapper);
    }



    @DataScope
    @Override
    public List<SysUser> test(Integer tenantId) {
        String dataScopeSql = DataScopeContextHolder.getDataScopeSql();
        log.info("DataScopeContextHolder: "+ dataScopeSql);
        LambdaQueryWrapper<SysUser> usersQuery = Wrappers.lambdaQuery();
        usersQuery.select(SysUser::getUserId, SysUser::getUserName)
                .eq(SysUser::getDelFlag, 0).eq(SysUser::getStatus, tenantId)
                .apply(Objects.nonNull(dataScopeSql), dataScopeSql);
        return sysUserMapper.selectList(usersQuery);
    }


}
