package com.zcloud.accident.command.query;

import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.MultiResponse;
import com.zcloud.accident.command.convertor.AccidentCoConvertor;
import com.zcloud.accident.command.convertor.AccidentCorpCountStatConvertor;
import com.zcloud.accident.command.convertor.AccidentCountStatConvertor;
import com.zcloud.accident.domain.gateway.AccidentGateway;
import com.zcloud.accident.domain.model.AccidentCorpCountStat;
import com.zcloud.accident.domain.model.AccidentCountStat;
import com.zcloud.accident.dto.AccidentCountQry;
import com.zcloud.accident.dto.AccidentPageQry;
import com.zcloud.accident.dto.clientobject.AccidentCO;
import com.zcloud.accident.dto.clientobject.AccidentCorpCountStatCO;
import com.zcloud.accident.dto.clientobject.AccidentCountStatCO;
import com.zcloud.accident.persistence.dataobject.AccidentDO;
import com.zcloud.accident.persistence.repository.AccidentRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * web-app
 *
 * @Author fangjiakai
 * @Date 2025-10-29 14:28:45
 */
@Component
@AllArgsConstructor
public class AccidentQueryExe {
    private final AccidentRepository accidentRepository;
    private final AccidentCoConvertor accidentCoConvertor;
    private final AccidentGateway accidentGateway;
    private final AccidentCountStatConvertor accidentCountStatConvertor;
    private final AccidentCorpCountStatConvertor accidentCorpCountStatConvertor;

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public AccidentCO queryById(Long id) {
        return accidentCoConvertor.converDOToCO(accidentRepository.getById(id));
    }
    /**
     * 分页
     *
     * @param accidentPageQry
     * @return
     */
    public PageResponse<AccidentCO> execute(AccidentPageQry accidentPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(accidentPageQry);
        String menuPerms = "";
        if(accidentPageQry.getEqType() == 1){
            menuPerms = "zcloud-event";
        }else{
            menuPerms = "zcloud-accident";
        }
        PageResponse<AccidentDO> pageResponse = accidentRepository.listPage(params,menuPerms);
        List<AccidentCO> examCenterCOS = accidentCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    /**
     * 获取所有符合条件的数据
     *
     * @param accidentPageQry 查询条件
     * @return 数据列表
     */
    public List<AccidentCO> listAll(AccidentPageQry accidentPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(accidentPageQry);
        String menuPerms = "";
        if(accidentPageQry.getEqType() == 1){
            menuPerms = "zcloud-event";
        }else{
            menuPerms = "zcloud-accident";
        }
        List<AccidentDO> accidentDOs = accidentRepository.listAll(params,menuPerms);
        return accidentCoConvertor.converDOsToCOs(accidentDOs);
    }
    /**
     * 按公司统计事件和事故数量
     * @param accidentCountQry 查询条件，包含公司ID列表
     * @return 统计结果列表
     */
    public MultiResponse<AccidentCorpCountStatCO> countByCorpinfo(AccidentCountQry accidentCountQry) {
        List<AccidentCorpCountStat> resultList = accidentGateway.countByCorpinfo(accidentCountQry.getCorpinfoIds());
        List<AccidentCorpCountStatCO> coList = accidentCorpCountStatConvertor.convertToCOList(resultList);
        // 补全无数据的公司，事件和事故数量为0
        if (accidentCountQry.getCorpinfoIds() != null) {
            Set<Long> existIds = new HashSet<>();
            for (AccidentCorpCountStatCO co : coList) {
                existIds.add(co.getCorpinfoId());
            }
            for (String idStr : accidentCountQry.getCorpinfoIds()) {
                Long corpinfoId = Long.valueOf(idStr);
                if (!existIds.contains(corpinfoId)) {
                    AccidentCorpCountStatCO co = new AccidentCorpCountStatCO();
                    co.setCorpinfoId(corpinfoId);
                    co.setEventCount(0L);
                    co.setAccidentCount(0L);
                    coList.add(co);
                }
            }
        }
        return MultiResponse.of(coList);
    }

    /**
     * 按照corpinfoId和incidentType统计个数
     * @param accidentCountQry 查询条件，包含公司ID列表
     * @return 统计结果列表
     */
    public MultiResponse<AccidentCountStatCO> countByCorpinfoIdAndIncidentType(AccidentCountQry accidentCountQry) {
        List<com.zcloud.accident.domain.model.AccidentCountStat> resultList = accidentGateway.countByCorpinfoIdAndIncidentType(accidentCountQry.getCorpinfoIds(),accidentCountQry.getEqAccidentType());
        List<AccidentCountStatCO> coList = accidentCountStatConvertor.convertToCOList(resultList);
        return MultiResponse.of(coList);
    }


}

