package com.chinaweal.aiccs.aiccs.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.market.entity.AreaSlicePolygon;
import com.chinaweal.aiccs.aiccs.market.mapper.AreaSlicePolygonMapper;
import com.chinaweal.aiccs.aiccs.market.service.AreaSlicePolygonService;
import com.chinaweal.aiccs.common.enums.SliceTypeEnums;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 片区围点表 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-11-27
 */
@Service
public class AreaSlicePolygonServiceImpl extends BaseServiceImpl<AreaSlicePolygonMapper, AreaSlicePolygon> implements AreaSlicePolygonService {

    @Override
    public List<AreaSlicePolygon> getPolygonInDistance(double x, double y, Double distance, List<String> excludeIds, String pSliceId, SliceTypeEnums sliceType) {
        return baseMapper.selectPolygonInDistance(x, y, distance, excludeIds, pSliceId, sliceType != null ? sliceType.getCode() : null);
    }

    @Override
    public void updatePolygonByWKT(String wkt, String gId) {
        baseMapper.updatePolygonByWKT(wkt, gId);
    }

    @Override
    public String savePolygonByWKT(String wkt, String sliceId) {
        AreaSlicePolygon polygon = getOne(new LambdaQueryWrapper<AreaSlicePolygon>()
                .eq(AreaSlicePolygon::getSliceId, sliceId));
        String uuid;
        if(polygon == null){
             uuid = UUID.randomUUID().toString().replace("-", "");
            baseMapper.insertPolygonByWKT(uuid, wkt, sliceId);
        }else{
            uuid = polygon.getGId();
            baseMapper.updatePolygonByWKT(wkt, polygon.getGId());
        }
        return uuid;
    }
}
