package com.ruoyi.ibs.custmap.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.custmap.domain.dto.CustMapCircleDTO;
import com.ruoyi.ibs.custmap.domain.dto.CustMapPolygonDTO;
import com.ruoyi.ibs.custmap.domain.dto.PolygonDTO;
import com.ruoyi.ibs.custmap.domain.entity.CustInfoRadar;
import com.ruoyi.ibs.custmap.domain.vo.CustMapVO;
import com.ruoyi.ibs.custmap.mapper.CustMapMapper;
import com.ruoyi.ibs.custmap.service.CustMapSelectService;
import com.ruoyi.ibs.grid.service.AddressAnalyseService;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author 吴凯程
 * @Date 2025/5/15
 **/

@Service
public class CustMapSelectServiceImpl implements CustMapSelectService {

    @Resource
    private CustMapMapper custMapMapper;

    @Resource
    private AddressAnalyseService addressAnalyseService;


    /**
     * @param
     * @return
     */
    @Override
    public List<CustMapVO> selectCustListByCircle(CustMapCircleDTO custMapCircleDTO) {
        custMapCircleDTO.setDept(SecurityUtils.getHeadId());
        custMapCircleDTO.setPageIndex((custMapCircleDTO.getNum() - 1) * custMapCircleDTO.getSize());
        custMapCircleDTO.setLatRange(custMapCircleDTO.getDist() / 111.0);
        custMapCircleDTO.setLngRange(custMapCircleDTO.getDist() / (111.0 * Math.cos(Math.toRadians(custMapCircleDTO.getTargetLat()))));
        return custMapMapper.selectCustMapListByCircle(custMapCircleDTO);
    }

    /**
     * @param custMapCircleDTO
     * @return
     */
    @Override
    public Long countCustMapListByCircle(CustMapCircleDTO custMapCircleDTO) {
        custMapCircleDTO.setDept(SecurityUtils.getHeadId());
        custMapCircleDTO.setLatRange(custMapCircleDTO.getDist() / 111.0);
        custMapCircleDTO.setLngRange(custMapCircleDTO.getDist() / (111.0 * Math.cos(Math.toRadians(custMapCircleDTO.getTargetLat()))));
        return custMapMapper.countCustMapListByCircle(custMapCircleDTO);
    }

    /**
     * @param custMapPolygonDTO
     * @return
     */
    @Override
    public List<CustMapVO> selectCustListByPolygon(CustMapPolygonDTO custMapPolygonDTO) {
        if(!addressAnalyseService.checkWktValid(custMapPolygonDTO.getWkt())){
            return new ArrayList<>();
        }
        custMapPolygonDTO.setDept(SecurityUtils.getHeadId());
        custMapPolygonDTO.setPageIndex((custMapPolygonDTO.getNum() - 1) * custMapPolygonDTO.getSize());
        PolygonDTO polygonDTO = getPolygonDTO(custMapPolygonDTO.getWkt());
        BeanUtils.copyProperties(polygonDTO,custMapPolygonDTO);
        return custMapMapper.selectCustMapListByPolygon(custMapPolygonDTO);
    }

    /**
     * @param custMapPolygonDTO
     * @return
     */
    @Override
    public Long countCustMapListByPolygon(CustMapPolygonDTO custMapPolygonDTO) {
        if(!addressAnalyseService.checkWktValid(custMapPolygonDTO.getWkt())){
            return 0L;
        }
        custMapPolygonDTO.setDept(SecurityUtils.getHeadId());
        PolygonDTO polygonDTO = getPolygonDTO(custMapPolygonDTO.getWkt());
        BeanUtils.copyProperties(polygonDTO,custMapPolygonDTO);
        return custMapMapper.countCustMapListByPolygon(custMapPolygonDTO);
    }

    /**
     * @param custId
     * @return
     */
    @Override
    public CustInfoRadar selectCustInfoRadarById(Integer id) {
        return custMapMapper.selectById(id);
    }

    private PolygonDTO getPolygonDTO(String wkt){
        PolygonDTO polygonDTO = new PolygonDTO();
        WKTReader wktReader = new WKTReader();
        try {
            Geometry geometry = wktReader.read(wkt);
            Coordinate[] coordinates = geometry.getCoordinates();
            for (Coordinate coordinate : coordinates) {
                if(Objects.isNull(polygonDTO.getMinLat())){
                    polygonDTO.setMinLat(coordinate.getY());
                }else{
                    polygonDTO.setMinLat(Math.min(coordinate.getY(), polygonDTO.getMinLat()) );
                }
                if (Objects.isNull(polygonDTO.getMinLng())){
                    polygonDTO.setMinLng(coordinate.getX());
                }else {
                    polygonDTO.setMinLng(Math.min(coordinate.getX(), polygonDTO.getMinLng()));
                }
                if (Objects.isNull(polygonDTO.getMaxLat())){
                    polygonDTO.setMaxLat(coordinate.getY());
                }else {
                    polygonDTO.setMaxLat(Math.max(coordinate.getY(), polygonDTO.getMaxLat()));
                }
                if (Objects.isNull(polygonDTO.getMaxLng())){
                    polygonDTO.setMaxLng(coordinate.getX());
                }else {
                    polygonDTO.setMaxLng(Math.max(coordinate.getX(), polygonDTO.getMaxLng()));
                }
            }
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return polygonDTO;
    }


}
