package com.mosty.wzzx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.wzzx.LocationInfo;
import com.mosty.base.model.dto.wzzx.SjCyjlUpdateDto;
import com.mosty.base.model.entity.sjzx.TbSjCyjl;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.wzzx.mapper.TbSjCyjlMapper;
import com.mosty.wzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.service.TbSjCyjlService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Service
@RequiredArgsConstructor
public class TbSjCyjlServiceImpl extends ServiceImpl<TbSjCyjlMapper, TbSjCyjl> implements TbSjCyjlService {

    @Resource
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    public Integer updateJlZb(LocationInfo locationInfo) {
        if (StringUtils.isBlank(locationInfo.getUserId())) {
            return 0;
        }
        TbSjCyjl cyjl = this.baseMapper.queryBySfzh(locationInfo.getUserId());
        if (cyjl != null) {
            SjCyjlUpdateDto dto = new SjCyjlUpdateDto();
            BigDecimal jd = BigDecimal.valueOf(locationInfo.getLng());
            BigDecimal wd = BigDecimal.valueOf(locationInfo.getLat());
            dto.setId(cyjl.getId());
            dto.setJd(jd);
            dto.setWd(wd);
            JSONObject json = this.tbBaseAdaptRemoteService.getAddress(jd.toString(),wd.toString());
            if(json != null){
                dto.setDqwz(json.getString("address"));
            }
            dto.setZb(JtsUtils.getPoint(jd, wd));
            dto.setZbhash(GeoHashKit.encode(jd, wd));
            dto.setZbsj(new Date());
            return this.baseMapper.updateZb(dto);
        }
        return 0;
    }

}
