package com.mosty.yszx.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.base.GetDeptListDTO;
import com.mosty.base.model.dto.rwzx.TbRwTaskDto;
import com.mosty.base.model.dto.rwzx.TbRwTaskUserDto;
import com.mosty.base.model.dto.yszx.TbYsGajgAndGlbmDto;
import com.mosty.base.model.dto.yszx.TbYsGajgByJwdDto;
import com.mosty.base.model.dto.yszx.TbYsGajgDto;
import com.mosty.base.model.dto.yszx.TbYsGajgGlbmDto;
import com.mosty.base.model.entity.rwzx.TbRwTask;
import com.mosty.base.model.vo.yszx.TbYsGajgGlbmVo;
import com.mosty.base.utils.*;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.yszx.TbYsGajgQuery;
import com.mosty.common.core.business.entity.vo.Base64Str;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.entity.yszx.TbYsGajg;
import com.mosty.base.model.entity.yszx.TbYsGajgGlbm;
import com.mosty.base.model.vo.yszx.TbYsGajgVo;
import com.mosty.yszx.mapper.TbYsGajgGlbmMapper;
import com.mosty.yszx.mapper.TbYsGajgMapper;
import com.mosty.yszx.remote.TbBaseAdaptRemoteService;
import com.mosty.yszx.remote.TbRwzxAdaptRemoteService;
import com.mosty.yszx.service.TbYsGajgService;
import org.locationtech.jts.geom.Coordinate;
import lombok.AllArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @author dw
 * @since 2022/7/20
 **/
