package com.qmth.distributed.print.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.distributed.print.business.entity.CloudUserPushStatus;
import com.qmth.distributed.print.business.mapper.CloudUserPushStatusMapper;
import com.qmth.distributed.print.business.service.CloudUserPushStatusService;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.userPush.SyncStatusEnum;
import com.qmth.teachcloud.common.enums.userPush.UserPushResultEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 云阅卷用户推送服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-08-08
 */
@Service
public class CloudUserPushStatusServiceImpl extends ServiceImpl<CloudUserPushStatusMapper, CloudUserPushStatus> implements CloudUserPushStatusService {

    @Override
    public CloudUserPushStatus findRecordByUserId(Long userId, Long schoolId) {
        List<CloudUserPushStatus> cloudUserPushStatusList = this.list(new QueryWrapper<CloudUserPushStatus>()
                .lambda()
                .eq(CloudUserPushStatus::getSchoolId, schoolId)
                .eq(CloudUserPushStatus::getUserId, userId));

        CloudUserPushStatus result = new CloudUserPushStatus();

        if (cloudUserPushStatusList != null) {
            if (cloudUserPushStatusList.size() > 1) {
                throw ExceptionResultEnum.ERROR.exception("异常:用户id【" + userId + "】在云阅卷用户推送状态表中有多条数据");
            } else if (cloudUserPushStatusList.size() == 1) {
                result = cloudUserPushStatusList.get(0);
            }
        }
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateUserPushRecord(Long userId, UserPushResultEnum markerPushResult, UserPushResultEnum leaderPushResult, SyncStatusEnum syncStatus, SysUser requestUser) {
        Long schoolId = requestUser.getSchoolId();
        // 云阅卷用户推送记录
        CloudUserPushStatus db = this.findRecordByUserId(userId, schoolId);
        CloudUserPushStatus cloudUserPushStatus = new CloudUserPushStatus();
        cloudUserPushStatus.setSchoolId(schoolId);
        cloudUserPushStatus.setUserId(userId);
        cloudUserPushStatus.setMarkerStatus(markerPushResult);
        cloudUserPushStatus.setMarkerLeaderStatus(leaderPushResult);
        cloudUserPushStatus.setSyncStatus(syncStatus);
        if (Objects.isNull(db)) {
            // 新增
            cloudUserPushStatus.insertInfo(requestUser.getId());
        } else {
            // 编辑
            cloudUserPushStatus.setId(db.getId());
            cloudUserPushStatus.updateInfo(requestUser.getId());
        }
        this.saveOrUpdate(cloudUserPushStatus);
    }

    @Override
    public List<Long> findNeedPushAgainUserIdList(Long schoolId) {
        return this.list(new QueryWrapper<CloudUserPushStatus>()
                        .lambda()
                        .eq(CloudUserPushStatus::getSchoolId, schoolId)
                        .eq(CloudUserPushStatus::getSyncStatus, SyncStatusEnum.NEED_PUSH_AGAIN))
                .stream().map(CloudUserPushStatus::getUserId).distinct().collect(Collectors.toList());
    }
}
