package cn.iocoder.yudao.module.system.job;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
 * @author: ziin
 * @date: 2025/7/22 14:45
 */
@Component
public class DisableExpiredTenant implements JobHandler {

    @Resource
    private TenantMapper tenantMapper;

    @Override
    public String execute(String param) throws Exception {
        LambdaQueryWrapper<TenantDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.lt(TenantDO::getExpireTime, LocalDateTime.now())
                .eq(TenantDO::getStatus, CommonStatusEnum.ENABLE.getStatus());
        List<TenantDO> tenantDOS = tenantMapper.selectList(queryWrapper);
        ArrayList<String> tenantNames = new ArrayList<>();
        if (tenantDOS.isEmpty()) {
           return "没有到期租户";
        }
        for (TenantDO tenantDO : tenantDOS) {
            tenantDO.setStatus(CommonStatusEnum.DISABLE.getStatus());
            tenantMapper.updateById(tenantDO);
            tenantNames.add(tenantDO.getName());
        }
        return String.format("到期租户:{%s}", tenantNames);
    }
}
