package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bsentity.domain.FsFileDataListDo;
import com.bsentity.domain.FsTrainingStaffDo;
import com.rongwei.bscommon.sys.service.FsFileDataListService;
import com.rongwei.bscommon.sys.service.RemindService;
import com.rongwei.bscommon.sys.utils.FileSystemUtils;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * RemindServiceImpl class
 *
 * @author XH
 * @date 2023/07/25
 */
@Service
public class RemindServiceImpl implements RemindService {
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private FsTrainingStaffServiceImpl fsTrainingStaffService;
    @Autowired
    private FsFileDataListService fsFileDataListService;
    /**
     * 培训提醒标题
     */
    public static final String TRAIN_TITLE = "培训提醒";


    @Override
    public R trainRemind(List<String> fileDataList) {
        if (fileDataList == null || fileDataList.isEmpty()) {
            log.debug("文件列表主键为空");
            return R.ok();
        }
        Collection<FsFileDataListDo> fsFileDataListDos = fsFileDataListService.listByIds(fileDataList);
        if (fsFileDataListDos.isEmpty()) {
            log.debug("无法通过：{}获取文件列表", fileDataList);
            return R.ok();
        }
        List<FsTrainingStaffDo> trainingStaffDos = fsTrainingStaffService.list(new LambdaQueryWrapper<FsTrainingStaffDo>()
                .in(FsTrainingStaffDo::getMasterid, fileDataList)
                .eq(FsTrainingStaffDo::getDeleted, "0"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        fsFileDataListDos.forEach(info -> {
            List<String> remindUserId = trainingStaffDos.stream()
                    .filter(trainingStaff -> info.getId().equals(trainingStaff.getMasterid()))
                    .map(FsTrainingStaffDo::getUserid)
                    .filter(StringUtils::isNotBlank)
                    .distinct().collect(Collectors.toList());
            if (!remindUserId.isEmpty()) {
                FileSystemUtils.sendNotify(TRAIN_TITLE, info.getFilename(), simpleDateFormat.format(info.getModifydate()),
                        remindUserId, info.getId());
            }
        });
        return null;
    }
}
