package com.tuoheng.admin.service.inspection.query;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.domain.Inspection;
import com.tuoheng.admin.entity.domain.User;
import com.tuoheng.admin.enums.InspectionStatusEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ChengWang
 * @Date 2023/5/22
 */
@Slf4j
@Service
public class QueryInspectionListDataService {

    @Autowired
    private InspectionMapper inspectionMapper;

    /**
     * 获取最新的五条数据
     * @return
     */
    public JsonResult inspectionList() {
        User user = CurrentUserUtil.getUserInfo();
        String userId = user.getId();
        String tenantId = user.getTenantId();
        JsonResult jsonResult = this.check(userId,tenantId);
        if(0 != jsonResult.getCode()){
            log.info("进入查询任务列表数据失败：{}",jsonResult.getMsg());
            return JsonResult.error("查询任务数据失败");
        }
        List<Inspection> inspectionList = inspectionMapper.selectList(Wrappers.<Inspection>lambdaQuery()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .eq(Inspection::getTenantId, tenantId)
                .notIn(Inspection::getStatus, InspectionStatusEnum.FLIGHT_FAILED.getCode())
                .orderByAsc(Inspection::getStatus)
                .orderByDesc(Inspection::getCreateTime)
                .last("limit 5"));
        return JsonResult.success(inspectionList);
    }


    private JsonResult check(String userId, String tenantId) {
        return JsonResult.success();
    }
}
