package com.yeejoin.amos.boot.module.jg.biz.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.module.jg.api.entity.SafetyProblemTracing;
import com.yeejoin.amos.boot.module.jg.api.enums.SafetyProblemSourceTypeEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.SafetyProblemStatusEnum;
import com.yeejoin.amos.boot.module.jg.api.enums.SafetyProblemTypeEnum;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.SafetyProblemTracingServiceImpl;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.StartPlatformTokenService;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.component.emq.EmqKeeper;
import org.typroject.tyboot.component.emq.EmqxListener;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class SafetyProblemTopicMessage extends EmqxListener {

    @Resource
    protected EmqKeeper emqKeeper;

    @Value("${safety.problem.tracing.topic:safetyProblemTracing/wb/cq}")
    private String problemEventTopic;

    @PostConstruct
    void init() throws Exception {
        emqKeeper.subscript(problemEventTopic, 2, this);
    }

    @Autowired
    RedisUtils redisUtil;

    @Autowired
    StartPlatformTokenService platformTokenService;

    @Autowired
    SafetyProblemTracingServiceImpl safetyProblemTracingService;

    @Override
    public void processMessage(String topic, MqttMessage message) {
        // TODO 提出单独模块处理生成问题
        log.info("接收问题生产消息开始");
        platformTokenService.getToken();
        JSONObject jsonObject = JSON.parseObject(message.toString());
        if (topic.endsWith(SafetyProblemTypeEnum.WBCQ.getTopic())) {
            handleMaintenanceProblem(jsonObject);
        }
        log.info("接收问题生产消息完成");
    }

    private void handleMaintenanceProblem(JSONObject jsonObject) {
        JSONArray maintenanceRecords = jsonObject.getJSONArray("outOfMaintenanceRecords");
        List<SafetyProblemTracing> safetyProblemTracingList = Lists.newArrayList();
        maintenanceRecords.forEach(item -> {
            JSONObject json = (JSONObject) item;
            SafetyProblemTracing safetyProblemTracing = new SafetyProblemTracing();
//            safetyProblemTracing.setProblemNum(json.getString("problemNum"));
            safetyProblemTracing.setProblemType(SafetyProblemTypeEnum.WBCQ.getName());
            safetyProblemTracing.setProblemTypeCode(SafetyProblemTypeEnum.WBCQ.getCode());
            safetyProblemTracing.setProblemDesc(SafetyProblemTypeEnum.WBCQ.getDesc());
//            safetyProblemTracing.setProblemLevel(json.getString("problemLevel"));
//            safetyProblemTracing.setProblemLevelCode(json.getString("problemLevelCode"));
            safetyProblemTracing.setSourceType(SafetyProblemSourceTypeEnum.EQUIP.getName());
            safetyProblemTracing.setSourceTypeCode(SafetyProblemSourceTypeEnum.EQUIP.getCode());
            safetyProblemTracing.setSourceId(json.getString("RECORD"));
            safetyProblemTracing.setProblemTime(new Date());
            safetyProblemTracing.setEquipSuperviseCode(json.getString("supervisoryCode"));
            safetyProblemTracing.setEquipList(json.getString("equList"));
            safetyProblemTracing.setEquipListCode(json.getString("equListCode"));
            safetyProblemTracing.setPrincipalUnit(json.getString("useUnitName"));
            safetyProblemTracing.setPrincipalUnitCode(json.getString("useUnitCreditCode"));
            safetyProblemTracing.setPrincipalUnitType(json.getString("unitType"));
            safetyProblemTracing.setGoverningBody(json.getString("orgBranchName"));
            safetyProblemTracing.setGoverningBodyCode(json.getString("companyOrgBranchCode"));
            safetyProblemTracing.setGoverningBodyOrgCode(json.getString("orgBranchCode"));
//            safetyProblemTracing.setRegionName(json.getString("regionName"));
            safetyProblemTracing.setRegionCode(json.getString("regionCode"));
            safetyProblemTracing.setCreateDate(new Date());
//            safetyProblemTracing.setCreateUser(json.getString("createUser"));
//            safetyProblemTracing.setCreateUserId(json.getString("createUserId"));
            safetyProblemTracing.setProblemStatus(SafetyProblemStatusEnum.UNHANDLED.getName());
            safetyProblemTracing.setProblemStatusCode(SafetyProblemStatusEnum.UNHANDLED.getCode());
            safetyProblemTracingList.add(safetyProblemTracing);
        });
        safetyProblemTracingService.saveBatch(safetyProblemTracingList);
    }
}
