/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.rtu.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.project.entity.ProjectInfoEntity;
import org.springblade.modules.baseinfo.project.service.IBaseProjectService;
import org.springblade.modules.baseinfo.region.entity.RegionInfoEntity;
import org.springblade.modules.baseinfo.region.service.IBaseRegionService;
import org.springblade.modules.baseinfo.rtu.entity.RtuInfoEntity;
import org.springblade.modules.baseinfo.rtu.vo.RtuInfoVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;

import java.util.*;

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

    private static IDictBizService dictBizService;

    private static IDictBizService getDictBizService() {
        if (dictBizService == null) {
            dictBizService = SpringUtil.getBean(IDictBizService.class);
        }
        return dictBizService;
    }

    private static IBaseRegionService regionService;

    private static IBaseRegionService getRegionService() {
        if (regionService == null) {
            regionService = SpringUtil.getBean(IBaseRegionService.class);
        }
        return regionService;
    }

    private static IDeptService deptService;

    private static IDeptService getDeptService() {
        if (deptService == null) {
            deptService = SpringUtil.getBean(IDeptService.class);
        }
        return deptService;
    }

    private static IBaseProjectService projectService;

    private static IBaseProjectService getProjectService() {
        if (projectService == null) {
            projectService = SpringUtil.getBean(IBaseProjectService.class);
        }
        return projectService;
    }

    @Override
    public RtuInfoVO entityVO(RtuInfoEntity entity) {
        RtuInfoVO rtuInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, RtuInfoVO.class));
        return this.entityVO(rtuInfoVO);
    }

    public RtuInfoVO entityVO(RtuInfoVO entity) {
        if (entity.getRtuKinds() != null) {
            StringBuilder rtuKindDesc = new StringBuilder();
            String[] kinds = entity.getRtuKinds().split(",");
            for (String kind : kinds) {
                DictBiz dictBiz = getDictBizService().getById(Func.toLong(kind));
                if (rtuKindDesc.length() > 0) {
                    rtuKindDesc.append("|");
                }
                rtuKindDesc.append(dictBiz.getDictValue());
            }
            entity.setRtuKindDesc(rtuKindDesc.toString());
        }

        if (entity.getAdName() == null && entity.getAdCode() != null) {
            RegionInfoEntity regionInfoEntity = getRegionService().getByCode(entity.getAdCode());
            if (null != regionInfoEntity) {
                entity.setAdName(regionInfoEntity.getAdnm());
            }
        }

        if (entity.getManageCompany() == null && entity.getOrgId() != null) {
            Dept dept = getDeptService().getById(entity.getOrgId());
            if (dept != null) {
                entity.setManageCompany(dept.getDeptName());
            }
        }

        if (entity.getProjectName() == null && entity.getProjectId() != null) {
            ProjectInfoEntity projectInfoEntity = getProjectService().getProjectInfoById(entity.getProjectId());
            if (projectInfoEntity != null) {
                entity.setProjectName(projectInfoEntity.getProjectName());
            }
        }

        if (entity.getAdCode() != null) {
            RegionInfoEntity regionInfoEntity = getRegionService().getByCode(entity.getAdCode());
            if (regionInfoEntity != null) {
                StringBuilder areaName = new StringBuilder();
                String[] ads = regionInfoEntity.getAncestors().split(",");
                if (ads.length > 1) {
                    for (int i = 1; i < ads.length; i++) {
                        String adCode = ads[i];
                        RegionInfoEntity region = getRegionService().getByCode(adCode);
                        if (areaName.length() > 0) {
                            areaName.append("/");
                        }
                        areaName.append(region.getAdnm());
                    }
                }
                if (areaName.length() > 0) {
                    areaName.append("/");
                }
                areaName.append(regionInfoEntity.getAdnm());
                entity.setAreaName(areaName.toString());
            }
        }
        return entity;
    }
}
