package com.bonus.core.customer.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.constant.DelFlagEnum;
import com.bonus.constant.LeConstants;
import com.bonus.core.autth.api.MgrAuthApi;
import com.bonus.core.common.constant.LeMqConstant;
import com.bonus.core.common.redis.RedisUtil;
import com.bonus.core.customer.api.CustInfoApi;
import com.bonus.core.customer.business.CustOrgPointBusiness;
import com.bonus.core.customer.constants.CustCacheKey;
import com.bonus.core.customer.constants.CustConstant;
import com.bonus.core.customer.dto.CustAccExcelModel;
import com.bonus.core.customer.dto.CustOrgPageDTO;
import com.bonus.core.customer.dto.CustOrgTreeDTO;
import com.bonus.core.customer.mapper.CustOrgMapper;
import com.bonus.core.customer.model.CustInfo;
import com.bonus.core.customer.model.CustOrg;
import com.bonus.core.customer.po.QueryOrgsBasicInfoPO;
import com.bonus.core.customer.po.UpdateChildOrgPO;
import com.bonus.core.customer.service.CustInfoService;
import com.bonus.core.customer.service.CustOrgCheckService;
import com.bonus.core.customer.service.CustOrgService;
import com.bonus.core.customer.utils.CommonConstants;
import com.bonus.core.customer.utils.CustomerCacheUtil;
import com.bonus.core.customer.vo.CustOrgVO;
import com.bonus.core.customer.vo.OrgBasicInfoVO;
import com.bonus.core.order.mq.MqUtil;
import com.bonus.utils.id.Id;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CustOrgServiceImpl extends ServiceImpl<CustOrgMapper, CustOrg> implements CustOrgService {
   private static final Logger log = LoggerFactory.getLogger(CustOrgServiceImpl.class);
   @Autowired
   @Lazy
   private CustInfoService custInfoService;
   @Autowired
   @Lazy
   private CustInfoApi custInfoApi;
   @Autowired
   @Lazy
   private MgrAuthApi mgrAuthApi;
   @Autowired
   @Lazy
   CustOrgCheckService custOrgCheckService;
   @Autowired
   @Lazy
   CustOrgMapper custOrgMapper;
   @Autowired
   @Lazy
   CustOrgPointBusiness custOrgPointBusiness;

   public Page<CustOrgVO> pageCustOrgByParams(Page<CustOrg> page, CustOrgPageDTO content) {
      this.setOrgFullId(content);
      Page<CustOrgVO> recordPage = this.custOrgMapper.pageCustOrgByParams(page, content);
      this.setOrgInfo(recordPage.getRecords());
      this.custOrgPointBusiness.didPageCustOrg(content, recordPage);
      return recordPage;
   }

   public List<Long> queryChildCustOrgId(CustOrg superOrg) {
      List<Long> orgIdList = Lists.newArrayList();
      orgIdList.add(superOrg.getOrgId());
      List<CustOrg> allOrgList = this.list((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getIfDel, LeConstants.COMMON_NO));
      if (CollectionUtils.isEmpty(allOrgList)) {
         return orgIdList;
      } else {
         List<CustOrg> childrenOrgList = this.queryChildren(superOrg.getOrgId(), Lists.newArrayList(), allOrgList);
         Iterator var5 = childrenOrgList.iterator();

         while(var5.hasNext()) {
            CustOrg temp = (CustOrg)var5.next();
            orgIdList.add(temp.getOrgId());
         }

         return orgIdList;
      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void addCustOrg(CustOrg custOrg) {
      this.custOrgCheckService.addCheck(custOrg);
      CustOrg superOrg = this.getCustOrgByOrgId(custOrg.getSuperId());
      custOrg.setOrgId(Id.next());
      setCustOrgInfo(custOrg, superOrg);
      this.insert(custOrg);
      this.custOrgPointBusiness.didAddCustOrg(custOrg);
   }

   public void batchAddCustOrgAndSerOrgIdNotCheck(List<CustAccExcelModel> custAccExcelModelList) {
      if (!CollUtil.isEmpty(custAccExcelModelList)) {
         Map<String, CustOrg> superIdAndName_org = (Map)this.listAllCustOrg().stream().collect(Collectors.toMap((e) -> {
            Long var10000 = e.getSuperId();
            return "" + var10000 + e.getOrgName();
         }, Function.identity(), (e1, e2) -> {
            return e1;
         }));
         List<CustOrg> addCustOrgList = new ArrayList();
         CustOrg topOrg = this.getTopOrg();
         Iterator var5 = custAccExcelModelList.iterator();

         while(var5.hasNext()) {
            CustAccExcelModel excelModel = (CustAccExcelModel)var5.next();
            CustOrg superOrg = topOrg;

            for(int i = 2; i <= 6; ++i) {
               String orgNameByLevel = excelModel.getOrgNameByLevel(i);
               if (CharSequenceUtil.isBlank(orgNameByLevel)) {
                  break;
               }

               Long var10001 = superOrg.getOrgId();
               CustOrg existOrg = (CustOrg)superIdAndName_org.get("" + var10001 + orgNameByLevel);
               if (ObjectUtil.isNotNull(existOrg)) {
                  superOrg = existOrg;
                  excelModel.setOrgId(existOrg.getOrgId());
               } else {
                  CustOrg custOrg = (new CustOrg()).setOrgId(Id.next()).setOrgName(orgNameByLevel).setSuperId(superOrg.getOrgId());
                  setCustOrgInfo(custOrg, superOrg);
                  addCustOrgList.add(custOrg);
                  superIdAndName_org.put(custOrg.getSuperId() + custOrg.getOrgName(), custOrg);
                  superOrg = custOrg;
                  excelModel.setOrgId(custOrg.getOrgId());
               }
            }
         }

         log.info("新增组织数量={}", CollUtil.size(addCustOrgList));
         this.insertBatch(addCustOrgList);
      }
   }

   public boolean isTopOrg(Long orgId) {
      CustOrg topOrg = this.getTopOrg();
      return ObjectUtil.equal(topOrg.getOrgId(), orgId);
   }

   public List<CustOrgVO> getOrgLazyTree(CustOrgTreeDTO content) {
      if (ObjectUtil.isNull(content)) {
         throw new ServiceException("入参不能为空");
      } else {
         Long parentId = (Long)Optional.ofNullable(content.getSuperId()).orElse(CommonConstants.MENU_TREE_ROOT_ID);
         return this.custOrgMapper.getCustOrgLazyTree(parentId);
      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void dragOrgSort(CustOrgTreeDTO content) {
      String dragSortKey = CustCacheKey.getTenantKey("org:drag:sort");
      if (!RedisUtil.setNx(dragSortKey, 0, 300)) {
         throw new ServiceException("正在执行排序,请稍后再试!");
      } else {
         try {
            Long orgId = content.getOrgId();
            Integer newSort = content.getSort();
            CustOrg oldOrg = (CustOrg)this.getOne((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getOrgId, orgId));
            if (ObjectUtil.isNull(oldOrg)) {
               throw new ServiceException("组织不存在");
            }

            Integer oldSort = oldOrg.getSort();
            List<CustOrg> siblings = this.list((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getSuperId, content.getSuperId()));
            Iterator var8 = siblings.iterator();

            while(var8.hasNext()) {
               CustOrg sibling = (CustOrg)var8.next();
               if (sibling.getOrgId().equals(orgId)) {
                  this.updateSortByOrgId(orgId, newSort);
               } else if (oldSort < newSort && sibling.getSort() > oldSort && sibling.getSort() <= newSort) {
                  this.updateSortByOrgId(sibling.getOrgId(), sibling.getSort() - 1);
               } else if (oldSort > newSort && sibling.getSort() >= newSort && sibling.getSort() < oldSort) {
                  this.updateSortByOrgId(sibling.getOrgId(), sibling.getSort() + 1);
               }
            }
         } finally {
            RedisUtil.delete(dragSortKey);
         }

      }
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void updateOrg(CustOrg custOrg) {
      CustOrg oldOrg = this.getCustOrgByOrgId(custOrg.getOrgId());
      if (ObjectUtil.isNull(custOrg.getOrgFullId())) {
         custOrg.setOrgFullId(oldOrg.getOrgFullId());
      }

      this.custOrgCheckService.updateCheck(custOrg);
      CustOrg superOrg = this.getCustOrgByOrgId(custOrg.getSuperId());
      setCustOrgInfo(custOrg, superOrg);
      this.updateByOrgId(custOrg);
      List<Long> childOrgIdList = this.custOrgMapper.listDescendantOrgIdByOrgFullId(oldOrg.getOrgFullId());
      if (CollUtil.isNotEmpty(childOrgIdList)) {
         UpdateChildOrgPO updateChildOrgPO = (new UpdateChildOrgPO()).setOldOrgFullName(oldOrg.getOrgFullName()).setOrgFullName(custOrg.getOrgFullName()).setOldOrgFullId(oldOrg.getOrgFullId()).setOrgFullId(custOrg.getOrgFullId()).setDiffLevel(calculateLeaveDiff(custOrg.getOrgLevel(), oldOrg.getOrgLevel())).setEndDate(custOrg.getEndDate()).setChildOrgIdList(childOrgIdList);
         this.custOrgMapper.updateChildOrg(updateChildOrgPO);
      }

      ArrayList<Long> orgIdList = CollUtil.newArrayList(childOrgIdList);
      orgIdList.add(custOrg.getOrgId());
      this.changeCustInfoRedundantFields(orgIdList);
      this.custOrgPointBusiness.didUpdateCustOrg(custOrg);
   }

   @Transactional(
      rollbackFor = {Exception.class}
   )
   public void deleteByOrdId(Long orgId) {
      List<Long> orgIdList = new ArrayList();
      orgIdList.add(orgId);
      List<Long> childOrgIdList = this.custOrgMapper.listChildOrgIdByOrgId(orgId);
      orgIdList.addAll(childOrgIdList);
      this.custOrgCheckService.deleteCheck(orgIdList);
      this.deleteByOrgId(orgIdList);
      this.custOrgPointBusiness.didDeleteCustOrg(orgId, orgIdList);
   }

   public List<CustOrgVO> queryCustOrgList(List<Long> orgIdList) {
      return this.custOrgMapper.queryCustOrgVOList(orgIdList);
   }

   public JsonNode queryDiningPlaceByOrgId(Long orgId) {
      //CustOrg custOrg = (CustOrg)this.custOrgMapper.selectOne((Wrapper)Wrappers.lambdaQuery(CustOrg.class).select(new SFunction[]{CustOrg::getDiningPlace}).eq(CustOrg::getOrgId, orgId));
      CustOrg custOrg = this.custOrgMapper.selectOne(Wrappers.lambdaQuery(CustOrg.class)
              .select(CustOrg::getDiningPlace)
              .eq(CustOrg::getOrgId, orgId));
      return ObjectUtil.isNull(custOrg) ? null : custOrg.getDiningPlace();
   }

   public CustOrg initTopCustOrg(String merchantName) {
      CustOrg custOrg = new CustOrg();
      custOrg.setOrgId(Id.next());
      String merchantDefault = merchantName + "(默认)";
      custOrg.setOrgName(merchantDefault);
      custOrg.setOrgNum("0");
      custOrg.setOrgFullName(merchantDefault);
      custOrg.setOrgFullId("/" + custOrg.getOrgId() + "/");
      custOrg.setOrgLevel(1);
      custOrg.setSuperId(CustConstant.DATA_DEFAULT_LONG);
      this.insert(custOrg);
      return custOrg;
   }

   public CustOrg initTempCustOrg(CustOrg superOrg) {
      CustOrg custOrg = new CustOrg();
      custOrg.setOrgId(CustConstant.TEMPORARY_ORG_ID);
      custOrg.setOrgFullId(assembleOrgFullId(custOrg.getOrgId(), superOrg.getOrgFullId()));
      custOrg.setOrgName("临时部门");
      custOrg.setOrgLevel(superOrg.getOrgLevel() + 1);
      custOrg.setOrgFullName(assembleOrgFullName("临时部门", superOrg.getOrgFullName()));
      custOrg.setOrgNum("letemporaryorg");
      custOrg.setSuperId(superOrg.getOrgId());
      custOrg.setEndDate(calculateEndDate((LocalDate)null, superOrg.getEndDate()));
      this.insert(custOrg);
      return custOrg;
   }

   public List<Tree<Long>> getSystemOrgTree() {
      return this.getOrgTree(this.custOrgMapper.listAllOrg());
   }

   protected List<Tree<Long>> getOrgTree(List<CustOrgTreeDTO> systemOrgTree) {
      TreeNodeConfig treeNodeConfig = getTreeNodeConfig();
      return TreeUtil.build(systemOrgTree, CustConstant.DATA_DEFAULT_LONG, treeNodeConfig, (org, treeNode) -> {
         treeNode.setId(org.getOrgId());
         treeNode.setParentId(org.getSuperId());
         treeNode.setName(org.getOrgName());
         treeNode.setWeight(org.getSort());
         treeNode.putExtra("orgFullName", org.getOrgFullName());
         treeNode.putExtra("orgNum", org.getOrgNum());
         treeNode.putExtra("orgLevel", org.getOrgLevel());
         treeNode.putExtra("halfSelect", ObjectUtil.isNull(org.getHalfSelect()) ? 1 : org.getHalfSelect());
      });
   }

   protected static TreeNodeConfig getTreeNodeConfig() {
      TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
      treeNodeConfig.setParentIdKey("superId");
      treeNodeConfig.setNameKey("text");
      treeNodeConfig.setWeightKey("sort");
      return treeNodeConfig;
   }

   public List<Tree<Long>> getTenantOrgTree() {
      List<CustOrgTreeDTO> tenantOrg = this.mgrAuthApi.getTenantOrg(LeConstants.COMMON_NO);
      return this.getOrgTree(tenantOrg);
   }

   protected void setOrgFullId(CustOrgPageDTO content) {
      if (!ObjectUtil.isEmpty(content.getOrgId())) {
         String orgFullId = this.getOrgFullIdByOrgId(content.getOrgId());
         content.setOrgFullId(orgFullId);
      }
   }

   protected String getOrgFullIdByOrgId(Long orgId) {
      //CustOrg custOrg = (CustOrg)this.custOrgMapper.selectOne((Wrapper)Wrappers.lambdaQuery(CustOrg.class).select(new SFunction[]{CustOrg::getOrgFullId}).eq(CustOrg::getOrgId, orgId));
      CustOrg custOrg = this.custOrgMapper.selectOne(Wrappers.lambdaQuery(CustOrg.class)
              .select(CustOrg::getOrgFullId)
              .eq(CustOrg::getOrgId, orgId));
      return null == custOrg ? null : custOrg.getOrgFullId();
   }

   protected void setOrgInfo(List<CustOrgVO> custOrgVOList) {
      Set<Long> custIdSet = (Set)custOrgVOList.stream().map(CustOrgVO::getOrgHeadId).filter(Objects::nonNull).collect(Collectors.toSet());
      Map<Long, CustInfo> custId_entity = new HashMap();
      if (CollUtil.isNotEmpty(custIdSet)) {
//         custId_entity = (Map)this.custInfoApi.selectCustBasicsInfoListByCustIds(custIdSet).stream().collect(Collectors.toMap(CustInfo::getCustId, Function.identity()));
      }

      Iterator var4 = custOrgVOList.iterator();

      while(var4.hasNext()) {
         CustOrgVO custOrgVO = (CustOrgVO)var4.next();
         custOrgVO.setExpires(!ObjectUtil.isNull(custOrgVO.getEndDate()) && LocalDate.now().isAfter(custOrgVO.getEndDate()));
         Map<Long, Integer> orgId_customerNum = this.getCustomerNumInOrg();
         if (custOrgVO.getOrgId() == CustConstant.TEMPORARY_ORG_ID) {
            custOrgVO.setCustomerNum((Integer)orgId_customerNum.getOrDefault(CustConstant.TEMPORARY_ORG_ID.intValue(), 0));
         } else {
            custOrgVO.setCustomerNum((Integer)orgId_customerNum.getOrDefault(custOrgVO.getOrgId(), 0));
         }

         CustInfo custInfo = (CustInfo)((Map)custId_entity).get(custOrgVO.getOrgHeadId());
         if (ObjectUtil.isNotEmpty(custInfo)) {
            custOrgVO.setCustNum(custInfo.getCustNum());
            custOrgVO.setCustName(custInfo.getCustName());
         }
      }

   }

   protected List<CustOrg> queryChildren(Long orgId, List<CustOrg> resultList, List<CustOrg> allOrgList) {
      Iterator var4 = allOrgList.iterator();

      while(var4.hasNext()) {
         CustOrg node = (CustOrg)var4.next();
         if (orgId.equals(node.getSuperId())) {
            resultList.add(node);
            this.queryChildren(node.getOrgId(), resultList, allOrgList);
         }
      }

      return resultList;
   }

   public Map<Long, Integer> getCustomerNumInOrg() {
      Map<Long, Integer> cacheOrgId_customerNum = (Map)Optional.ofNullable(CustomerCacheUtil.getCustomerNumBelongOrgMapCache()).orElse(new HashMap());
      if (CollUtil.isNotEmpty(cacheOrgId_customerNum)) {
         return cacheOrgId_customerNum;
      } else {
         LocalDateTime now1 = LocalDateTime.now();
         Map<Long, Integer> orgId_partCustomerNum = this.custInfoService.getCustomerNumBelongOrg();
         List<Tree<Long>> tenantOrgTree = this.getSystemOrgTree();
         if (CollUtil.isEmpty(tenantOrgTree)) {
            return MapUtil.empty();
         } else {
            getOrgCountRecursion(cacheOrgId_customerNum, tenantOrgTree, orgId_partCustomerNum);
            log.info("cacheOrgId_customerNum【{}】", cacheOrgId_customerNum);
            CustomerCacheUtil.insertCustomerNumBelongOrgMapCache(cacheOrgId_customerNum);
            LocalDateTime now2 = LocalDateTime.now();
            log.info("用时[{}]毫秒", now1.until(now2, ChronoUnit.MILLIS));
            return cacheOrgId_customerNum;
         }
      }
   }

   public static int getOrgCountRecursion(Map<Long, Integer> orgId_customerNum, List<Tree<Long>> orgTree, Map<Long, Integer> orgId_partCustomerNum) {
      int totalCustomerNum = 0;
      if (CollUtil.isEmpty(orgTree)) {
         return totalCustomerNum;
      } else {
         int customerNum;
         for(Iterator var4 = orgTree.iterator(); var4.hasNext(); totalCustomerNum += customerNum) {
            Tree<Long> tree = (Tree)var4.next();
            if (tree.hasChild()) {
               customerNum = (Integer)orgId_partCustomerNum.getOrDefault(tree.getId(), 0) + getOrgCountRecursion(orgId_customerNum, tree.getChildren(), orgId_partCustomerNum);
            } else {
               customerNum = (Integer)orgId_partCustomerNum.getOrDefault(tree.getId(), 0);
            }

            orgId_customerNum.put((Long)tree.getId(), customerNum);
         }

         return totalCustomerNum;
      }
   }

   protected static void setCustOrgInfo(CustOrg custOrg, CustOrg superOrg) {
      custOrg.setOrgFullName(assembleOrgFullName(custOrg.getOrgName(), superOrg.getOrgFullName()));
      custOrg.setOrgFullId(assembleOrgFullId(custOrg.getOrgId(), superOrg.getOrgFullId()));
      custOrg.setEndDate(calculateEndDate(custOrg.getEndDate(), superOrg.getEndDate()));
      int orgLevel = custOrg.getOrgFullName().split("/").length;
      custOrg.setOrgLevel(orgLevel);
   }

   protected static LocalDate calculateEndDate(LocalDate endDate, LocalDate superEndDate) {
      if (endDate == null) {
         return superEndDate;
      } else if (superEndDate == null) {
         return null;
      } else {
         return endDate.isAfter(superEndDate) ? superEndDate : endDate;
      }
   }

   protected static String assembleOrgFullName(String orgName, String superOrgFullName) {
      return superOrgFullName + "/" + orgName;
   }

   protected static String assembleOrgFullId(Long orgId, String superOrgFullId) {
      return superOrgFullId + orgId + "/";
   }

   protected static int calculateLeaveDiff(Integer newLeave, Integer oldLeave) {
      return newLeave - oldLeave;
   }

   protected void changeCustInfoRedundantFields(Collection<Long> orgIdList) {
      this.custInfoService.updateCustomersOrgNumAndOrgFullName(orgIdList);
   }

   protected void insert(CustOrg custOrg) {
      if (!ObjectUtil.isNull(custOrg)) {
         this.custOrgMapper.insert(custOrg);
         MqUtil.sendDataChange(custOrg, LeMqConstant.DataChangeType.ADD, LeMqConstant.Topic.DATA_CHANGE_ORG);
      }
   }

   protected void insertBatch(List<CustOrg> custOrgList) {
      if (!ObjectUtil.isNull(custOrgList)) {
         this.saveBatch(custOrgList);
      }
   }

   protected void deleteByOrgId(Collection<Long> orgIdList) {
      if (!CollUtil.isEmpty(orgIdList)) {
        // this.custOrgMapper.update((Object)null, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustOrg.class).in(CustOrg::getOrgId, orgIdList)).set(CustOrg::getIfDel, LeConstants.COMMON_YES));
         this.custOrgMapper.update(null, Wrappers.lambdaUpdate(CustOrg.class).in(CustOrg::getOrgId, orgIdList).set(CustOrg::getIfDel, LeConstants.COMMON_YES));
         Iterator var2 = orgIdList.iterator();

         while(var2.hasNext()) {
            Long orgId = (Long)var2.next();
            CustOrg custOrg = new CustOrg();
            custOrg.setOrgId(orgId);
            MqUtil.sendDataChange(custOrg, LeMqConstant.DataChangeType.REMOVE, LeMqConstant.Topic.DATA_CHANGE_ORG);
         }

      }
   }

   protected void updateByOrgId(CustOrg custOrg) {
      if (!ObjectUtil.isNull(custOrg)) {
         //this.update(custOrg, (Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustOrg.class).set(CustOrg::getOrgHeadId, custOrg.getOrgHeadId())).eq(CustOrg::getOrgId, custOrg.getOrgId()));
         this.update(custOrg, Wrappers.lambdaUpdate(CustOrg.class)
                 .set(CustOrg::getOrgHeadId, custOrg.getOrgHeadId())
                 .eq(CustOrg::getOrgId, custOrg.getOrgId()));
         CustomerCacheUtil.deleteCustomerNumBelongOrgMapCache();
         CustomerCacheUtil.deleteCustomerNumBelongOrgMapCache();
         MqUtil.sendDataChange(custOrg, LeMqConstant.DataChangeType.UPDATE, LeMqConstant.Topic.DATA_CHANGE_ORG);
      }
   }

   public CustOrg getCustOrg(Long id) {
      return (CustOrg)this.custOrgMapper.selectById(id);
   }

   protected List<CustOrg> listAllCustOrg() {
      return this.custOrgMapper.selectList((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getIfDel, DelFlagEnum.DEL_FALSE.key()));
   }

   public CustOrg getCustOrgByOrgId(@NotNull Long orgId) {
      return (CustOrg)this.custOrgMapper.selectOne((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getOrgId, orgId));
   }

   public CustOrg getTopOrg() {
      //return (CustOrg)this.custOrgMapper.selectOne((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getIfDel, DelFlagEnum.DEL_FALSE.key())).eq(CustOrg::getOrgLevel, 1));
      return (CustOrg)this.custOrgMapper.selectOne((LambdaQueryWrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getIfDel, DelFlagEnum.DEL_FALSE.key()).eq(CustOrg::getOrgLevel, 1));
   }

   public List<OrgBasicInfoVO> listOrgBasicInfoByParams(QueryOrgsBasicInfoPO queryOrgsBasicInfoPO) {
      return this.custOrgMapper.listOrgBasicInfoByParams(queryOrgsBasicInfoPO);
   }

   public List<CustOrg> listOrgByLevel(Integer orgLeave) {
     // return this.custOrgMapper.selectList((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getOrgLevel, orgLeave)).eq(CustOrg::getIfDel, DelFlagEnum.DEL_FALSE.key()));
      return this.custOrgMapper.selectList(Wrappers.lambdaQuery(CustOrg.class)
              .eq(CustOrg::getOrgLevel, orgLeave)
              .eq(CustOrg::getIfDel, DelFlagEnum.DEL_FALSE.key()));
   }

   public CustOrg getTempCustOrg() {
      return (CustOrg)this.custOrgMapper.selectOne((Wrapper)Wrappers.lambdaQuery(CustOrg.class).eq(CustOrg::getOrgNum, "letemporaryorg"));
   }

   public void updateSortByOrgId(Long orgId, Integer newSort) {
     // this.update((Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustOrg.class).set(CustOrg::getSort, newSort)).eq(CustOrg::getOrgId, orgId));
      this.update(Wrappers.lambdaUpdate(CustOrg.class).set(CustOrg::getSort, newSort).eq(CustOrg::getOrgId, orgId));
   }

   // $FF: synthetic method
//   private static Object $deserializeLambda$(SerializedLambda lambda) {
//      switch (lambda.getImplMethodName()) {
//         case "getOrgFullId":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//               return CustOrg::getOrgFullId;
//            }
//            break;
//         case "getDiningPlace":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Lcom/fasterxml/jackson/databind/JsonNode;")) {
//               return CustOrg::getDiningPlace;
//            }
//            break;
//         case "getSort":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getSort;
//            }
//            break;
//         case "getOrgHeadId":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgHeadId;
//            }
//            break;
//         case "getOrgLevel":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getOrgLevel;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getOrgLevel;
//            }
//            break;
//         case "getOrgId":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getOrgId;
//            }
//            break;
//         case "getSuperId":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustOrg::getSuperId;
//            }
//            break;
//         case "getIfDel":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getIfDel;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getIfDel;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getIfDel;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getIfDel;
//            }
//
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustOrg::getIfDel;
//            }
//            break;
//         case "getOrgNum":
//            if (lambda.getImplMethodKind() == 5 && lambda.getFunctionalInterfaceClass().equals("com/baomidou/mybatisplus/core/toolkit/support/SFunction") && lambda.getFunctionalInterfaceMethodName().equals("apply") && lambda.getFunctionalInterfaceMethodSignature().equals("(Ljava/lang/Object;)Ljava/lang/Object;") && lambda.getImplClass().equals("net/xnzn/core/customer/model/CustOrg") && lambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//               return CustOrg::getOrgNum;
//            }
//      }
//
//      throw new IllegalArgumentException("Invalid lambda deserialization");
//   }
}
