/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.yjxt.business.check.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.system.entity.Dept;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.yjxt.base.map.entity.DangerAreaEntity;
import org.springblade.modules.yjxt.base.map.service.IDangerAreaService;
import org.springblade.modules.yjxt.business.check.entity.DangerAreaCheckInfoEntity;
import org.springblade.modules.yjxt.business.check.vo.DangerAreaCheckInfoVO;

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

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

    private static IBaseRegionInfoService baseRegionInfoService;

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

    private static IDangerAreaService dangerAreaService;

    private static IDangerAreaService getDangerAreaService() {
        if (dangerAreaService == null) {
            dangerAreaService = SpringUtil.getBean(IDangerAreaService.class);
        }
        return dangerAreaService;
    }

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

    public DangerAreaCheckInfoVO entityVO(DangerAreaCheckInfoVO vo) {

        if (Func.isNull(vo.getAdName())) {
            LambdaQueryWrapper<BaseRegionInfoEntity> wrapper = Wrappers.<BaseRegionInfoEntity>query().lambda();
            wrapper.eq(BaseRegionInfoEntity::getAdcd, vo.getAdcd());
            wrapper.last("limit 1");
            BaseRegionInfoEntity entity = getBaseRegionInfoService().getOne(wrapper);
            vo.setAdName(entity.getAdnm());
        }

        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.getAdName())) {

                    fullCode.append(vo.getAdName());
                }
                vo.setFullAadName(fullCode.toString());
            }

        }

        if (Func.notNull(vo.getDangerAreaPid())) {
            LambdaQueryWrapper<DangerAreaEntity> wrapper = Wrappers.<DangerAreaEntity>query().lambda();
            wrapper.eq(DangerAreaEntity::getDangerAreaPid, vo.getDangerAreaPid());
            wrapper.last("limit 1");
            DangerAreaEntity entity = getDangerAreaService().getOne(wrapper);
            if (Func.notNull(entity)) {
                vo.setDangerAreaName(entity.getDangerAreaName());
            }
        } else {
            vo.setDangerAreaName("");
        }
        return vo;
    }
}
