package com.mosty.yszx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.dto.yszx.TbYsAjDelDto;
import com.mosty.base.model.dto.yszx.TbYsAjDto;
import com.mosty.base.model.entity.yszx.TbYsXsaj;
import com.mosty.base.model.entity.yszx.TbYsXzaj;
import com.mosty.base.model.query.yszx.TbYsAjNoPageQuery;
import com.mosty.base.model.query.yszx.TbYsAjQuery;
import com.mosty.base.model.vo.yszx.TbYsAjVo;
import com.mosty.yszx.mapper.TbYsXsajMapper;
import com.mosty.yszx.mapper.TbYsXzajMapper;
import com.mosty.yszx.remote.TbBaseAdaptRemoteService;
import com.mosty.yszx.service.TbYsAjService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author dw
 * @since 2022/7/21
 **/
@Service
@AllArgsConstructor
public class TbYsAjServiceImpl implements TbYsAjService {

    private final TbYsXsajMapper tbYsXsajMapper;
    private final TbYsXzajMapper tbYsXzajMapper;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public void addAj(TbYsAjDto dto) {
        String id = UUIDGenerator.getUUID();
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
        if ("1".equals(dto.getAjfl())) {
            TbYsXsaj xs = new TbYsXsaj();
            BeanUtils.copyProperties(dto, xs);
            if (dto.getJd() != null && dto.getWd() != null) {
                xs.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
                xs.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
            }
            xs.setJsnr(dto.getAjmc() + " " + dto.getAjbh() + " " + dto.getDzmc() +
                    " " + dto.getAymc() + " " + dto.getAjnr());
            xs.setId(id);
            xs.setXtSjly("1");
            if (dept != null) {
                xs.setSsbm(dept.getDeptname());
                xs.setSsbmid(dept.getDeptid());
                xs.setSsbmdm(dept.getDeptcode());
                xs.setSsxgajdm(dept.getFxjcode());
                xs.setSsxgajid(dept.getFxjid());
                xs.setSsxgaj(dept.getFxjname());
                xs.setSssgaj(dept.getDszname());
                xs.setSssgajdm(dept.getDszcode());
                xs.setSssgajid(dept.getDszid());
            }
            this.tbYsXsajMapper.insertXsaj(xs);
        } else if ("2".equals(dto.getAjfl())) {
            TbYsXzaj xz = new TbYsXzaj();
            BeanUtils.copyProperties(dto, xz);
            if (dto.getJd() != null && dto.getWd() != null) {
                xz.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
                xz.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
            }
            xz.setJsnr(dto.getAjmc() + " " + dto.getAjbh() + " " + dto.getDzmc() +
                    " " + dto.getAymc() + " " + dto.getAjnr());
            xz.setId(id);
            xz.setXtSjly("1");
            if (dept != null) {
                xz.setSsbm(dept.getDeptname());
                xz.setSsbmid(dept.getDeptid());
                xz.setSsbmdm(dept.getDeptcode());
                xz.setSsxgajdm(dept.getFxjcode());
                xz.setSsxgajid(dept.getFxjid());
                xz.setSsxgaj(dept.getFxjname());
                xz.setSssgaj(dept.getDszname());
                xz.setSssgajdm(dept.getDszcode());
                xz.setSssgajid(dept.getDszid());
            }
            this.tbYsXzajMapper.insertXzaj(xz);
        }
    }

