package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.utils.DateUtils;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.BasDevice;
import cn.cecep.talroad.mapper.BasDeviceMapper;
import cn.cecep.talroad.service.analysis.IBasDeviceStatusService;
import cn.cecep.talroad.service.strategy.StrategyFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

//import cn.cecep.talroad.util.MsgProducer;

/**
 * @Auther: cjh
 * @Date: 2023-03-31 16:10
 * @Class: BasDeviceStatusServiceImpl
 * @Deseription:
 * @Version V1.0
 */
@Slf4j
@Service
public class BasDeviceStatusServiceImpl implements IBasDeviceStatusService {

    @Autowired
    private BasDeviceMapper deviceMapper;
    @Autowired
    private StrategyFactory strategyFactory;
//    @Autowired
//    private MsgProducer msgProducer;

    @Override
    public void doCheckDeviceStatus(String industry, String deviceType) {
        long start = System.currentTimeMillis();
        Date curr = DateUtils.addMinutes(new Date(),-10);
        String strategyName =  StringUtils.upperCase(industry + deviceType);
        strategyFactory.getByName(strategyName).doCheckProcess(industry,deviceType,curr);
        long end = System.currentTimeMillis();
        log.info("行业："+industry+",设备类型："+deviceType+",耗时："+(end-start)+"毫秒");
    }

    @Override
    public void doCheckCount(String industry, String deviceType) {
        // 查询未停用设备
        QueryWrapper<BasDevice> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.select("device_id");
        deviceQueryWrapper.eq("industry", industry);
        deviceQueryWrapper.eq("type", deviceType);
        deviceQueryWrapper.eq("is_status", "1");
        List<BasDevice> deviceList = deviceMapper.selectList(deviceQueryWrapper);
        if (deviceList != null && deviceList.size() > 0) {
            // 查询设备类型的参数配置
            deviceList.forEach(d -> {
//                msgProducer.sendYieldStatisticMsg(UUID.randomUUID().toString(),d.getDeviceId());
            });
        }
    }

    @Override
    public void doCheckCountTemp(String factoryId, String industry, String deviceType,String startTime) {

        //拼接策略服务名称
        String strategyName = StringUtils.upperCase(industry + deviceType + deviceType);
        String begin = "";
        String end = "";

        if (startTime.contains("#")) {
            String[] arr = startTime.split("#");
            begin = arr[0];
            end = arr[1];

            List<String> hourList = DateUtils.getRangeTimes("H", begin, end);
            hourList.forEach(h -> {
                try {
                    //执行策略，处理设备状态
                    strategyFactory.getByName(strategyName).doCheckProcessTemp(factoryId,industry,deviceType,DateUtils.parseDate(h, "yyyy-MM-dd HH"));
                } catch (ParseException e) {
                    log.error("日期转换异常");
                }
            });
        }

        if (startTime.contains("&")) {
            String stName = StringUtils.upperCase(industry + deviceType);
            String[] arr = startTime.split("&");
            begin = arr[0];
            end = arr[1];

            List<String> hourList = DateUtils.getRangeTenTimes(begin, end);
            hourList.forEach(h -> {
                try {
                    log.info("时间:"+h+"策略名称:"+stName);
                    //执行策略，处理设备状态
                    strategyFactory.getByName(stName).doCheckProcess(industry,deviceType,DateUtils.parseDate(h, "yyyy-MM-dd HH:mm"));
                } catch (ParseException e) {
                    log.error("日期转换异常");
                }
            });
        }
    }

}
