package com.mosty.base.manager.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.manager.mapper.TbJcglKfdMapper;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.manager.remote.TbYsZxAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglKfdService;
import com.mosty.base.model.dto.jcgl.TbJcglKfdDto;
import com.mosty.base.model.dto.qwzx.AppSearchQuery;
import com.mosty.base.model.dto.yszx.TbYsGajgDto;
import com.mosty.base.model.entity.jcgl.TbJcglKfd;
import com.mosty.base.model.entity.jcgl.TbJcglXfqy;
import com.mosty.base.model.query.jcgl.TbJcglKfdQuery;
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.UUIDGenerator;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
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.util.*;

@AllArgsConstructor
@Service
public class TbJcglKfdServiceImpl extends ServiceImpl<TbJcglKfdMapper, TbJcglKfd>
        implements TbJcglKfdService {

    private TbYsZxAdaptRemoteService tbYsZxAdaptRemoteService;
    private TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    public IPage<TbJcglKfdDto> selectKfdList(TbJcglKfdQuery dto) {
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));
        Map<String, Object> map = new HashMap<>();
        map.put("dto", dto);
        IPage<TbJcglKfdDto> page;
//        UserInfo userInfo = UserInfoManager.get();
//        String useSql = PermissionsUtil.createSql("", userInfo);
        page = new Page<>(dto.getPageNum(), dto.getPageSize());
        map.put("pageIndex", (dto.getPageNum() - 1) * dto.getPageSize());
        map.put("pageSize", dto.getPageSize());
