package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fdkankan.common.constant.DownloadStatus;
import com.fdkankan.common.constant.RecStatus;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.scene.entity.SceneCooperation;
import com.fdkankan.scene.entity.SceneDataDownload;
import com.fdkankan.scene.entity.SceneDownloadLog;
import com.fdkankan.scene.mapper.ISceneDownloadLogMapper;
import com.fdkankan.scene.service.ISceneDownloadLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 场景下载记录 服务实现类
 * </p>
 *
 * @author dengsixing
 * @since 2021-12-23
 */
@Service
public class SceneDownloadLogServiceImpl extends ServiceImpl<ISceneDownloadLogMapper, SceneDownloadLog> implements ISceneDownloadLogService {

    @Override
    public void updateStatusByNum(String sceneNum, Integer status) {
        this.update(new LambdaUpdateWrapper<SceneDownloadLog>()
                .eq(SceneDownloadLog::getNum, sceneNum)
                .eq(SceneDownloadLog::getTbStatus, TbStatus.VALID.code())
                .set(SceneDownloadLog::getDownloadStatus, status));
    }

    @Override
    public void deleteStatusByNum(String sceneNum) {

        this.update(new LambdaUpdateWrapper<SceneDownloadLog>()
                .eq(SceneDownloadLog::getNum, sceneNum)
                .eq(SceneDownloadLog::getTbStatus, TbStatus.VALID.code())
                .eq(SceneDownloadLog::getDownloadStatus, DownloadStatus.SUCCESS.code())
                .set(SceneDownloadLog::getUpdateTime, Calendar.getInstance().getTime())
                .set(SceneDownloadLog::getTbStatus, TbStatus.DELETE.code()));

    }

    @Override
    public SceneDownloadLog findByStatusAndNum(String sceneNum, Integer status) {
        List<SceneDownloadLog> list =
            this.list(new LambdaQueryWrapper<SceneDownloadLog>()
                .eq(SceneDownloadLog::getNum, sceneNum)
                .eq(SceneDownloadLog::getDownloadStatus, status)
                .orderByDesc(SceneDownloadLog::getId));
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }

    @Override
    public SceneDownloadLog findLastOneByNum(String sceneNum) {
        SceneDownloadLog downloadLog =
            this.getOne(new LambdaQueryWrapper<SceneDownloadLog>()
                .eq(SceneDownloadLog::getNum, sceneNum)
                .orderByDesc(SceneDownloadLog::getId).last("limit 1"));
        return downloadLog;
    }

    @Override
    public void updateSceneDownloadLog(String num, Integer status, String url, String reason) {

        List<SceneDownloadLog> downloadLogIngList = this.list(
            new LambdaQueryWrapper<SceneDownloadLog>().eq(SceneDownloadLog::getNum, num)
                .select(SceneDownloadLog::getId)
                .eq(SceneDownloadLog::getDownloadStatus, DownloadStatus.DOWNLOADING.code()));
        if(CollUtil.isEmpty(downloadLogIngList)){
            return;
        }
        List<Long> idList = downloadLogIngList.stream().map(SceneDownloadLog::getId).collect(Collectors.toList());
        this.update(new LambdaUpdateWrapper<SceneDownloadLog>()
            .set(SceneDownloadLog::getDownloadStatus, status)
            .set(SceneDownloadLog::getDownloadUrl, url)
            .set(SceneDownloadLog::getReason, reason)
            .in(SceneDownloadLog::getId, idList));
    }

}
