package com.njcn.prepare.harmonic.service.mysql.Impl.device;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.jeffreyning.mybatisplus.service.MppServiceImpl;
import com.njcn.prepare.harmonic.mapper.mysql.line.RMpMonitorEvaluateDMapper;
import com.njcn.prepare.harmonic.pojo.mysql.RMpMonitorEvaluateDPO;
import com.njcn.prepare.harmonic.pojo.param.DeviceParam;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.system.enums.DicDataEnum;
import com.njcn.system.pojo.po.DictData;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.njcn.prepare.harmonic.mapper.mysql.device.RStatDevSignalDPOMapper;
import com.njcn.prepare.harmonic.pojo.po.RStatDevSignalDPO;
import com.njcn.prepare.harmonic.service.mysql.device.RStatDevSignalDPOService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * Description:
 * Date: 2023/5/4  14:25【需求编号】
 *


 * @author clam
 * @version V1.0.0
 */
@Service
@RequiredArgsConstructor
public class RStatDevSignalDPOServiceImpl extends MppServiceImpl<RStatDevSignalDPOMapper,RStatDevSignalDPO > implements RStatDevSignalDPOService{

    private final RMpMonitorEvaluateDMapper revaluateDMapper;
    private final
    DicDataFeignClient dicDataFeignClient;
    @Override
    @Transactional(rollbackFor = {Exception.class})
    @Async("asyncExecutor")
    @SneakyThrows
    public void eventdetailHandler(DeviceParam deviceParam) {
        List<RStatDevSignalDPO> rStatDevSignalDPOList = new ArrayList<>();
        Date date = DateUtil.parse(deviceParam.getDataDate());
        QueryWrapper<RMpMonitorEvaluateDPO> wrapper = new QueryWrapper<>();
        DictData data = dicDataFeignClient.getDicDataByCode(DicDataEnum.RUNNING.getCode()).getData();
        deviceParam.getDetailList().forEach(temp -> {

            RStatDevSignalDPO rStatDevSignalDPO = new RStatDevSignalDPO();
            rStatDevSignalDPO.setDevId(temp.getDevId());
            rStatDevSignalDPO.setDeviceStatus(data.getId());
            rStatDevSignalDPO.setUpdateTime(temp.getUpdateTime());
            rStatDevSignalDPO.setDataDate(date);
            /**
             * 1、运行状态：设备下任意一个监测点有效接入分钟数（effective_minute_count > 0）为在线状态，反之离线状态
             */
            Integer effective_minute_count = 0;
            List<String> lineIds = temp.getLineIds();
            if(lineIds.size() > 0){
                wrapper.clear();
                wrapper.select("sum(effective_minute_count) as effective_minute_count ");
                wrapper.eq("device_id", temp.getDevId());
                wrapper.in("measurement_point_id", lineIds);
                RMpMonitorEvaluateDPO rMpMonitorEvaluateDPO = revaluateDMapper.selectOne(wrapper);
                effective_minute_count = rMpMonitorEvaluateDPO.getEffectiveMinuteCount();
            }
            String runStatus = "0";
            if(effective_minute_count>0&&temp.getDeviceStatus().equals("0")){
                runStatus = "1";
            }
            rStatDevSignalDPO.setRunStatus(runStatus);
            rStatDevSignalDPOList.add(rStatDevSignalDPO);

        });
        this.saveOrUpdateBatchByMultiId(rStatDevSignalDPOList,500);
    }
}
