package com.fdkankan.scene.service.impl;

import cn.hutool.core.collection.CollUtil;
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 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::getUpdateTime, Calendar.getInstance().getTime())
                .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 QueryWrapper<SceneDownloadLog>()
                .eq("tb_status", TbStatus.VALID.code())
                .eq("rec_status", RecStatus.VALID.code())
                .eq("scene_code", sceneNum)
                .eq("download_status", status)
                .orderByDesc("id"));
        if(CollUtil.isEmpty(list)){
            return null;
        }
        return list.get(0);

    }

}
