package nckd.jxccl.hr.task;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.constant.FormConstant;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 政策公告下架后台任务插件
 * @author turborao
 * 时间 2026/1/15 16:05
 */
@SuppressWarnings("unused")
public class AnnouncementsTask extends AbstractTask implements Plugin {

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {

        try {
            // 获取今天的日期（不包含时间部分）
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 23);
            calendar.set(Calendar.MINUTE, 59);
            calendar.set(Calendar.SECOND, 59);
            Date today = calendar.getTime();

            // 构建查询条件：nckd_downdate <= 今天的日期
            QFilter filter = new QFilter("nckd_downdate", QCP.less_equals, today);

            String selectFields = "id,nckd_billstatus,nckd_downdate";
            DynamicObject[] dyns = BusinessDataServiceHelper.load(
                    FormConstant.NCKD_ANNOUNCEMENTS,
                    selectFields,
                    new QFilter[]{filter}
            );

            // 更新查询到的数据
            if (dyns != null && dyns.length > 0) {
                for (DynamicObject item : dyns) {
                    // 设置 nckd_billstatus 为 D
                    item.set("nckd_billstatus", "D");
                }
                // 保存更新
                SaveServiceHelper.save(dyns);
            }

        } catch (Exception e) {
            throw new KDException("更新公告状态失败：" + e.getMessage());
        }
    }
}