package com.mosty.yjzl.service.Impl;

import cn.hutool.core.bean.BeanUtil;
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.feign.remote.MostyQwzxFeignServiceRemote;
import com.mosty.base.model.dto.qwzx.TbQwXfbbVo;
import com.mosty.base.model.dto.yjzl.TbZdxlFgxlrwUpdateDTO;
import com.mosty.base.model.dto.yjzl.ZdyxlFgxlrwDto;
import com.mosty.base.model.entity.qwzx.TbQwJl;
import com.mosty.base.model.entity.qwzx.TbQwXfbb;
import com.mosty.base.model.entity.yjzl.zddw.*;
import com.mosty.base.model.query.yjzl.TbZdxlFgxlrwQuery;
import com.mosty.base.model.vo.yjzl.TbZdxlFgdwBddxlrwVO;
import com.mosty.base.model.vo.yjzl.TbZdxlFgxlrwVO;
import com.mosty.base.utils.*;
import com.mosty.common.base.util.StringUtils;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.yjzl.mapper.TbZdxlFgdwBddMapper;
import com.mosty.yjzl.mapper.TbZdxlFgdwBddxlrwMapper;
import com.mosty.yjzl.mapper.TbZdxlFgxlrwMapper;
import com.mosty.yjzl.service.*;
import com.mosty.yjzl.utils.TbZdxlFgxlrwUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhangzhao
 * @description 指导巡逻方格巡逻任务表Service实现类
 * @modifier zhangzhao
 * @modifiedTime 2025/05/10 22:03
 * @since 2025/05/10 22:03
 */
@Service
@AllArgsConstructor
public class TbZdxlFgxlrwServiceImpl extends ServiceImpl<TbZdxlFgxlrwMapper, TbZdxlFgxlrw> implements TbZdxlFgxlrwService {


    /**
     * 指导巡逻Service
     */
    private final TbZdxlFgdwService tbZdxlFgdwService;

    /**
     * 指导巡逻方格默认配置表Service
     */
    private final TbZdxlFgmrpzService tbZdxlFgmrpzService;

    /**
     * 指导巡逻方格点位必到点巡逻任务表Service
     */
    private final TbZdxlFgdwBddxlrwService tbZdxlFgdwBddxlrwService;

    /**
     * 指导巡逻方格点位必到点巡逻任务记录表Service
     */
    private final TbZdxlFgdwBddxlrwJlService tbZdxlFgdwBddxlrwJlService;

    private final TbZdxlFgxlrwMapper tbZdxlFgxlrwMapper;

    private final TbZdxlFgdwBddMapper tbZdxlFgdwBddMapper;

