package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.StoreVipLog;
import shop.alien.entity.store.vo.StoreVipLogVo;
import shop.alien.mapper.StoreVipLogMapper;
import shop.alien.store.service.StoreVipLogService;

import java.util.stream.Collectors;

/**
 * <p>
 * 商家会员记录 服务实现类
 * </p>
 *
 * @author ssk
 * @since 2025-02-20
 */
@Service
@RequiredArgsConstructor
public class StoreVipLogServiceImpl extends ServiceImpl<StoreVipLogMapper, StoreVipLog> implements StoreVipLogService {

    private final StoreVipLogMapper storeVipLogMapper;

    /**
     * 分页查询vip记录
     *
     * @param pageNum        页码
     * @param pageSize       页容
     * @param storeName      门店名称
     * @param orderNo        订单编号
     * @param vipStatus      vip状态(0:未过期,1:已过期)
     * @param vipType        vip类型(1:黄金, 2:白金)
     * @param buyDuration    购买时长(1:包月, 2:包季, 3:包年)
     * @param createdDate    创建时间
     * @param expirationDate 过期时间
     * @return IPage<StoreVipLog>
     */
    @Override
    public IPage<StoreVipLogVo> getStoreVipLogPage(Integer pageNum, Integer pageSize, String storeName, String orderNo, Integer vipStatus, Integer vipType, Integer buyDuration, String createdDate, String expirationDate) {
        IPage<StoreVipLogVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<StoreVipLogVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!"".equals(storeName), "f.store_name", storeName)
                .eq(!"".equals(orderNo), " b.out_biz_no", orderNo)
                .eq(vipType != null, "a.vip_type", vipType)
                .eq(buyDuration != null, "a.buy_duration", buyDuration)
                .gt(!"".equals(createdDate), "a.created_time", createdDate)
                .gt(!"".equals(expirationDate), "a.expiration_date", expirationDate);
        IPage<StoreVipLogVo> storeVipLogPage = storeVipLogMapper.getStoreVipLogPage(page, queryWrapper);
        storeVipLogPage.getRecords().forEach(item -> {
            if (item.getExpirationDate().getTime() > System.currentTimeMillis()) {
                item.setVipStatus("未过期");
            } else {
                item.setVipStatus("已过期");
            }
        });
        if (null != vipStatus && 0 == vipStatus) {
            storeVipLogPage.setRecords(storeVipLogPage.getRecords().stream().filter(item -> item.getExpirationDate().getTime() > System.currentTimeMillis()).collect(Collectors.toList()));
        }
        if (null != vipStatus && 1 == vipStatus) {
            storeVipLogPage.setRecords(storeVipLogPage.getRecords().stream().filter(item -> item.getExpirationDate().getTime() < System.currentTimeMillis()).collect(Collectors.toList()));
        }
        return storeVipLogPage;
    }
}
