package com.czg.task;

import cn.hutool.core.date.DateUtil;
import com.czg.account.entity.ShopUser;
import com.czg.account.service.ShopUserService;
import com.czg.constant.TableValueConstant;
import com.czg.market.service.TbMemberConfigService;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 会员奖励发放
 *
 * @author ww
 * @description
 */
@Slf4j
@Component
public class MemberTask {
    @DubboReference
    private ShopUserService shopUserService;

    @Resource
    private TbMemberConfigService memberConfigService;

    /**
     * 奖励发放
     */
//    @Scheduled(cron = "0 0 1 * * ? ")
    public void task() {
        shopUserService.list(new QueryWrapper().eq(ShopUser::getIsVip, 1).lt(ShopUser::getNextDeliverTime, DateUtil.date().toLocalDateTime())).forEach(item -> {
            //周任务
            memberConfigService.deliver(item, TableValueConstant.MemberExpFlow.Type.MEMBER_TASK, null, null, null);
        });
    }

    @Scheduled(cron = "0 */10 * * * *")
    public void check() {
        List<ShopUser> shopUsers = shopUserService.list(new QueryWrapper()
                .isNotNull(ShopUser::getEndTime)
                .eq(ShopUser::getIsVip, 1)
                .lt(ShopUser::getEndTime, LocalDateTime.now()));
        shopUsers.forEach(item -> item.setIsVip(0));

        shopUserService.updateBatch(shopUsers);
    }
}
