package cn.cslg.pas.service;

import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.common.utils.ResponseEnum;
import cn.cslg.pas.common.utils.WebSocketServer;
import cn.cslg.pas.domain.ProjectImportStatus;
import cn.cslg.pas.mapper.ProjectImportStatusMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【os_thematic_import(专题库数据导入记录)】的数据库操作Service
* @createDate 2022-04-25 15:51:55
*/
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectImportStatusService extends ServiceImpl<ProjectImportStatusMapper, ProjectImportStatus> {

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void add(Integer importId, Integer userId, Integer status) {
        ProjectImportStatus projectImportStatus = new ProjectImportStatus();
        projectImportStatus.setImportId(importId);
        projectImportStatus.setCreateTime(new Date());
        projectImportStatus.setStatus(status);
        projectImportStatus.insert();
        WebSocketServer.sendInfo(Response.websocket(projectImportStatus, ResponseEnum.PROJECT_IMPORT_TASK_SUCCESS), String.valueOf(userId));
    }

    public List<ProjectImportStatus> getProjectImportByImportId(Integer importId) {
        LambdaQueryWrapper<ProjectImportStatus> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProjectImportStatus::getImportId, importId);
        queryWrapper.orderByAsc(ProjectImportStatus::getCreateTime);
        return this.list(queryWrapper);
    }

    public void deleteByImportId(Integer importId) {
        this.remove(Wrappers.<ProjectImportStatus>lambdaQuery().eq(ProjectImportStatus::getImportId, importId));
    }
}
