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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.gzvhb.entity.QcSsgqzysytdqkGzv;
import org.jeecg.modules.zrzyqc.gzvhb.mapper.QcSsgqzysytdqkGzvMapper;
import org.jeecg.modules.zrzyqc.gzvhb.model.ResultCount;
import org.jeecg.modules.zrzyqc.gzvhb.service.IQcSsgqzysytdqkGzvService;
import org.jeecg.modules.zrzyqc.qyxx.entity.QcQyxx;
import org.jeecg.modules.zrzyqc.qyxx.mapper.QcQyxxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

/**
 * @Description: qc_ssgqzysytdqk_gzv
 * @Author: jeecg-boot
 * @Date: 2024-07-27
 * @Version: V1.0
 */
@Service
public class QcSsgqzysytdqkGzvServiceImpl extends ServiceImpl<QcSsgqzysytdqkGzvMapper, QcSsgqzysytdqkGzv> implements IQcSsgqzysytdqkGzvService {
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private QcSsgqzysytdqkGzvMapper qcSsgqzysytdqkGzvMapper;
    @Autowired
    private QcQyxxMapper qcQyxxMapper;

    @Override
    public void upSfsb(String id) {
        qcSsgqzysytdqkGzvMapper.upSfsb(id);
    }
    @Override
    public QcQyxx selectQyxx(String username, String orCode) {
        return qcSsgqzysytdqkGzvMapper.selectQyxx(username, orCode);
    }

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

    /**
     * 查询地块面积和数量
     *
     * @param queryWrapper
     * @return
     */
    @Override
    public ResultCount queryResultCount(QueryWrapper<QcSsgqzysytdqkGzv> queryWrapper) {
        List<QcSsgqzysytdqkGzv> qcSsgqzysytdqkGzvs = qcSsgqzysytdqkGzvMapper.selectList(queryWrapper);
        if (!qcSsgqzysytdqkGzvs.isEmpty()) {
            ResultCount resultCount = new ResultCount();
            resultCount.setCountDk(qcSsgqzysytdqkGzvs.size());
            resultCount.setAreaDk((qcSsgqzysytdqkGzvs.stream().mapToDouble(QcSsgqzysytdqkGzv::getSjzytdmj).sum()) * 0.0015);
            return resultCount;
        }
        return null;
    }

    @Override
    public ResultCount queryResultCountMoney(QueryWrapper<QcSsgqzysytdqkGzv> queryWrapper) {
        List<QcSsgqzysytdqkGzv> qcSsgqzysytdqkGzvs = qcSsgqzysytdqkGzvMapper.selectList(queryWrapper);
        if (!qcSsgqzysytdqkGzvs.isEmpty()) {
            ResultCount resultCount = new ResultCount();
            resultCount.setCountDk(qcSsgqzysytdqkGzvs.size());
            resultCount.setAreaDk(qcSsgqzysytdqkGzvs.stream()
                    .filter(qc -> qc.getWorth() != null) // 过滤掉 null 值
                    .mapToDouble(QcSsgqzysytdqkGzv::getWorth)
                    .sum() / 10000);
            return resultCount;
        }
        return null;
    }

