package com.kxs.product.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.product.api.model.KxsBrand;
import com.kxs.product.api.vo.kxsapp.ticket.TicketListVO;
import com.kxs.product.biz.mapper.KxsBrandMapper;
import com.kxs.product.biz.mapper.KxsTicketMapper;
import com.kxs.product.api.model.KxsTicket;
import com.kxs.product.biz.service.KxsTicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客小爽券码表(KxsTicket)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsTicketService")
@RequiredArgsConstructor
public class KxsTicketServiceImpl extends ServiceImpl<KxsTicketMapper, KxsTicket> implements KxsTicketService {

    private final KxsBrandMapper kxsBrandMapper;

    @Override
    public IPage<TicketListVO> getByPage(Page<TicketListVO> page, Integer ticketType) {

        Long userId = SecurityUtils.getUser().getId();

        //查询品牌
        List<KxsBrand> brands = kxsBrandMapper.selectList(Wrappers.<KxsBrand>lambdaQuery().eq(KxsBrand::getBrandType, ticketType));
        List<Integer> brandIds = brands.stream().map(KxsBrand::getId).toList();

        //查询券
        IPage<TicketListVO> byPage = baseMapper.getByPage(page, userId, ticketType);
        byPage.getRecords().forEach(item -> item.setBrandList(brandIds));

        return byPage;

    }
}