//        map.put("useSql", useSql);
        List<String> dwlxList = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getDwlx())) {
            dwlxList = Arrays.asList(dto.getDwlx().split(","));
        }
        map.put("dwlxList", dwlxList);
        int count = this.baseMapper.getCount(map);
        List<TbJcglKfd> list = this.baseMapper.getPageList(map);
        List<TbJcglKfdDto> rList = new ArrayList<>();
        list.forEach(item -> {
            TbJcglKfdDto vo = new TbJcglKfdDto();
            BeanUtils.copyProperties(item, vo);
            if (item.getYfzfw() != null) vo.setYfzfw(JtsUtils.decodePolygon(item.getYfzfw()));
            if (item.getSfzfw() != null) vo.setSfzfw(JtsUtils.decodePolygon(item.getSfzfw()));
            if (item.getWfzfw() != null) vo.setWfzfw(JtsUtils.decodePolygon(item.getWfzfw()));
            rList.add(vo);
        });
        page.setRecords(rList);
        page.setTotal(count);
        return page;
    }

    @Override
    @Transactional
    public int addKfd(TbJcglKfdDto dto) {
        TbJcglKfd item = new TbJcglKfd();
        BeanUtils.copyProperties(dto, item);
        item.setKfdId(UUIDGenerator.getUUID());
        if (StringUtils.isNotBlank(item.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            item.setSsbm(dept.getOrgJc());
            item.setSsbmid(dept.getDeptid());
            item.setSsbmdm(dept.getDeptcode());
            item.setSsxgajdm(dept.getFxjcode());
            item.setSsxgajid(dept.getFxjid());
            item.setSsxgaj(dept.getFxjname());
            item.setSssgaj(dept.getDszname());
            item.setSssgajdm(dept.getDszcode());
            item.setSssgajid(dept.getDszid());
        }
        if (item.getJd() != null && item.getWd() != null) {
            item.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            item.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (!CollectionUtils.isEmpty(dto.getYfzfw())) {
            item.setYfzfw(JtsUtils.getPolygon(dto.getYfzfw()));
        }
        if (!CollectionUtils.isEmpty(dto.getSfzfw())) {
            item.setSfzfw(JtsUtils.getPolygon(dto.getSfzfw()));
        }
        if (!CollectionUtils.isEmpty(dto.getWfzfw())) {
            item.setWfzfw(JtsUtils.getPolygon(dto.getWfzfw()));
        }
        item.setXtSjly("1");
        int ret = this.baseMapper.insertEntity(item);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(item.getKfdId());
        ysGajgDto.setLybm("tb_jcgl_kfd");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001008");
        if (StringUtils.isNotEmpty(dto.getDwlx()) && dto.getDwlx().equals("02")) {
            ysGajgDto.setYsdl("001001009");
        }
        ysGajgDto.setGajgmc(item.getKfdMc());
        ysGajgDto.setGajgjc(item.getKfdMc());
        ysGajgDto.setWzXzqh(item.getXzqhdm());
        ysGajgDto.setWzXz(item.getKfdDz());
        ysGajgDto.setWzBc(item.getKfdDz());
        ysGajgDto.setJd(item.getJd());
        ysGajgDto.setWd(item.getWd());
        ysGajgDto.setSsbm(item.getSsbm());
        ysGajgDto.setSsbmid(item.getSsbmid());
        ysGajgDto.setSsbmdm(item.getSsbmdm());
        // 一分钟圈层
        List<BigDecimal> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getYfzfw())) {
            dto.getYfzfw().forEach(it -> {
                list.add(it[0]);
                list.add(it[1]);
            });
        }
        ysGajgDto.setQy(list);
        // 三分钟圈层
        List<BigDecimal> list1 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getSfzfw())) {
            dto.getSfzfw().forEach(it -> {
                list1.add(it[0]);
                list1.add(it[1]);
            });
        }
        ysGajgDto.setQy1(list1);
        // 五分钟圈层
        List<BigDecimal> list2 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getWfzfw())) {
            dto.getWfzfw().forEach(it -> {
                list2.add(it[0]);
                list2.add(it[1]);
            });
        }
        ysGajgDto.setQy2(list2);
        tbYsZxAdaptRemoteService.addGajg(ysGajgDto);
        return ret;
    }

    @Override
    @Transactional
    public int updateKfd(TbJcglKfdDto dto) {
        TbJcglKfd item = new TbJcglKfd();
        BeanUtils.copyProperties(dto, item);
        if (StringUtils.isNotBlank(item.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(item.getSsbmdm());
            item.setSsbm(dept.getOrgJc());
            item.setSsbmid(dept.getDeptid());
            item.setSsbmdm(dept.getDeptcode());
            item.setSsxgajdm(dept.getFxjcode());
            item.setSsxgajid(dept.getFxjid());
            item.setSsxgaj(dept.getFxjname());
            item.setSssgaj(dept.getDszname());
            item.setSssgajdm(dept.getDszcode());
            item.setSssgajid(dept.getDszid());
        }
        if (item.getJd() != null && item.getWd() != null) {
            item.setZb(JtsUtils.getPoint(dto.getJd(), dto.getWd()));
            item.setZbhash(GeoHashKit.encode(dto.getJd(), dto.getWd()));
        }
        if (!CollectionUtils.isEmpty(dto.getYfzfw())) {
            item.setYfzfw(JtsUtils.getPolygon(dto.getYfzfw()));
        }
        if (!CollectionUtils.isEmpty(dto.getSfzfw())) {
            item.setSfzfw(JtsUtils.getPolygon(dto.getSfzfw()));
        }
        if (!CollectionUtils.isEmpty(dto.getWfzfw())) {
            item.setWfzfw(JtsUtils.getPolygon(dto.getWfzfw()));
        }
        int ret = this.baseMapper.updateEntity(item);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(item.getKfdId());
        ysGajgDto.setLybm("tb_jcgl_kfd");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001008");
        if (StringUtils.isNotEmpty(dto.getDwlx()) && dto.getDwlx().equals("02")) {
            ysGajgDto.setYsdl("001001009");
        }
        ysGajgDto.setGajgmc(item.getKfdMc());
        ysGajgDto.setGajgjc(item.getKfdMc());
        ysGajgDto.setWzXzqh(item.getXzqhdm());
        ysGajgDto.setWzXz(item.getKfdDz());
        ysGajgDto.setWzBc(item.getKfdDz());
        ysGajgDto.setJd(item.getJd());
        ysGajgDto.setWd(item.getWd());
        ysGajgDto.setSsbm(item.getSsbm());
        ysGajgDto.setSsbmid(item.getSsbmid());// 一分钟圈层
        ysGajgDto.setSsbmdm(item.getSsbmdm());// 一分钟圈层
        List<BigDecimal> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getYfzfw())) {
            dto.getYfzfw().forEach(it -> {
                list.add(it[0]);
                list.add(it[1]);
            });
        }
        ysGajgDto.setQy(list);
        // 三分钟圈层
        List<BigDecimal> list1 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getSfzfw())) {
            dto.getSfzfw().forEach(it -> {
                list1.add(it[0]);
                list1.add(it[1]);
            });
        }
        ysGajgDto.setQy1(list1);
        // 五分钟圈层
        List<BigDecimal> list2 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(dto.getWfzfw())) {
            dto.getWfzfw().forEach(it -> {
                list2.add(it[0]);
                list2.add(it[1]);
            });
        }
        ysGajgDto.setQy2(list2);
        tbYsZxAdaptRemoteService.updateGajg(ysGajgDto);
        return ret;
    }

    @Override
    public TbJcglKfdDto selectByid(String id) {
        TbJcglKfd kfd = this.baseMapper.getById(id);
        if (kfd != null) {
            TbJcglKfdDto dto = new TbJcglKfdDto();
            BeanUtils.copyProperties(kfd, dto);
            if (kfd.getYfzfw() != null) dto.setYfzfw(JtsUtils.decodePolygon(kfd.getYfzfw()));
            if (kfd.getSfzfw() != null) dto.setSfzfw(JtsUtils.decodePolygon(kfd.getSfzfw()));
            if (kfd.getWfzfw() != null) dto.setWfzfw(JtsUtils.decodePolygon(kfd.getWfzfw()));
            return dto;
        }
        return null;
    }

    @Override
    public void deleteKfd(List<String> id) {
        this.baseMapper.deleteKfd(id);
        for (String s : id) {
            TbYsGajgDto ysGajgDto = new TbYsGajgDto();
            ysGajgDto.setLyid(s);
            ysGajgDto.setLybm("tb_jcgl_kfd");
            tbYsZxAdaptRemoteService.delEntity(ysGajgDto);
        }
    }


    @Override
    public List<TbJcglKfdDto> getListAll() {
        List<TbJcglKfd> list = this.baseMapper.getListAll();
        List<TbJcglKfdDto> rList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                TbJcglKfdDto vo = new TbJcglKfdDto();
                BeanUtils.copyProperties(item, vo);
                if (item.getYfzfw() != null) vo.setYfzfw(JtsUtils.decodePolygon(item.getYfzfw()));
                if (item.getSfzfw() != null) vo.setSfzfw(JtsUtils.decodePolygon(item.getSfzfw()));
                if (item.getWfzfw() != null) vo.setWfzfw(JtsUtils.decodePolygon(item.getWfzfw()));
                rList.add(vo);
            });
        }
        return rList;
    }

    @Override
    public Page<TbJcglKfd> getKfdList(AppSearchQuery dto) {
        UserInfo user = UserInfoManager.get();
        LambdaQueryWrapper<TbJcglKfd> wrapper = new LambdaQueryWrapper<TbJcglKfd>()
                .eq(TbJcglKfd::getXtSjzt, "1")
                .eq(StringUtils.isNotBlank(dto.getDwlx()), TbJcglKfd::getDwlx, dto.getDwlx());

        if ("1".equals(dto.getType())) {

            wrapper.like(StringUtils.isNotBlank(dto.getKeyword()), TbJcglKfd::getKfdMc, dto.getKeyword())
//                    .eq(TbJcglKfd::getSsbmdm, String.valueOf(user.getDeptCode()));
                    .likeRight(TbJcglKfd::getSsbmdm, user.getDeptCode().substring(0, 6));

        } else {
            String ssbm = this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null);
            wrapper.likeRight(StringUtils.isNotBlank(ssbm), TbJcglKfd::getSsbmdm, ssbm)
                    .and(StringUtils.isNotBlank(dto.getKeyword()),
                            it -> it.like(TbJcglKfd::getKfdMc, dto.getKeyword())
                                    .or()
                                    .like(TbJcglKfd::getSsbm, dto.getKeyword())
                    );
        }
        return this.baseMapper.selectPage(new Page<>(dto.getPageCurrent(), dto.getPageSize()), wrapper);
    }
}
