package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemFireStatisticsAttach;
import com.usky.fire.mapper.DemFireStatisticsAttachMapper;
import com.usky.fire.service.DemFireStatisticsAttachService;
import com.usky.fire.service.vo.FireLevelRatioVO;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 火灾统计表附表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-13
 */
@Service
public class DemFireStatisticsAttachServiceImpl extends AbstractCrudService<DemFireStatisticsAttachMapper, DemFireStatisticsAttach> implements DemFireStatisticsAttachService {

    @Override
    public List<DemFireStatisticsAttach> fireAccidentStatistic(List<String> fireNumberList) {
        LambdaQueryWrapper<DemFireStatisticsAttach> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemFireStatisticsAttach::getFireCauseType1, DemFireStatisticsAttach::getFireSiteType1,
                DemFireStatisticsAttach::getFireNumber, DemFireStatisticsAttach::getId)
                .in(DemFireStatisticsAttach::getFireNumber, fireNumberList);
        List<DemFireStatisticsAttach> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<String> fireCauseType(){
        LambdaQueryWrapper<DemFireStatisticsAttach> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemFireStatisticsAttach::getFireCauseType1)
                .groupBy(DemFireStatisticsAttach::getFireCauseType1);
        List<DemFireStatisticsAttach> list1 = this.list(queryWrapper);
        List<String> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list1)){
            for (int i = 0; i < list1.size(); i++) {
                list.add(list1.get(i).getFireCauseType1());
            }
        }
        return list;
    }

    @Override
    public Integer fireStatisticsAttachCount(String fireCauseType1){
        LambdaQueryWrapper<DemFireStatisticsAttach> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(fireCauseType1),DemFireStatisticsAttach::getFireCauseType1, fireCauseType1);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public List<String> getFireType() {
        List<FireLevelRatioVO> list = new ArrayList<>();
        List<String> fireTypes = this.fireCauseType();
        if (CollectionUtils.isNotEmpty(fireTypes)) {
            for (String cause : fireTypes) {
                FireLevelRatioVO fireLevelRatioVo = new FireLevelRatioVO();
                fireLevelRatioVo.setFireType(cause);
                Integer count = this.fireStatisticsAttachCount(cause);
                fireLevelRatioVo.setRadio(count.doubleValue());
                list.add(fireLevelRatioVo);
            }
        }
        return list.stream().sorted(Comparator.comparing(FireLevelRatioVO::getRadio).reversed())
                .map(FireLevelRatioVO::getFireType).collect(Collectors.toList());
    }
}