    /**
     * 获取所有一级企业名称
     *
     * @return
     */
    @Override
    public List<QcSsgqzysytdqkGzv> getFirstOrgNames(List<QcSsgqzysytdqkGzv> list) {
        Map<String, Map<String, Object>> resultMap = qcQyxxMapper.getFirstOrgNames();
        Map<String, String> map = resultMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().get("value").toString()
                ));
        for (QcSsgqzysytdqkGzv qcSsgqzysytdqkGzv : list) {
            if (qcSsgqzysytdqkGzv.getSysOrgCode() != null && qcSsgqzysytdqkGzv.getSysOrgCode().length()>=6) {
                String code = qcSsgqzysytdqkGzv.getSysOrgCode().substring(0, 6);
                String value = map.get(code).toString();
                qcSsgqzysytdqkGzv.setFirstOrgName(value);
            }
        }
        return list;
    }

    @Override
    public QcSsgqzysytdqkGzv getFirstOrgName(QcSsgqzysytdqkGzv qcSsgqzysytdqkGzv) {
        Map<String, Map<String, Object>> resultMap = qcQyxxMapper.getFirstOrgNames();
        Map<String, String> map = resultMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().get("value").toString()
                ));
        if (null != qcSsgqzysytdqkGzv) {

            if (qcSsgqzysytdqkGzv.getSysOrgCode() != null && qcSsgqzysytdqkGzv.getSysOrgCode().length()>=6) {
                String code = qcSsgqzysytdqkGzv.getSysOrgCode().substring(0, 6);
                String value = map.get(code).toString();
                qcSsgqzysytdqkGzv.setFirstOrgName(value);
            }
        }
        return qcSsgqzysytdqkGzv;
    }

    @Override
    public List<QcQyxx> getFirstOrgNamesQyxx(List<QcQyxx> list) {
        Map<String, Map<String, Object>> resultMap = qcQyxxMapper.getFirstOrgNames();
        Map<String, String> map = resultMap.entrySet().stream()
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> entry.getValue().get("value").toString()
                ));
        for (QcQyxx qcQyxx : list) {
            if (qcQyxx.getSysOrgCode() != null && qcQyxx.getSysOrgCode().length()>=6) {
                String code = qcQyxx.getSysOrgCode().substring(0, 6);
                String value = map.get(code).toString();
                qcQyxx.setFirstOrgName(value);
            }
        }
        return list;
    }


    /**
     * 省属企业统计图表
     * @param list
     * @return
     */
    @Override
    public List<Map<String, Double>> sumList(List<QcSsgqzysytdqkGzv> 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 LinkedHashMap<>();  //实际用途
        Map<String, Double> syxzMap = new LinkedHashMap<>();  //使用现状
        Map<String, Double> wjbhMap = new LinkedHashMap<>();  //文件
        //1-使用权使用方式,占比地块数量
        Map<String, List<QcSsgqzysytdqkGzv>> collect = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqkGzv::getSyqsyfs)));
        for (String s : collect.keySet()) {
            map.put(s, (double) collect.get(s).size());
        }
        result.add(map);
        //2-行政区划
        for (QcSsgqzysytdqkGzv 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(QcSsgqzysytdqkGzv::getXzqh,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjsyjzmj() * 0.0015).orElse(0.0))));
        result.add(xzqhByAreaCollect);
        //土地使用现状编码
        Map<String, List<QcSsgqzysytdqkGzv>> syxzCollect = list.stream().collect(
                Collectors.groupingBy((QcSsgqzysytdqkGzv::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(QcSsgqzysytdqkGzv::getSyqsyfs,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmj() * 0.0015).orElse(0.0))));
        result.add(syxzByAreaCollect);
        //文件类型
        Map<String, Long> wjlxCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqkGzv::getWjlx,
                        Collectors.counting())
        );
        for (String s : wjlxCollect.keySet()) {
            wjbhMap.put(s, (double) wjlxCollect.get(s));
        }
        result.add(wjbhMap);
        //实际用途
        for (QcSsgqzysytdqkGzv qcSsgqzysytdqk : list) {
            for (SysCategoryModel sysCategoryModel : sysCategoryModels) {
                if (qcSsgqzysytdqk.getSjyt().equals(sysCategoryModel.getId())) {
                    qcSsgqzysytdqk.setSjyt(sysCategoryModel.getName());
                }
            }
        }
        Map<String, List<QcSsgqzysytdqkGzv>> collectBysjyt = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqkGzv::getSjyt)));
        for (String s : collectBysjyt.keySet()) {
            sjytMap.put(s, (double) collectBysjyt.get(s).size());
        }
        result.add(sjytMap);
        return result;
    }

    /**
     * 省直企业统计图表
     * @param list
     * @return
     */
    @Override
    public List<Map<String, Double>> sumListBysz(List<QcSsgqzysytdqkGzv> 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 LinkedHashMap<>();
        Map<String, Double> syxzMap = new LinkedHashMap<>();
        Map<String, Double> wjbhMap = new LinkedHashMap<>();
        Map<String, List<QcSsgqzysytdqkGzv>> collect = list.stream().collect(Collectors.groupingBy((QcSsgqzysytdqkGzv::getSyqsyfs)));
        for (String s : collect.keySet()) {
            map.put(s, (double) collect.get(s).size());
        }
        result.add(map);
        //行政区划
        for (QcSsgqzysytdqkGzv 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(QcSsgqzysytdqkGzv::getXzqh,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjsyjzmj() * 0.0015).orElse(0.0))));
        result.add(xzqhByAreaCollect);
        //土地使用方式
        Map<String, List<QcSsgqzysytdqkGzv>> syxzCollect = list.stream().collect(
                Collectors.groupingBy((QcSsgqzysytdqkGzv::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(QcSsgqzysytdqkGzv::getSyqsyfs,
                        Collectors.summingDouble(s -> Optional.ofNullable(s.getSjzytdmj() * 0.0015).orElse(0.0))));
        result.add(syxzByAreaCollect);
        //文件类型
        Map<String, Long> wjlxCollect = list.stream().collect(
                Collectors.groupingBy(QcSsgqzysytdqkGzv::getZslx,
                        Collectors.counting())
        );
        for (String s : wjlxCollect.keySet()) {
            wjbhMap.put(s, (double) wjlxCollect.get(s));
        }
        result.add(wjbhMap);

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

}
