package com.ruoyi.gysl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.entity.BuildingInformation;
import com.ruoyi.gysl.entity.Pjpz;
import com.ruoyi.gysl.entity.Xmpjqd;
import com.ruoyi.gysl.entity.request.XmpjqdPageReq;
import com.ruoyi.gysl.entity.response.XmhxDetail.XmhxDetail;
import com.ruoyi.gysl.entity.response.XmhxResponse;
import com.ruoyi.gysl.entity.response.XmpjqdOneResponse;
import com.ruoyi.gysl.mapper.XmpjqdMapper;
import com.ruoyi.gysl.service.BuildingInformationService;
import com.ruoyi.gysl.service.PjpzService;
import com.ruoyi.gysl.service.XmpjqdService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目评价清单(Xmpjqd)表服务实现类
 *
 * @author makejava
 * @since 2025-03-22 09:22:33
 */
@Service("xmpjqdService")
public class XmpjqdServiceImpl extends ServiceImpl<XmpjqdMapper, Xmpjqd> implements XmpjqdService {

    @Resource
    private XmpjqdMapper xmpjqdMapper;

    @Resource
    private BuildingInformationService buildingInformationService;

    @Resource
    private PjpzService pjpzService;

    /**
     * 分页查询所有目录
     *
     * @param page 分页对象
     * @return 所有数据
     */
    @Override
    public Page<Xmpjqd> page(Page<Xmpjqd> page, XmpjqdPageReq req) {
        Page<Xmpjqd> page1 = xmpjqdMapper.page(page, req);
        for (int i = 0; i < page1.getRecords().size(); i++) {
            page1.getRecords().get(i).setPjdj(i + 1);
            page1.getRecords().get(i).setCount((int) page1.getTotal());
        }
        return page1;
    }

