package com.fastbee.xunjian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.xunjian.domain.XjInspectionNote;
import com.fastbee.xunjian.domain.XjInspectionRoutes;
import com.fastbee.xunjian.mapper.XjInspectionRoutesMapper;
import com.fastbee.xunjian.service.IXjInspectionNoteService;
import com.fastbee.xunjian.service.IXjInspectionRoutesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 巡检路线Service业务层处理
 *
 * @author kenlixunjian
 * @date 2023-08-04
 */
@Service
public class XjInspectionRoutesServiceImpl implements IXjInspectionRoutesService {
    @Autowired
    private XjInspectionRoutesMapper XjInspectionRoutesMapper;
//    @Autowired
//    private KlxjQrCodeMapper klxjQrCodeMapper;

    private static final Logger log = LoggerFactory.getLogger(XjInspectionRoutesServiceImpl.class);

    @Autowired
    private IXjInspectionNoteService iXjInspectionNoteService;

//    @Autowired
//    private ISysConfigService configService;
//
//    @Autowired
//    private ISysDictTypeService dictTypeService;

    /**
     * 查询巡检路线
     *
     * @param id 巡检路线主键
     * @return 巡检路线
     */
    @Override
    public XjInspectionRoutes selectXjInspectionRoutesById(Long id) {
        XjInspectionRoutes XjInspectionRoutes = XjInspectionRoutesMapper.selectXjInspectionRoutesById(id);

        if (XjInspectionRoutes.getInspectionNoteIds() != null && !XjInspectionRoutes.getInspectionNoteIds().equals("")) {
            XjInspectionNote xjInspectionNote = new XjInspectionNote();
//            XjInspectionNote.setParams(new HashMap<String, Object>() {{
//                put("ids", XjInspectionRoutes.getInspectionNoteIds());
//            }});
            List<XjInspectionNote> XjInspectionNotes =
                    iXjInspectionNoteService.selectXjInspectionNoteList(xjInspectionNote);
            XjInspectionRoutes.setInspectionNote(XjInspectionNotes);
        }

        return XjInspectionRoutes;
    }

    /**
     * 查询巡检路线列表
     *
     * @param XjInspectionRoutes 巡检路线
     * @return 巡检路线
     */
    @Override
    public List<XjInspectionRoutes> selectXjInspectionRoutesList(XjInspectionRoutes XjInspectionRoutes) {
        List<XjInspectionRoutes> XjInspectionRoutes1 = XjInspectionRoutesMapper.selectXjInspectionRoutesList(XjInspectionRoutes);
        return XjInspectionRoutes1;
    }


