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.FrontPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.foreverwin.mesnac.equip.model.ResourceFaultReceive;
import com.foreverwin.mesnac.equip.mapper.ResourceFaultReceiveMapper;
import com.foreverwin.mesnac.equip.service.ResourceFaultReceiveService;
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-07-13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ResourceFaultReceiveServiceImpl extends ServiceImpl<ResourceFaultReceiveMapper, ResourceFaultReceive> implements ResourceFaultReceiveService {


    @Autowired
    private ResourceFaultReceiveMapper resourceFaultReceiveMapper;
    @Autowired
    private ResourceFaultReceiveService resourceFaultReceiveService;

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

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

    @Override
    public String parseResourceFaultInfo(String text) {
        JSONObject jsonObject = JSONObject.parseObject(text);

        String handle = jsonObject.getString("TRANID");
        String site = jsonObject.getString("SITE");
        String resource = jsonObject.getString("RESOURCE");
        String faultList = jsonObject.getString("FAULT_LIST");

        String sendTime = jsonObject.getString("SEND_TIME");
        DateTimeFormatter dataFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime sendDate = LocalDateTime.parse(sendTime, dataFormatter);

        if (StringUtil.isBlank(handle)) {
            throw BusinessException.build("唯一标识不能为空！");
        }
        if (StringUtil.isBlank(site)) {
            throw BusinessException.build("站点不能为空！");
        }
        if (StringUtil.isBlank(resource)) {
            throw BusinessException.build("设备编号不能为空！");
        }
        List<ResourceFaultReceive> list = new ArrayList<>();
        // 故障编码及故障时间
        JSONArray jsonArray = JSON.parseArray(faultList);
        for (int i = 0; i < jsonArray.size(); i++) {
            ResourceFaultReceive resourceFaultReceive = new ResourceFaultReceive();
            // 设备编号、站点、创建时间-使用SEND_TIME、创建用户
            resourceFaultReceive.setResrce(resource);
            resourceFaultReceive.setSite(site);
            resourceFaultReceive.setCreatedDateTime(sendDate);
            resourceFaultReceive.setCreateUser("SITE_ADMIN");

            JSONObject jsonObj = (JSONObject) jsonArray.get(i);
            String faultCode = jsonObj.getString("FAULT_CODE");
            resourceFaultReceive.setFaultCode(faultCode);
            String dateTime = jsonObj.getString("DATE_TIME");
            LocalDateTime faultDate = LocalDateTime.parse(dateTime, dataFormatter);
            resourceFaultReceive.setFaultDate(faultDate);
            resourceFaultReceive.setHandle(UUID.randomUUID().toString());
            list.add(resourceFaultReceive);
        }
        resourceFaultReceiveService.saveBatch(list);
        return handle;
    }


}