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

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.equip.mapper.ResourceStatusReceiveMapper;
import com.foreverwin.mesnac.equip.model.ResourceStatusReceive;
import com.foreverwin.mesnac.equip.service.ResourceStatusReceiveService;
import com.foreverwin.mesnac.meapi.model.Resrce;
import com.foreverwin.mesnac.meapi.service.ResrceService;
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.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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.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 {

    private static Logger logger = LoggerFactory.getLogger(ResourceFaultReceiveServiceImpl.class);
    @Autowired
    private ResourceFaultReceiveService resourceFaultReceiveService;
    @Autowired
    private ResrceService resrceService;
    @Autowired
    private ResourceStatusReceiveService resourceStatusReceiveService;
    @Autowired
    private ResourceStatusReceiveMapper resourceStatusReceiveMapper;

    @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 void parseResourceFaultInfo(String text) {
        String user = CommonMethods.getUser();
        JSONObject jsonObject = JSONObject.parseObject(text);

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

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

        String faultCode = jsonObject.getString("FAULT_CODE");
        String description = jsonObject.getString("ALARM_CONTENT");

        String dateTime = jsonObject.getString("DATE_TIME");
        LocalDateTime faultDate = LocalDateTime.parse(dateTime, dataFormatter);

        if (StringUtil.isBlank(handle)) {
            throw BusinessException.build("唯一标识不能为空！");
        }
        if (StringUtil.isBlank(site)) {
            throw BusinessException.build("站点不能为空！");
        }
        if (StringUtil.isBlank(resource)) {
            throw BusinessException.build("设备编号不能为空！");
        }
        String resourceHandle = HandleEnum.RESOURCE.getHandle(site, resource);
        Resrce resourceById = resrceService.getById(resourceHandle);
        if (resourceById == null){
            throw BusinessException.build("未找到设备编号为" + resource + "的设备！");
        }

        ResourceFaultReceive resourceFaultReceive = new ResourceFaultReceive();
        // 设备编号、站点、创建时间-使用SEND_TIME、创建用户
        resourceFaultReceive.setResrce(resource);
        resourceFaultReceive.setSite(site);
        resourceFaultReceive.setCreatedDateTime(sendDate);
        resourceFaultReceive.setCreateUser(user);

        resourceFaultReceive.setFaultCode(faultCode);
        resourceFaultReceive.setDescription(description);
        resourceFaultReceive.setFaultDate(faultDate);
        resourceFaultReceive.setHandle(UUID.randomUUID().toString());

        resourceFaultReceiveService.save(resourceFaultReceive);

        /** 更新设备状态表的设备状态
        ResourceStatusReceive resourceStatus = resourceStatusReceiveMapper.getOneByMaxCreateDateTime(site,resource);
        if (resourceStatus != null){
            resourceStatus.setStatus("WARN");
            resourceStatus.setCreateUser(user);
            resourceStatus.setCreatedDateTime(resourceStatus.getModifiedDateTime());
            resourceStatus.setModifiedDateTime(faultDate);
            resourceStatus.setModifyUser(user);
            resourceStatusReceiveMapper.updateById(resourceStatus);
        }else{
            // 数据库录入新数据
            ResourceStatusReceive resourceStatusReceive = new ResourceStatusReceive();
            resourceStatusReceive.setHandle(UUID.randomUUID().toString());
            resourceStatusReceive.setSite(site);
            resourceStatusReceive.setResrce(resource);
            resourceStatusReceive.setStatus("WARN");
            resourceStatusReceive.setCreateUser(user);
            resourceStatusReceive.setCreatedDateTime(faultDate);
            resourceStatusReceive.setModifyUser(user);
            resourceStatusReceive.setModifiedDateTime(faultDate);
            resourceStatusReceiveMapper.insert(resourceStatusReceive);
            logger.info("设备编号：" + resource + "，为设备状态表记录第一笔数据！");
        }*/

    }


}