/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.yjxt.business.resident.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.yjxt.business.resident.entity.DangerAreaResidentInfoEntity;
import org.springblade.modules.yjxt.business.resident.vo.DangerAreaResidentInfoVO;


import java.util.Objects;

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

    private static IBaseRegionInfoService baseRegionInfoService;

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

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

    public DangerAreaResidentInfoVO entityVO(DangerAreaResidentInfoVO vo) {
        if (Func.notNull(vo.getAdCode())) {
            LambdaQueryWrapper<BaseRegionInfoEntity> queryWrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
            queryWrapper.eq(BaseRegionInfoEntity::getAdcd, vo.getAdCode());
            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.getAdName())) {
                    fullCode.append(vo.getAdName());
                }
                vo.setFullAadName(fullCode.toString());
            }
        }
        return vo;
    }
}