    /**
     * 新增巡检路线
     *
     * @param XjInspectionRoutes 巡检路线
     * @return 结果
     */
    @Override
    public int insertXjInspectionRoutes(XjInspectionRoutes XjInspectionRoutes) {
        XjInspectionRoutes.setCreateTime(DateUtils.getNowDate());
//        if (XjInspectionRoutes.getQrCodeId() != null && XjInspectionRoutes.getQrCodeId() != 0) {
//            KlxjQrCode klxjQrCode = klxjQrCodeMapper.selectKlxjQrCodeById(XjInspectionRoutes.getQrCodeId());
//            if (selectXjInspectionRoutesByQrSn(klxjQrCode.getCodeSn()) != null) {
//                throw new RuntimeException("二维码已被绑定");
//            }
//            XjInspectionRoutes.setQrCodeSn(klxjQrCode.getCodeSn());
//        }
//        if (XjInspectionRoutes.getEngineeringObjectType() != null && XjInspectionRoutes.getEngineeringObjectId() != null && XjInspectionRoutes.getEngineeringObjectId() != 0L) {
//            KlxjEngineeringVo klxjEngineeringVo = new KlxjEngineeringVo();
//            klxjEngineeringVo.setEngineeringObjectType(XjInspectionRoutes.getEngineeringObjectType());
//            klxjEngineeringVo.setEngineeringObjectId(XjInspectionRoutes.getEngineeringObjectId().toString());
//            Object engineeringData = getEngineeringData(klxjEngineeringVo);
//            setRouteDataByEngineeringObjectId(XjInspectionRoutes, klxjEngineeringVo, engineeringData);
//        }
        return XjInspectionRoutesMapper.insertXjInspectionRoutes(XjInspectionRoutes);
    }

//    private void setRouteDataByEngineeringObjectId(XjInspectionRoutes XjInspectionRoutes, KlxjEngineeringVo klxjEngineeringVo, Object engineeringData) {
////        switch (EngineeringObjectType.getName(klxjEngineeringVo.getEngineeringObjectType())) {
////            case SHUIZHA_TYPE:
////
////                BaseSluiceInfo baseSluiceInfo = BeanUtil.toBean(engineeringData, BaseSluiceInfo.class);
////                if (baseSluiceInfo != null) {
////                    XjInspectionRoutes.setName(baseSluiceInfo.getName());
////                    XjInspectionRoutes.setQuyuma(baseSluiceInfo.getRegion());
////                    XjInspectionRoutes.setPosition(baseSluiceInfo.getPosition());
////                    ArrayList<Object> list = new ArrayList<>();
////                    if (StringUtils.isNotEmpty(baseSluiceInfo.getLatitude()) || StringUtils.isNotEmpty(baseSluiceInfo.getLongitude())) {
////                        list.add(baseSluiceInfo.getLongitude());
////                        list.add(baseSluiceInfo.getLatitude());
////                    }
////                    String str = "{\"geometry\":{\"coordinates\":" + list.toString() + ",\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"altitude\":\"0\",\"textDisplayStyle\":\"0\",\"iconStyle\":\"33\"}}";
////                    XjInspectionRoutes.setLatAndLong(str);
////                }
////                break;
////            case BENGFANG_TYPE:
////                BasePumpHouseInfo basePumpHouseInfo = BeanUtil.toBean(engineeringData, BasePumpHouseInfo.class);
////                if (basePumpHouseInfo != null) {
////                    XjInspectionRoutes.setName(basePumpHouseInfo.getName());
////                    XjInspectionRoutes.setQuyuma(basePumpHouseInfo.getRegion());
////                    XjInspectionRoutes.setPosition(basePumpHouseInfo.getPosition());
////                    ArrayList<Object> list = new ArrayList<>();
////                    if (StringUtils.isNotEmpty(basePumpHouseInfo.getLatitude()) || StringUtils.isNotEmpty(basePumpHouseInfo.getLongitude())) {
////                        list.add(basePumpHouseInfo.getLongitude());
////                        list.add(basePumpHouseInfo.getLatitude());
////                    }
////                    String str = "{\"geometry\":{\"coordinates\":" + list.toString() + ",\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"altitude\":\"0\",\"textDisplayStyle\":\"0\",\"iconStyle\":\"33\"}}";
////                    XjInspectionRoutes.setLatAndLong(str);
////                }
////
////                break;
////            case SHUIKU_TYPE:
////                BaseReservoirInfo baseReservoirInfo = BeanUtil.toBean(engineeringData, BaseReservoirInfo.class);
////                if (baseReservoirInfo != null) {
////                    XjInspectionRoutes.setName(baseReservoirInfo.getName());
////                    XjInspectionRoutes.setQuyuma(baseReservoirInfo.getRegion());
////                    XjInspectionRoutes.setPosition(baseReservoirInfo.getPosition());
////                    ArrayList<Object> list = new ArrayList<>();
////                    if (StringUtils.isNotEmpty(baseReservoirInfo.getLatitude()) || StringUtils.isNotEmpty(baseReservoirInfo.getLongitude())) {
////                        list.add(baseReservoirInfo.getLongitude());
////                        list.add(baseReservoirInfo.getLatitude());
////                    }
////                    String str = "{\"geometry\":{\"coordinates\":" + list.toString() + ",\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"altitude\":\"0\",\"textDisplayStyle\":\"0\",\"iconStyle\":\"33\"}}";
////                    XjInspectionRoutes.setLatAndLong(str);
////                }
////                break;
////            case DUCAO_TYPE:
////                KlAqueduct klAqueduct = BeanUtil.toBean(engineeringData, KlAqueduct.class);
////                if (klAqueduct != null) {
////                    XjInspectionRoutes.setName(klAqueduct.getName());
////                    XjInspectionRoutes.setPosition(klAqueduct.getPosition());
////                    ArrayList<Object> list = new ArrayList<>();
////                    if (StringUtils.isNotEmpty(klAqueduct.getLongLat())) {
////                        list.addAll(Arrays.asList(klAqueduct.getLongLat().split(",")));
////                    }
////                    String str = "{\"geometry\":{\"coordinates\":" + list.toString() + ",\"type\":\"Point\"},\"type\":\"Feature\",\"properties\":{\"altitude\":\"0\",\"textDisplayStyle\":\"0\",\"iconStyle\":\"33\"}}";
////                    XjInspectionRoutes.setLatAndLong(str);
////                }
////                break;
////        }
//    }

