package com.zhongshu.card.server.core.dao.projectAbout.impl;

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.github.microservice.core.util.token.TokenUtil;
import com.zhongshu.card.client.model.projectAbout.PayChannelConfigSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.projectAbout.extend.PayChannelConfigDaoExtend;
import com.zhongshu.card.server.core.dao.projectAbout.extend.ProjectMainPaySettingDaoExtend;
import com.zhongshu.card.server.core.domain.paySetting.PayChannelConfig;
import com.zhongshu.card.server.core.domain.paySetting.ProjectMainPaySetting;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author TRX
 * @CreateDate: 2023/4/12
 * @Version: 1.0
 */
public class ProjectMainPaySettingDaoImpl extends BaseImpl implements ProjectMainPaySettingDaoExtend {

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private MongoTemplate mongoTemplate;

    public String acquire(String key, Long expiration) {
        Query query = Query.query(Criteria.where("_id").is(key).and("token").isNull());
        String token = TokenUtil.create();
        Update update = new Update().set("expireAt", System.currentTimeMillis() + expiration).set("token", token);

        FindAndModifyOptions options = new FindAndModifyOptions().upsert(false).returnNew(true);

        ProjectMainPaySetting doc = mongoTemplate.findAndModify(query, update, options, ProjectMainPaySetting.class);
        if (doc == null) {
            ProjectMainPaySetting lockObj = mongoTemplate.findOne(Query.query(Criteria.where("_id").is(key)), ProjectMainPaySetting.class);
            doc = lockObj;
        }
        boolean locked = doc != null && doc.getToken() != null && doc.getToken().equals(token);

        // 如果已过期
        if (!locked && doc != null && doc.getExpireAt() < System.currentTimeMillis()) {
            release(key);
            // 成功释放锁， 再次尝试获取锁
            return this.acquire(key, expiration);
        }
        return locked ? token : null;
    }

    public boolean release(String key) {
        Query releaseQuery = Query.query(Criteria.where("_id").is(key));
        Update releaseUpdate = new Update().set("expireAt", null).set("token", null);
        FindAndModifyOptions releaseOptions = new FindAndModifyOptions().upsert(true).returnNew(true);
        ProjectMainPaySetting flowDisposition = mongoTemplate.findAndModify(releaseQuery, releaseUpdate, releaseOptions, ProjectMainPaySetting.class);
        return StringUtils.isEmpty(flowDisposition.getToken());
    }

}
