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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsentity.domain.FsTrainingStaffDo;
import com.bsentity.enums.BoolEnum;
import com.rongwei.bscommon.sys.dao.FsTrainingStaffDao;
import com.rongwei.bscommon.sys.service.FsFileDataListRecordService;
import com.rongwei.bscommon.sys.service.FsFileDataListService;
import com.rongwei.bscommon.sys.service.FsTrainingStaffRecordService;
import com.rongwei.bscommon.sys.service.FsTrainingStaffService;
import com.rongwei.bscommon.sys.utils.FileSystemUtils;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.base.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 奥克宁可——培训人员清单 服务实现类
 * </p>
 *
 * @author trj
 * @since 2023-07-17
 */
@Slf4j
@Service
public class FsTrainingStaffServiceImpl extends ServiceImpl<FsTrainingStaffDao, FsTrainingStaffDo> implements FsTrainingStaffService {


    @Autowired
    private FsTrainingStaffRecordService trainingStaffRecordService;

    @Autowired
    private FsFileDataListRecordService  fileDataListRecordService;

    @Override
    public R personnelTraining(List<String> fileListIds) {
        //查询当前登录用户
        SysUserVo currentUser = FileSystemUtils.getCurrentUser();
        LambdaQueryWrapper<FsTrainingStaffDo> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(FsTrainingStaffDo::getMasterid, fileListIds)
                .eq(FsTrainingStaffDo::getUserid, currentUser.getId());
        List<FsTrainingStaffDo> list = this.list(queryWrapper);

        //判断该改记录是否存在
        if (list.size() == 0) {
            //数据不存在
            return R.ok();
        }
        //直接修改子表数据
        LambdaUpdateWrapper<FsTrainingStaffDo> trainingStaffQueryWrapper = new LambdaUpdateWrapper<>();
        Date val = new Date();
        trainingStaffQueryWrapper.in(FsTrainingStaffDo::getId,list.stream().map(FsTrainingStaffDo::getId).collect(Collectors.toList()))
                .set(FsTrainingStaffDo::getTraining, BoolEnum.yes.getCode())
                .set(FsTrainingStaffDo::getTrainingtime, val)
                .set(FsTrainingStaffDo::getModifydate, val)
                .set(FsTrainingStaffDo::getModifyuserid, currentUser.getId())
                .set(FsTrainingStaffDo::getModifyusername, currentUser.getName());
        update(trainingStaffQueryWrapper);

        //根据当前表查询历史文件表Id
        final List<String> fileDataListRecordIds = fileDataListRecordService.updateTrainingByMasterId(fileListIds);
        //根据历史文件表Id修改当前培训人状态
        trainingStaffRecordService.updateTrainingByMasterId(fileDataListRecordIds,currentUser,val);

        return R.ok();
    }
}
