package com.mosty.wzzx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.wzzx.LocationInfo;
import com.mosty.base.model.entity.wzzx.TbWzLswz;
import com.mosty.base.model.entity.wzzx.TbWzRywz;
import com.mosty.base.model.entity.wzzx.TbWzSswz;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.DistanceUtils;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.wzzx.mapper.TbWzLswzMapper;
import com.mosty.wzzx.mapper.TbWzSswzMapper;
import com.mosty.wzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.service.TbWzLswzService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import sun.util.resources.sq.LocaleNames_sq;

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

@Service
@RequiredArgsConstructor
public class TbWzLswzServiceImpl extends ServiceImpl<TbWzSswzMapper, TbWzSswz> implements TbWzLswzService {

    private final TbWzLswzMapper tbWzLswzMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    public void saveLswz(LocationInfo locationInfo, TbWzRywz oldRywz) {
        BigDecimal jd = BigDecimal.valueOf(locationInfo.getLng());
        BigDecimal wd = BigDecimal.valueOf(locationInfo.getLat());
        BigDecimal oldJd = null;
        BigDecimal oldWd = null;
        Date oldTime = null;
        if (oldRywz != null) {
            oldJd = oldRywz.getJd();
            oldWd = oldRywz.getWd();
//            oldTime = oldRywz.getJssj();
        }
        if (locationInfo.getLastTime() != null) {
            oldTime = locationInfo.getLastTime();
        }
        TbWzLswz lswz = new TbWzLswz();
        Integer lc = DistanceUtils.getLc(0, oldJd, oldWd, jd, wd);
        Integer sc = DistanceUtils.getSc(oldTime);
        String sjly = "移动设备";
        if (StringUtils.isNotBlank(locationInfo.getUserName())) {
            sjly = sjly + "-" + locationInfo.getUserName();
        }
        if (StringUtils.isNotBlank(locationInfo.getUserId())) {
            sjly = sjly + "-" + locationInfo.getUserId();
        }
        lswz.setSjly(sjly);
        lswz.setSjlydm("02");
        lswz.setYwid(locationInfo.getBbid());
        lswz.setJzId(locationInfo.getJzid());
        lswz.setJzMc(locationInfo.getJzmc());
        lswz.setDwrq(new Date());
        lswz.setDwsj(new Date());
        lswz.setJd(jd);
        lswz.setWd(wd);
        lswz.setXfsc(sc);
        lswz.setXflc(lc);
        lswz.setZb(JtsUtils.getPoint(jd, wd));
        lswz.setZbhash(GeoHashKit.encode(jd, wd));
        lswz.setYhId(locationInfo.getYhid());
        lswz.setYhXm(locationInfo.getUserName());
        lswz.setYhSfzh(locationInfo.getUserId());
        lswz.setWztgz("02");//综合定位
        lswz.setXtSjly("1");

        if (ObjectUtils.isNotEmpty(oldRywz)) {
            lswz.setSsbm(oldRywz.getSsbm());
            lswz.setSsbmdm(oldRywz.getSsbmdm());
            lswz.setSsbmid(oldRywz.getSsbmid());
            lswz.setSsxgaj(oldRywz.getSsxgaj());
            lswz.setSsxgajid(oldRywz.getSsxgajid());
            lswz.setSsxgajdm(oldRywz.getSsxgajdm());
            lswz.setSssgajid(oldRywz.getSssgajid());
            lswz.setSssgaj(oldRywz.getSssgaj());
            lswz.setSssgajdm(oldRywz.getSssgajdm());
        } else {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(locationInfo.getDeptId());
            if (dept != null) {
                lswz.setSsbm(dept.getDeptname());
                lswz.setSsbmdm(dept.getDeptcode());
                lswz.setSsbmid(dept.getDeptid());
                lswz.setSsxgaj(dept.getFxjname());
                lswz.setSsxgajid(dept.getFxjid());
                lswz.setSsxgajdm(dept.getFxjcode());
                lswz.setSssgajid(dept.getDszid());
                lswz.setSssgaj(dept.getDszname());
                lswz.setSssgajdm(dept.getDszcode());
            }
        }
        lswz.setGuo(locationInfo.getCountry());
        lswz.setSheng(locationInfo.getProvinc());
        lswz.setShi(locationInfo.getCity());
        lswz.setCsbm(locationInfo.getCityCode());
        lswz.setSsqx(locationInfo.getDistrict());
        lswz.setQxbm(locationInfo.getAdCode());
        lswz.setDzxz(locationInfo.getAddress());
        lswz.setDzjc(locationInfo.getPoiName());
        lswz.setDwbz(locationInfo.getZbly());
        tbWzLswzMapper.insertEntity(lswz);
    }
}
