package com.usky.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.meeting.domain.MeetingFile;
import com.usky.meeting.mapper.MeetingFileMapper;
import com.usky.meeting.service.MeetingFileService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.meeting.service.vo.MeetingFileListVO;
import com.usky.meeting.service.vo.MeetingFileRequestVO;
import org.springframework.beans.factory.wiring.BeanWiringInfo;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 会议文件 服务实现类
 * </p>
 *
 * @author zyj
 * @since 2024-03-08
 */
@Service
public class MeetingFileServiceImpl extends AbstractCrudService<MeetingFileMapper, MeetingFile> implements MeetingFileService {

    @Override
    public void insertMeetingFile(List<MeetingFileRequestVO> requestVOList){
        if(requestVOList.size() > 0){
            for (int i = 0; i < requestVOList.size(); i++) {
                MeetingFile meetingFile = new MeetingFile();
                meetingFile.setMeetingId(requestVOList.get(i).getMeetingId());
                meetingFile.setFileUrl(requestVOList.get(i).getFileUrl());
                meetingFile.setFileType(requestVOList.get(i).getFileType());
                meetingFile.setUploadUser(SecurityUtils.getUsername());
                meetingFile.setUploadTime(LocalDateTime.now());
                meetingFile.setTenantId(SecurityUtils.getTenantId());
                meetingFile.setFileName(requestVOList.get(i).getFileName());

                this.save(meetingFile);
            }
        }

    }

    @Override
    public CommonPage<MeetingFile> meetingFileList(MeetingFileListVO meetingFileListVO){
        Integer current = meetingFileListVO.getCurrent();
        Integer size = meetingFileListVO.getSize();

        Page<MeetingFile> page = new Page<>(current,size);

        LambdaQueryWrapper<MeetingFile> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(null != meetingFileListVO.getMeetingId(),MeetingFile::getMeetingId,meetingFileListVO.getMeetingId())
                .like(StringUtils.isNotBlank(meetingFileListVO.getFileName()),MeetingFile::getFileName,meetingFileListVO.getFileName())
                .eq(MeetingFile::getTenantId,SecurityUtils.getTenantId())
                .orderByDesc(MeetingFile::getFileId);
        page = this.page(page,queryWrapper);

        return new CommonPage<>(page.getRecords(),page.getTotal(),size,current);
    }
}
