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

import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.customer.mapper.CustCasualMapper;
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.canteen.core.customer.service.CustCasualRelationService;
import com.bonus.canteen.core.customer.service.CustCasualService;
import com.bonus.canteen.core.customer.utils.DelFlagEnum;
import com.bonus.common.houqin.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;

   @Autowired
   private CustCasualMapper custCasualMapper;

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

   public String getOpenidByCustId(Long custId, Integer sourceType) {
//      return this.custCasualMapper.selectOpenidByCustId(custId, sourceType, LeConstants.COMMON_NO);
        return null;
   }

   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;
   }

   public CustCasual getCustCasualByCustIdAndSourceType(Long custId, Integer sourceType) {
      return this.custCasualService.getOne((Wrapper)((LambdaQueryWrapper<CustCasual>) Wrappers
              .lambdaQuery(CustCasual.class).eq(CustCasual::getCustId, custId)
              .eq(CustCasual::getSourceType, sourceType)).eq(CustCasual::getIfDel, DelFlagEnum.DEL_FALSE.key()));
   }

}
