package com.inspect.metadata.mqtt.controller;

import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.montdata.service.IBasedataMontTaskResultService;
import com.inspect.metadata.mqtt.domain.HrDroneBattery;
import com.inspect.metadata.mqtt.service.IHrDroneBatteryService;
import com.inspect.metadata.mqtt.vo.HrLifeDetectionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hr/lifeDetection")
public class HrLifeDetectionController extends BaseController {
    @Autowired
    private IBasedataEqpBookService iBasedataEqpBookService;

    @Autowired
    private IBasedataMontTaskResultService iBasedataMontTaskResultService;

    @Autowired
    private IHrDroneBatteryService droneBatteryService;

    /**
     * 计算两个时间之间的年数差
     */
    public static double calculateYearsDifference(LocalDateTime targetTime, LocalDateTime sourceTime) {
        long daysBetween = ChronoUnit.DAYS.between(targetTime, sourceTime);
        // 计算年数（考虑闰年）
        double years = daysBetween / 365.25;

        // 保留一位小数
        return Math.round(years * 10.0) / 10.0;
    }

    @GetMapping("/list")
    public AjaxResult list() {
        BasedataEqpBook eqpBook = new BasedataEqpBook();
        eqpBook.setAreaName("华软无人机");
        List<BasedataEqpBook> eqpBooks = iBasedataEqpBookService.selectBasedataEqpBookList(eqpBook);
        String[] patrolDeviceCodes = eqpBooks.stream().map(book -> book.getPatrolDeviceCode()).toArray(String[]::new);

        List<HrLifeDetectionVO> detectionList = new ArrayList<>();
        for (BasedataEqpBook book : eqpBooks) {
            HrLifeDetectionVO vo = new HrLifeDetectionVO();
            String patrolDeviceCode = book.getPatrolDeviceCode();
            // 设备名称
            vo.setName(book.getPatrolDeviceName());
            // 设备编码
            vo.setNestCode(patrolDeviceCode);
            // 已使用寿命
            Date createTime = iBasedataMontTaskResultService.selectMinCreateTimeByPatrolDeviceCode(patrolDeviceCode);
            if (createTime != null) {
                LocalDateTime createTimeLocal = createTime.toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDateTime();
                Double useYears = calculateYearsDifference(createTimeLocal, LocalDateTime.now());
                vo.setServiceLife(useYears);
            } else {
                vo.setServiceLife(0.0);
            }
            // 飞行次数
            Integer countTimes = iBasedataMontTaskResultService.countTimesByPatrolDeviceCode(patrolDeviceCode);
            vo.setFlightCount(countTimes);
            HrDroneBattery droneBattery = new HrDroneBattery();
            droneBattery.setNestCode(patrolDeviceCode);
            // 电池充电次数
            HrDroneBattery battery = droneBatteryService.selectDroneBatteryOne(patrolDeviceCode);
            if (battery != null) {
                vo.setChargingTimes(battery.getBatteryNumberOfDisCharges());
            }
            // 剩余使用寿命
            vo.setDroneUsefulLife(vo.getDroneLife() - vo.getServiceLife());
            vo.setNestUsefulLife(vo.getNestLife() - vo.getServiceLife());
            vo.setBatteryUsefulLife(vo.getBatteryLife() - vo.getServiceLife());
            vo.setLoadUsefulLife(vo.getLoadLife() - vo.getServiceLife());
            detectionList.add(vo);
        }
        logger.info("lifeDetection:{}", detectionList);
        return AjaxResult.success(detectionList);
    }
}
