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

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.common.utils.SysUtil;
import com.bonus.canteen.core.customer.mapper.CustInfoMapper;
import com.bonus.canteen.core.customer.model.CustInfo;
import com.bonus.canteen.core.customer.model.CustPhoto;
import com.bonus.canteen.core.customer.service.CustInfoService;
import com.bonus.canteen.core.customer.v4.mobile.dto.ProfilePhotoDTO;
import com.bonus.canteen.core.customer.v4.mobile.vo.FacePhotoVO;
import com.bonus.canteen.core.customer.v4.mobile.vo.ProfilePhotoVO;
import com.bonus.common.houqin.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 javax.annotation.Resource;
import java.util.List;

@Service
public class CustomerMobileService extends ServiceImpl<CustInfoMapper, CustInfo> {
   private static final Logger log = LoggerFactory.getLogger(CustomerMobileService.class);
   @Resource
   @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(Wrappers.lambdaQuery(CustInfo.class).eq(CustInfo::getCustId, content.getCustId()));
         if (count == 0L) {
            throw new ServiceException("customer.customer.inExists");
         } else {
            String headPortraitUrl = SysUtil.getCutPath(content.getHeadPortraitUrl());
            this.update(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(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;
      }
   }

   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");
//   }
}
