package com.njcn.device.pq.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.njcn.device.biz.pojo.dto.SubGetBase;
import com.njcn.device.biz.pojo.param.SubstationParam;
import com.njcn.device.pq.mapper.YwZtSubstationMapper;
import com.njcn.device.pq.pojo.po.Substation;
import com.njcn.device.pq.pojo.po.YwZtSubstation;
import com.njcn.device.pq.service.ISubstationService;
import com.njcn.device.pq.service.IYwZtSubstationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.device.substation.mapper.SubstationMapper;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.dto.DeptDTO;
import com.njcn.user.pojo.po.Dept;
import com.njcn.web.utils.WebUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wr
 * @since 2024-08-07
 */
@Service
@RequiredArgsConstructor
public class YwZtSubstationServiceImpl extends ServiceImpl<YwZtSubstationMapper, YwZtSubstation> implements IYwZtSubstationService {


    private final SubstationMapper substationMapper;
    private final ISubstationService substationService;
    private final DeptFeignClient deptFeignClient;



    @Override
    public Boolean syncYwZtSubstation() {
        List<Substation> test = substationMapper.test();
        //数据库经纬度
        Map<String, Substation> collect1 = test.stream().collect(Collectors.toMap(Substation::getScale, Function.identity()));
        //中台变电站信息
        List<YwZtSubstation> ztSubs =  this.list();

        //匹配不到数据
        List<Substation> loseInfo=new ArrayList<>();
        //匹配成功数据
        List<Substation> info=new ArrayList<>();

        collect1.forEach((key,value)->{
            key=key.substring(key.indexOf("V")+1);
            String dzmc = getDzmc(key);
            List<YwZtSubstation> ztNormalSubs =  ztSubs.stream().filter(
                    x -> dzmc.indexOf(getDzmc(x.getName())) != -1 || getDzmc(x.getName()).indexOf(dzmc) != -1
            ).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(ztNormalSubs)) {
                if(ztNormalSubs.size()>1){
                    List<YwZtSubstation> collect = ztNormalSubs.stream().filter(
                            x -> dzmc.equals(getDzmc(x.getName()))
                    ).collect(Collectors.toList());
                    if(CollUtil.isNotEmpty(collect)){
                        value.setLng(collect.get(0).getLng());
                        value.setScale(null);
                        value.setLat(collect.get(0).getLat());
                        info.add(value);
                    }
                }else{
                    value.setLng(ztNormalSubs.get(0).getLng());
                    value.setScale(null);
                    value.setLat(ztNormalSubs.get(0).getLat());
                    info.add(value);
                }
            }else{
                loseInfo.add(value);
            }
        });
        List<String> collect = loseInfo.stream().filter(x->x.getLat().doubleValue()==0.0).map(Substation::getScale).collect(Collectors.toList());
        System.out.println(collect);
        System.out.println(info);
        return substationService.updateBatchById(info);
    }

    @Override
    public List<YwZtSubstation> getYwZtSubstation(String orgId) {
        //获取变电站信息
        List<String> orgIds = new ArrayList<>();
        if(StrUtil.isNotBlank(orgId)){
            List<DeptDTO> deptDTOList = deptFeignClient.getDeptDescendantIndexes(orgId, WebUtil.filterDeptType()).getData();
            orgIds= deptDTOList.stream().map(DeptDTO::getCode).collect(Collectors.toList());
        }
        return this.list(new LambdaQueryWrapper<YwZtSubstation>().in(CollUtil.isNotEmpty(orgIds),YwZtSubstation::getOrgId,orgIds));
    }

    @Override
    public List<SubGetBase> getSubstation(SubstationParam substationParam) {
        List<String> orgIds = substationParam.getOrgIds();
        List<Dept> deptList = deptFeignClient.getDeptInfoListByIds(orgIds).getData();
        List<String> deptCode = deptList.stream().map(Dept::getCode).collect(Collectors.toList());
        substationParam.setOrgIds(deptCode);
        List<SubGetBase> subGetBases = this.baseMapper.selectSubstationList(substationParam);
        if(CollUtil.isNotEmpty(subGetBases)){
            Map<String, Dept> deptMap = deptList.stream().collect(Collectors.toMap(Dept::getCode, Function.identity()));
            subGetBases.stream().forEach(x->{
                if(deptMap.containsKey(x.getOrgId())){
                    Dept deptDTO = deptMap.get(x.getOrgId());
                    x.setOrgName(deptDTO.getName());
                    x.setOrgId(deptDTO.getId());
                }
            });
            return subGetBases;
        }
        return Collections.emptyList();
    }


    public String getDzmc(String dzmc) {
        if(StrUtil.isNotBlank(dzmc)) {
            dzmc = dzmc.replace("变电站", "")
                    .replace("火电厂", "")
                    .replace("供电所", "")
                    .replace("风光储", "")
                    .replace("风电场", "")
                    .replace("光伏电站", "")
                    .replace("光储电站", "")
                    .replace("开关站", "")
                    .replace("桥接站", "");
        }
        return dzmc;
    }

}
