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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.manager.remote.TbBaseAdaptRemoteService;
import com.mosty.base.model.dto.yszx.TbYsGajgDto;
import com.mosty.base.model.entity.jcgl.TbJcglBxd;
import com.mosty.base.model.query.jcgl.TbJcglBxdNearByQuery;
import com.mosty.base.model.query.jcgl.TbJcglBxdQuery;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.utils.*;
import com.mosty.base.manager.mapper.TbJcglBxdMapper;
import com.mosty.base.manager.remote.TbYsZxAdaptRemoteService;
import com.mosty.base.manager.service.TbJcglBxdService;
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.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class TbJcglBxdServiceImpl extends ServiceImpl<TbJcglBxdMapper, TbJcglBxd>
        implements TbJcglBxdService {

    private final TbYsZxAdaptRemoteService tbYsZxAdaptRemoteService;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;

    @Override
    @Transactional
    public int addBxd(TbJcglBxd bxd) {
        bxd.setId(UUIDGenerator.getUUID());
        if (bxd.getJd() != null && bxd.getWd() != null) {
            bxd.setZb(JtsUtils.getPoint(bxd.getJd(), bxd.getWd()));
            bxd.setZbhash(GeoHashKit.encode(bxd.getJd(), bxd.getWd()));
        }
        if (StringUtils.isNotBlank(bxd.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(bxd.getSsbmdm());
            bxd.setSsbm(dept.getDeptname());
            bxd.setSsbmid(dept.getDeptid());
            bxd.setSsbmdm(dept.getDeptcode());
            bxd.setSsxgajdm(dept.getFxjcode());
            bxd.setSsxgajid(dept.getFxjid());
            bxd.setSsxgaj(dept.getFxjname());
            bxd.setSssgaj(dept.getDszname());
            bxd.setSssgajdm(dept.getDszcode());
            bxd.setSssgajid(dept.getDszid());
        }
        bxd.setXtSjly("1");
        int ret = this.baseMapper.insertExtity(bxd);
        //封装数据
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(bxd.getId());
        ysGajgDto.setLybm("tb_jcgl_bxd");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001011");
        ysGajgDto.setGajgmc(bxd.getBxdMc());
        ysGajgDto.setGajgjc(bxd.getBxdMc());
        ysGajgDto.setWzXzqh(bxd.getXzqhdm());
        ysGajgDto.setWzXz(bxd.getXxdz());
        ysGajgDto.setWzBc(bxd.getXxdz());
        ysGajgDto.setJd(bxd.getJd());
        ysGajgDto.setWd(bxd.getWd());
        ysGajgDto.setSsbm(bxd.getSsbm());
        ysGajgDto.setSsbmid(bxd.getSsbmid());
        ysGajgDto.setSsbmdm(bxd.getSsbmdm());
        tbYsZxAdaptRemoteService.addGajg(ysGajgDto);
        return ret;
    }

    @Override
    public IPage<TbJcglBxd> queryBxd(TbJcglBxdQuery dto) {
        IPage<TbJcglBxd> page = PageUtils.buildPage(dto.getPageSize(), dto.getPageCurrent());
        QueryWrapper<TbJcglBxd> qw = new QueryWrapper<>();
//        UserInfo userInfo = UserInfoManager.get();
//        PermissionsUtil.queryWrapperUtil(qw, userInfo);
        dto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(dto.getSsbmdm(), null));

        qw.lambda().eq(StringUtils.isNotBlank(dto.getSsXfqyId()), TbJcglBxd::getSsXfqyId, dto.getSsXfqyId())
                .like(StringUtils.isNotBlank(dto.getBxdMc()), TbJcglBxd::getBxdMc, dto.getBxdMc())
                .likeRight(StringUtils.isNotBlank(dto.getSsbmdm()), TbJcglBxd::getSsbmdm, dto.getSsbmdm())
                .eq(StringUtils.isNotBlank(dto.getBxdLx()), TbJcglBxd::getBxdLx, dto.getBxdLx())
                .eq(StringUtils.isNotBlank(dto.getXtSjzt()), TbJcglBxd::getXtSjzt, dto.getXtSjzt())
                .like(StringUtils.isNotBlank(dto.getXxdz()), TbJcglBxd::getXxdz, dto.getXxdz())
                .like(StringUtils.isNotBlank(dto.getXzqhdm()), TbJcglBxd::getXzqhdm, dto.getXzqhdm())
                .eq(StringUtils.isNotBlank(dto.getBxdlydm()), TbJcglBxd::getBxdlydm, dto.getBxdlydm())
                .eq(StringUtils.isNotBlank(dto.getSsbmid()), TbJcglBxd::getSsbmid, dto.getSsbmid());
        return this.baseMapper.selectPage(page, qw);
    }

    @Override
    public List<TbJcglBxd> selectNearbyList(TbJcglBxdNearByQuery query) {
        if(ObjectUtils.isEmpty(query)){
            return null;
        }

        if(StringUtils.isNotBlank(query.getBxdLx())){
            query.setBxdLxList(Arrays.stream(query.getBxdLx().split(",")).filter(StringUtils::isNotBlank).collect(Collectors.toList()));
        }

        if(query.getRadius() == null || query.getRadius() == 0){
            query.setRadius(1000);
        }

        if(StringUtils.isBlank(query.getSsbmdm())){
            UserInfo user = UserInfoManager.get();
            query.setSsbmdm(tbBaseAdaptRemoteService.getSsbm(user.getDeptCode(), Constant.DICT_ITEM_D_BZ_SF_YES));
        }

        return this.baseMapper.selectNearbyList(query);
    }

    @Override
    @Transactional
    public int editBxd(TbJcglBxd bxd) {
        if (bxd.getJd() != null && bxd.getWd() != null) {
            bxd.setZb(JtsUtils.getPoint(bxd.getJd(), bxd.getWd()));
            bxd.setZbhash(GeoHashKit.encode(bxd.getJd(), bxd.getWd()));
        }
        if (StringUtils.isNotBlank(bxd.getSsbmdm())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(bxd.getSsbmdm());
            bxd.setSsbm(dept.getDeptname());
            bxd.setSsbmid(dept.getDeptid());
            bxd.setSsbmdm(dept.getDeptcode());
            bxd.setSsxgajdm(dept.getFxjcode());
            bxd.setSsxgajid(dept.getFxjid());
            bxd.setSsxgaj(dept.getFxjname());
            bxd.setSssgaj(dept.getDszname());
            bxd.setSssgajdm(dept.getDszcode());
            bxd.setSssgajid(dept.getDszid());
        }
        int ret = this.baseMapper.updateEntity(bxd);
        TbYsGajgDto ysGajgDto = new TbYsGajgDto();
        ysGajgDto.setLyid(bxd.getId());
        ysGajgDto.setLybm("tb_jcgl_bxd");
        ysGajgDto.setYsdl("001");
        ysGajgDto.setYszl("001001");
        ysGajgDto.setYsxl("001001011");
        ysGajgDto.setGajgmc(bxd.getBxdMc());
        ysGajgDto.setGajgjc(bxd.getBxdMc());
        ysGajgDto.setWzXzqh(bxd.getXzqhdm());
        ysGajgDto.setWzXz(bxd.getXxdz());
        ysGajgDto.setWzBc(bxd.getXxdz());
        ysGajgDto.setJd(bxd.getJd());
        ysGajgDto.setWd(bxd.getWd());
        ysGajgDto.setSsbm(bxd.getSsbm());
        ysGajgDto.setSsbmid(bxd.getSsbmid());
        ysGajgDto.setSsbmdm(bxd.getSsbmdm());
        tbYsZxAdaptRemoteService.updateGajg(ysGajgDto);
        return ret;
    }

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

    @Override
    public TbJcglBxd selectById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void createdBxd(List<Map<String, Object>> list) {
        // 先需要注释之前生成的所有的必巡点的信息
        this.baseMapper.updateYesterdayBxd();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> item = list.get(i);
            TbJcglBxd bxd = new TbJcglBxd();
            bxd.setId(UUIDGenerator.getUUID());
            bxd.setXtSjly("1");
            String address = this.tbBaseAdaptRemoteService.getLocation(item.get("jd").toString(), item.get("wd").toString());
            bxd.setBxdMc(DateUtils.getQueryDateString(new Date(), "01") + "重点必巡点" + (i + 1));
            bxd.setBxdLx("2");
            bxd.setYxq(new Date());
            bxd.setJd(new BigDecimal(item.get("jd").toString()));
            bxd.setWd(new BigDecimal(item.get("wd").toString()));
            bxd.setZb(JtsUtils.getPoint(bxd.getJd(), bxd.getWd()));
            bxd.setZbhash(GeoHashKit.encode(bxd.getJd(), bxd.getWd()));
            bxd.setXxdz(address);
            bxd.setBxdlydm("03");
            String ssbmdm = item.get("ssbmdm") == null ? "" : String.valueOf(item.get("ssbmdm"));
            bxd.setXzqhdm(ssbmdm.substring(0, 6));
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByOrgcode(ssbmdm);
            bxd.setSsbm(dept.getDeptname());
            bxd.setSsbmid(dept.getDeptid());
            bxd.setSsbmdm(dept.getDeptcode());
            bxd.setSsxgajdm(dept.getFxjcode());
            bxd.setSsxgajid(dept.getFxjid());
            bxd.setSsxgaj(dept.getFxjname());
            bxd.setSssgaj(dept.getDszname());
            bxd.setSssgajdm(dept.getDszcode());
            bxd.setSssgajid(dept.getDszid());
            bxd.setBz(String.valueOf(item.get("bz")));
            this.addBxd(bxd);
        }
    }
}
