package com.ruoyi.gysl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.entity.WysmxInformation;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.entity.response.WysmxDetail.WysmxDetail;
import com.ruoyi.gysl.entity.response.WysmxResponse;
import com.ruoyi.gysl.mapper.WysmxInformationMapper;
import com.ruoyi.gysl.service.WysmxInformationService;
import org.springframework.stereotype.Service;

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

/**
 * 五要素模型信息(WysmxInformation)表服务实现类
 *
 * @author makejava
 * @since 2025-02-24 14:29:58
 */
@Service("wysmxInformationService")
public class WysmxInformationServiceImpl extends ServiceImpl<WysmxInformationMapper, WysmxInformation> implements WysmxInformationService {

    /**
     * 判断是否允许上楼
     *
     * @param materialLevel           危化品等级
     * @param fireRiskLevel           生产火灾危险等级
     * @param environmentalProtection 环境保护是否达标
     * @return
     */
    public boolean canGoUpstairs(String materialLevel,
                                 String fireRiskLevel,
                                 String environmentalProtection) {
        // 检查危化品等级：一级或二级禁止上楼
        boolean isHazardousForbidden = "一级".equals(materialLevel) || "二级".equals(materialLevel);
        // 检查火灾危险等级：甲类或乙类禁止上楼
        boolean isFireRiskForbidden = "甲".equals(fireRiskLevel) || "乙".equals(fireRiskLevel);
        // 环境保护未达标禁止上楼
        boolean isEnvironmentForbidden = "否".equals(environmentalProtection);
        return isHazardousForbidden || isFireRiskForbidden || isEnvironmentForbidden;
    }




    /**
     * 根据项目id查询该项目的五要素模型信息
     *
     * @param zwIdPageReq 查询实体
     * @return 所有数据
     */
    @Override
    public List<WysmxResponse> list(ZwIdPageReq zwIdPageReq) {
        List<WysmxInformation> allList = baseMapper.list(zwIdPageReq);
        return getWysFinal(allList);
    }

    /**
     * 通过逻辑获取最终五要素数据
     */
    @Override
    public List<WysmxResponse> getWysFinal(List<WysmxInformation> a1) {
        Map<String, WysmxResponse> groupedMap =
                a1.stream().collect(Collectors.groupingBy(WysmxInformation::getYsmc,
                        Collectors.collectingAndThen(Collectors.toList(), list -> {
                            WysmxResponse data = new WysmxResponse();
                            data.setId(list.get(0).getId());
                            data.setXmId(list.get(0).getXmId());
                            data.setYsmc(list.get(0).getYsmc());
                            List<WysmxDetail> itemList = list.stream().map(e -> {
                                WysmxDetail item = new WysmxDetail();
                                item.setZdname(e.getZdname());
                                item.setId(e.getId());
                                item.setZdinfor(e.getZdinfor());
                                return item;
                            }).collect(Collectors.toList());
                            data.setList(itemList);
                            return data;
                        })));
        return groupedMap.values().stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
}

