package com.yuchen.saas.park.service.service.door.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuchen.saas.park.api.dto.door.DoorDeviceEventDTO;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.door.DoorDeviceEvent;
import com.yuchen.saas.park.api.entity.door.DoorDevicePoint;
import com.yuchen.saas.park.api.vo.door.DoorEnentOutVO;
import com.yuchen.saas.park.api.vo.door.DoorEventInfoVO;
import com.yuchen.saas.park.service.mapper.door.DoorDeviceEventMapper;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.service.door.DoorDeviceEventService;
import com.yuchen.saas.park.service.service.door.DoorDevicePointService;
import com.yuchen.saas.payment.api.util.PageConverterUtils;
import lombok.extern.slf4j.Slf4j;
import org.nest.springwrap.core.mp.base.BaseServiceImpl;
import org.nest.springwrap.core.mp.support.NestPage;
import org.nest.springwrap.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 设备事件 服务实现类
 * </p>
 *
 * @author miaoqs
 * @since 2023-12-20
 */
@Slf4j
@Service
public class DoorDeviceEventServiceImpl extends BaseServiceImpl<DoorDeviceEventMapper, DoorDeviceEvent> implements DoorDeviceEventService {

    @Resource
    private DoorDeviceEventMapper deviceEventMapper;
    @Resource
    private PmgBatSmartEquipmentService smartEquipmentService;
    @Resource
    private DoorDevicePointService doorDevicePointService;

    @Override
    public NestPage<DoorDeviceEvent> paramPage(Page<DoorDeviceEvent> page, DoorDeviceEventDTO req) {
        Page<DoorDeviceEvent> doorUserInOutEventPage = deviceEventMapper.paramParam(page, req);
        return PageConverterUtils.convert(doorUserInOutEventPage);
    }

    @Override
    public boolean receiveIotDoorEventInfo(String doorEventInfoJson) {
        log.info("====== 接收到设备事件信息 doorEventInfoJson: {}" , doorEventInfoJson);
        if(Func.isEmpty(doorEventInfoJson)){
            log.error("====== 接收到设备事件信息为空");
            return false;
        }

        JSONObject jsonObject = JSON.parseObject(doorEventInfoJson);
        String type = jsonObject.getString("type");
        log.info("====== type: " + type);
        //设备事件
        if(type.equals("event")){
            return saveDoorEvent(jsonObject);
        }
        return false;
    }

    /**
     * 保存设备事件
     * @param jsonObject
     * @return
     */
    private boolean saveDoorEvent(JSONObject jsonObject) {
        String contentStr = jsonObject.getString("content");
        DoorEventInfoVO doorEventInfoVO = JSON.parseObject(contentStr, DoorEventInfoVO.class);

        JSONObject contentJson = JSON.parseObject(contentStr);
        String outStr = contentJson.getString("out");
        DoorEnentOutVO doorEnentOutVO = JSON.parseObject(outStr, DoorEnentOutVO.class);

        DoorDeviceEvent doorDeviceEvent = new DoorDeviceEvent();
        if(Func.isNotEmpty(doorEventInfoVO)){
            String productKey = doorEventInfoVO.getProductKey();
            String deviceName = doorEventInfoVO.getDeviceName();
            PmgBatSmartEquipment equipment = smartEquipmentService.getDetails(productKey, deviceName);
            DoorDevicePoint point = doorDevicePointService.getOne(new LambdaQueryWrapper<DoorDevicePoint>().eq(DoorDevicePoint::getEquipmentId, equipment.getId()));
            if(Func.isNotEmpty(point)){
                doorDeviceEvent.setDevicePointId(point.getId());
            }
            doorDeviceEvent.setDeviceId(equipment.getId());
            doorDeviceEvent.setEventType(doorEventInfoVO.getEventType());
            doorDeviceEvent.setParkId(equipment.getParkId());
            //doorDeviceEvent.setCardReader();
        }
        if (Func.isNotEmpty(doorEnentOutVO)) {
            if(Func.isNotEmpty(doorEnentOutVO.getDateTime())) {
                doorDeviceEvent.setEventTime(Func.parseDate(doorEnentOutVO.getDateTime(), "yyyy-MM-dd HH:mm:ss"));
            }
            //事件主类型是5
            if(Func.isNotEmpty(doorEnentOutVO.getMajorEventType()) && doorEnentOutVO.getMajorEventType().equals("5")) {
                doorDeviceEvent.setEventType(doorEnentOutVO.getSubEventType());
            }
        }
        return save(doorDeviceEvent);
    }

}
