package com.mosty.yszx.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.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.base.model.vo.yszx.TbYsDlQuery;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.base.model.vo.yszx.TbYsDlVo;
import com.mosty.base.model.vo.yszx.TbYsDlDto;
import com.mosty.base.model.entity.yszx.TbYsDl;
import com.mosty.yszx.mapper.TbYsDlMapper;
import com.mosty.yszx.remote.TbBaseAdaptRemoteService;
import com.mosty.yszx.service.TbYsDlService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 要素-地理要素表(TbYsDl)表服务实现类
 *
 * @author zhangHaiJun
 * @since 2022-07-12 10:08:40
 */
@Service("tbYsDlService")
public class TbYsDlServiceImpl extends ServiceImpl<TbYsDlMapper, TbYsDl>
        implements TbYsDlService {

    @Resource
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbYsDlVo> queryAll(TbYsDlQuery dto) {
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
        UserInfo user = UserInfoManager.getUser();
        String useSql = user != null ? PermissionsUtil.createSql("", user) : "";
        map.put("useSql", useSql);
        IPage<TbYsDlVo> page;
        if (dto.getPageNum() != null && dto.getPageSize() != null) {
            page = new Page<>(dto.getPageNum(), dto.getPageSize());
            map.put("pageIndex", (dto.getPageNum() - 1) * dto.getPageSize());
            map.put("pageSize", dto.getPageSize());
            int count = this.baseMapper.getCount(map);
            List<TbYsDl> list = this.baseMapper.getPageList(map);
            List<TbYsDlVo> rList = new ArrayList<>();
            list.forEach(item -> {
                TbYsDlVo vo = new TbYsDlVo();
                BeanUtils.copyProperties(item, vo);
                rList.add(vo);
            });
            page.setRecords(rList);
            page.setTotal(count);
        } else {
            page = new Page<>();
            List<TbYsDl> list = this.baseMapper.getList(map);
            List<TbYsDlVo> rList = new ArrayList<>();
            list.forEach(item -> {
                TbYsDlVo vo = new TbYsDlVo();
                BeanUtils.copyProperties(item, vo);
                rList.add(vo);
            });
            page.setRecords(rList);
        }
        return page;
    }

    @Override
    public TbYsDlVo queryById(String id) {
        TbYsDl item = this.baseMapper.getInfoById(id);
        TbYsDlVo vo = null;
        if (item != null) {
            vo = new TbYsDlVo();
            BeanUtils.copyProperties(item, vo);
            vo.setDtm(JtsUtils.decodePolygon(item.getDtm()));
            vo.setZb(new BigDecimal[]{item.getJd(), item.getWd()});
        }
        return vo;
    }

    @Override
    public void insert(TbYsDlDto dto) {
        TbYsDl dl = new TbYsDl();
        BeanUtils.copyProperties(dto, dl);
        dl.setId(UUIDGenerator.getUUID());
        if (dto.getJd() != null && dto.getWd() != null) {
            dl.setZb(JtsUtils.getPoint(dl.getJd(), dl.getWd()));
            dl.setZbhash(GeoHashKit.encode(dl.getJd(), dl.getWd()));
        }
        if (dto.getQy() != null) {
            ArrayList<BigDecimal[]> list = new ArrayList<>();
            List<BigDecimal> qw = dto.getQy();
            for (int i = 0; i < qw.size(); i++) {
                if (i != qw.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw.get(i);
                    des[1] = qw.get(i + 1);
                    list.add(des);
                }
            }
            if (list.size() > 0) {
                dl.setDtm(JtsUtils.getPolygon(list));
            }
        }
        dl.setJsnr(dto.getYsmc() + " " + dto.getYsjc() + " " + dto.getYswzXz()
                + " " + dto.getYswzBc());
        dl.setXtSjly("1");
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(dto.getSsbmid());
        if (dept != null) {
            dl.setSsbm(dept.getDeptname());
            dl.setSsbmid(dept.getDeptid());
            dl.setSsbmdm(dept.getDeptcode());
            dl.setSsxgajdm(dept.getFxjcode());
            dl.setSsxgajid(dept.getFxjid());
            dl.setSsxgaj(dept.getFxjname());
            dl.setSssgaj(dept.getDszname());
            dl.setSssgajdm(dept.getDszcode());
            dl.setSssgajid(dept.getDszid());
        }
        this.baseMapper.insertEntity(dl);
    }

    @Override
    public void update(TbYsDlDto dto) {
        TbYsDl dl = new TbYsDl();
        BeanUtils.copyProperties(dto, dl);
        if (dto.getJd() != null && dto.getWd() != null) {
            dl.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            dl.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        ArrayList<BigDecimal[]> list = new ArrayList<>();
        List<BigDecimal> qw = dto.getQy();
        if (qw != null) {
            for (int i = 0; i < qw.size(); i++) {
                if (i != qw.size() - 1 && i % 2 == 0) {
                    BigDecimal[] des = new BigDecimal[2];
                    des[0] = qw.get(i);
                    des[1] = qw.get(i + 1);
                    list.add(des);
                }
            }
        }
        if (list.size() > 0) {
            dl.setDtm(JtsUtils.getPolygon(list));
        }
        this.baseMapper.updateEntity(dl);
        dl = this.baseMapper.getInfoById(dl.getId());
        dl.setJsnr(dl.getYsmc() + " " + dl.getYsjc() + " " + dl.getYswzXz() +
                " " + dl.getYswzBc());
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(dto.getSsbmid());
        if (dept != null) {
            dl.setSsbmdm(dept.getDeptid());
            dl.setSsbm(dept.getDeptname());
            dl.setSsxgaj(dept.getFxjname());
            dl.setSsxgajdm(dept.getFxjid());
            dl.setSssgaj(dept.getDszname());
            dl.setSssgajdm(dept.getDszid());
        }
        this.baseMapper.updateEntity(dl);
    }

    @Override
    public void deleteById(String id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public List<TbYsDl> selectVideoServo(TbYsDl tbYsDl) {
        //地理位置
        String dlsql = "( \n" +
                "6371 * acos( \n" +
                "cos(radians(jd))\n" +
                "* cos( radians( " + tbYsDl.getJd() + ") )\n" +
                "* cos(radians( " + tbYsDl.getWd() + " ) - radians(wd))\n" +
                "+ sin( radians(jd) ) \n" +
                "* sin( radians(" + tbYsDl.getJd() + ") ) \n" +
                ") \n" +
                ")";
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(queryWrapper, userInfo);
        QueryWrapper<TbYsDl> queryWrapper = new QueryWrapper<>();
        tbYsDl.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(tbYsDl.getSsbmdm(), null));

        List<TbYsDl> tbYsDlList = baseMapper.selectList((Wrapper<TbYsDl>) queryWrapper
                .like(StringUtils.isNotBlank(tbYsDl.getYsdl()), "ysdl", tbYsDl.getYsdl())
                .like(StringUtils.isNotBlank(tbYsDl.getYszl()), "yszl", tbYsDl.getYszl())
                .likeRight(StringUtils.isNotBlank(tbYsDl.getSsbmdm()), "ssbmdm", tbYsDl.getSsbmdm())
                .like(StringUtils.isNotBlank(tbYsDl.getYsxl()), "ysxl", tbYsDl.getYsxl())
                .le(StringUtils.isNotBlank(dlsql), dlsql, tbYsDl.getCxjl())
                .eq("xt_sjzt", 1)
        );
        return tbYsDlList;
    }

    @Override
    public List<TbYsDlVo> selectByNr(String keyword) {
        List<TbYsDl> list = this.baseMapper.selectList(
                new LambdaQueryWrapper<TbYsDl>()
                        .like(TbYsDl::getJsnr, keyword)
                        .eq(TbYsDl::getXtScbz, "0")
        );
        List<TbYsDlVo> rList = new ArrayList<>();
        list.forEach(item -> {
            TbYsDlVo vo = new TbYsDlVo();
            BeanUtils.copyProperties(item, vo);
            vo.setDtm(JtsUtils.decodePolygon(item.getDtm()));
            vo.setZb(new BigDecimal[]{item.getJd(), item.getWd()});
            rList.add(vo);
        });
        return rList;
    }

    @Override
    public TbYsDl selectDlys(String ysmc) {
        return this.baseMapper.selectOne(
                new LambdaQueryWrapper<TbYsDl>()
                        .eq(TbYsDl::getYsmc, ysmc)
                        .eq(TbYsDl::getXtSjzt, "1")
                        .last(" limit 1")
        );
    }
}