    //    private void getRange(HhReachInfo hhReachInfo, Map mapparse, int type) {
//
//        List<Map<String, Object>> features = (List<Map<String, Object>>) mapparse.get("features");
//        ArrayList<Object> list1 = new ArrayList<>();
//        if (features != null && features.size() > 0) {
//            for (Map<String, Object> feature : features) {
//                ArrayList<ArrayList> list = new ArrayList<>();
//                ArrayList<ArrayList> listfan = new ArrayList<>();
//                ArrayList<ArrayList> listdata = new ArrayList<>();
//                Map<String, Object> geometry = (Map<String, Object>) feature.get("geometry");
//                List<List<String>> coordinates = (List<List<String>>) geometry.get("coordinates");
//                for (int i = 1; i < coordinates.size(); i++) {
//                    List<BigDecimal> objects1 = (List<BigDecimal>) Convert.toList(coordinates.get(i - 1));
//                    List<BigDecimal> objects2 = (List<BigDecimal>) Convert.toList(coordinates.get(i));
//                    BigDecimal long1 = objects1.get(0);
//                    BigDecimal lat1 = objects1.get(1);
//                    BigDecimal long2 = objects2.get(0);
//                    BigDecimal lat2 = objects2.get(1);
//                    if (long1 != null && lat1 != null && lat2 != null && long2 != null) {
//                        if (long2.compareTo(long1) == 0 || lat2.compareTo(lat1) == 0) {
//                            BigDecimal longdata = long2;
//                            BigDecimal latdata = lat2;
//                            BigDecimal longdatafan = long2;
//                            BigDecimal latdatafan = lat2;
//
//                            BigDecimal lomglat = new BigDecimal(0.00001);
//                            BigDecimal num = lomglat.multiply(new BigDecimal(type)).setScale(12, RoundingMode.FLOOR);
//                            if (i == 1) {
//                                BigDecimal longdata1 = long1;
//                                BigDecimal latdata1 = lat1;
//                                BigDecimal longdata1fan = long1;
//                                BigDecimal latdata1fan = lat1;
//                                if (long2.compareTo(long1) == 0) {
//                                    if (lat2.compareTo(lat1) > 0) {
//                                        longdata1 = long1.subtract(num);
//                                        longdata1fan = long1.add(num);
//                                    } else if (lat2.compareTo(lat1) < 0) {
//                                        longdata1 = long1.add(num);
//                                        longdata1fan = long1.subtract(num);
//                                    }
//                                }
//                                if (lat2.compareTo(lat1) == 0) {
//                                    if (long2.compareTo(long1) > 0) {
//                                        latdata1 = lat1.add(num);
//                                        latdata1fan = lat1.subtract(num);
//                                    } else if (long2.compareTo(long1) < 0) {
//                                        latdata1 = lat1.subtract(num);
//                                        latdata1fan = lat1.add(num);
//                                    }
//                                }
//                                ArrayList newlist = new ArrayList<>();
//                                ArrayList newlistfan = new ArrayList<>();
//                                newlistfan.add(longdata1fan);
//                                newlistfan.add(latdata1fan);
//                                listfan.add(newlist);
//                                newlist.add(longdata1);
//                                newlist.add(latdata1);
//                                list.add(newlist);
//                            }
//
//                            if (long2.compareTo(long1) == 0) {
//                                if (lat2.compareTo(lat1) > 0) {
//                                    longdata = long2.subtract(num);
//                                    longdatafan = long2.add(num);
//                                } else if (lat2.compareTo(lat1) < 0) {
//                                    longdata = long2.add(num);
//                                    longdatafan = long2.subtract(num);
//                                }
//                            }
//                            if (lat2.compareTo(lat1) == 0) {
//                                if (long2.compareTo(long1) > 0) {
//                                    latdata = lat2.add(num);
//                                    latdatafan = lat2.subtract(num);
//                                } else if (long2.compareTo(long1) < 0) {
//                                    latdata = lat2.subtract(num);
//                                    latdatafan = lat2.add(num);
//                                }
//                            }
//                            ArrayList newlist = new ArrayList<>();
//                            ArrayList newlistfan = new ArrayList<>();
//                            newlist.add(longdata);
//                            newlist.add(latdata);
//                            newlistfan.add(longdatafan);
//                            newlistfan.add(latdatafan);
//                            listfan.add(newlistfan);
//                            list.add(newlist);
//
//                        } else {
//                            isSanjiaoxing(type, list, listfan, i, long1, lat1, long2, lat2);
//                        }
//                    }
//
//                }
//                ArrayList<ArrayList> oneSemicircle = new ArrayList<>();
//                ArrayList<ArrayList> twoSemicircle = new ArrayList<>();
//                if (list.size() > 0 && listfan.size() > 0) {
//                    oneSemicircle = getSemicircle(list.get(0), coordinates.get(0), type, true);
//                    twoSemicircle = getSemicircle(list.get(list.size() - 1), coordinates.get(coordinates.size() - 1), type, false);
//                }
//                Collections.reverse(listfan);
//                listdata.addAll(oneSemicircle);
//                listdata.addAll(list);
//                listdata.addAll(twoSemicircle);
//                listdata.addAll(listfan);
//                String str = "{\"type\": \"Feature\",\"properties\": {\"type\": \"polyline\",\"style\": {\"color\": \"#ff0000\", \"opacity\": 0.5,\"outline\": true,\"clampToGround\": true},\"group\": \"默认分组\"},\"geometry\": {\"type\": \"LineString\",\"coordinates\": " + listdata + "} }";
//                Map<String, Object> newparse = (Map<String, Object>) JSON.parse(str);
////            features.add(newparse); //在原有内容上添加
////            mapparse.put("features",features);
//                list1.add(newparse);
//            }
//        }
//
//
//        mapparse.put("features", list1);
//        if (type == 1000) {
//            hhReachInfo.setRange1000(mapparse.toString());
//        } else if (type == 5000) {
//            hhReachInfo.setRange5000(mapparse.toString());
//        } else if (type == 10000) {
//            hhReachInfo.setRange10000(mapparse.toString());
//        }
//    }
    private ArrayList<ArrayList> getSemicircle(ArrayList arrayList, List<String> strings, int type, boolean flag) {
        BigDecimal long1 = (BigDecimal) arrayList.get(0);
        List<BigDecimal> objects1 = (List<BigDecimal>) Convert.toList(strings);
        BigDecimal long2 = objects1.get(0);
        BigDecimal lat1 = (BigDecimal) arrayList.get(1);
        BigDecimal lat2 = objects1.get(1);
        BigDecimal angle = new BigDecimal(0);
        BigDecimal lomglat = new BigDecimal(0.00001);
        BigDecimal num = lomglat.multiply(new BigDecimal(type)).setScale(8, RoundingMode.FLOOR);
        ArrayList<ArrayList> data = new ArrayList<>();
        //  平行于经纬度
        if (long2.compareTo(long1) == 0 || lat2.compareTo(lat1) == 0) {
            if (long2.compareTo(long1) == 0) {
                if (lat2.compareTo(lat1) > 0) {
                    angle = flag ? new BigDecimal(90) : new BigDecimal(270);

                }
                if (lat2.compareTo(lat1) < 0) {
                    angle = flag ? new BigDecimal(270) : new BigDecimal(90);
                }
            }

            if (lat2.compareTo(lat1) == 0) {
                if (long2.compareTo(long1) > 0) {
                    angle = flag ? new BigDecimal(360) : new BigDecimal(180);

                }
                if (long2.compareTo(long1) < 0) {
                    angle = flag ? new BigDecimal(180) : new BigDecimal(360);
                }
            }

        } else {

//            double degrees = Math.acos((a*a +  b*b -c*c)/(2.0*a*b));
//            // 用角度表示的角
//            Math.toDegrees(degrees);

            if (long2.compareTo(long1) > 0 && lat2.compareTo(lat1) > 0) {
                BigDecimal c = lat2.subtract(lat1);
                BigDecimal b = long2.subtract(long1);
                BigDecimal a = num;
                double degrees = getjiaodu(c, b, a);
                if (flag) {
                    angle = new BigDecimal((180 + degrees - 180) < 0 ? 360.0 + (180 + degrees - 180) : 180 + degrees - 180).setScale(12, RoundingMode.FLOOR);
                } else {
                    angle = new BigDecimal(180 + degrees).setScale(12, RoundingMode.FLOOR);
                }
                //右下画
            } else if (long2.compareTo(long1) > 0 && lat2.compareTo(lat1) < 0) {
                BigDecimal c = lat1.subtract(lat2);
                BigDecimal b = num;
                BigDecimal a = long2.subtract(long1);
                double degrees = getjiaodu(c, b, a);
                if (flag) {
                    angle = new BigDecimal((180 - degrees - 180) < 0 ? 360.0 + (180 - degrees - 180) : 180 - degrees - 180).setScale(12, RoundingMode.FLOOR);
                } else {
                    angle = new BigDecimal(180 - degrees).setScale(12, RoundingMode.FLOOR);
                }
                //左上画
            } else if (long2.compareTo(long1) < 0 && lat2.compareTo(lat1) > 0) {
                BigDecimal c = lat2.subtract(lat1);
                BigDecimal b = num;
                BigDecimal a = long1.subtract(long2);
                double degrees = getjiaodu(c, b, a);
                if (flag) {
                    angle = new BigDecimal((360.0 - degrees - 180) < 0 ? 360.0 + (360.0 - degrees - 180) : 360.0 - degrees - 180).setScale(12, RoundingMode.FLOOR);
                } else {
                    angle = new BigDecimal(360.0 - degrees).setScale(12, RoundingMode.FLOOR);
                }
                //坐下画
            } else if (long2.compareTo(long1) < 0 && lat2.compareTo(lat1) < 0) {
                BigDecimal c = lat1.subtract(lat2);
                BigDecimal b = num;
                BigDecimal a = long1.subtract(long2);
                double degrees = getjiaodu(c, b, a);
                if (flag) {
                    angle = new BigDecimal((degrees - 180) < 0 ? 360.0 + (degrees - 180) : degrees - 180).setScale(12, RoundingMode.FLOOR);
                } else {
                    angle = new BigDecimal(degrees).setScale(12, RoundingMode.FLOOR);
                }
            }


        }


//            x1 = x0 + r * cos(a0 * π/180)
//            y1 = y0 + r * sin(a0 * π/180)
        if (angle.compareTo(new BigDecimal(0)) > 0) {
            for (int i = 1; i < 17; i++) {
                BigDecimal newangle = angle.subtract(new BigDecimal(i * 10));
                newangle = newangle.compareTo(BigDecimal.ZERO) < 1 ? (new BigDecimal(360)).add(newangle) : newangle;
                BigDecimal longdata = long2.add(num.multiply(BigDecimal.valueOf(Math.cos(Double.valueOf(String.valueOf(newangle)) * Math.PI / 180.0))).setScale(12, RoundingMode.FLOOR));
                BigDecimal latdata = lat2.add(num.multiply(BigDecimal.valueOf(Math.sin(Double.valueOf(String.valueOf(newangle)) * Math.PI / 180.0))).setScale(12, RoundingMode.FLOOR));
                ArrayList<BigDecimal> newlist = new ArrayList<>();
                newlist.add(longdata);
                newlist.add(latdata);
                data.add(newlist);
            }
        }


        return data;
    }

