package com.mosty.wzzx.service.impl;

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.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.query.wzzx.TbWzRywzSearchDto;
import com.mosty.base.model.query.wzzx.TbWzSswzSearchDto;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.*;
import com.mosty.wzzx.mapper.TbWzLswzMapper;
import com.mosty.wzzx.mapper.TbWzRywzMapper;
import com.mosty.wzzx.mapper.TbWzSswzMapper;
import com.mosty.wzzx.remote.TbBaseAdaptRemoteService;
import com.mosty.wzzx.service.TbWzSswzService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

@Service
@RequiredArgsConstructor
public class TbWzSswzServiceImpl extends ServiceImpl<TbWzSswzMapper, TbWzSswz> implements TbWzSswzService {

    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final TbWzLswzMapper tbWzLswzMapper;
    private final TbWzRywzMapper tbWzRywzMapper;

    public void saveSswz(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();
        }
        if (locationInfo.getLastTime() != null) {
            oldTime = locationInfo.getLastTime();
        }
        TbWzSswz sswz = new TbWzSswz();
        Integer lc = DistanceUtils.getLc(0, oldJd, oldWd, jd, wd);
        Integer sc = DistanceUtils.getSc(oldTime);
        sswz.setSjlydm("02");
        String sjly = "移动设备";
        if (StringUtils.isNotBlank(locationInfo.getUserName())) {
            sjly = sjly + "-" + locationInfo.getUserName();
        }
        if (StringUtils.isNotBlank(locationInfo.getUserId())) {
            sjly = sjly + "-" + locationInfo.getUserId();
        }
        sswz.setSjly(sjly);
        sswz.setYwid(locationInfo.getBbid());
        sswz.setJzId(locationInfo.getJzid());
        sswz.setJzMc(locationInfo.getJzmc());
        sswz.setDwrq(new Date());
        sswz.setDwsj(new Date());
        sswz.setJd(jd);
        sswz.setWd(wd);
        sswz.setXfsc(sc);
        sswz.setXflc(lc);
        sswz.setZb(JtsUtils.getPoint(jd, wd));
        sswz.setZbhash(GeoHashKit.encode(jd, wd));
        sswz.setYhId(locationInfo.getYhid());
        sswz.setYhXm(locationInfo.getUserName());
        sswz.setYhSfzh(locationInfo.getUserId());
        sswz.setJrbh(locationInfo.getAccessNumber());
        sswz.setWztgz("02");//综合定位
        if (ObjectUtils.isNotEmpty(oldRywz)) {
            sswz.setSsbm(oldRywz.getSsbm());
            sswz.setSsbmdm(oldRywz.getSsbmdm());
            sswz.setSsbmid(oldRywz.getSsbmid());
            sswz.setSsxgaj(oldRywz.getSsxgaj());
            sswz.setSsxgajid(oldRywz.getSsxgajid());
            sswz.setSsxgajdm(oldRywz.getSsxgajdm());
            sswz.setSssgajid(oldRywz.getSssgajid());
            sswz.setSssgaj(oldRywz.getSssgaj());
            sswz.setSssgajdm(oldRywz.getSssgajdm());
        } else {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(locationInfo.getDeptId());
            if (dept != null) {
                sswz.setSsbm(dept.getDeptname());
                sswz.setSsbmdm(dept.getDeptcode());
                sswz.setSsbmid(dept.getDeptid());
                sswz.setSsxgaj(dept.getFxjname());
                sswz.setSsxgajid(dept.getFxjid());
                sswz.setSsxgajdm(dept.getFxjcode());
                sswz.setSssgajid(dept.getDszid());
                sswz.setSssgaj(dept.getDszname());
                sswz.setSssgajdm(dept.getDszcode());
            }
        }
        sswz.setGuo(locationInfo.getCountry());
        sswz.setSheng(locationInfo.getProvinc());
        sswz.setShi(locationInfo.getCity());
        sswz.setCsbm(locationInfo.getCityCode());
        sswz.setSsqx(locationInfo.getDistrict());
        sswz.setQxbm(locationInfo.getAdCode());
        sswz.setDzxz(locationInfo.getAddress());
        sswz.setDzjc(locationInfo.getPoiName());
        sswz.setDwbz(locationInfo.getZbly());
        this.baseMapper.insertEntity(sswz);
    }

    public TbWzSswz insert(LocationInfo locationInfo) {
        TbWzSswz fronSswz = this.baseMapper.querySswzNow(locationInfo.getUserId(), null);
        TbWzSswz sswz = new TbWzSswz();
        BigDecimal jd = BigDecimal.valueOf(locationInfo.getLng());
        BigDecimal wd = BigDecimal.valueOf(locationInfo.getLat());
        Integer lc = 0;
        Integer sc = 0;
        if (fronSswz != null) {
            lc = DistanceUtils.getLc(0, fronSswz.getJd(), fronSswz.getWd(), jd, wd);
            sc = DistanceUtils.getSc(fronSswz.getDwsj());
        }
        sswz.setYhId(locationInfo.getUserId());
        sswz.setYhXm(locationInfo.getUserName());
        sswz.setYhSfzh(locationInfo.getUserId());
        String sjly = "移动设备";
        if (StringUtils.isNotBlank(locationInfo.getUserName())) {
            sjly = sjly + "-" + locationInfo.getUserName();
        }
        if (StringUtils.isNotBlank(locationInfo.getUserId())) {
            sjly = sjly + "-" + locationInfo.getUserId();
        }
        sswz.setSjly(sjly);
        sswz.setSjlydm("02");
        sswz.setDwrq(new Date());
        sswz.setDwsj(new Date());
        sswz.setJd(jd);
        sswz.setWd(wd);
        sswz.setZb(JtsUtils.getPoint(jd, wd));
        sswz.setZbhash(GeoHashKit.encode(jd, wd));
        sswz.setJrbh(locationInfo.getAccessNumber());
        sswz.setWztgz("02");//综合定位
        sswz.setXtSjly("1");
        sswz.setId(UUIDGenerator.getUUID());
        sswz.setGuo(locationInfo.getCountry());
        sswz.setSheng(locationInfo.getProvinc());
        sswz.setShi(locationInfo.getCity());
        sswz.setQxbm(locationInfo.getCityCode());
        sswz.setDzxz(locationInfo.getAddress());
        sswz.setDzjc(locationInfo.getPoiName());
        sswz.setYwid(locationInfo.getBbid());
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(locationInfo.getDeptId());
        if (dept != null) {
            sswz.setSsbm(dept.getDeptname());
            sswz.setSsbmdm(dept.getDeptcode());
            sswz.setSsbmid(dept.getDeptid());
            sswz.setSsxgaj(dept.getFxjname());
            sswz.setSsxgajid(dept.getFxjid());
            sswz.setSsxgajdm(dept.getFxjcode());
            sswz.setSssgajid(dept.getDszid());
            sswz.setSssgaj(dept.getDszname());
            sswz.setSssgajdm(dept.getDszcode());
        }
        sswz.setXfsc(sc);
        sswz.setXflc(lc);
        this.baseMapper.insertEntity(sswz);
        return sswz;
    }

    public TbWzSswz insertEntity(TbWzSswz sswz) {
        this.baseMapper.insertEntity(sswz);
        return sswz;
    }

    public TbWzSswz queryById(String id) {
        return this.baseMapper.queryById(id);
    }

    public TbWzSswz querySswzNow(String yhid, String jrbh) {
        return this.baseMapper.querySswzNow(yhid, jrbh);
    }

    public void delHistory() {
        this.baseMapper.delHistory();
    }

    @Override
    public IPage<TbWzSswz> getRygjList(TbWzSswzSearchDto dto) {
        IPage<TbWzLswz> page = this.tbWzLswzMapper.selectPage(
                new Page<>(dto.getPageCurrent(), dto.getPageSize()),
                new LambdaQueryWrapper<TbWzLswz>()
                        .eq(TbWzLswz::getYhSfzh, dto.getSfzh())
                        .eq(TbWzLswz::getXtSjzt, "1")
                        .ge(TbWzLswz::getDwrq, dto.getDwrq())
        );
        IPage<TbWzSswz> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbWzSswz> resList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbWzSswz sswz = new TbWzSswz();
                BeanUtils.copyProperties(item, sswz);
                sswz.setZb(null);
                resList.add(sswz);
            });
            resPage.setRecords(resList);
            resPage.setTotal(page.getTotal());
        }
        return resPage;
    }

    @Override
    public TbWzRywz getRywz(TbWzRywzSearchDto dto) {
        TbWzRywz wz = this.tbWzRywzMapper.selectBySfz(dto);
        if (wz != null) {
            wz.setZbList(JtsUtils.decodeLineString(wz.getZb()));
            wz.setZb(null);
        }
        return wz;
    }
}