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.core.constant.CommonConstants;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.product.api.model.KxsBrand;
import com.kxs.product.api.model.KxsMachine;
import com.kxs.product.api.model.KxsShopOrder;
import com.kxs.product.api.model.KxsTicket;
import com.kxs.product.api.vo.kxsapp.ticket.TicketListVO;
import com.kxs.product.biz.constant.enums.TicketTypeEnum;
import com.kxs.product.biz.mapper.KxsBrandMapper;
import com.kxs.product.biz.mapper.KxsMachineMapper;
import com.kxs.product.biz.mapper.KxsShopOrderMapper;
import com.kxs.product.biz.mapper.KxsTicketMapper;
import com.kxs.product.biz.service.KxsTicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
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;
    private final KxsShopOrderMapper kxsShopOrderMapper;
    private final KxsMachineMapper kxsMachineMapper;
    @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;

    }

    @Override
    public Boolean verifyUserTicketCountAndShopAndAct(Long userId) {
        //券码数量
        Long ticketCount = baseMapper.selectCount(Wrappers.<KxsTicket>query().lambda().eq(KxsTicket::getUserId, userId).eq(KxsTicket::getStatus, TicketTypeEnum.CODE_UNUSED.getType()));
        if (ticketCount >= 3){
            return true;
        }
        //指定商品id  (10-电签机具/广电卡通兑券,11-大POS兑换券,77-来客吧产品组合包A.78-来客吧产品组合包B.79-来客产品组合包C)
        List<Long> shopIds = Arrays.asList(10L,11L,77L,78L,79L);
        //校验是否下单过指定商品
        List<KxsShopOrder> kxsShopOrderList = kxsShopOrderMapper.verifyUserTicketCountAndShopAndAct(userId,shopIds);
        if (!kxsShopOrderList.isEmpty()){
            return true;
        }
        //查询激活数 条件为用户名下已激活已绑定的机具
        Long count = kxsMachineMapper.selectCount(Wrappers.<KxsMachine>query().lambda().eq(KxsMachine::getUserId, userId).eq(KxsMachine::getIsAct, 1).eq(KxsMachine::getBindStatus,1));
        return count >= 3;
    }

    @Override
    public Boolean checkUserPreLevelTicket(Long userId, Long level) {

        Long ticketCount = baseMapper.selectCount(Wrappers.<KxsTicket>query().lambda().eq(KxsTicket::getUserId, userId).eq(KxsTicket::getStatus, TicketTypeEnum.CODE_UNUSED.getType()));

        if(level <= 4){
            return ticketCount >= 3;
        } else if (level <= 6) {
            return ticketCount >= 6;
        } else if (level <= 8){
            return ticketCount >= 9;
        }else {
            return ticketCount >= 15;
        }

    }

    @Override
    public Boolean userBuyTicket(Long userId, Integer ticketType, Integer orderId, Integer number) {
        //电签兑换券流程
        List<KxsTicket> kxsTickets = baseMapper.selectList( Wrappers.<KxsTicket>query().lambda()
                .eq(KxsTicket::getUserId, 0)
                .eq(KxsTicket::getTicketType, ticketType)
                .eq(KxsTicket::getStatus, TicketTypeEnum.CODE_UNUSED.getType()).last("limit " + number));
        if (kxsTickets == null || kxsTickets.size() != 3){
            throw new GlobalCustomerException("劵码库存不足订单id:" + orderId);
        }
        for (KxsTicket kxsTicket : kxsTickets) {
            kxsTicket.setUserId(userId.intValue());
            kxsTicket.setTicketAllotPath(CommonConstants.FAIL + "," + userId + "," );
            kxsTicket.setOrderId(orderId);
            //todo 盟主 合伙人标记
//            kxsTicket.setLeaderId();
//            kxsTicket.setPartnerId();
            baseMapper.updateById(kxsTicket);
        }
        return Boolean.TRUE;
    }
}