    private double getjiaodu(BigDecimal c, BigDecimal b, BigDecimal a) {

        //a2+b2-c2*   /   2*ab
        BigDecimal aa = a.multiply(a);
        BigDecimal bb = b.multiply(b);
        BigDecimal cc = b.multiply(c);
        BigDecimal abc = aa.add(bb).subtract(cc);
        BigDecimal ab2 = (new BigDecimal(2.0)).multiply(a).multiply(b);
        BigDecimal divide = abc.divide(ab2, 26, RoundingMode.FLOOR);
        double degrees = Math.toDegrees(Math.acos(new Double(String.valueOf(divide))));
        return degrees;
    }

    private void isSanjiaoxing(int type, ArrayList<ArrayList> list, ArrayList<ArrayList> listfan, int i, BigDecimal long1, BigDecimal lat1, BigDecimal long2, BigDecimal lat2) {
        BigDecimal subtract = long2.subtract(long1);
        BigDecimal subtract1 = lat2.subtract(lat1);
        BigDecimal mu = new BigDecimal(100000000);
        BigDecimal longabs = subtract.abs().multiply(mu).setScale(0, RoundingMode.FLOOR);
        BigDecimal latabs = subtract1.abs().multiply(mu).setScale(0, RoundingMode.FLOOR);
        //斜边的长度
        BigDecimal sqrt = BigDecimal.valueOf(Math.sqrt(Math.pow(Double.valueOf(String.valueOf(longabs)), 2) + Math.pow(Double.valueOf(String.valueOf(latabs)), 2))).setScale(8, RoundingMode.FLOOR);
        //米数
        BigDecimal lomglat = new BigDecimal(0.00001);
        //1千米的经纬度度数
        BigDecimal multiply = lomglat.multiply(new BigDecimal(type)).setScale(8, RoundingMode.FLOOR);
        //
        BigDecimal bizhi = multiply.divide(sqrt, 12, RoundingMode.FLOOR);
        BigDecimal longnum = bizhi.multiply(longabs).setScale(8, RoundingMode.FLOOR);
        BigDecimal latnum = bizhi.multiply(latabs).setScale(8, RoundingMode.FLOOR);
//                    break;
        ArrayList<Object> newlist = new ArrayList<>();
        ArrayList<Object> newlistfan = new ArrayList<>();
        //右上画
        if (long2.compareTo(long1) > 0 && lat2.compareTo(lat1) > 0) {
            if (i == 1) {
                ArrayList<Object> listone = new ArrayList<>();
                listone.add(long1.subtract(latnum));
                listone.add(lat1.add(longnum));
                list.add(listone);
                ArrayList<Object> listonefan = new ArrayList<>();
                listonefan.add(long1.add(latnum));
                listonefan.add(lat1.subtract(longnum));
                listfan.add(listonefan);
            }
            newlist.add(long2.subtract(latnum));
            newlist.add(lat2.add(longnum));
            newlistfan.add(long2.add(latnum));
            newlistfan.add(lat2.subtract(longnum));
            //右下画
        } else if (long2.compareTo(long1) > 0 && lat2.compareTo(lat1) < 0) {
            if (i == 1) {
                ArrayList<Object> listone = new ArrayList<>();
                listone.add(long1.add(latnum));
                listone.add(lat1.add(longnum));
                list.add(listone);

                ArrayList<Object> listonefan = new ArrayList<>();
                listonefan.add(long1.subtract(latnum));
                listonefan.add(lat1.subtract(longnum));
                listfan.add(listonefan);
            }
            newlist.add(long2.add(latnum));
            newlist.add(lat2.add(longnum));
            newlistfan.add(long2.subtract(latnum));
            newlistfan.add(lat2.subtract(longnum));
            //左上画
        } else if (long2.compareTo(long1) < 0 && lat2.compareTo(lat1) > 0) {
            if (i == 1) {
                ArrayList<Object> listone = new ArrayList<>();
                listone.add(long1.subtract(latnum));
                listone.add(lat1.subtract(longnum));
                list.add(listone);
                ArrayList<Object> listonefan = new ArrayList<>();
                listonefan.add(long1.add(latnum));
                listonefan.add(lat1.add(longnum));
                listfan.add(listonefan);
            }
            newlist.add(long2.subtract(latnum));
            newlist.add(lat2.subtract(longnum));
            newlistfan.add(long2.add(latnum));
            newlistfan.add(lat2.add(longnum));
            //坐下画
        } else if (long2.compareTo(long1) < 0 && lat2.compareTo(lat1) < 0) {
            if (i == 1) {
                ArrayList<Object> listone = new ArrayList<>();
                listone.add(long1.add(latnum));
                listone.add(lat1.subtract(longnum));
                list.add(listone);
                ArrayList<Object> listonefan = new ArrayList<>();
                listonefan.add(long1.subtract(latnum));
                listonefan.add(lat1.add(longnum));
                listfan.add(listonefan);
            }
            newlist.add(long2.add(latnum));
            newlist.add(lat2.subtract(longnum));
            newlistfan.add(long2.subtract(latnum));
            newlistfan.add(lat2.add(longnum));
        }
        listfan.add(newlistfan);
        list.add(newlist);
    }

