package com.tuoheng.admin.service.cycle.update;

import cn.hutool.core.collection.CollectionUtil;
import com.tuoheng.admin.entity.InspectionCycle;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.mapper.InspectionCycleMapper;
import com.tuoheng.admin.request.cycle.EditInspectionCycleRequest;
import com.tuoheng.admin.service.third.airport.GetAirportListByAirportIds;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.AirportInfoVo;
import com.tuoheng.common.core.utils.DateUtils;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class UpdateInspectionCycleService {

    @Autowired
    private InspectionCycleMapper inspectionCycleMapper;

    @Autowired
    private EditInspectionCycleCheckParamService editInspectionCycleCheckParamService;

    @Autowired
    private GetAirportListByAirportIds getAirportListByAirportIds;

    public JsonResult edit(EditInspectionCycleRequest request) {
        JsonResult result = editInspectionCycleCheckParamService.check(request);
        if (0 != result.getCode()) {
            return result;
        }
        User user = CurrentUserUtil.getUserInfo();
        //编辑周期性任务
        InspectionCycle inspectionCycle = inspectionCycleMapper.selectById(request.getId());
        BeanUtils.copyProperties(request,inspectionCycle);
        //无人机id
        getDroneId(request.getAirportId(),inspectionCycle);
        //任务code
        inspectionCycle.setCycleTime(DateUtils.dateTime("HH:mm:ss",request.getCycleTime()));
        //inspectionCycle.setCycleNextTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",request.getCycleNextTime()));
        inspectionCycle.setCycleStartTime(DateUtils.dateTime("yyyy-MM-dd",request.getCycleStartTime()));
        inspectionCycle.setCycleEndTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",request.getCycleEndTime()+" 23:59:59"));
        inspectionCycle.setUpdateTime(DateUtils.now());
        inspectionCycle.setUpdateUser(user.getId());
        int count = inspectionCycleMapper.updateById(inspectionCycle);
        if(count <= 0){
            return JsonResult.error("任务编辑失败");
        }

        return JsonResult.success();
    }

    /**
     * 根据机场id获取无人机id
     * @param airportId
     * @return
     */
    private void getDroneId(Integer airportId,InspectionCycle inspectionCycle) {
        List<Integer> airportIdList = new ArrayList<>(1);
        airportIdList.add(airportId);
        String airportIds = airportIdList.stream().map(x -> String.valueOf(x)).collect(Collectors.joining(",", "", ""));
        List<AirportInfoVo> airportInfoVos = getAirportListByAirportIds.getAirportInfo(airportIds);
        if(CollectionUtil.isEmpty(airportInfoVos)  && airportInfoVos.size() == 0){
            log.info("机场对应的无人机id为空,airportInfoVos:{}",airportInfoVos);
            return;
        }
        if(airportInfoVos.get(0).getDroneId() != null){
            inspectionCycle.setDroneId(airportInfoVos.get(0).getDroneId());
        }

    }

}
