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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yeejoin.amos.boot.module.common.api.dao.ESEquipmentCategory;
import com.yeejoin.amos.boot.module.common.api.dto.ESEquipmentCategoryDto;
import com.yeejoin.amos.boot.module.jg.api.entity.SafetyProblemTracing;
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.api.event.SafetyProblemEvent;
import com.yeejoin.amos.boot.module.jg.api.event.handler.SafetyProblemEventHandler;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.IdxBizJgOtherInfoServiceImpl;
import com.yeejoin.amos.boot.module.jg.biz.service.impl.SafetyProblemTracingServiceImpl;
import com.yeejoin.amos.boot.module.ymt.api.entity.IdxBizJgOtherInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.typroject.tyboot.core.foundation.utils.ValidationUtil;

import java.util.List;
import java.util.stream.Collectors;

/**
 * WBCQEventHandler 类实现了 SafetyProblemEventHandler 接口，
 * 用于处理检验报检安全问题事件。
 */
@Component
public class JYBJEventHandler implements SafetyProblemEventHandler {

    SafetyProblemTracingServiceImpl safetyProblemTracingService;

    IdxBizJgOtherInfoServiceImpl idxBizJgOtherInfoService;

    ESEquipmentCategory esEquipmentCategory;

    @Autowired
    public JYBJEventHandler(SafetyProblemTracingServiceImpl safetyProblemTracingService, IdxBizJgOtherInfoServiceImpl idxBizJgOtherInfoService, ESEquipmentCategory esEquipmentCategory) {
        this.safetyProblemTracingService = safetyProblemTracingService;
        this.idxBizJgOtherInfoService = idxBizJgOtherInfoService;
        this.esEquipmentCategory = esEquipmentCategory;
    }


    /**
     * 处理安全问题事件。
     *
     * @param event 安全问题事件对象，包含事件的详细信息。
     *              该参数用于描述发生的安全问题事件。
     */
    @Override
    public void handle(SafetyProblemEvent event) {
        // 此处为处理安全问题事件的逻辑代码
        JSONArray jsonArray = JSONObject.parseArray(event.getMessage().toString());
        handleInspectionRecord(jsonArray);
    }

    private void handleInspectionRecord(JSONArray jsonArray) {
        List<String> equipRecords = jsonArray.stream().map(obj -> JSONObject.parseObject(obj.toString()).getString("record")).collect(Collectors.toList());
        if (!ValidationUtil.isEmpty(equipRecords)) {
            safetyProblemTracingService.lambdaUpdate()
                    .set(SafetyProblemTracing::getProblemStatusCode, SafetyProblemStatusEnum.HANDLED.getCode())
                    .set(SafetyProblemTracing::getProblemStatus, SafetyProblemStatusEnum.HANDLED.getName())
                    .in(SafetyProblemTracing::getSourceId, equipRecords)
                    .eq(SafetyProblemTracing::getProblemTypeCode, SafetyProblemTypeEnum.JYCQ.getCode()).update();
            idxBizJgOtherInfoService.lambdaUpdate().set(IdxBizJgOtherInfo::getStatus, SafetyProblemStatusEnum.HANDLED.getCode())
                    .in(IdxBizJgOtherInfo::getRecord, equipRecords).update();
            Iterable<ESEquipmentCategoryDto> equipEsDtoIter = esEquipmentCategory.findAllById(equipRecords);
            for (ESEquipmentCategoryDto equipEsDto : equipEsDtoIter) {
                equipEsDto.setProblemStatus(SafetyProblemStatusEnum.HANDLED.getCode());
            }
            esEquipmentCategory.saveAll(equipEsDtoIter);
        }
    }
}