    /**
     * 修改巡检路线
     *
     * @param XjInspectionRoutes 巡检路线
     * @return 结果
     */
    @Override
    public int updateXjInspectionRoutes(XjInspectionRoutes XjInspectionRoutes) {
        XjInspectionRoutes.setUpdateTime(DateUtils.getNowDate());
//        if (XjInspectionRoutes.getQrCodeId() != null && XjInspectionRoutes.getQrCodeId() != 0) {
//            KlxjQrCode klxjQrCode = klxjQrCodeMapper.selectKlxjQrCodeById(XjInspectionRoutes.getQrCodeId());
//            XjInspectionRoutes XjInspectionRoutes1 = selectXjInspectionRoutesByQrSn(klxjQrCode.getCodeSn());
//            if (XjInspectionRoutes1 != null && !XjInspectionRoutes1.getId().equals(XjInspectionRoutes.getId())) {
//                throw new RuntimeException("二维码已被绑定");
//            }
//            XjInspectionRoutes.setQrCodeSn(klxjQrCode.getCodeSn());
//        }
//        if (XjInspectionRoutes.getEngineeringObjectType() != null && XjInspectionRoutes.getEngineeringObjectId() != null && XjInspectionRoutes.getEngineeringObjectId() != 0L) {
//            KlxjEngineeringVo klxjEngineeringVo = new KlxjEngineeringVo();
//            klxjEngineeringVo.setEngineeringObjectType(XjInspectionRoutes.getEngineeringObjectType());
//            klxjEngineeringVo.setEngineeringObjectId(XjInspectionRoutes.getEngineeringObjectId().toString());
//            Object engineeringData = getEngineeringData(klxjEngineeringVo);
//            setRouteDataByEngineeringObjectId(XjInspectionRoutes, klxjEngineeringVo, engineeringData);
//        }
        return XjInspectionRoutesMapper.updateXjInspectionRoutes(XjInspectionRoutes);
    }