    @Override
    public void updateAj(TbYsAjDto dto) {
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
        if ("1".equals(dto.getAjfl())) {
            TbYsXsaj xs = new TbYsXsaj();
            BeanUtils.copyProperties(dto, xs);
            if (xs.getJd() != null && xs.getWd() != null) {
                xs.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
                xs.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
            }
            if (dept != null) {
                xs.setSsbm(dept.getDeptname());
                xs.setSsbmid(dept.getDeptid());
                xs.setSsbmdm(dept.getDeptcode());
                xs.setSsxgajdm(dept.getFxjcode());
                xs.setSsxgajid(dept.getFxjid());
                xs.setSsxgaj(dept.getFxjname());
                xs.setSssgaj(dept.getDszname());
                xs.setSssgajdm(dept.getDszcode());
                xs.setSssgajid(dept.getDszid());
            }this.tbYsXsajMapper.updateXsEntity(xs);
            xs = this.tbYsXsajMapper.selectXsAjById(xs.getId());
            xs.setJsnr(dto.getAjmc() + " " + dto.getAjbh() + " " + dto.getDzmc() +
                    " " + dto.getAymc() + " " + dto.getAjnr());
            this.tbYsXsajMapper.updateXsEntity(xs);
        } else if ("2".equals(dto.getAjfl())) {
            TbYsXzaj xz = new TbYsXzaj();
            BeanUtils.copyProperties(dto, xz);
            if (xz.getJd() != null && xz.getWd() != null) {
                xz.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
                xz.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
            }
            if (dept != null) {
                xz.setSsbm(dept.getDeptname());
                xz.setSsbmid(dept.getDeptid());
                xz.setSsbmdm(dept.getDeptcode());
                xz.setSsxgajdm(dept.getFxjcode());
                xz.setSsxgajid(dept.getFxjid());
                xz.setSsxgaj(dept.getFxjname());
                xz.setSssgaj(dept.getDszname());
                xz.setSssgajdm(dept.getDszcode());
                xz.setSssgajid(dept.getDszid());
            }
            this.tbYsXzajMapper.updateXzEntity(xz);
            xz = this.tbYsXzajMapper.selectXzAjById(xz.getId());
            xz.setJsnr(dto.getAjmc() + " " + dto.getAjbh() + " " + dto.getDzmc() +
                    " " + dto.getAymc() + " " + dto.getAjnr());
            this.tbYsXzajMapper.updateXzEntity(xz);
        }
    }

    @Override
    public IPage<TbYsAjVo> getPageList(TbYsAjQuery dto) {
        if (StringUtils.isNotBlank(dto.getStartTime())) {
            dto.setStartTime(dto.getStartTime() + " 00:00:00");
        }
        if (StringUtils.isNotBlank(dto.getEndTime())) {
            dto.setEndTime(dto.getEndTime() + " 23:59:59");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("pageIndex", (dto.getPageNum() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
        map.put("dto", dto);
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("", user);
        map.put("useSql", useSql);
        int count = this.tbYsXzajMapper.getCount(map);
        List<TbYsAjVo> list = this.tbYsXzajMapper.getList(map);
        IPage<TbYsAjVo> page = new Page<>();
        page.setRecords(list);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<TbYsAjVo> getList(TbYsAjNoPageQuery dto) {
        if (StringUtils.isNotBlank(dto.getStartTime())) {
            dto.setStartTime(dto.getStartTime() + " 00:00:00");
        }
        if (StringUtils.isNotBlank(dto.getEndTime())) {
            dto.setEndTime(dto.getEndTime() + " 23:59:59");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("", user);
        map.put("useSql", useSql);
        return this.tbYsXzajMapper.getListAll(map);
    }

    @Override
    public void delAj(TbYsAjDelDto dto) {
        this.tbYsXzajMapper.delAj(dto);
    }

    @Override
    public List<Map<String, Object>> getAjHotMap(TbYsAjNoPageQuery dto) {
        if (StringUtils.isNotBlank(dto.getStartTime())) {
            dto.setStartTime(dto.getStartTime() + " 00:00:00");
        }
        if (StringUtils.isNotBlank(dto.getEndTime())) {
            dto.setEndTime(dto.getEndTime() + " 23:59:59");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
        UserInfo user = UserInfoManager.get();
        String useSql = PermissionsUtil.createSql("", user);
        map.put("useSql", useSql);
        List<TbYsAjVo> list = this.tbYsXzajMapper.getListAll(map);
        List<Map<String, Object>> rList = new ArrayList<>();
        list.forEach(item -> {
            Map<String, Object> mp = new HashMap<>();
            mp.put("lng", item.getJd());
            mp.put("lat", item.getWd());
            mp.put("count", 1);
            rList.add(mp);
        });
        return rList;
    }

    @Override
    public TbYsAjVo getInfo(String id, String ajfl) {
        TbYsAjVo vo = new TbYsAjVo();
        if ("1".equals(ajfl)) {
            TbYsXsaj xs = this.tbYsXsajMapper.selectXsAjById(id);
            BeanUtils.copyProperties(xs, vo);
            return vo;
        } else if ("2".equals(ajfl)) {
            TbYsXzaj xz = this.tbYsXzajMapper.selectXzAjById(id);
            BeanUtils.copyProperties(xz, vo);
            return vo;
        }
        return null;
    }
}
