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

import java.util.Objects;

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

    public static OrgInfoWrapper build() {
        return new OrgInfoWrapper();
    }

    private static IBaseRegionService baseInfoRegionService;

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

    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 OrgInfoVO entityVO(OrgEntity entity) {
        OrgInfoVO orgInfoVO = Objects.requireNonNull(BeanUtil.copy(entity, OrgInfoVO.class));
        return orgInfoVO;
    }

    public OrgInfoVO entityVO(OrgInfoVO vo) {
        vo.setDictValue(vo.getDeptName());

        Dept dept = getDeptService().getById(vo.getParentId());
        if (Func.notNull(dept)) {
            vo.setParentName(dept.getDeptName());
        } else {
            vo.setParentName("");
        }

//        LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
//        deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
//        deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, vo.getId());
//        DeptRegionEntity deptRegionEntity = getDeptRegionService().getOne(deptRegionEntityLambdaQueryWrapper);
//        if (deptRegionEntity != null) {
//            LambdaQueryWrapper<RegionInfoEntity> regionInfoEntityLambdaQueryWrapper = Wrappers.<RegionInfoEntity>query().lambda();
//            regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getIsDeleted, 0);
//            regionInfoEntityLambdaQueryWrapper.eq(RegionInfoEntity::getAdcd, deptRegionEntity.getAdcd());
//            RegionInfoEntity regionInfoEntity = getBaseInfoRegionService().getOne(regionInfoEntityLambdaQueryWrapper);
//            if (null != regionInfoEntity) {
//                vo.setAdcd(regionInfoEntity.getAdcd());
//                vo.setAdnm(regionInfoEntity.getAdnm());
//            }
//        }
        return vo;
    }
}