    private final TbZdxlFgdwBddxlrwMapper tbZdxlFgdwBddxlrwMapper;
    @Override
    public TbZdxlFgxlrw selectById(String id) {
        if(StringUtils.isBlank(id)){
            return null;
        }
        return this.baseMapper.selectOne(new QueryWrapper<TbZdxlFgxlrw>()
                .eq(TbZdxlFgxlrw.ID_FIELD, id)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    public IPage<TbZdxlFgxlrwVO> selectPage(TbZdxlFgxlrwQuery query) {
        IPage<TbZdxlFgxlrw> page = PageUtils.buildPage(query.getPageSize(), query.getPageCurrent());
        QueryWrapper<TbZdxlFgxlrw> qw = new QueryWrapper<>();

        //组装查询参数
        this.buildListSelectQueryWrapper(qw, query);

        //返回Page
        IPage<TbZdxlFgxlrwVO> resultPage = new Page<>(query.getPageCurrent(), query.getPageSize());

        //查询数据
        page = this.baseMapper.selectPage(page, qw);
        List<TbZdxlFgxlrw> list = page.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return resultPage;
        }

        //转为VO
        List<TbZdxlFgxlrwVO> resultList = this.buildAllInfoListByEntityList(list);

        //组装VO
        resultPage.setRecords(resultList);
        resultPage.setTotal(page.getTotal());
        return resultPage;
    }

    @Override
    public IPage<TbZdxlFgxlrwVO> selectRwPage(TbZdxlFgxlrwQuery query) {
        IPage<TbZdxlFgxlrw> page = PageUtils.buildPage(query.getPageSize(), query.getPageCurrent());
        QueryWrapper<TbZdxlFgxlrw> qw = new QueryWrapper<>();

        //组装默认任务状态
        query.setRwZt(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_YXF + "," + TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_XLZ + "," + TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_XLJS);

        //组装查询参数
        this.buildListSelectQueryWrapper(qw, query);

        //组装本人所在巡组需要打卡或者没有指定巡组的任务
        UserInfo user = UserInfoManager.get();
        TbQwXfbb xfbb = MostyQwzxFeignServiceRemote.getMyXfbbTodayNew(null);
        if(ObjectUtils.isEmpty(xfbb)){
            qw.isNull(TbZdxlFgxlrw.XFBB_ID_FIELD).isNull(TbZdxlFgxlrw.XFBB_QW_JL_ID_FIELD);
        }else{
            qw.and(qw1 -> qw1.eq(TbZdxlFgxlrw.XFBB_ID_FIELD, xfbb.getId()).or().isNull(TbZdxlFgxlrw.XFBB_ID_FIELD));
            //组装巡防力量查询条件
            String xfllId = ObjectUtils.isEmpty(user.getUserId()) ? user.getXfllId() : user.getUserId().toString();
            TbQwJl jl = MostyQwzxFeignServiceRemote.selectQwJlByBbInfo(xfbb.getId(), xfllId);
            if(ObjectUtils.isEmpty(jl)){
                qw.isNull(TbZdxlFgxlrw.XFBB_QW_JL_ID_FIELD);
            }else{
                qw.and(qw1 -> qw1.eq(TbZdxlFgxlrw.XFBB_QW_JL_ID_FIELD, jl.getId()).or().isNull(TbZdxlFgxlrw.XFBB_QW_JL_ID_FIELD));
            }
        }

        //返回Page
        IPage<TbZdxlFgxlrwVO> resultPage = new Page<>(query.getPageCurrent(), query.getPageSize());

        //查询数据
        page = this.baseMapper.selectPage(page, qw);
        List<TbZdxlFgxlrw> list = page.getRecords();
        if(CollectionUtils.isEmpty(list)){
            return resultPage;
        }

        //转为VO
        List<TbZdxlFgxlrwVO> resultList = this.buildAllInfoListByEntityList(list);

        //组装VO
        resultPage.setRecords(resultList);
        resultPage.setTotal(page.getTotal());
        return resultPage;
    }

    @Override
    public TbZdxlFgxlrwVO selectByBBdId(TbZdxlFgxlrwQuery query) {
        TbZdxlFgxlrw xlrw = this.baseMapper.selectById(query.getBbdId());
        TbZdxlFgxlrwVO resultVO = this.buildAllInfoByEntity(xlrw);
        return resultVO;
    }

    @Override
    public List<TbZdxlFgxlrwVO> selectList(TbZdxlFgxlrwQuery query) {
        QueryWrapper<TbZdxlFgxlrw> qw = new QueryWrapper<>();

        //组装查询参数
        this.buildListSelectQueryWrapper(qw, query);

        //查询数据
        List<TbZdxlFgxlrw> list = this.baseMapper.selectList(qw);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }

        //转为VO并根据必到点打卡进度正序返回
        List<TbZdxlFgxlrwVO> resultList = this.buildAllInfoListByEntityList(list);
        if(CollectionUtils.isNotEmpty(resultList)){
            resultList = resultList.stream().sorted(Comparator.comparing(TbZdxlFgxlrwVO::getBddAllProgress, Comparator.nullsFirst(Double::compareTo))).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<TbZdxlFgxlrw> selectListByIds(String ids) {
        if(StringUtils.isBlank(ids)){
            return null;
        }

        return this.baseMapper.selectList(new QueryWrapper<TbZdxlFgxlrw>()
                .in(TbZdxlFgxlrw.ID_FIELD, Arrays.asList(ids.split(Constant.SEPARATOR_ENGLISH_COMMA)))
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }



    @Override
    public List<TbZdxlFgxlrw> selectListByRwRq(Date rwRq) {
        if (ObjectUtils.isEmpty(rwRq)) {
            return null;
        }
        return this.baseMapper.selectList(new QueryWrapper<TbZdxlFgxlrw>()
                .eq(TbZdxlFgxlrw.RW_RQ_FIELD, rwRq)
                .eq("xt_sjzt", "1")
                .eq("xt_scbz", "0")
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String updateEntity(TbZdxlFgxlrwUpdateDTO dto) {
        //复制参数至实体
        TbZdxlFgxlrw entity = BeanUtil.copyProperties(dto, TbZdxlFgxlrw.class);

        //修改实体
        int resultInt = this.baseMapper.updateById(entity);
        if(resultInt > 0){
            //保存必到点信息
            tbZdxlFgdwBddxlrwService.handleList(dto.getBddList(), dto.getId());
            //返回主键
            return entity.getId();
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateListRwZtToYdc(List<TbZdxlFgxlrw> entityList) {
        if(CollectionUtils.isEmpty(entityList)){
            return -1;
        }

        List<TbZdxlFgxlrw> updateList = new ArrayList<>();
        for(TbZdxlFgxlrw entity : entityList){
            if(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_CJ.equals(entity.getRwZt())){
                TbZdxlFgxlrw updateEntity = new TbZdxlFgxlrw();
                updateEntity.setId(entity.getId());
                updateEntity.setRwZt(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_YDC);
                updateList.add(updateEntity);
            }
        }
        if(CollectionUtils.isNotEmpty(updateList)){
            boolean success = this.updateBatchById(updateList);
            if(success){
                return updateList.size();
            }
        }
        return 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int importListByMb(List<TbZdxlFgxlrw> entityList) {
        if(CollectionUtils.isEmpty(entityList)){
            return -1;
        }

        //查询默认配置
        TbZdxlFgmrpz mrpz = tbZdxlFgmrpzService.selectDefault();
        if(ObjectUtils.isEmpty(mrpz)){
            mrpz = new TbZdxlFgmrpz();
            mrpz.setPcCl(0);
            mrpz.setPcRy(0);
            mrpz.setXlSc(0.0);
            mrpz.setXlLc(0.0);
        }

        //筛选保存和修改列表
        List<TbZdxlFgxlrw> saveList = new ArrayList<>(), updateList = new ArrayList<>();
        for(TbZdxlFgxlrw entity : entityList){
            TbZdxlFgxlrw baseEntity = this.selectById(entity.getId());

            if(ObjectUtils.isEmpty(baseEntity)){
                //设置默认值
                entity.setXlghSc(mrpz.getXlSc());
                entity.setXlghXllc(mrpz.getXlLc());
                entity.setXlghPcCl(mrpz.getPcCl());
                entity.setXlghPcRy(mrpz.getPcRy());
                entity.setRwlx("1");
                //保存
                saveList.add(entity);
            }else{
                //修改
                if(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_CJ.equals(baseEntity.getRwZt()) ||
                        TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_YDC.equals(baseEntity.getRwZt())){
                    updateList.add(entity);
                }
            }
        }

        //分别保存和修改
        int resultInt = 0;
        if(CollectionUtils.isNotEmpty(saveList)){
            boolean success = this.saveBatch(saveList);
            if(success){
                resultInt += saveList.size();
            }
        }
        if(CollectionUtils.isNotEmpty(updateList)){
            boolean success = this.updateBatchById(updateList);
            if(success){
                resultInt += updateList.size();
            }
        }

        return resultInt;
    }

    @Override
    public int importListByMbZdy(List<TbZdxlFgzdyrw> entityList) {
        if(CollectionUtils.isEmpty(entityList)){
            return -1;
        }

        //查询默认配置
        TbZdxlFgmrpz mrpz = tbZdxlFgmrpzService.selectDefault();
        if(ObjectUtils.isEmpty(mrpz)){
            mrpz = new TbZdxlFgmrpz();
            mrpz.setPcCl(0);
            mrpz.setPcRy(0);
            mrpz.setXlSc(0.0);
            mrpz.setXlLc(0.0);
        }

        //筛选保存和修改列表
        for(TbZdxlFgzdyrw entity : entityList){
            TbZdxlFgxlrw baseEntity = this.selectById(entity.getId());

            if(ObjectUtils.isEmpty(baseEntity)){
                //设置默认值
                entity.setXlghSc(mrpz.getXlSc());
                entity.setXlghXllc(mrpz.getXlLc());
                entity.setXlghPcCl(mrpz.getPcCl());
                entity.setXlghPcRy(mrpz.getPcRy());
                entity.setRwlx("2");
                baseEntity = new TbZdxlFgxlrw();
                BeanUtils.copyProperties(entity,baseEntity);
                //保存
                this.baseMapper.insert(baseEntity);
            }else{
                //修改
                BeanUtils.copyProperties(entity,baseEntity);
                this.baseMapper.updateById(baseEntity);
            }
        }

        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int issueList(List<TbZdxlFgxlrw> entityList) {
        if(CollectionUtils.isEmpty(entityList)){
            return -1;
        }

        List<TbZdxlFgxlrw> updateList = new ArrayList<>();
        for(TbZdxlFgxlrw entity : entityList){
            TbZdxlFgxlrw updateEntity = new TbZdxlFgxlrw();
            updateEntity.setId(entity.getId());
            updateEntity.setRwZt(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_YXF);
            updateList.add(updateEntity);
        }

        boolean success = this.updateBatchById(updateList);
        if(success){
            //再保存一次必到点巡逻任务信息
            List<TbZdxlFgdwBddxlrw> bddxlrwList = tbZdxlFgdwBddxlrwService.handleListByFgxlrwList(entityList);
            if(CollectionUtils.isNotEmpty(bddxlrwList)){
                //必到点巡逻任务保存后，生成对应的必到点巡逻任务记录
                tbZdxlFgdwBddxlrwJlService.createListByBddxlrwList(bddxlrwList);
            }
            //返回
            return updateList.size();
        }
        return 0;
    }

    @Override
    public TbZdxlFgxlrw addZdyxlFgxlrw(ZdyxlFgxlrwDto dto) {
        if(CollectionUtils.isNotEmpty(dto.getBddDtos())){
            List<String> bddIds = new ArrayList<>();
            dto.getBddDtos().forEach(bddDto ->{
                TbZdxlFgdwBdd bdd = new TbZdxlFgdwBdd();
                bdd.setId(UUIDGenerator.getUUID());
                bdd.setFgdwId(dto.getFgdwId());
                bdd.setBddMc(bddDto.getBddMc());
                bdd.setBddDz(bddDto.getBddDz());
                bdd.setJd(bddDto.getJd());
                bdd.setWd(bddDto.getWd());
                bdd.setDkjgsj(dto.getDkjgsj());
                this.tbZdxlFgdwBddMapper.insert(bdd);
                bddIds.add(bdd.getId());
            });
            TbZdxlFgxlrw xlrw = new TbZdxlFgxlrw();
            xlrw.setId(UUIDGenerator.getUUID());
            xlrw.setRwlx("3");
            xlrw.setFgRwbt(dto.getFgRwbt());
            xlrw.setFgId(dto.getFgdwId());
            xlrw.setFgPx(1);
            xlrw.setFgYjdj("01");
            xlrw.setFgYjys("01");
            xlrw.setRwRq(new Date());
            xlrw.setRwZt("01");
            xlrw.setRwLqZt("01");
            xlrw.setXlghXllc(2.0);
            this.tbZdxlFgxlrwMapper.insert(xlrw);
            bddIds.forEach(bddId ->{
                TbZdxlFgdwBddxlrw bddxlrw = new TbZdxlFgdwBddxlrw();
                bddxlrw.setId(UUIDGenerator.getUUID());
                bddxlrw.setFgxlrwId(xlrw.getId());
                bddxlrw.setBddId(bddId);
                bddxlrw.setXlghDkcs(1);
                bddxlrw.setXlsjDkcs(0);
                bddxlrw.setSfKs("0");
                bddxlrw.setSfJs("0");
                this.tbZdxlFgdwBddxlrwMapper.insert(bddxlrw);
            });
        }
        return null;
    }

    @Override
    public TbZdxlFgxlrw editZdyxlFgxlrw(ZdyxlFgxlrwDto dto) {
        TbZdxlFgxlrw tbZdxlFgxlrw = this.tbZdxlFgxlrwMapper.selectById(dto.getId());
        tbZdxlFgxlrw.setFgId(dto.getFgdwId());
        tbZdxlFgxlrw.setFgRwbt(dto.getFgRwbt());
        tbZdxlFgxlrw.setRwRq(new Date());
        List<TbZdxlFgdwBddxlrw> fgbddrws = this.tbZdxlFgdwBddxlrwMapper.selectList(
                new LambdaQueryWrapper<TbZdxlFgdwBddxlrw>()
                        .eq(TbZdxlFgdwBddxlrw::getFgxlrwId,tbZdxlFgxlrw.getId())
        );
        List<String> bddIdList = fgbddrws.stream().map(TbZdxlFgdwBddxlrw::getBddId).collect(Collectors.toList());
        this.tbZdxlFgdwBddMapper.deleteBatchIds(bddIdList);
        tbZdxlFgdwBddxlrwMapper.delete(
                new LambdaQueryWrapper<TbZdxlFgdwBddxlrw>()
                        .eq(TbZdxlFgdwBddxlrw::getFgxlrwId,tbZdxlFgxlrw.getId())
        );
        if(CollectionUtils.isNotEmpty(dto.getBddDtos())){
            List<String> bddIds = new ArrayList<>();
            dto.getBddDtos().forEach(bddDto ->{
                TbZdxlFgdwBdd bdd = new TbZdxlFgdwBdd();
                bdd.setId(UUIDGenerator.getUUID());
                bdd.setFgdwId(dto.getFgdwId());
                bdd.setBddMc(bddDto.getBddMc());
                bdd.setBddDz(bddDto.getBddDz());
                bdd.setJd(bddDto.getJd());
                bdd.setWd(bddDto.getWd());
                bdd.setDkjgsj(dto.getDkjgsj());
                this.tbZdxlFgdwBddMapper.insert(bdd);
                bddIds.add(bdd.getId());
            });
            bddIds.forEach(bddId ->{
                TbZdxlFgdwBddxlrw bddxlrw = new TbZdxlFgdwBddxlrw();
                bddxlrw.setId(UUIDGenerator.getUUID());
                bddxlrw.setFgxlrwId(tbZdxlFgxlrw.getId());
                bddxlrw.setBddId(bddId);
                bddxlrw.setXlghDkcs(1);
                bddxlrw.setXlsjDkcs(0);
                bddxlrw.setSfKs("0");
                bddxlrw.setSfJs("0");
                this.tbZdxlFgdwBddxlrwMapper.insert(bddxlrw);
            });
        }
        return tbZdxlFgxlrw;
    }

    @Override
    public Boolean delZdyxlFgxlrw(ZdyxlFgxlrwDto dto) {
        this.tbZdxlFgxlrwMapper.deleteById(dto.getId());
        return true;
    }

    /**
     * 组装查询参数
     * @param qw QueryWrapper
     * @param query 查询对象
     */
    private void buildListSelectQueryWrapper(QueryWrapper<TbZdxlFgxlrw> qw, TbZdxlFgxlrwQuery query){
        //默认使用排序字段
        if(StringUtils.isBlank(query.getSort())){
            query.setSort(TbZdxlFgxlrw.RW_RQ_FIELD);
            query.setOrder(QueryWrapperUtils.ORDER_DESC_STRING);
        }
        //组装其他排序方式
        QueryWrapperUtils.buildQueryWrapperOrder(qw, query.getSort(), query.getOrder());
        //未注销数据
        qw.eq("xt_sjzt", "1").eq("xt_scbz", "0");
        //方格ID
        qw.eq(ObjectUtils.isNotEmpty(query.getFgId()), TbZdxlFgxlrw.FG_ID_FIELD, query.getFgId());
        qw.eq(StringUtils.isNotEmpty(query.getRwlx()), TbZdxlFgxlrw.FG_RWLX_FIELD, query.getRwlx());
        //方格名称
        qw.inSql(StringUtils.isNotBlank(query.getFgMc()), TbZdxlFgxlrw.FG_ID_FIELD, "select id from tb_zdxl_fgdw where mc1 like '%" + query.getFgMc() + "%' ");
        //方格预警等级
        if(StringUtils.isNotBlank(query.getFgYjdj())){
            qw.in(TbZdxlFgxlrw.FG_YJDJ_FIELD, Arrays.stream(query.getFgYjdj().split(",")).filter(StringUtils::isNotBlank).collect(Collectors.toList()));
        }
        //任务日期
        qw.eq(ObjectUtils.isNotEmpty(query.getRwRq()), TbZdxlFgxlrw.RW_RQ_FIELD, query.getRwRq());
        //任务状态
        if(StringUtils.isNotBlank(query.getRwZt())){
            qw.in(TbZdxlFgxlrw.RW_ZT_FIELD, Arrays.stream(query.getRwZt().split(",")).filter(StringUtils::isNotBlank).collect(Collectors.toList()));
        }
    }

    /**
     * 组装全量信息列表（根据实体列表）
     * @param entityList 实体列表
     * @return 全量信息列表
     */
    private List<TbZdxlFgxlrwVO> buildAllInfoListByEntityList(List<TbZdxlFgxlrw> entityList){
        List<TbZdxlFgxlrwVO> allInfoVOList = new ArrayList<>();
        //组装返回数据
        for(TbZdxlFgxlrw entity : entityList){
            TbZdxlFgxlrwVO resultVO = this.buildAllInfoByEntity(entity);
            allInfoVOList.add(resultVO);
        }
        //返回列表
        if(CollectionUtils.isEmpty(allInfoVOList)){
            return null;
        }
        return allInfoVOList;
    }

    /**
     * 组装全量信息（根据实体信息）
     * @param entity 实体信息
     * @return 全量信息
     */
    private TbZdxlFgxlrwVO buildAllInfoByEntity(TbZdxlFgxlrw entity){
        TbZdxlFgxlrwVO resultVO = BeanUtil.copyProperties(entity, TbZdxlFgxlrwVO.class);

        if(ObjectUtils.isNotEmpty(resultVO.getFgId())){
            //组装方格数据
            TbZdxlFgdw fg = tbZdxlFgdwService.selectById(resultVO.getFgId());
            if(ObjectUtils.isNotEmpty(fg)){
                resultVO.setFgMc(fg.getMc1());
                resultVO.setX1(fg.getX11());
                resultVO.setY1(fg.getY11());
                resultVO.setX2(fg.getX21());
                resultVO.setY2(fg.getY21());
                resultVO.setZxX(fg.getZxX());
                resultVO.setZxY(fg.getZxY());
            }

            //组装必到点信息
            List<TbZdxlFgdwBddxlrwVO> bddList;
            if(TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_CJ.equals(resultVO.getRwZt()) || TbZdxlFgxlrwUtils.DICT_ITEM_D_ZDXL_FGXLRW_RWZT_YDC.equals(resultVO.getRwZt())){
                //未下发查询实时必到点
                bddList = tbZdxlFgdwBddxlrwService.selectVoListByFgxlrwId(resultVO.getId());
            }else{
                //已下发查询固定必到点
                bddList = tbZdxlFgdwBddxlrwService.selectVoListByYxfFgxlrwId(resultVO.getId());
            }
            if(CollectionUtils.isNotEmpty(bddList)){
                int totalGhCs = bddList.stream().mapToInt(TbZdxlFgdwBddxlrwVO::getXlghDkcs).sum();
                int totalSjCs = bddList.stream().mapToInt(TbZdxlFgdwBddxlrwVO::getXlsjDkcs).sum();
                resultVO.setBddAllProgress(MathUtils.percentage(totalSjCs, totalGhCs));
                resultVO.setBddList(bddList);
            }
        }

        //组装巡逻里程总进度
        double xlghXllc = resultVO.getXlghXllc();
        double xlsjXllc = 0.0;
        if(ObjectUtils.isNotEmpty(resultVO.getXlsjXllc())){
            xlsjXllc = resultVO.getXlsjXllc();
        }
        resultVO.setXllcProgress(MathUtils.percentage(new BigDecimal(xlsjXllc + ""), new BigDecimal(xlghXllc + "")));

        //组装巡防报备信息
        if(StringUtils.isNotBlank(resultVO.getXfbbId())){
            TbQwXfbbVo xfbb = MostyQwzxFeignServiceRemote.getXfbbInfo(resultVO.getXfbbId());
            if(ObjectUtils.isNotEmpty(xfbb)){
                resultVO.setXfbb(xfbb);

                //组装选择的巡防报备勤务警力信息
                if(StringUtils.isNotBlank(resultVO.getXfbbQwJlId()) && CollectionUtils.isNotEmpty(xfbb.getJlList())){
                    List<TbQwJl> selectedQwjlList = xfbb.getJlList().stream().filter(xfbbJl -> xfbbJl.getId().equals(resultVO.getXfbbQwJlId())).collect(Collectors.toList());
                    if(CollectionUtils.isNotEmpty(selectedQwjlList)){
                        resultVO.setXfbbQwJl(selectedQwjlList.get(0));
                    }
                }
            }
        }

        return resultVO;
    }

}
