package com.bonus.core.customer.v4.mobile.service;

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.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.core.common.utils.SysUtil;
import com.bonus.core.customer.mapper.CustInfoMapper;
import com.bonus.core.customer.model.CustPhoto;
import com.bonus.core.customer.service.CustInfoService;
import com.bonus.core.customer.v4.mobile.dto.ProfilePhotoDTO;
import com.bonus.core.customer.v4.mobile.vo.FacePhotoVO;
import com.bonus.core.customer.v4.mobile.vo.ProfilePhotoVO;
import com.bonus.domain.CustInfo;
import com.bonus.i18n.I18n;
import com.bonus.utils.LeBeanUtil;
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 java.io.Serializable;
import java.lang.invoke.SerializedLambda;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

@Service
public class CustomerMobileService extends ServiceImpl<CustInfoMapper, CustInfo> {
   private static final Logger log = LoggerFactory.getLogger(CustomerMobileService.class);
   @Autowired
   @Lazy
   private CustPhotoService custPhotoService;
   @Autowired
   @Lazy
   private CustInfoService custInfoService;

   public void saveProfilePhoto(ProfilePhotoDTO content) {
//      if (ObjectUtil.isEmpty(content.getHeadPortraitUrl())) {
//         throw new ServiceException("头像地址不能为空");
//      } else {
//         long count = this.count((Wrapper)Wrappers.lambdaQuery(CustInfo.class).eq(CustInfo::getCustId, content.getCustId()));
//         if (count == 0L) {
//            throw new ServiceException(I18n.getMessage("customer.customer.inExists", new Object[0]));
//         } else {
//            String headPortraitUrl = SysUtil.getCutPath(content.getHeadPortraitUrl());
//            this.update((Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustInfo.class).set(CustInfo::getHeadPortraitUrl, headPortraitUrl)).eq(CustInfo::getCustId, content.getCustId()));
//         }
//      }
   }

   public ProfilePhotoVO queryProfilePhoto(ProfilePhotoDTO content) {
      CustInfo info = (CustInfo)this.getOne((Wrapper)Wrappers.lambdaQuery(CustInfo.class).eq(CustInfo::getCustId, content.getCustId()));
      return (ProfilePhotoVO) LeBeanUtil.copyCreateProperties(info, ProfilePhotoVO.class);
   }

   public FacePhotoVO facePhotoStatus(ProfilePhotoDTO content) {
//      List<CustPhoto> list = this.custPhotoService.list((Wrapper)((LambdaQueryWrapper)Wrappers.lambdaQuery(CustPhoto.class).eq(CustPhoto::getCustId, content.getCustId())).orderByDesc(CustPhoto::getUptime));
//      if (list.isEmpty()) {
//         return new FacePhotoVO();
//      } else {
//         CustPhoto custPhoto = (CustPhoto)list.get(0);
//         FacePhotoVO facePhotoVO = new FacePhotoVO();
//         facePhotoVO.setFacePhotoUrl(custPhoto.getPhotoUrl());
//         facePhotoVO.setExpireDate(custPhoto.getExpireDate());
//         facePhotoVO.setCustId(custPhoto.getCustId());
//         return facePhotoVO;
//      }
      return null;
   }

   public void activateFacePhoto(ProfilePhotoDTO content) {
//      long count = this.custInfoService.count((Wrapper)Wrappers.lambdaQuery(CustInfo.class).eq(CustInfo::getCustId, content.getCustId()));
//      if (count == 0L) {
//         throw new ServiceException(I18n.getMessage("customer.customer.inExists", new Object[0]));
//      } else {
//         this.custPhotoService.update((Wrapper)((LambdaUpdateWrapper)Wrappers.lambdaUpdate(CustPhoto.class).set(CustPhoto::getExpireDate, CustomerUtil.getExpireDate(content.getExpirDay()))).eq(CustPhoto::getCustId, content.getCustId()));
//         DeviceMqMessage.deviceUpdatePersonalValidityConfigV4();
//      }
   }


   // $FF: synthetic method
//   private static Object $deserializeLambda$(SerializedLambda lambda) {
//      switch (lambda.getImplMethodName()) {
//         case "getUptime":
//            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/CustPhoto") && lambda.getImplMethodSignature().equals("()Ljava/time/LocalDateTime;")) {
//               return CustPhoto::getUptime;
//            }
//            break;
//         case "getHeadPortraitUrl":
//            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/CustInfo") && lambda.getImplMethodSignature().equals("()Ljava/lang/String;")) {
//               return CustInfo::getHeadPortraitUrl;
//            }
//            break;
//         case "getCustId":
//            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/CustInfo") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustInfo::getCustId;
//            }
//
//            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/CustInfo") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustInfo::getCustId;
//            }
//
//            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/CustInfo") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustInfo::getCustId;
//            }
//
//            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/CustPhoto") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustPhoto::getCustId;
//            }
//
//            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/CustInfo") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustInfo::getCustId;
//            }
//
//            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/CustPhoto") && lambda.getImplMethodSignature().equals("()Ljava/lang/Long;")) {
//               return CustPhoto::getCustId;
//            }
//            break;
//         case "getExpireDate":
//            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/CustPhoto") && lambda.getImplMethodSignature().equals("()Ljava/time/LocalDate;")) {
//               return CustPhoto::getExpireDate;
//            }
//      }
//
//      throw new IllegalArgumentException("Invalid lambda deserialization");
//   }
}
