package com.bonus.core.customer.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.customer.constants.CustConstant;
import com.bonus.core.customer.dto.AddPsnTypeDTO;
import com.bonus.core.customer.mapper.CustPsnTypeMapper;
import com.bonus.core.customer.model.CustPsnType;
import com.bonus.core.customer.service.CustPsnTypeService;
import com.google.common.base.Joiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.invoke.SerializedLambda;
import java.util.*;
import java.util.stream.Collectors;

//@Component
public class CustPsnTypeApi {
   private static final Logger log = LoggerFactory.getLogger(CustPsnTypeApi.class);

   @Autowired
   CustPsnTypeMapper custPsnTypeMapper;

   @Autowired
   private CustPsnTypeService custPsnTypeService;

   public Map<Integer, String> mapByPsnType(Set<Integer> psnTypeList) {
      Set<Integer> psnTypes = (Set)psnTypeList.stream().filter(Objects::nonNull).collect(Collectors.toSet());
      //return (Map)this.custPsnTypeMapper.selectList(Wrappers.lambdaQuery(CustPsnType.class).select(new SFunction[]{CustPsnType::getPsnType, CustPsnType::getPsnTypeName}).in(CollUtil.isNotEmpty(psnTypes), CustPsnType::getPsnType, psnTypes)).stream().collect(Collectors.toMap(CustPsnType::getPsnType, CustPsnType::getPsnTypeName));

      return this.custPsnTypeMapper.selectList(Wrappers.lambdaQuery(CustPsnType.class)
                      .select(CustPsnType::getPsnType, CustPsnType::getPsnTypeName)
                      .in(CollUtil.isNotEmpty(psnTypes), CustPsnType::getPsnType, psnTypes))
              .stream()
              .collect(Collectors.toMap(CustPsnType::getPsnType, CustPsnType::getPsnTypeName));
   }

   public List<CustPsnType> listPsnType(List<Integer> psnTypes) {
      //return CollUtil.isEmpty(psnTypes) ? Collections.emptyList() : this.custPsnTypeMapper.selectList((Wrapper)Wrappers.lambdaQuery().in(CustPsnType::getPsnType, psnTypes));
      return psnTypes.isEmpty() ? Collections.emptyList() : this.custPsnTypeMapper.selectList(Wrappers.lambdaQuery(CustPsnType.class).in(CustPsnType::getPsnType, psnTypes));
   }

   protected void initTempPsnType() {
      CustPsnType psnType = new CustPsnType();
      psnType.setPsnType(CustConstant.TEMPORARY_PSN_TYPE);
      psnType.setPsnTypeName("临时人员");
      this.custPsnTypeMapper.insert(psnType);
   }

   public List<Integer> syncPsnType(List<AddPsnTypeDTO> addPsnTypeDTOList) {
      List<Integer> errorType = new ArrayList();
      if (CollUtil.isEmpty(addPsnTypeDTOList)) {
         return errorType;
      } else {
         Iterator var3 = addPsnTypeDTOList.iterator();

         while(var3.hasNext()) {
            AddPsnTypeDTO psnTypeDTO = (AddPsnTypeDTO)var3.next();

            try {
               this.custPsnTypeService.insertOrUpdatePsnType(psnTypeDTO.getPsnType(), psnTypeDTO.getPsnTypeName());
            } catch (ServiceException var6) {
               log.error("同步新增或更新人员类别[{}]异常:{}", new Object[]{Joiner.on(":").join(psnTypeDTO.getPsnType(), psnTypeDTO.getPsnTypeName(), new Object[0]), var6.getMessage(), var6});
               errorType.add(psnTypeDTO.getPsnType());
            }
         }

         return errorType;
      }
   }

   // $FF: synthetic method
//   private static Object $deserializeLambda$(SerializedLambda lambda) {
//      switch (lambda.getImplMethodName()) {
//         case "getPsnType":
//            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/CustPsnType") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustPsnType::getPsnType;
//            }
//
//            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/CustPsnType") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustPsnType::getPsnType;
//            }
//
//            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/CustPsnType") && lambda.getImplMethodSignature().equals("()Ljava/lang/Integer;")) {
//               return CustPsnType::getPsnType;
//            }
//            break;
//         case "getPsnTypeName":
//            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/CustPsnType") && lambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//               return CustPsnType::getPsnTypeName;
//            }
//      }
//
//      throw new IllegalArgumentException("Invalid lambda deserialization");
//   }
}
