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

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.bonus.common.core.exception.ServiceException;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.customer.constants.PersonalStatusEnum;
import com.bonus.canteen.core.customer.mapper.CustOrgMapper;
import com.bonus.canteen.core.customer.model.CustOrg;
import com.bonus.common.houqin.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.lang.invoke.SerializedLambda;
import java.util.Collection;
import java.util.Collections;

@Component
public class CustOrgCheckService {
   @Autowired
   @Lazy
   private CustOrgService custOrgService;
   @Autowired
   @Lazy
   private CustOrgMapper custOrgMapper;
   @Autowired
   @Lazy
   private CustInfoService custInfoService;

   public void addCheck(CustOrg custOrg) {
      this.checkOrgChildNameExist((Long)null, custOrg.getSuperId(), custOrg.getOrgName());
      this.checkOrgNumExist((Long)null, custOrg.getOrgNum());
   }

   public void updateCheck(CustOrg custOrg) {
      this.checkOrgChildNameExist(custOrg.getOrgId(), custOrg.getSuperId(), custOrg.getOrgName());
      this.checkOrgNumExist(custOrg.getOrgId(), custOrg.getOrgNum());
      this.checkSuperOrgNotUnderDescendantNode(custOrg.getSuperId(), custOrg.getOrgFullId());
   }

   public void deleteCheck(Collection<Long> orgIds) {
      this.checkTopOrgCannotDelete(orgIds);
      this.checkExistCustomer(orgIds);
   }

   protected void checkTopOrgCannotDelete(Collection<Long> orgIds) {
      CustOrg topOrg = this.custOrgService.getTopOrg();
      if (orgIds.contains(topOrg.getOrgId())) {
         throw new ServiceException(I18n.getMessage("customer.org.top.cannotDelete", new Object[0]));
      }
   }

   protected void checkExistCustomer(Collection<Long> orgIds) {
      Long count = this.custInfoService.customerCountByParams(Collections.singleton(PersonalStatusEnum.NORMAL.getKey()), orgIds);
      if (count > 0L) {
         throw new ServiceException(I18n.getMessage("customer.org.haveCustomer", new Object[0]));
      }
   }

   protected void checkOrgNumExist(Long orgId, String orgNum) {
      if (!CharSequenceUtil.isBlank(orgNum)) {
       //  boolean exists = this.custOrgMapper.exists((Wrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery().ne(ObjectUtil.isNotNull(orgId), CustOrg::getOrgId, orgId)).eq(CustOrg::getOrgNum, orgNum)).eq(CustOrg::getIfDel, LeConstants.COMMON_NO));
         boolean exists = this.custOrgMapper.exists(Wrappers.lambdaQuery(CustOrg.class)
                 .ne(ObjectUtil.isNotNull(orgId), CustOrg::getOrgId, orgId)
                 .eq(CustOrg::getOrgNum, orgNum)
                 .eq(CustOrg::getIfDel, LeConstants.COMMON_NO));
         if (exists) {
            String var10002 = I18n.getMessage("customer.org.num.exists", new Object[0]);
            throw new ServiceException(var10002 + "[" + orgNum + "]");
         }
      }
   }

   protected void checkOrgChildNameExist(Long orgId, Long superId, String orgName) {
     // boolean exists = this.custOrgMapper.exists((Wrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery().ne(ObjectUtil.isNotNull(orgId), CustOrg::getOrgId, orgId)).eq(CustOrg::getSuperId, superId)).eq(CustOrg::getOrgName, orgName)).eq(CustOrg::getIfDel, LeConstants.COMMON_NO));
      boolean exists = this.custOrgMapper.exists(Wrappers.lambdaQuery(CustOrg.class)
              .ne(ObjectUtil.isNotNull(orgId), CustOrg::getOrgId, orgId)
              .eq(CustOrg::getSuperId, superId)
              .eq(CustOrg::getOrgName, orgName)
              .eq(CustOrg::getIfDel, LeConstants.COMMON_NO));
      if (exists) {
         String var10002 = I18n.getMessage("customer.org.sameLevel.name.exists", new Object[0]);
         throw new ServiceException(var10002 + "[" + orgName + "]");
      }
   }

   protected void checkSuperOrgNotUnderDescendantNode(Long superId, String orgFullId) {
      if (!ObjectUtil.isNull(superId) && !CharSequenceUtil.isBlank(orgFullId)) {
         if (this.custOrgMapper.superInDescendantNode(orgFullId, superId)) {
            throw new ServiceException(I18n.getMessage("customer.org.superInChild", new Object[0]));
         }
      }
   }

   // $FF: synthetic method
//   private static Object $deserializeLambda$(SerializedLambda lambda) {
//      switch (lambda.getImplMethodName()) {
//         case "getOrgName":
//            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::getOrgName;
//            }
//            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;
//            }
//            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;
//            }
//            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");
//   }
}