    /**
     * 批量删除巡检路线
     *
     * @param ids 需要删除的巡检路线主键
     * @return 结果
     */
    @Override
    public int deleteXjInspectionRoutesByIds(Long[] ids) {
        return XjInspectionRoutesMapper.deleteXjInspectionRoutesByIds(ids);
    }

    /**
     * 删除巡检路线信息
     *
     * @param id 巡检路线主键
     * @return 结果
     */
    @Override
    public int deleteXjInspectionRoutesById(Long id) {
        return XjInspectionRoutesMapper.deleteXjInspectionRoutesById(id);
    }

    @Override
    public String importData(List<XjInspectionRoutes> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0) {
            throw new ServiceException("导入巡检对象数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<String> collect = userList.stream().filter(t -> t.getQrCodeSn() != null && !t.getQrCodeSn().equals("")).map(t -> t.getQrCodeSn()).collect(Collectors.toList());
        Map<String, Long> idmap = new HashMap<>();
//        if (collect != null && collect.size() > 0) {
//            List<KlxjQrCode> qrCodeSn = klxjQrCodeMapper.selectKlxjQrCodeList(new KlxjQrCode() {{
//                setParams(new HashMap<String, Object>() {{
//                    put("qrCodeSns", collect);
//                }});
//            }});
//            idmap = qrCodeSn.stream().collect(Collectors.toMap(t -> t.getCodeSn(), t -> t.getId(), (key1, key2) -> key2));
//        }
        HashMap<String, List<Object>> EngineeringMap = new HashMap<>();

//        KlxjEngineeringVo klxjEngineeringVo = new KlxjEngineeringVo();
//        klxjEngineeringVo.setEngineeringObjectType(SHUIZHA_TYPE.getType());
//        List<Object> shuizhaList = getEngineeringList(klxjEngineeringVo);
////        Map<String, BaseSluiceInfo> collect1 = ;
//        EngineeringMap.put(SHUIZHA_TYPE.getType(), shuizhaList);
//        klxjEngineeringVo.setEngineeringObjectType(BENGFANG_TYPE.getType());
//        List<Object> bengfangList = getEngineeringList(klxjEngineeringVo);
////        Map<String, BaseSluiceInfo> collect1 = ;
//        EngineeringMap.put(BENGFANG_TYPE.getType(), bengfangList);
//        klxjEngineeringVo.setEngineeringObjectType(SHUIKU_TYPE.getType());
//        List<Object> shuikuList = getEngineeringList(klxjEngineeringVo);
////        Map<String, BaseSluiceInfo> collect1 = ;
//        EngineeringMap.put(SHUIKU_TYPE.getType(), shuikuList);
//        klxjEngineeringVo.setEngineeringObjectType(DUCAO_TYPE.getType());
//        List<Object> ducaoList = getEngineeringList(klxjEngineeringVo);
////        Map<String, BaseSluiceInfo> collect1 = ;
//        EngineeringMap.put(DUCAO_TYPE.getType(), ducaoList);
//
//
//        for (XjInspectionRoutes user : userList) {
//            try {
//                user.setQrCodeId(idmap.get(user.getQrCodeSn()));
//                // 验证是否存在这个用户
//                XjInspectionRoutes u = XjInspectionRoutesMapper.selectXjInspectionRoutesByName(user.getName());
//                if (StringUtils.isNull(u)) {
//                    if (StringUtils.isNotEmpty(user.getEngineeringObjectType())) {
//                        if (EngineeringMap.get(user.getEngineeringObjectType()) != null) {
//                            if ((user.getEngineeringObjectId() == null && StringUtils.isNotEmpty(user.getName())) || user.getEngineeringObjectId() == 0L && StringUtils.isNotEmpty(user.getName())) {
//                                switch (EngineeringObjectType.getName(user.getEngineeringObjectType())) {
//                                    case SHUIZHA_TYPE:
//                                        List<Object> objects = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects) {
//                                            BaseSluiceInfo basePumpHouseInfo = BeanUtil.toBean(t, BaseSluiceInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                                break;
//                                            }
//                                        }
//                                        break;
//                                    case BENGFANG_TYPE:
//                                        List<Object> objects1 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects1) {
//                                            BasePumpHouseInfo basePumpHouseInfo = BeanUtil.toBean(t, BasePumpHouseInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//
//                                        break;
//                                    case SHUIKU_TYPE:
//                                        List<Object> objects2 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects2) {
//                                            BaseReservoirInfo basePumpHouseInfo = BeanUtil.toBean(t, BaseReservoirInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//
//                                        break;
//                                    case DUCAO_TYPE:
//                                        List<Object> objects3 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects3) {
//                                            KlAqueduct basePumpHouseInfo = BeanUtil.toBean(t, KlAqueduct.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//                                        break;
//                                }
//                            }
//                        }
//                    }
//                    user.setCreateBy(operName);
//                    this.insertXjInspectionRoutes(user);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、水库 " + user.getName() + " 导入成功");
//                } else if (updateSupport) {
//
//                    if (StringUtils.isNotEmpty(user.getEngineeringObjectType())) {
//                        if (EngineeringMap.get(user.getEngineeringObjectType()) != null) {
//                            if (u.getEngineeringObjectId() != null && !u.getEngineeringObjectId().equals(0L) && (user.getEngineeringObjectId() == null || user.getEngineeringObjectId() == 0L)) {
//                                user.setEngineeringObjectId(u.getEngineeringObjectId());
//                            }
//                            if ((user.getEngineeringObjectId() == null && StringUtils.isNotEmpty(user.getName())) || user.getEngineeringObjectId() == 0L && StringUtils.isNotEmpty(user.getName())) {
//                                switch (EngineeringObjectType.getName(user.getEngineeringObjectType())) {
//                                    case SHUIZHA_TYPE:
//                                        List<Object> objects = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects) {
//                                            BaseSluiceInfo basePumpHouseInfo = BeanUtil.toBean(t, BaseSluiceInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                                break;
//                                            }
//                                        }
//                                        break;
//                                    case BENGFANG_TYPE:
//                                        List<Object> objects1 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects1) {
//                                            BasePumpHouseInfo basePumpHouseInfo = BeanUtil.toBean(t, BasePumpHouseInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//
//                                        break;
//                                    case SHUIKU_TYPE:
//                                        List<Object> objects2 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects2) {
//                                            BaseReservoirInfo basePumpHouseInfo = BeanUtil.toBean(t, BaseReservoirInfo.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//
//                                        break;
//                                    case DUCAO_TYPE:
//                                        List<Object> objects3 = EngineeringMap.get(user.getEngineeringObjectType());
//                                        for (Object t : objects3) {
//                                            KlAqueduct basePumpHouseInfo = BeanUtil.toBean(t, KlAqueduct.class);
//                                            if (basePumpHouseInfo.getName().contains(user.getName())) {
//                                                user.setEngineeringObjectId(basePumpHouseInfo.getId());
//                                            }
//                                        }
//                                        break;
//                                }
//                            }
//                        }
//                    }
//
//
//                    user.setUpdateBy(operName);
//                    user.setId(u.getId());
//                    this.updateXjInspectionRoutes(user);
//                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、水库 " + user.getName() + " 更新成功");
//                } else {
//                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、水库 " + user.getName() + " 已存在");
//                }
//            } catch (Exception e) {
//                failureNum++;
//                String msg = "<br/>" + failureNum + "、水库 " + user.getName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
//            }
//        }
//        if (failureNum > 0) {
//            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
//            throw new ServiceException(failureMsg.toString());
//        } else {
//            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
//        }
        return "";
    }

    @Override
    public XjInspectionRoutes selectXjInspectionRoutesByQrSn(String qrSn) {
        XjInspectionRoutes XjInspectionRoutes = XjInspectionRoutesMapper.selectXjInspectionRoutesByQrSn(qrSn);
        if (XjInspectionRoutes != null && XjInspectionRoutes.getInspectionNoteIds() != null && !XjInspectionRoutes.getInspectionNoteIds().equals("")) {
            XjInspectionNote XjInspectionNote = new XjInspectionNote();
            XjInspectionNote.setParams(new HashMap<String, Object>() {{
                put("ids", XjInspectionRoutes.getInspectionNoteIds());
            }});
            List<XjInspectionNote> XjInspectionNotes = iXjInspectionNoteService.selectXjInspectionNoteList(XjInspectionNote);
            XjInspectionRoutes.setInspectionNote(XjInspectionNotes);
        }
        return XjInspectionRoutes;
    }

//    @Override
//    public List<Object> getEngineeringList(KlxjEngineeringVo klxjEngineeringVo) {
//
//        if (!StringUtils.isNotEmpty(klxjEngineeringVo.getEngineeringObjectType())) {
//            throw new RuntimeException("工程类型不能为空");
//        }
//        JSONObject formMap = new JSONObject();
//        String ip = configService.selectConfigByKey("kenli.ip");
//        String username = configService.selectConfigByKey("kenli.user");
//        String password = configService.selectConfigByKey("kenli.password");
//        formMap.set("username", username);
//        formMap.set("password", password);
//        String body = HttpUtil.createPost(ip + "/login").body(formMap.toString()).execute().body();
//        JSONObject jsonObject = new JSONObject(body);
//        if (jsonObject.get("code").toString().equals("200")) {
//            String token = jsonObject.get("token").toString();
//            String Bearer = "Bearer " + token;
//            List<SysDictData> engineering_object_type
//                    = dictTypeService.selectDictDataByType("engineering_object_type");
//            if (engineering_object_type
//                    != null) {
//                Map<String, SysDictData> collect = engineering_object_type.stream().collect(Collectors.toMap(t -> t.getDictValue(), t -> t));
//                SysDictData sysDictData = collect.get(klxjEngineeringVo.getEngineeringObjectType());
//                JSONObject map = new JSONObject();
//                map.set("pageSize", 99999);
//                map.set("pageNum", 1);
//                HashMap<String, String> headers = new HashMap<>();
//                headers.put("Authorization", Bearer);
//                String body1 = HttpUtil.createGet(ip + sysDictData.getParam() + "list").headerMap(headers, false).form(map).execute().body();
//                Object rows = new JSONObject(body1).get("rows");
//                if (rows != null) {
//                    JSONArray dataList = new JSONArray(rows.toString());
//                    return dataList.toBean(List.class);
//                }
//
//            }
//        }
//        return new ArrayList<>();
//    }

//    @Override
//    public Object getEngineeringData(KlxjEngineeringVo klxjEngineeringVo) {
//        if (!StringUtils.isNotEmpty(klxjEngineeringVo.getEngineeringObjectType())) {
//            throw new RuntimeException("工程类型不能为空");
//        }
//        JSONObject formMap = new JSONObject();
//        String ip = configService.selectConfigByKey("kenli.ip");
//        String username = configService.selectConfigByKey("kenli.user");
//        String password = configService.selectConfigByKey("kenli.password");
//        formMap.set("username", username);
//        formMap.set("password", password);
//        String body = HttpUtil.createPost(ip + "/login").body(formMap.toString()).execute().body();
//        JSONObject jsonObject = new JSONObject(body);
//        if (jsonObject.get("code").toString().equals("200")) {
//            String token = jsonObject.get("token").toString();
//            String Bearer = "Bearer " + token;
//            List<SysDictData> engineering_object_type
//                    = dictTypeService.selectDictDataByType("engineering_object_type");
//            if (engineering_object_type
//                    != null) {
//                Map<String, SysDictData> collect = engineering_object_type.stream().collect(Collectors.toMap(t -> t.getDictValue(), t -> t));
//                SysDictData sysDictData = collect.get(klxjEngineeringVo.getEngineeringObjectType());
//                HashMap<String, String> headers = new HashMap<>();
//                headers.put("Authorization", Bearer);
//                String body1 = HttpUtil.createGet(ip + sysDictData.getParam() + klxjEngineeringVo.getEngineeringObjectId()).headerMap(headers, false).execute().body();
//                JSONObject data = new JSONObject(body1).getJSONObject("data");
//                if (data != null) {
//                    switch (EngineeringObjectType.getName(klxjEngineeringVo.getEngineeringObjectType())) {
//                        case SHUIZHA_TYPE:
//                            return data.toBean(BaseSluiceInfo.class);
//                        case BENGFANG_TYPE:
//                            return data.toBean(BasePumpHouseInfo.class);
//                        case SHUIKU_TYPE:
//                            return data.toBean(BaseReservoirInfo.class);
//                        case DUCAO_TYPE:
//                            return data.toBean(KlAqueduct.class);
//                    }
//                }
//            }
//        }
//        return null;
//    }


}
