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


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwe.scentity.domian.FsFileDataListDo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.safecommon.utils.CXCommonUtils;
import com.rongwei.sfcommon.sys.dao.FsFileDataListDao;
import com.rongwei.sfcommon.sys.service.FsFileDataListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 奥克宁可——文件清单 服务实现类
 * </p>
 *
 * @author trj
 * @since 2023-07-17
 */
@Slf4j
@Service
public class FsFileDataListServiceImpl extends ServiceImpl<FsFileDataListDao, FsFileDataListDo> implements FsFileDataListService {

    @Autowired
    private FsFileDataListDao fileDataListDao;


    @Override
    public R trainingReminder() {
        //查询需要发送消息的文件数据
        List<FsFileDataListDo> list = fileDataListDao.selectByTrainingStartDate();

        //查询每个文件的培训人，组装消息发送
        for (FsFileDataListDo item : list) {
            String id = item.getId();

            String trainingStartDate = ObjectUtil.isNotEmpty(item.getTrainingStartDate()) ? DateUtil.format(item.getTrainingStartDate(), "yyy-MM-dd") : "";
            String trainingEndDate = ObjectUtil.isNotEmpty(item.getTrainingEndDate()) ? DateUtil.format(item.getTrainingEndDate(), "yyy-MM-dd") : "";

            //组装消息内容
            String msg = "您好，您有需要参与的安全生产制度培训：【"+item.getFilename()+"】，培训时间为：【"+trainingStartDate+"】到【"+trainingEndDate+"】，请及时参与，如已参与，请忽略！";
            //查询培训人
            List<String> userIdList = fileDataListDao.selectPeopleIdListByFileId(id);

            //发送消息
            CXCommonUtils.sendNotify("制度培训提醒",
                    msg,
                    null,
                    userIdList,
                    item.getId(),
                    "trainingreminder");
        }

        return R.ok();
    }
}
