package com.foreverwin.mesnac.equip.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.modular.core.exception.BusinessException;
import com.foreverwin.modular.core.util.CommonMethods;
import com.foreverwin.modular.core.util.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.equip.model.ResourceRunTime;
import com.foreverwin.mesnac.equip.mapper.ResourceRunTimeMapper;
import com.foreverwin.mesnac.equip.service.ResourceRunTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pavel.liu
 * @since 2021-09-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceRunTimeServiceImpl extends ServiceImpl<ResourceRunTimeMapper, ResourceRunTime> implements ResourceRunTimeService {

    @Autowired
    private ResourceRunTimeService resourceRunTimeService;

    @Override
    public IPage<ResourceRunTime> selectPage(FrontPage<ResourceRunTime> frontPage, ResourceRunTime resourceRunTime) {
        QueryWrapper<ResourceRunTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceRunTime);
        return super.page(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<ResourceRunTime> selectList(ResourceRunTime resourceRunTime) {
        QueryWrapper<ResourceRunTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(resourceRunTime);
        return super.list(queryWrapper);
    }

    @Override
    public void saveResourceRunTimeByMq(String text) {
        String user = CommonMethods.getUser();
        JSONObject jsonObject = JSONObject.parseObject(text);
        String site = jsonObject.getString("SITE");
        // 数据传输时间
        String sendTime = jsonObject.getString("SEND_TIME");
        DateTimeFormatter dataFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(sendTime,dataFormatter);
        // 设备运行时间的日期
        String sendDate = jsonObject.getString("SENE_DATE");

        if (StringUtil.isBlank(site)) {
            throw BusinessException.build("站点不能为空！");
        }

        String resourceList = jsonObject.getString("RESOURCE_LIST");
        JSONArray jsonArray = JSON.parseArray(resourceList);
        List<ResourceRunTime> list = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            ResourceRunTime resourceRunTime = new ResourceRunTime();
            // 设备编号、站点、创建时间-使用SEND_TIME、创建用户
            resourceRunTime.setSite(site);
            resourceRunTime.setCreatedDataTime(dateTime);
            resourceRunTime.setCreatedUser(user);
            resourceRunTime.setSendDate(sendDate);

            JSONObject jsonObj = (JSONObject) jsonArray.get(i);
            String runTime = jsonObj.getString("RUN_TIME");
            String resource = jsonObj.getString("RESOURCE");
            resourceRunTime.setRunTime(Double.valueOf(runTime));
            resourceRunTime.setResrce(resource);
            String upTime = jsonObj.getString("UP_TIME");
            if (upTime != null && !upTime.equals("")){
                LocalDateTime upDate = LocalDateTime.parse(upTime,dataFormatter);
                resourceRunTime.setUpTime(upDate);
            }
            resourceRunTime.setHandle(UUID.randomUUID().toString());
            list.add(resourceRunTime);
        }
        resourceRunTimeService.saveBatch(list);

    }


}