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

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.tuoheng.admin.entity.InspectionCycle;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.mapper.InspectionCycleMapper;
import com.tuoheng.admin.request.cycle.AddInspectionCycleRequest;
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;

@Slf4j
@Component
public class AddInspectionCycleService {

    private final static String CAPITAL_CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    @Autowired
    private InspectionCycleMapper inspectionCycleMapper;

    @Autowired
    private AddInspectionCycleCheckParamService addInspectionCycleCheckParamService;

    @Autowired
    private GetAirportListByAirportIds getAirportListByAirportIds;

    public JsonResult add(AddInspectionCycleRequest request) {
        JsonResult result = addInspectionCycleCheckParamService.check(request);
        if (0 != result.getCode()) {
            return result;
        }
        User user = CurrentUserUtil.getUserInfo();
        String tenantId = user.getTenantId();

        //新增周期性任务
        InspectionCycle inspectionCycle = new InspectionCycle();
        BeanUtils.copyProperties(request, inspectionCycle);
        //无人机id
        getDroneId(request.getAirportId(), inspectionCycle);
        //任务code
        String code = DateUtils.generateCode("GSZQ", DateUtils.YYYYMMDD, RandomUtil.randomString(CAPITAL_CHAR, 3));
        inspectionCycle.setCode(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.setTenantId(tenantId);
        inspectionCycle.setDeptId(user.getDeptId());
        inspectionCycle.setIsEnable(1);
        inspectionCycle.setCreateTime(DateUtils.now());
        inspectionCycle.setCreateUser(user.getId());

        int count = inspectionCycleMapper.insert(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());
        }

    }

}
