/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/24
 */
package org.springblade.modules.business.warning.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.SpringUtil;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.business.warning.entity.RtuWarningInfoEntity;
import org.springblade.modules.business.warning.vo.RtuWarningInfoVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;

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

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

	private static IDeptRegionService deptRegionService;

	private static IDeptRegionService getDeptRegionService() {
		if (deptRegionService == null) {
			deptRegionService = SpringUtil.getBean(IDeptRegionService.class);
		}
		return deptRegionService;
	}

	private static IDeptService deptService;

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

	@Override
	public RtuWarningInfoVO entityVO(RtuWarningInfoEntity entity) {
		RtuWarningInfoVO rtuWarningInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, RtuWarningInfoVO.class));
		return this.entityVO(rtuWarningInfoVO);
	}

	public RtuWarningInfoVO entityVO(RtuWarningInfoVO vo) {
		String ancestors = vo.getAncestors();
		if (null != ancestors && ancestors.length() > 0) {
			String[] ancestorsList = ancestors.split(",");
			String adCode;
			if (ancestorsList.length >= 3) {
				adCode = ancestorsList[2];
			} else {
				adCode = vo.getAdCode();
			}
			if (null != adCode) {
				LambdaQueryWrapper<DeptRegionEntity> wrapper = Wrappers.<DeptRegionEntity>query().lambda();
				wrapper.eq(DeptRegionEntity::getIsDeleted, 0);
				wrapper.eq(DeptRegionEntity::getAdcd, adCode);
				List<DeptRegionEntity> deptRegionEntities = getDeptRegionService().list(wrapper);
				if (null != deptRegionEntities && deptRegionEntities.size() > 0) {
					String manageCompany = "";
					for (DeptRegionEntity deptRegionEntity : deptRegionEntities) {
						Dept dept = getDeptService().getById(deptRegionEntity.getDeptId());
						if(null != dept) {
							if (manageCompany.length() > 0) {
								manageCompany += " / ";
							}
							manageCompany += dept.getDeptName();
						}
					}
					vo.setManageCompany(manageCompany);
				}
			}
		}
		return vo;
	}
}
