package com.zcloud.fire.check.command;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.domain.enums.CommonFlagEnum;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordGateway;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordHiddenGateway;
import com.zcloud.fire.check.domain.gateway.FireCheckRecordUserGateway;
import com.zcloud.fire.check.domain.model.FireCheckRecordE;
import com.zcloud.fire.check.domain.model.FireCheckRecordHiddenE;
import com.zcloud.fire.check.domain.model.FireCheckRecordUserE;
import com.zcloud.fire.check.dto.AppCheckRecordAddCmd;
import com.zcloud.fire.check.dto.CorpStatisticsPageQry;
import com.zcloud.fire.check.dto.FireCheckRecordAddCmd;
import com.zcloud.fire.check.dto.FireCheckRecordUserAddCmd;
import com.zcloud.fire.check.dto.clientobject.CorpStatisticsCO;
import com.zcloud.fire.check.dto.clientobject.FireCheckRecordCO;
import com.zcloud.fire.check.dto.clientobject.FireCheckRecordPointCO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckListDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordHiddenDO;
import com.zcloud.fire.check.persistence.repository.FireCheckListRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordHiddenRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordRepository;
import com.zcloud.gbscommon.todolistmq.TodoListEventPusherUtil;
import com.zcloud.gbscommon.todolistmq.event.TodoListCompleteEvent;
import com.zcloud.gbscommon.zcloudhidden.face.ZcloudHiddenFacade;
import com.zcloud.gbscommon.zcloudhidden.request.HiddenAddCmd;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-12-12 11:10:58
 */
@Component
@AllArgsConstructor
@Slf4j
public class FireCheckRecordAddExe {
    private final FireCheckRecordGateway fireCheckRecordGateway;
    private final FireCheckRecordRepository fireCheckRecordRepository;
    private final FireCheckRecordUserGateway checkRecordUserGateway;
    private final FireCheckRecordHiddenGateway fireCheckRecordHiddenGateway;
    private final FireCheckRecordHiddenRepository checkRecordHiddenRepository;
    private final FireCheckRecordHiddenRepository fireCheckRecordHiddenRepository;
    @DubboReference
    private final ZcloudHiddenFacade zcloudHiddenFacade;
    private final TodoListEventPusherUtil todoListEventPusherUtil;
    private final FireCheckListRepository fireCheckListRepository;

    @Transactional(rollbackFor = Exception.class)
    public boolean execute(FireCheckRecordAddCmd cmd) {
        FireCheckRecordE fireCheckRecordE = new FireCheckRecordE();
        BeanUtils.copyProperties(cmd, fireCheckRecordE);
        boolean res = false;
        try {
            res = fireCheckRecordGateway.add(fireCheckRecordE);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("保存失败");
        }
        return true;
    }
    @Transactional(rollbackFor = Exception.class)
    public FireCheckRecordCO saveCheckRecord(AppCheckRecordAddCmd appCheckRecordAddCmd) {
        // 检查点位信息
        FireCheckRecordDO fireCheckRecordDO = fireCheckRecordRepository.getCheckRecordInfoByFireRecordId(appCheckRecordAddCmd.getFireRecordId());
        if (fireCheckRecordDO == null) {
            throw new BizException("检查记录不存在");
        }
        FireCheckRecordE fireCheckRecordE = new FireCheckRecordE();
        BeanUtils.copyProperties(appCheckRecordAddCmd, fireCheckRecordE);
        fireCheckRecordE.setId(fireCheckRecordDO.getId());
        fireCheckRecordE.setFinishedFlag(CommonFlagEnum.YES.getCode());
        Boolean update = fireCheckRecordGateway.update(fireCheckRecordE);
        if (!update) {
            throw new BizException("修改失败");
        }
        //先删除后新增，打回重复提交的
        fireCheckRecordE.setFireCheckId(fireCheckRecordDO.getFireCheckId());
        checkRecordUserGateway.deletedByCheckRecordId(appCheckRecordAddCmd.getFireRecordId());
        appCheckRecordAddCmd.getFireCheckRecordUserAddCmdList().forEach(info -> {
            FireCheckRecordUserE checkRecordUserE = new FireCheckRecordUserE();
            BeanUtils.copyProperties(info, checkRecordUserE);
            checkRecordUserE.init(fireCheckRecordE);
            checkRecordUserGateway.add(checkRecordUserE);
        });
        // 查找隐患，存在更新或删除，不存在新增
        List<FireCheckRecordHiddenDO> fireCheckRecordHiddenDOList = checkRecordHiddenRepository.getByOtherCheckRecordId(appCheckRecordAddCmd.getFireRecordId());
        if(CollUtil.isNotEmpty(fireCheckRecordHiddenDOList)){
            fireCheckRecordHiddenDOList.forEach(info->{
                checkRecordHiddenRepository.deletedFireCheckRecordHiddenById(info);
            });
        }

        //新增隐患信息,隐患需要判断是新增还是修改
        if(CollUtil.isNotEmpty(appCheckRecordAddCmd.getHiddenAddCmdList())){
            appCheckRecordAddCmd.getHiddenAddCmdList().forEach(info -> {
                FireCheckRecordHiddenE fireCheckRecordHiddenE = new FireCheckRecordHiddenE();
                fireCheckRecordHiddenE.initOtherFireCheckRecordHidden(fireCheckRecordE, info);
                fireCheckRecordHiddenGateway.addOtherHidden(fireCheckRecordHiddenE,info);
            });
        }


        fireCheckRecordHiddenRepository.updateHiddenStatus(fireCheckRecordE.getFireRecordId(), CollUtil.isNotEmpty(appCheckRecordAddCmd.getHiddenAddCmdList()));


        FireCheckRecordCO fireCheckRecordCO = new FireCheckRecordCO();
        BeanUtils.copyProperties(fireCheckRecordDO, fireCheckRecordCO);
        try{
            ;
           FireCheckListDO fireCheckListDO = fireCheckListRepository.getInfoByUUId(fireCheckRecordDO.getFireCheckId());
            TodoListCompleteEvent event1 = new TodoListCompleteEvent();
            event1.setForeignSubsidiaryKey(fireCheckListDO.getId());// 业务附表ID 没有附表时为foreignKey的值
            log.info("完成待办,请求：{}",fireCheckListDO.getId());
            boolean b = todoListEventPusherUtil.sendMessageCompleteEvent(event1);
            log.info("完成待办,结果：{}",b);
        }catch (Exception e){
            log.error("发送待办消息失败",e);
        }
        return fireCheckRecordCO;
    }


}

