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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.dto.InspectionDto;
import com.tuoheng.admin.entity.Inspection;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.code.inspection.QueryInspectionListServiceEnum;
import com.tuoheng.admin.mapper.InspectionFileMapper;
import com.tuoheng.admin.mapper.InspectionMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.SecurityUserUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author ChengWang
 * @Date 2022/11/29
 */
@Service
@Slf4j
public class QueryInspectionListService {

    @Autowired
    private InspectionMapper inspectionMapper;

    @Autowired
    private InspectionFileMapper inspectionFileMapper;

    @Autowired
    private UserMapper userMapper;

    public JsonResult getListByAirportId(InspectionDto dto) {
//        if(null == dto.getAirportId() && StringUtils.isEmpty(dto.getUserId())){
//            return JsonResult.error(QueryInspectionListServiceEnum.QUERY_IS_FAILED.getCode(),QueryInspectionListServiceEnum.QUERY_IS_FAILED.getMsg());
//        }
        String username = SecurityUserUtils.username();
        if(StringUtils.isEmpty(username)){
            return JsonResult.error(QueryInspectionListServiceEnum.QUERY_IS_FAILED.getCode(),QueryInspectionListServiceEnum.QUERY_IS_FAILED.getMsg());
        }
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getMark, MarkEnum.VALID.getCode()).eq(User::getStatus,1)
                .eq(User::getUsername,username));

        if(ObjectUtil.isNull(user)){
            return JsonResult.error(QueryInspectionListServiceEnum.USER_IS_NOT_EXIST.getCode(),QueryInspectionListServiceEnum.USER_IS_NOT_EXIST.getMsg());
        }
        String deptId = user.getDeptId();
        if(StringUtils.isEmpty(deptId)){
            return JsonResult.error(QueryInspectionListServiceEnum.DEPT_ID_IS_NULL.getCode(),QueryInspectionListServiceEnum.DEPT_ID_IS_NULL.getMsg());
        }
        List<Inspection> inspections = inspectionMapper.selectList(new LambdaQueryWrapper<Inspection>()
                .eq(Inspection::getMark, MarkEnum.VALID.getCode())
                .eq(Inspection::getTenantId, CurrentUserUtil.getTenantId())
                .eq(Inspection::getDeptId, deptId)
                .eq(Inspection::getAirportId, dto.getAirportId())
                .in(Inspection::getStatus, 10, 15)
                .orderByDesc(Inspection::getCreateTime));
        return JsonResult.success(inspections);
    }
}
