package com.bonus.core.customer.api;

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.core.customer.service.CustCasualRelationService;
import com.bonus.core.customer.service.CustCasualService;
import com.bonus.domain.CustCasual;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class CustCasualApi {

   @Autowired
   @Lazy
   private CustCasualRelationService custCasualRelationService;
   @Autowired
   @Lazy
   private CustCasualService custCasualService;


   public void updateLoginState(Long userId, String openid) {
      this.custCasualRelationService.updateBycustIdAndOpenid(userId, openid);
   }

   public Long getCustIdByOpenidAndSourceType(String openid, Integer sourceType) {
      CustCasual casual = (CustCasual)this.custCasualService.getOne(Wrappers
              .lambdaQuery(CustCasual.class).select(CustCasual::getCustId)
              .eq(CustCasual::getOpenid, openid).eq(CustCasual::getSourceType, sourceType));
      return Objects.nonNull(casual) ? casual.getCustId() : null;
   }

}
