package com.bonus.core.account.v4.api;

import cn.hutool.core.collection.CollUtil;
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.toolkit.Wrappers;
import com.bonus.core.account.v4.constant.CardStatusEnum;
import com.bonus.core.account.v4.web.model.AccCard;
import com.bonus.core.account.v4.web.service.CardInfoV4Service;
import com.bonus.core.account.vo.AccCardBaseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CardInfoServiceV4Api {
    @Autowired
    @Lazy
    private CardInfoV4Service cardInfoService;

    public AccCard queryOneCardInfoByCustId(Long custId) {
        List<AccCard> accCards = this.queryCardInfoByCustIdOrSerialNum(custId, (String) null);
        return CollUtil.isNotEmpty(accCards) ? (AccCard) accCards.get(0) : null;
    }

    public List<AccCardBaseInfoVO> listAccCardBaseInfo() {
        return this.cardInfoService.listAccCardBaseInfo();
    }


    public List<AccCard> queryCardInfoByCustIdOrSerialNum(Long custId, String serialNum) {
        return this.cardInfoService.list(Wrappers.lambdaQuery(AccCard.class)
                .eq(ObjectUtil.isNotNull(custId), AccCard::getCustId, custId)
                .eq(ObjectUtil.isNotNull(serialNum), AccCard::getSerialNum, serialNum)
                .in(AccCard::getCardStatus, CardStatusEnum.normalAndLoss())
                .orderByDesc(AccCard::getUptime));
    }
}
