package com.bonus.core.account.v4.web.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.core.account.v4.constant.CardStatusEnum;
import com.bonus.core.account.v4.web.mapper.AccCardMapper;
import com.bonus.core.account.v4.web.model.AccCard;
import com.bonus.core.account.vo.AccCardBaseInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import com.bonus.core.customer.business.CardInfoBurialPointBusiness;
import com.bonus.core.customer.service.CustInfoService;
import com.bonus.core.customer.v4.web.dto.CardInfoDTO;
import com.bonus.core.customer.v4.web.vo.CardInfoDetailVO;
import com.bonus.core.customer.v4.web.vo.CardInfoVO;
import com.bonus.domain.CustInfo;
import com.bonus.utils.LeBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CardInfoV4Service extends ServiceImpl<AccCardMapper, AccCard> {

    @Autowired
    @Lazy
    private AccCardMapper accCardMapper;

    public List<AccCardBaseInfoVO> listAccCardBaseInfo() {
        return this.accCardMapper.listAccCardBaseInfo();
    }
    @Resource
    @Lazy
    CustInfoService custInfoService;
    @Resource
    @Lazy
    private CardInfoBurialPointBusiness cardInfoBurialPointBusiness;

    public CardInfoDetailVO queryCardsByCustId(CardInfoDTO content) {
        CustInfo custInfo = (CustInfo)this.custInfoService.getOne(Wrappers.lambdaQuery(CustInfo.class)
                .eq(CustInfo::getCustId, content.getCustId()));
        if (custInfo == null) {
            return new CardInfoDetailVO();
        } else {
            CardInfoDetailVO cardInfoDetailVO = (CardInfoDetailVO) LeBeanUtil.copyCreateProperties(custInfo, CardInfoDetailVO.class);
            List<AccCard> cards = this.list(Wrappers.lambdaQuery(AccCard.class)
                    .eq(AccCard::getCustId, content.getCustId())
                    .in(AccCard::getCardStatus,
                    CardStatusEnum.normalAndLoss()));
            if (cards == null) {
                return cardInfoDetailVO;
            } else {
                List<CardInfoVO> cardInfoVO = BeanUtil.copyToList(cards, CardInfoVO.class);
                cardInfoDetailVO.setCardInfoList(cardInfoVO);
                this.cardInfoBurialPointBusiness.didGetCardList(cardInfoDetailVO);
                return cardInfoDetailVO;
            }
        }
    }


}
