package com.fizz.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fizz.business.domain.ProStoppage;
import com.fizz.business.form.ProStoppageForm;
import com.fizz.business.mapper.ProStoppageMapper;
import com.fizz.business.service.ProStoppageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ProStoppageServiceImpl extends ServiceImpl<ProStoppageMapper, ProStoppage> implements ProStoppageService {

    @Override
    public List<ProStoppage> listAll(ProStoppageForm form) {
        QueryWrapper<ProStoppage> queryWrapper = new QueryWrapper<>();

        queryWrapper.ge("end_date", form.getStartDate() + " 00:00:00")
                .le("end_date", form.getEndDate() + " 23:59:59")
                .ge("duration", 300)
                .orderByDesc("end_date");

        List<ProStoppage> list = baseMapper.selectList(queryWrapper);
        list.forEach(item->item.setDuration(item.getDuration().divide(BigDecimal.valueOf(60),2)));
        return list;
    }

    @Override
    public boolean updateProStoppage(ProStoppage proStoppage) {
        return  this.updateById(proStoppage);
    }

    @Override
    public boolean addProStoppage(ProStoppage proStoppage) {
        return this.save(proStoppage);
    }

    @Override
    public boolean deleteProStoppage(Long stopid) {
        return this.deleteProStoppage(stopid);
    }

    @Override
    public List<Object> calc(ProStoppageForm form) {
        // 构建查询条件，筛选指定时间范围内的停机记录
        QueryWrapper<ProStoppage> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("start_date", form.getStartDate() + " 00:00:00")
                .le("end_date", form.getEndDate() + " 23:59:59");

        // 查询符合条件的停机记录列表
        List<ProStoppage> stoppageList = baseMapper.selectList(queryWrapper);

        // 计算停机次数
        int stopCount = stoppageList.size();

        // 计算停机总时长（单位：小时，保留两位小数）
        BigDecimal totalStopDuration = BigDecimal.ZERO;
        for (ProStoppage stoppage : stoppageList) {
            totalStopDuration = totalStopDuration.add(stoppage.getDuration().divide(BigDecimal.valueOf(3600), 2, BigDecimal.ROUND_HALF_UP));
        }

        // 计算总时长（查询时间范围的时长，单位：小时）
        long totalTimeMillis = DateUtil.parse(form.getEndDate() + " 23:59:59").getTime() - DateUtil.parse(form.getStartDate() + " 00:00:00").getTime();
        BigDecimal totalDuration = BigDecimal.valueOf(totalTimeMillis).divide(BigDecimal.valueOf(3600 * 1000), 2, BigDecimal.ROUND_HALF_UP);

        // 计算作业率（保留两位小数，百分比形式）
        BigDecimal operationRate = BigDecimal.ONE.subtract(totalStopDuration.divide(totalDuration, 4, BigDecimal.ROUND_HALF_UP))
                .multiply(BigDecimal.valueOf(100)).setScale(2, BigDecimal.ROUND_HALF_UP);

        // 创建List来承载结果
        List<Object> resultList = new ArrayList<>();
        resultList.add(stopCount);
        resultList.add(totalStopDuration);
        resultList.add(operationRate);

        return resultList;
    }
}