    /**
     * 通过主键查询单条项目评价清单
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    public List<XmpjqdOneResponse> selectOnePj(Long id) {
        List<XmpjqdOneResponse> xmpjqdOneResponses = xmpjqdMapper.selectOnePj(id);
        //获取项目评价配置
        pjpzService.list().forEach(x -> {
            if(!Objects.equals(x.getPjys(), "总投资额")){
                XmpjqdOneResponse xr = new XmpjqdOneResponse();
                xr.setYsmc(x.getPjys());
                xmpjqdOneResponses.add(xr);
            }
        });
        return xmpjqdOneResponses;
    }

    /**
     * 通过主键查询单条项目画像
     *
     * @param id 主键
     * @return 单条数据
     */
    @Override
    public List<XmhxResponse> oneXmhx(Long id) {
        //返回体
        List<XmhxResponse> obj = new ArrayList<>();
        //最大值
        BuildingInformation allMax1 = buildingInformationService.getAllMax();
        BuildingInformation allMax = allMax1 == null ? new BuildingInformation() : allMax1;
        //本项目
        BuildingInformation th1 = buildingInformationService.getOneXmIdToMax(id);
        BuildingInformation th = th1 == null ? new BuildingInformation() : th1;
        //查询所有的建筑信息,只保留是重要楼栋的max条
        List<BuildingInformation> ib = buildingInformationService.getImportantBuildings();
        //获取项目评价配置数据
        List<Pjpz> list = pjpzService.list();
        //分类数据
        XmhxResponse xr1 = new XmhxResponse();
        xr1.setType(1);
        List<XmhxDetail> l1 = new ArrayList<>();
        XmhxResponse xr2 = new XmhxResponse();
        xr2.setType(2);
        List<XmhxDetail> l2 = new ArrayList<>();
        XmhxResponse xr3 = new XmhxResponse();
        xr3.setType(3);
        List<XmhxDetail> l3 = new ArrayList<>();
        list.forEach(x -> {
            switch (x.getPjys()) {
                case "总投资额":
                    l1.add(xmpjqdMapper.zdzZtze() == null ? new XmhxDetail("总投资额", "0") : xmpjqdMapper.zdzZtze());
                    l2.add(xmpjqdMapper.meZtze(id) == null ? new XmhxDetail("总投资额", "0") : xmpjqdMapper.meZtze(id));
                    if (xmpjqdMapper.zwsZtze() != null) {
                        l3.add(getXmhxDetails(xmpjqdMapper.zwsZtze(), "总投资额"));
                    } else {
                        l3.add(new XmhxDetail("总投资额", "0"));
                    }
                    break;
                case "层数":
                    l1.add(new XmhxDetail("层数", String.valueOf(allMax.getFloor() == null ? 0 : allMax.getFloor())));
                    l2.add(new XmhxDetail("层数", String.valueOf(th.getFloor() == null ? 0 : th.getFloor())));
                    //层数拿出来
                    if (ib.isEmpty()) {
                        l3.add(new XmhxDetail("层数", "0"));
                    } else {
                        List<Integer> floor = ib.stream()
                                .map(BuildingInformation::getFloor)
                                .filter(Objects::nonNull)
                                .sorted()
                                .collect(Collectors.toList());
                        String s;
                        if (floor.size() % 2 == 1) {
                            s = String.valueOf(floor.get(floor.size() / 2));
                        } else {
                            if (!floor.isEmpty()) {
                                Integer lower = floor.get(floor.size() / 2 - 1);
                                Integer upper = floor.get(floor.size() / 2);
                                s = String.valueOf((lower + upper) / 2);
                            } else {
                                s = "0";
                            }
                        }
                        l3.add(new XmhxDetail("层数", s));
                    }
                    break;
                case "首层层高":
                    l1.add(new XmhxDetail("首层层高", String.valueOf(allMax.getScgd() == null ? 0 : allMax.getScgd())));
                    l2.add(new XmhxDetail("首层层高", String.valueOf(th.getScgd() == null ? 0 : th.getScgd())));
                    if (ib.isEmpty()) {
                        l3.add(new XmhxDetail("首层层高", "0"));
                    } else {
                        List<BigDecimal> sccg = ib.stream()
                                .map(BuildingInformation::getScgd)
                                .filter(Objects::nonNull)
                                .sorted()
                                .collect(Collectors.toList());
                        l3.add(getXmhxDetails(sccg, "首层层高"));
                    }
                    break;
                case "二层及以上层高":
                    l1.add(new XmhxDetail("二层及以上层高", String.valueOf(allMax.getTwoAndFourCg() == null ? 0 : allMax.getTwoAndFourCg())));
                    l2.add(new XmhxDetail("二层及以上层高", String.valueOf(th.getTwoAndFourCg() == null ? 0 : th.getTwoAndFourCg())));
                    if (ib.isEmpty()) {
                        l3.add(new XmhxDetail("二层及以上层高", "0"));
                    } else {
                        List<BigDecimal> ec = ib.stream()
                                .map(BuildingInformation::getTwoAndFourCg)
                                .filter(Objects::nonNull)
                                .sorted()
                                .collect(Collectors.toList());
                        l3.add(getXmhxDetails(ec, "二层及以上层高"));
                    }
                    break;
                case "二层及以上楼面荷载":
                    l1.add(new XmhxDetail("二层及以上楼面荷载", String.valueOf(allMax.getTwoAndThreeLmhz() == null ? 0 : allMax.getTwoAndThreeLmhz())));
                    l2.add(new XmhxDetail("二层及以上楼面荷载", String.valueOf(th.getTwoAndThreeLmhz() == null ? 0 : th.getTwoAndThreeLmhz())));
                    if (ib.isEmpty()) {
                        l3.add(new XmhxDetail("二层及以上楼载荷", "0"));
                    } else {
                        List<BigDecimal> zh = ib.stream()
                                .map(BuildingInformation::getTwoAndThreeLmhz)
                                .filter(Objects::nonNull)
                                .sorted()
                                .collect(Collectors.toList());
                        l3.add(getXmhxDetails(zh, "二层及以上楼载荷"));
                    }
                    break;
                default:
                    l1.add(new XmhxDetail(x.getPjys(), "0"));
                    l2.add(new XmhxDetail(x.getPjys(), "0"));
                    l3.add(new XmhxDetail(x.getPjys(), "0"));
                    break;
            }
        });
        xr1.setList(l1);
        xr2.setList(l2);
        xr3.setList(l3);
        obj.add(xr1);
        obj.add(xr2);
        obj.add(xr3);
        return obj;
    }

    private static XmhxDetail getXmhxDetails(List<BigDecimal> bigDecimals, String content) {
        String s;
        int length = bigDecimals.size();
        int middleIndex = length / 2;
        // 根据奇偶性计算中位数
        if (length % 2 == 1) {
            s = String.valueOf(bigDecimals.get(middleIndex));
        } else {
            if (!bigDecimals.isEmpty()) {
                BigDecimal lower = bigDecimals.get(middleIndex - 1);
                BigDecimal upper = bigDecimals.get(middleIndex);
                s = String.valueOf(
                        lower.add(upper)
                                .divide(BigDecimal.valueOf(2), 2, RoundingMode.HALF_UP)  // 2 表示保留两位小数
                );
            } else {
                s = "0";
            }
        }
        return new XmhxDetail(content, s);
    }
}

