/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/9/13
 */
package org.springblade.modules.yjxt.base.map.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.baseinfo.region.whole.entity.BaseRegionInfoEntity;
import org.springblade.modules.baseinfo.region.whole.service.IBaseRegionInfoService;
import org.springblade.modules.baseinfo.stbase.entity.MvAttStBaseEntity;
import org.springblade.modules.baseinfo.stbase.service.IMvAttStBaseService;
import org.springblade.modules.yjxt.base.map.dto.DangerAreaLeaveLineDTO;
import org.springblade.modules.yjxt.base.map.entity.DangerAreaEntity;
import org.springblade.modules.yjxt.base.map.entity.DangerAreaPointEntity;
import org.springblade.modules.yjxt.base.map.service.IDangerAreaLeaveLineService;
import org.springblade.modules.yjxt.base.map.service.IDangerAreaPointService;
import org.springblade.modules.yjxt.base.map.vo.DangerAreaLeaveLineVO;
import org.springblade.modules.yjxt.base.map.vo.DangerAreaVO;
import org.springblade.modules.yjxt.business.resident.dto.DangerAreaResidentInfoDTO;
import org.springblade.modules.yjxt.business.resident.entity.DangerAreaResidentInfoEntity;
import org.springblade.modules.yjxt.business.resident.service.IDangerAreaResidentService;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/***
 * Date:2023/9/13
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
public class DangerAreaWrapper extends BaseEntityWrapper<DangerAreaEntity, DangerAreaVO> {
    public static DangerAreaWrapper build() {
        return new DangerAreaWrapper();
    }

    //    private static IRtuBaseInfoService rtuBaseInfoService;
//
//    private static IRtuBaseInfoService getRtuBaseInfoService() {
//        if (rtuBaseInfoService == null) {
//            rtuBaseInfoService = SpringUtil.getBean(IRtuBaseInfoService.class);
//        }
//        return rtuBaseInfoService;
//    }
//
    private static IBaseRegionInfoService baseRegionInfoService;
    private static IBaseRegionInfoService getBaseRegionInfoService() {
        if (baseRegionInfoService == null) {
            baseRegionInfoService = SpringUtil.getBean(IBaseRegionInfoService.class);
        }
        return baseRegionInfoService;
    }

    private static IDangerAreaLeaveLineService dangerAreaLeaveLineService;
    private static IDangerAreaLeaveLineService getDangerAreaLeaveLineService() {
        if (dangerAreaLeaveLineService == null) {
            dangerAreaLeaveLineService = SpringUtil.getBean(IDangerAreaLeaveLineService.class);
        }
        return dangerAreaLeaveLineService;
    }

    private static IMvAttStBaseService mvAttStBaseService;
    private static IMvAttStBaseService getMvAttStBaseService() {
        if (mvAttStBaseService == null) {
            mvAttStBaseService = SpringUtil.getBean(IMvAttStBaseService.class);
        }
        return mvAttStBaseService;
    }

    private static IDangerAreaPointService dangerAreaPointService;
    private static IDangerAreaPointService getDangerAreaPointService() {
        if (dangerAreaPointService == null) {
            dangerAreaPointService = SpringUtil.getBean(IDangerAreaPointService.class);
        }
        return dangerAreaPointService;
    }

    private static IDangerAreaResidentService dangerAreaResidentService;
    private static IDangerAreaResidentService getDangerAreaResidentService() {
        if (dangerAreaResidentService == null) {
            dangerAreaResidentService = SpringUtil.getBean(IDangerAreaResidentService.class);
        }
        return dangerAreaResidentService;
    }


    @Override
    public DangerAreaVO entityVO(DangerAreaEntity entity) {
        DangerAreaVO vo = Objects.requireNonNull(BeanUtil.copy(entity, DangerAreaVO.class));
        return this.entityVO(vo);
    }

    public DangerAreaVO entityVO(DangerAreaVO vo) {

        if (Func.notNull(vo.getStCode())) {
            List<MvAttStBaseEntity> rtus = new ArrayList<>();
            String stCode = vo.getStCode();
            String[] stCodeArr = stCode.split(",");
            for (String st : stCodeArr) {
                LambdaQueryWrapper<MvAttStBaseEntity> rtuwrapper = Wrappers.<MvAttStBaseEntity>query().lambda();
                rtuwrapper.eq(MvAttStBaseEntity::getStCode, st);
                rtuwrapper.last("limit 1");
                MvAttStBaseEntity rtuInfoEntity = getMvAttStBaseService().getOne(rtuwrapper);
                rtus.add(rtuInfoEntity);
            }
            vo.setRtus(rtus);
        }
        vo.setLeaveLines(new ArrayList<>(0));
        if (Func.notNull(vo.getAdcd())) {
            DangerAreaLeaveLineDTO lineDTO = new DangerAreaLeaveLineDTO();
            lineDTO.setAdcd(vo.getAdcd());
            List<DangerAreaLeaveLineVO> lineVOList = getDangerAreaLeaveLineService().selectAdDangerArea(lineDTO);
            if (Func.notNull(lineVOList)) {
                vo.setLeaveLines(lineVOList);
            }
        }

        DangerAreaResidentInfoDTO dangerAreaResidentInfoDTO = new DangerAreaResidentInfoDTO();
        dangerAreaResidentInfoDTO.setIsHouseholder(1);
        dangerAreaResidentInfoDTO.setDangerAreaPid(vo.getDangerAreaPid());
        List<DangerAreaResidentInfoEntity> familyList = getDangerAreaResidentService().getFamilyPoints(dangerAreaResidentInfoDTO);
        if (Func.notNull(familyList) && familyList.size() > 0) {
            vo.setFamilyPoints(familyList);
            vo.setFamilyCount((long) familyList.size());
        } else {
            vo.setFamilyCount(0L);
        }


//        String stCode = vo.getStCode();
//        if (Func.notNull(stCode)) {
//            String[] stCodeArr = stCode.split(",");
//            List<MvAttStBaseEntity> rtus = new ArrayList<>(stCodeArr.length);
//            for (String st : stCodeArr) {
//                LambdaQueryWrapper<MvAttStBaseEntity> rtuwrapper = Wrappers.<MvAttStBaseEntity>query().lambda();
//                rtuwrapper.eq(MvAttStBaseEntity::getStCode, st);
//                rtuwrapper.last("limit 1");
//                MvAttStBaseEntity rtuInfoEntity = getMvAttStBaseService().getOne(rtuwrapper);
//
//                if (Func.notNull(rtuInfoEntity)) {
//                    rtus.add(rtuInfoEntity);
//                }
//            }
//            vo.setRtus(rtus);
//        }
//
        if (Func.notNull(vo.getAdcd())) {
            LambdaQueryWrapper<BaseRegionInfoEntity> queryWrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
            queryWrapper.eq(BaseRegionInfoEntity::getAdcd, vo.getAdcd());
            queryWrapper.last("limit 1");
            BaseRegionInfoEntity entity = getBaseRegionInfoService().getOne(queryWrapper);
            if (Func.notNull(entity)) {
                String ancestores = entity.getAncestors();
                StringBuilder fullCode = new StringBuilder();
                String[] codes = ancestores.split(",");
                for (int i = 2; i < codes.length; i++) {
                    if (fullCode.length() > 0) {
                        fullCode.append("/");
                    }
                    queryWrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
                    queryWrapper.eq(BaseRegionInfoEntity::getAdcd, codes[i]);
                    queryWrapper.last("limit 1");
                    BaseRegionInfoEntity region = getBaseRegionInfoService().getOne(queryWrapper);
                    fullCode.append(region.getAdnm());
                }
                if (fullCode.length() > 0) {
                    fullCode.append("/");
                }
                if (Func.notNull(vo.getAdnm())) {
                    fullCode.append(vo.getAdnm());
                }
                vo.setFullAadName(fullCode.toString());
            }

        }

        return vo;
    }
}
