package org.jeecg.modules.zrzyqc.qcsb.service.impl;

import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.SysCategoryModel;
import org.jeecg.modules.zrzyqc.qcsb.entity.QcSsgqzysytdqk;
import org.jeecg.modules.zrzyqc.qcsb.mapper.QcSsgqzysytdqkMapper;
import org.jeecg.modules.zrzyqc.qcsb.service.IQcSsgqzysytdqkService;
import org.jeecg.modules.zrzyqc.qyxx.entity.QcQyxx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 省属国企占有使用土地情况调查
 * @Author: jeecg-boot
 * @Date: 2022-06-29
 * @Version: V1.0
 */
@Service
public class QcSsgqzysytdqkServiceImpl extends ServiceImpl<QcSsgqzysytdqkMapper, QcSsgqzysytdqk> implements IQcSsgqzysytdqkService {
    @Autowired
    private QcSsgqzysytdqkMapper qcSsgqzysytdqkMapper;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Override
    public void upSfsb(String id) {
        qcSsgqzysytdqkMapper.upSfsb(id);
    }

    @Override
    public void upSfsh(String id) {

    }

    @Override
    public void upShjg(String id) {

    }

    @Override
    public QcQyxx selectQyxx(String username, String orCode) {
        return qcSsgqzysytdqkMapper.selectQyxx(username, orCode);
    }

    @Override
    public void insertDkxx(HashMap<String, Object> map) {
        qcSsgqzysytdqkMapper.insertDkxx(map);
    }

    @Override
    public List<Map<String, Double>> sumList(List<QcSsgqzysytdqk> list) {
        List<DictModel> xzqh = sysBaseAPI.getDictItems("xq");
        List<SysCategoryModel> sysCategoryModels = sysBaseAPI.queryAllSysCategory();
        Double num = Double.valueOf(0);
        List<Map<String, Double>> result = new ArrayList<>();
        Map<String, Double> map = new HashMap<>();
        Map<String, Double> sjytMap = new HashMap<>();
        Map<String, Double> syxzMap = new HashMap<>();
        Map<String, Double> wjbhMap = new HashMap<>();
        Map<String, List<QcSsgqzysytdqk>> collect = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqk::getSyqsyfs)));
        for (String s : collect.keySet()) {
            map.put(s, (double) collect.get(s).size());
        }
        result.add(map);

        for (QcSsgqzysytdqk qcSsgqzysytdqk : list) {
            String qh = qcSsgqzysytdqk.getXzqh().substring(0, 4);
            for (DictModel dictModel : xzqh) {
                if (qh.equals(dictModel.getValue())) {
                    qcSsgqzysytdqk.setXzqh(dictModel.getText());
                }
            }
        }
        Map<String, Double> xzqhByAreaCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getXzqh,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmjM()).orElse(0.0))));
        result.add(xzqhByAreaCollect);

        Map<String, List<QcSsgqzysytdqk>> syxzCollect = list.stream().collect(
                Collectors.groupingBy((QcSsgqzysytdqk::getTdsyxz)));
        for (String s : syxzCollect.keySet()) {
            syxzMap.put(s, (double) syxzCollect.get(s).size());
        }
        result.add(syxzMap);

        Map<String, Double> syxzByAreaCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getSyqsyfs,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmjM()).orElse(0.0))));
        result.add(syxzByAreaCollect);

        Map<String, Long> wjlxCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getWjlx,
                        Collectors.counting())
        );
        for (String s : wjlxCollect.keySet()) {
            wjbhMap.put(s, (double) wjlxCollect.get(s));
        }
        result.add(wjbhMap);

        for (QcSsgqzysytdqk qcSsgqzysytdqk : list) {
//            String qh = qcSsgqzysytdqk.getXzqh().substring(0, 4);
            for (SysCategoryModel sysCategoryModel : sysCategoryModels) {
                if (qcSsgqzysytdqk.getSjyt().equals(sysCategoryModel.getId())) {
                    qcSsgqzysytdqk.setSjyt(sysCategoryModel.getName());
                }
            }
        }
        Map<String, List<QcSsgqzysytdqk>> collectBysjyt = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqk::getSjyt)));
        for (String s : collectBysjyt.keySet()) {
            sjytMap.put(s, (double) collectBysjyt.get(s).size());
        }
        result.add(sjytMap);

        return result;
    }

    @Override
    public List<Map<String, Double>> sumListBysz(List<QcSsgqzysytdqk> list) {
        List<DictModel> xzqh = sysBaseAPI.getDictItems("xq");
        List<SysCategoryModel> sysCategoryModels = sysBaseAPI.queryAllSysCategory();
        Double num = Double.valueOf(0);
        List<Map<String, Double>> result = new ArrayList<>();
        Map<String, Double> map = new HashMap<>();
        Map<String, Double> sjytMap = new HashMap<>();
        Map<String, Double> syxzMap = new HashMap<>();
        Map<String, Double> wjbhMap = new HashMap<>();
        Map<String, List<QcSsgqzysytdqk>> collect = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqk::getSyqsyfs)));
        for (String s : collect.keySet()) {
            map.put(s, (double) collect.get(s).size());
        }
        result.add(map);

        for (QcSsgqzysytdqk qcSsgqzysytdqk : list) {
            String qh = qcSsgqzysytdqk.getXzqh().substring(0, 4);
            for (DictModel dictModel : xzqh) {
                if (qh.equals(dictModel.getValue())) {
                    qcSsgqzysytdqk.setXzqh(dictModel.getText());
                }
            }
        }
        Map<String, Double> xzqhByAreaCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getXzqh,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmj()).orElse(0.0))));
        result.add(xzqhByAreaCollect);

        Map<String, List<QcSsgqzysytdqk>> syxzCollect = list.stream().collect(
                Collectors.groupingBy((QcSsgqzysytdqk::getTdsyxz)));
        for (String s : syxzCollect.keySet()) {
            syxzMap.put(s, (double) syxzCollect.get(s).size());
        }
        result.add(syxzMap);

        Map<String, Double> syxzByAreaCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getSyqsyfs,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmj()).orElse(0.0))));
        result.add(syxzByAreaCollect);

        Map<String, Long> wjlxCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqk::getZslx,
                        Collectors.counting())
        );
        for (String s : wjlxCollect.keySet()) {
            wjbhMap.put(s, (double) wjlxCollect.get(s));
        }
        result.add(wjbhMap);

        for (QcSsgqzysytdqk qcSsgqzysytdqk : list) {
            for (SysCategoryModel sysCategoryModel : sysCategoryModels) {
                if (qcSsgqzysytdqk.getPzyt().equals(sysCategoryModel.getId())) {
                    qcSsgqzysytdqk.setPzyt(sysCategoryModel.getName());
                }
            }
        }
        Map<String, List<QcSsgqzysytdqk>> collectBysjyt = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqk::getPzyt)));
        for (String s : collectBysjyt.keySet()) {
            sjytMap.put(s, (double) collectBysjyt.get(s).size());
        }
        result.add(sjytMap);
        return result;
    }

}