@Service
@AllArgsConstructor
public class TbYsGajgServiceImpl extends ServiceImpl<TbYsGajgMapper, TbYsGajg> implements TbYsGajgService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbYsGajgGlbmMapper tbYsGajgGlbmMapper;
    private final TbRwzxAdaptRemoteService tbRwzxAdaptRemoteService;


    @Override
    public int addGajg(TbYsGajgAndGlbmDto dto) {
        TbYsGajg gajg = new TbYsGajg();
        BeanUtils.copyProperties(dto, gajg);
        gajg.setId(UUIDGenerator.getUUID());
        if (dto.getJd() != null && dto.getWd() != null) {
            gajg.setZb(JtsUtils.getPoint(gajg.getJd(), gajg.getWd()));
            gajg.setZbhash(GeoHashKit.encode(gajg.getJd(), gajg.getWd()));
        }
        if (dto.getQy() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
                if (list.size() > 0) {
                    gajg.setDtm(JtsUtils.getPolygon(list));
                }
            }
        }
        if (dto.getQy1() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy1();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm1(JtsUtils.getPolygon(list));
            }
        }
        if (dto.getQy2() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy2();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm2(JtsUtils.getPolygon(list));
            }
        }
        gajg.setJsnr(dto.getGajgmc() + " " + dto.getGajgjc() + " " + dto.getWzXz() + " " + dto.getWzBc());
        gajg.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmdm());
        if (dept != null) {
            gajg.setSsbm(dept.getDeptname());
            gajg.setSsbmid(dept.getDeptid());
            gajg.setSsbmdm(dept.getDeptcode());
            gajg.setSsxgajdm(dept.getFxjcode());
            gajg.setSsxgajid(dept.getFxjid());
            gajg.setSsxgaj(dept.getFxjname());
            gajg.setSssgaj(dept.getDszname());
            gajg.setSssgajdm(dept.getDszcode());
            gajg.setSssgajid(dept.getDszid());
        }
        List<TbYsGajgGlbmDto> dtoList = dto.getGlbmDtoList();
        dtoList.forEach(item -> {
            TbYsGajgGlbm bm = new TbYsGajgGlbm();
            bm.setId(UUIDGenerator.getUUID());
            bm.setGajgId(gajg.getId());
            DeptInfoVo vo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            if (vo != null) {
                bm.setSsbmdm(vo.getDeptcode());
                bm.setSsbm(vo.getDeptname());
                bm.setSsbmid(vo.getDeptid());
                bm.setSsxgaj(vo.getFxjname());
                bm.setSsxgajid(vo.getFxjid());
                bm.setSsxgajdm(vo.getFxjcode());
                bm.setSssgaj(vo.getDszname());
                bm.setSssgajdm(vo.getDszcode());
                bm.setSssgajid(vo.getDszid());
            }
            bm.setXtSjly("1");
            this.tbYsGajgGlbmMapper.insert(bm);
        });
        gajg.setXtSjly("1");
        // 添加关联部门
        return this.baseMapper.insertEntity(gajg);
    }

    @Override
    @Transactional
    public void editGajg(TbYsGajgAndGlbmDto dto) {
        TbYsGajg gajg = new TbYsGajg();
        BeanUtils.copyProperties(dto, gajg);
        if (dto.getJd() != null && dto.getWd() != null) {
            gajg.setZb(JtsUtils.getPoint(gajg.getJd(), gajg.getWd()));
            gajg.setZbhash(GeoHashKit.encode(gajg.getJd(), gajg.getWd()));
        }
        if (dto.getQy() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
                if (list.size() > 0) {
                    gajg.setDtm(JtsUtils.getPolygon(list));
                }
            }
        }
        if (dto.getQy1() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy1();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm1(JtsUtils.getPolygon(list));
            }
        }
        if (dto.getQy2() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy2();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm2(JtsUtils.getPolygon(list));
            }
        }
        gajg.setJsnr(dto.getGajgmc() + " " + dto.getGajgjc() + " " + dto.getWzXz() + " " + dto.getWzBc());
        gajg.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmdm());
        if (dept != null) {
            gajg.setSsbm(dept.getDeptname());
            gajg.setSsbmid(dept.getDeptid());
            gajg.setSsbmdm(dept.getDeptcode());
            gajg.setSsxgajdm(dept.getFxjcode());
            gajg.setSsxgajid(dept.getFxjid());
            gajg.setSsxgaj(dept.getFxjname());
            gajg.setSssgaj(dept.getDszname());
            gajg.setSssgajdm(dept.getDszcode());
            gajg.setSssgajid(dept.getDszid());
        }
        List<TbYsGajgGlbmDto> dtoList = dto.getGlbmDtoList();
        // 删除之前的配置
        this.deleteGlbm(gajg.getId());
        dtoList.forEach(item -> {
            TbYsGajgGlbm bm = new TbYsGajgGlbm();
            bm.setId(UUIDGenerator.getUUID());
            bm.setGajgId(gajg.getId());
            DeptInfoVo vo = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            if (vo != null) {
                bm.setSsbmdm(vo.getDeptcode());
                bm.setSsbm(vo.getDeptname());
                bm.setSsbmid(vo.getDeptid());
                bm.setSsxgaj(vo.getFxjname());
                bm.setSsxgajid(vo.getFxjid());
                bm.setSsxgajdm(vo.getFxjcode());
                bm.setSssgaj(vo.getDszname());
                bm.setSssgajdm(vo.getDszcode());
                bm.setSssgajid(vo.getDszid());
            }
            bm.setXtSjly("1");
            this.tbYsGajgGlbmMapper.insert(bm);
        });
        gajg.setXtSjly("1");
        // 添加关联部门
        this.baseMapper.updateEntity(gajg);
        // 添加任务提醒
        UserInfo user = UserInfoManager.get();
        this.addTask(gajg, user);
    }

    // 添加任务提醒功能
    private void addTask(TbYsGajg gajg, UserInfo user) {
        TbRwTaskDto dto = new TbRwTaskDto();
        String rwmc = "公安机关位置变更提醒";
        String rwnr = gajg.getGajgmc() + "信息发生变更，请知晓！！";
        dto.setRwmc(rwmc).setRwnr(rwnr)
                .setRwjsdx("02").setSfaqwc("0").setRwlx("1").setRwxl("06").setRwjjcd("3")
                .setRwms("2").setSsbmdm(gajg.getSsbmdm()).setSsbm(gajg.getSsbm()).setRwxxlx("WZBG").setSsbmid(gajg.getSsbmid());
        if (user != null) {
            dto.setCjrid(String.valueOf(user.getUserId())).setCjrxm(user.getUserName())
                    .setCjrsfzh(user.getIdEntityCard()).setSsbm(user.getDeptName()).setSsbmdm(String.valueOf(user.getDeptId()));
        } else {
            dto.setCjrid("-1").setCjrxm("系统").setCjrsfzh("000000");
        }
        // 添加执行人
        List<TbRwTaskUserDto> userDtoList = new ArrayList<>();
        TbRwTaskUserDto userDto = new TbRwTaskUserDto();
        userDto.setRwzflx("02").setJsbmdm(gajg.getSssgajdm()).setJsbmmc(gajg.getSssgaj());
        userDtoList.add(userDto);
        dto.setUserDtoList(userDtoList);
        this.tbRwzxAdaptRemoteService.addRw(dto);
    }

    @Override
    public List<TbYsGajgGlbmDto> getGlbmList(String id) {
        List<TbYsGajgGlbm> list = this.tbYsGajgGlbmMapper.selectList(
                new LambdaQueryWrapper<TbYsGajgGlbm>()
                        .eq(TbYsGajgGlbm::getXtSjzt, "1")
                        .eq(TbYsGajgGlbm::getGajgId, id)
        );
        List<TbYsGajgGlbmDto> rList = new ArrayList<>();
        list.forEach(item -> {
            TbYsGajgGlbmDto dto = new TbYsGajgGlbmDto();
            BeanUtils.copyProperties(item, dto);
            rList.add(dto);
        });
        return rList;
    }

    @Override
    public List<TbYsGajgGlbmVo> getGajgList(TbYsGajgQuery dto) {
        List<String> ysxlList = StringUtils.isEmpty(dto.getYsxl()) ? null : Arrays.asList(dto.getYsxl().split(","));
        Map<String, Object> map = new HashMap<>();
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        map.put("dto", dto);
//        UserInfo user = UserInfoManager.getUser();
//        String useSql = user != null ? PermissionsUtil.createSql("", user) : "";
//        map.put("useSql", useSql);
        map.put("ysxlList", ysxlList);
        List<TbYsGajg> list = this.baseMapper.getList(map);
        List<TbYsGajgGlbmVo> voList = new ArrayList<>();
        list.forEach(item -> {
            TbYsGajgGlbmVo vo = new TbYsGajgGlbmVo();
            BeanUtils.copyProperties(item, vo);
            // 查询关联部门
            List<TbYsGajgGlbmDto> glbmList = this.getGlbmList(item.getId());
            List<String> glbms = (glbmList != null && glbmList.size() != 0) ?
                    glbmList.stream().map(TbYsGajgGlbmDto::getSsbmid).collect(Collectors.toList()) : new ArrayList<>();
            glbms.add(item.getSsbmid());
            vo.setGlbmSsbmdmList(glbms);
            voList.add(vo);
        });
        return voList;
    }

    private void deleteGlbm(String id) {
        this.tbYsGajgGlbmMapper.deleteGlbm(id);
    }

    @Override
    public int insertEntity(TbYsGajgDto dto) {
        TbYsGajg gajg = new TbYsGajg();
        BeanUtils.copyProperties(dto, gajg);
        gajg.setId(UUIDGenerator.getUUID());
        if (dto.getJd() != null && dto.getWd() != null) {
            gajg.setZb(JtsUtils.getPoint(gajg.getJd(), gajg.getWd()));
            gajg.setZbhash(GeoHashKit.encode(gajg.getJd(), gajg.getWd()));
        }
        if (dto.getQy() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
                if (list.size() > 0) {
                    gajg.setDtm(JtsUtils.getPolygon(list));
                }
            }
        }
        if (dto.getQy1() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy1();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm1(JtsUtils.getPolygon(list));
            }
        }
        if (dto.getQy2() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy2();
            if (!CollectionUtils.isEmpty(qw)) {
                for (int i = 0; i < qw.size(); i++) {
                    if (i != qw.size() - 1 && i % 2 == 0) {
                        BigDecimal[] des = new BigDecimal[2];
                        des[0] = qw.get(i);
                        des[1] = qw.get(i + 1);
                        list.add(des);
                    }
                }
            }
            if (list.size() > 0) {
                gajg.setDtm2(JtsUtils.getPolygon(list));
            }
        }
        gajg.setJsnr(dto.getGajgmc() + " " + dto.getGajgjc() + " " + dto.getWzXz() + " " + dto.getWzBc());
        gajg.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmdm());
        if (dept != null) {
            gajg.setSsbm(dept.getDeptname());
            gajg.setSsbmid(dept.getDeptid());
            gajg.setSsbmdm(dept.getDeptcode());
            gajg.setSsxgajdm(dept.getFxjcode());
            gajg.setSsxgajid(dept.getFxjid());
            gajg.setSsxgaj(dept.getFxjname());
            gajg.setSssgaj(dept.getDszname());
            gajg.setSssgajdm(dept.getDszcode());
            gajg.setSssgajid(dept.getDszid());
        }
        return this.baseMapper.insertEntity(gajg);
    }

    @Override
    public void updateEntity(TbYsGajgDto dto) {
        TbYsGajg gajg = new TbYsGajg();
        BeanUtils.copyProperties(dto, gajg);
        if (dto.getJd() != null && dto.getWd() != null) {
            gajg.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            gajg.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        // 一分钟范围
        ArrayList<BigDecimal[]> list = new ArrayList<>();
        List<BigDecimal> qw = dto.getQy();
        if (!CollectionUtils.isEmpty(qw)) {
            for (int i = 0; i < qw.size(); i++) {
                if (i != qw.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw.get(i);
                    des[1] = qw.get(i + 1);
                    list.add(des);
                }
            }
        }
        if (list.size() > 0) {
            gajg.setDtm(JtsUtils.getPolygon(list));
        }
        // 三分钟范围
        ArrayList<BigDecimal[]> list1 = new ArrayList<>();
        List<BigDecimal> qw1 = dto.getQy1();
        if (!CollectionUtils.isEmpty(qw1)) {
            for (int i = 0; i < qw1.size(); i++) {
                if (i != qw1.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw1.get(i);
                    des[1] = qw1.get(i + 1);
                    list1.add(des);
                }
            }
        }
        if (list1.size() > 0) {
            gajg.setDtm1(JtsUtils.getPolygon(list1));
        }
        // 三分钟范围
        ArrayList<BigDecimal[]> list2 = new ArrayList<>();
        List<BigDecimal> qw2 = dto.getQy1();
        if (!CollectionUtils.isEmpty(qw2)) {
            for (int i = 0; i < qw2.size(); i++) {
                if (i != qw2.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw2.get(i);
                    des[1] = qw2.get(i + 1);
                    list2.add(des);
                }
            }
        }
        if (list2.size() > 0) {
            gajg.setDtm2(JtsUtils.getPolygon(list2));
        }
        this.baseMapper.updateEntity(gajg);
        gajg = this.baseMapper.getInfoById(gajg.getId());
        gajg.setJsnr(gajg.getGajgmc() + " " + gajg.getGajgjc() + " " + gajg.getWzXz() + " " + gajg.getWzBc());
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
        if (dept != null) {
            gajg.setSsbm(dept.getDeptname());
            gajg.setSsbmid(dept.getDeptid());
            gajg.setSsbmdm(dept.getDeptcode());
            gajg.setSsxgajdm(dept.getFxjcode());
            gajg.setSsxgajid(dept.getFxjid());
            gajg.setSsxgaj(dept.getFxjname());
            gajg.setSssgaj(dept.getDszname());
            gajg.setSssgajdm(dept.getDszcode());
            gajg.setSssgajid(dept.getDszid());
        }
        this.baseMapper.updateEntity(gajg);
    }

    @Override
    public void updateEntityByLY(TbYsGajgDto dto) {
        TbYsGajg gajg = this.baseMapper.selectOne(new LambdaQueryWrapper<TbYsGajg>()
                .eq(TbYsGajg::getLyid, dto.getLyid())
                .eq(TbYsGajg::getLybm, dto.getLybm()));
        dto.setId(gajg.getId());
        BeanUtils.copyProperties(dto, gajg);
        if (dto.getJd() != null && dto.getWd() != null) {
            gajg.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            gajg.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        // 一分钟圈层
        ArrayList<BigDecimal[]> list = new ArrayList<>();
        List<BigDecimal> qw = dto.getQy();
        if (!CollectionUtils.isEmpty(qw)) {
            for (int i = 0; i < qw.size(); i++) {
                if (i != qw.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw.get(i);
                    des[1] = qw.get(i + 1);
                    list.add(des);
                }
            }
        }
        if (list.size() > 0) {
            gajg.setDtm(JtsUtils.getPolygon(list));
        }
        // 三分钟圈层
        ArrayList<BigDecimal[]> list1 = new ArrayList<>();
        List<BigDecimal> qw1 = dto.getQy1();
        if (!CollectionUtils.isEmpty(qw1)) {
            for (int i = 0; i < qw1.size(); i++) {
                if (i != qw1.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw1.get(i);
                    des[1] = qw1.get(i + 1);
                    list1.add(des);
                }
            }
        }
        if (list1.size() > 0) {
            gajg.setDtm1(JtsUtils.getPolygon(list1));
        }
        // 五分钟圈层
        ArrayList<BigDecimal[]> list2 = new ArrayList<>();
        List<BigDecimal> qw2 = dto.getQy2();
        if (!CollectionUtils.isEmpty(qw2)) {
            for (int i = 0; i < qw2.size(); i++) {
                if (i != qw2.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw2.get(i);
                    des[1] = qw2.get(i + 1);
                    list2.add(des);
                }
            }
        }
        if (list2.size() > 0) {
            gajg.setDtm2(JtsUtils.getPolygon(list2));
        }
        this.baseMapper.updateEntity(gajg);
        gajg = this.baseMapper.getInfoById(gajg.getId());
        gajg.setJsnr(gajg.getGajgmc() + " " + gajg.getGajgjc() + " " + gajg.getWzXz() + " " + gajg.getWzBc());
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmdm());
        if (dept != null) {
            gajg.setSsbm(dept.getDeptname());
            gajg.setSsbmid(dept.getDeptid());
            gajg.setSsbmdm(dept.getDeptcode());
            gajg.setSsxgajdm(dept.getFxjcode());
            gajg.setSsxgajid(dept.getFxjid());
            gajg.setSsxgaj(dept.getFxjname());
            gajg.setSssgaj(dept.getDszname());
            gajg.setSssgajdm(dept.getDszcode());
            gajg.setSssgajid(dept.getDszid());
        }
        this.baseMapper.updateEntity(gajg);
    }

    @Override
    public TbYsGajgVo getInfo(String id) {
        TbYsGajg item = this.baseMapper.getInfoById(id);
        TbYsGajgVo vo = null;
        if (item != null) {
            vo = new TbYsGajgVo();
            BeanUtils.copyProperties(item, vo);
            vo.setDtm(JtsUtils.decodePolygon(item.getDtm()));
            vo.setDtm1(JtsUtils.decodePolygon(item.getDtm1()));
            vo.setDtm2(JtsUtils.decodePolygon(item.getDtm2()));
            vo.setZb(new BigDecimal[]{item.getJd(), item.getWd()});
            vo.setGlbmDtoList(
                    this.tbYsGajgGlbmMapper.selectList(
                            new LambdaQueryWrapper<TbYsGajgGlbm>()
                                    .eq(TbYsGajgGlbm::getGajgId, vo.getId())
                                    .eq(TbYsGajgGlbm::getXtSjzt, "1")
                    )
            );
        }
        return vo;
    }

    @Override
    public IPage<TbYsGajgVo> getPageList(TbYsGajgQuery dto) {
        List<String> ysxlList = StringUtils.isEmpty(dto.getYsxl()) ? null : Arrays.asList(dto.getYsxl().split(","));
        IPage<TbYsGajgVo> page;
        Map<String, Object> map = new HashMap<>();
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        map.put("dto", dto);
//        UserInfo user = UserInfoManager.getUser();
//        String useSql = user != null ? PermissionsUtil.createSql("", user) : "";
//        map.put("useSql", useSql);
        map.put("ysxlList", ysxlList);
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            page = new Page<>(dto.getPageNum(), dto.getPageSize());
            map.put("pageIndex", (dto.getPageNum() - 1) * dto.getPageSize());
            map.put("pageSize", dto.getPageSize());
            int count = this.baseMapper.getCount(map);
            List<TbYsGajg> list = this.baseMapper.getListPage(map);
            List<TbYsGajgVo> voList = new ArrayList<>();
            list.forEach(item -> {
                TbYsGajgVo vo = new TbYsGajgVo();
                BeanUtils.copyProperties(item, vo);
                vo.setDtm(JtsUtils.decodePolygon(item.getDtm()));
                vo.setDtm1(JtsUtils.decodePolygon(item.getDtm1()));
                vo.setDtm2(JtsUtils.decodePolygon(item.getDtm2()));
                vo.setZb(new BigDecimal[]{item.getJd(), item.getWd()});
                voList.add(vo);
            });
            page.setTotal(count);
            page.setRecords(voList);
        } else {
            page = new Page<>();
            List<TbYsGajg> list = this.baseMapper.getList(map);
            List<TbYsGajgVo> voList = new ArrayList<>();
            list.forEach(item -> {
                TbYsGajgVo vo = new TbYsGajgVo();
                BeanUtils.copyProperties(item, vo);
                vo.setDtm(JtsUtils.decodePolygon(item.getDtm()));
                vo.setDtm1(JtsUtils.decodePolygon(item.getDtm1()));
                vo.setDtm2(JtsUtils.decodePolygon(item.getDtm2()));
                vo.setZb(new BigDecimal[]{item.getJd(), item.getWd()});
                voList.add(vo);
            });
            page.setRecords(voList);
        }
        return page;
    }

    @Override
    public void delEntity(String id) {
        TbYsGajg item = this.baseMapper.getInfoById(id);
        if (item != null) {
            item.setXtScbz("1");
        }
        this.baseMapper.updateEntity(item);
    }

    @Override
    public void delEntity(TbYsGajgDto dto) {
        TbYsGajg tbYsGajg = this.baseMapper.selectOne(new LambdaQueryWrapper<TbYsGajg>()
                .eq(TbYsGajg::getLyid, dto.getLyid())
                .eq(TbYsGajg::getLybm, dto.getLybm()));
        if (tbYsGajg != null) {
            tbYsGajg.setXtScbz("1");
        }
        this.baseMapper.updateEntity(tbYsGajg);
    }

    @Override
    public TbYsGajgVo getGajgByJwd(TbYsGajgByJwdDto dto) {
        Map<String, Object> map = new HashMap<>();
        TbYsGajgQuery query = new TbYsGajgQuery();
        query.setYsdl(dto.getYsdl());
        query.setYszl(dto.getYszl());
        query.setYsxl(dto.getYsxl());
        map.put("dto", query);
        List<TbYsGajg> list = this.baseMapper.getList(map);
        for (TbYsGajg item : list) {
            List<BigDecimal[]> jwdList = JtsUtils.decodePolygon(item.getDtm());
            Coordinate[] coordinates = new Coordinate[jwdList.size()];
            for (int i = 0; i < jwdList.size(); i++) {
                BigDecimal[] jwd = jwdList.get(i);
                Coordinate coor = new Coordinate();
                coor.x = jwd[0].doubleValue();
                coor.y = jwd[1].doubleValue();
                coordinates[i] = coor;
            }
            boolean flag = Kit.isInGeometry(coordinates, dto.getJd() + "," + dto.getWd());
            if (flag) {
                TbYsGajgVo vo = new TbYsGajgVo();
                BeanUtils.copyProperties(item, vo);
                return vo;
            }
        }
        return null;
    }
}
