package com.mosty.base.manager.service.impl;

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.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.model.dto.jcgl.TbJcglXfwgInsertDto;
import com.mosty.base.model.dto.yszx.TbYsGajgDto;
import com.mosty.base.model.entity.jcgl.TbJcglXfwg;
import com.mosty.base.model.query.jcgl.TbJcglXfwgQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.GeoHashKit;
import com.mosty.base.utils.JtsUtils;
import com.mosty.base.utils.PageUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.base.manager.mapper.TbJcglXfwgMapper;
import com.mosty.base.manager.remote.TbYsZxAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglXfwgService;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zengbo
 * @title: TbJcglXfwgServiceImpl
 * @projectName mosty-rzzx
 * @date 2022/7/14 20:33
 */
@AllArgsConstructor
@Service
public class TbJcglXfwgServiceImpl implements TbJcglXfwgService {

    private TbJcglXfwgMapper tbJcglXfwgMapper;
    private TbYsZxAdaptRemoteService tbYsZxAdaptRemoteService;
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    @Transactional
    public int addXfwg(TbJcglXfwgInsertDto dto) {
        TbJcglXfwg xfwg = new TbJcglXfwg();
        BeanUtils.copyProperties(dto, xfwg);
        if (!CollectionUtils.isEmpty(dto.getPgis())) {
            xfwg.setPgis(JtsUtils.createLine(dto.getPgis()));
        }
        xfwg.setXtSjly("1");
        xfwg.setId(UUIDGenerator.getUUID());
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xfwg.getSsbmdm());
        xfwg.setSsbm(dept.getDeptname());
        xfwg.setSsbmid(dept.getDeptid());
        xfwg.setSsbmdm(dept.getDeptcode());
        xfwg.setSsxgajdm(dept.getFxjcode());
        xfwg.setSsxgajid(dept.getFxjid());
        xfwg.setSsxgaj(dept.getFxjname());
        xfwg.setSssgaj(dept.getDszname());
        xfwg.setSssgajdm(dept.getDszcode());
        xfwg.setSssgajid(dept.getDszid());
        int ret = tbJcglXfwgMapper.insertEntity(xfwg);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(xfwg.getId());
        ysGajgDto.setLybm("tb_jcgl_xfwg");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001010");
        ysGajgDto.setGajgmc(xfwg.getWgMc());
        ysGajgDto.setGajgjc(xfwg.getWgMc());
        ysGajgDto.setWzXzqh(xfwg.getXzqhdm());
        List<BigDecimal> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getPgis())) {
            dto.getPgis().forEach(it -> {
                list.add(it[0]);
                list.add(it[1]);
            });
        }
        ysGajgDto.setQy(list);
        ysGajgDto.setSsbm(xfwg.getSsbm());
        ysGajgDto.setSsbmid(xfwg.getSsbmid());
        ysGajgDto.setSsbmdm(xfwg.getSsbmdm());
        tbYsZxAdaptRemoteService.addGajg(ysGajgDto);
        return ret;
    }

    @Override
    public IPage<TbJcglXfwgInsertDto> getXfwg(TbJcglXfwgQuery dto) {
        IPage<TbJcglXfwg> page = PageUtils.buildPage(dto.getPageSize(), dto.getPageCurrent());
        QueryWrapper<TbJcglXfwg> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        qw.lambda().like(StringUtils.isNotBlank(dto.getWgMc()), TbJcglXfwg::getWgMc, dto.getWgMc())
                .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbJcglXfwg::getSsbmdm, dto.getSsbmdm())
                .eq(StringUtils.isNotBlank(dto.getWgBh()), TbJcglXfwg::getWgBh, dto.getWgBh())
                .eq(StringUtils.isNotBlank(dto.getWglx()), TbJcglXfwg::getWglx, dto.getWglx())
                .eq(StringUtils.isNotBlank(dto.getSsXfqyId()), TbJcglXfwg::getSsXfqyId, dto.getSsXfqyId())
                .like(StringUtils.isNotBlank(dto.getPgis()), TbJcglXfwg::getPgis, dto.getPgis())
                .like(StringUtils.isNotBlank(dto.getXzqhdm()), TbJcglXfwg::getXzqhdm, dto.getXzqhdm())
                .eq(StringUtils.isNotBlank(dto.getSsbmdm()), TbJcglXfwg::getSsbmdm, dto.getSsbmdm())
                .eq(StringUtils.isNotBlank(dto.getSsbmid()), TbJcglXfwg::getSsbmid, dto.getSsbmid());
        page = tbJcglXfwgMapper.selectPage(page, qw);
        IPage<TbJcglXfwgInsertDto> resPage = new Page<>(dto.getPageCurrent(), dto.getPageSize());
        List<TbJcglXfwgInsertDto> resList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(page.getRecords())) {
            page.getRecords().forEach(item -> {
                TbJcglXfwgInsertDto vo = new TbJcglXfwgInsertDto();
                BeanUtils.copyProperties(item, vo);
                vo.setPgis(JtsUtils.decodeLineString(item.getPgis()));
                resList.add(vo);
            });
        }
        resPage.setTotal(page.getTotal());
        resPage.setRecords(resList);
        return resPage;
    }

    @Override
    @Transactional
    public int editXfwg(TbJcglXfwgInsertDto dto) {
        TbJcglXfwg xfwg = new TbJcglXfwg();
        BeanUtils.copyProperties(dto, xfwg);
        if (!CollectionUtils.isEmpty(dto.getPgis())) {
            xfwg.setPgis(JtsUtils.createLine(dto.getPgis()));
        }
        DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(xfwg.getSsbmdm());
        xfwg.setSsbm(dept.getDeptname());
        xfwg.setSsbmid(dept.getDeptid());
        xfwg.setSsbmdm(dept.getDeptcode());
        xfwg.setSsxgajdm(dept.getFxjcode());
        xfwg.setSsxgajid(dept.getFxjid());
        xfwg.setSsxgaj(dept.getFxjname());
        xfwg.setSssgaj(dept.getDszname());
        xfwg.setSssgajdm(dept.getDszcode());
        xfwg.setSssgajid(dept.getDszid());
        int ret = tbJcglXfwgMapper.updateEntity(xfwg);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(xfwg.getId());
        ysGajgDto.setLybm("tb_jcgl_xfwg");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001010");
        ysGajgDto.setGajgmc(xfwg.getWgMc());
        ysGajgDto.setGajgjc(xfwg.getWgMc());
        ysGajgDto.setWzXzqh(xfwg.getXzqhdm());
        ysGajgDto.setSsbm(xfwg.getSsbm());
        ysGajgDto.setSsbmid(xfwg.getSsbmid());
        ysGajgDto.setSsbmdm(xfwg.getSsbmdm());
        List<BigDecimal> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getPgis())) {
            dto.getPgis().forEach(it -> {
                list.add(it[0]);
                list.add(it[1]);
            });
        }
        ysGajgDto.setQy(list);
        tbYsZxAdaptRemoteService.updateGajg(ysGajgDto);
        return ret;
    }

    @Override
    @Transactional
    public int deleteXfwg(List<String> list) {
        int ret = tbJcglXfwgMapper.deleteBatch(list);
        for (String s : list) {
            TbYsGajgDto ysGajgDto = new TbYsGajgDto();
            ysGajgDto.setLyid(s);
            ysGajgDto.setLybm("tb_jcgl_xfwg");
            tbYsZxAdaptRemoteService.delEntity(ysGajgDto);
        }
        return ret;
    }

    @Override
    public TbJcglXfwgInsertDto selectById(String id) {
        TbJcglXfwg xfwg = tbJcglXfwgMapper.selectById(id);
        if (xfwg != null) {
            TbJcglXfwgInsertDto vo = new TbJcglXfwgInsertDto();
            BeanUtils.copyProperties(xfwg, vo);
            vo.setPgis(JtsUtils.decodeLineString(xfwg.getPgis()));
            return vo;
        }
        return null;
    }
}
