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

import com.github.microservice.components.data.mongo.mongo.dao.MongoDao;
import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.pay.WithdrawPageParam;
import com.zhongshu.card.client.type.AuditStatus;
import com.zhongshu.card.client.type.WithdrawStatus;
import com.zhongshu.card.client.type.paySetting.WithdrawMethodType;
import com.zhongshu.card.server.core.dao.pay.WithdrawOrderDao;
import com.zhongshu.card.server.core.dao.pay.extend.WithdrawOrderDaoExtend;
import com.zhongshu.card.server.core.domain.pay.WithdrawOrder;
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.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 java.util.List;
import java.util.regex.Pattern;

public class WithdrawOrderDaoImpl implements WithdrawOrderDaoExtend {

    @Autowired
    DBHelper dbHelper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Page<WithdrawOrder> page(Pageable pageable, String projectOid, String oid, Long startTime, Long endTime, String search, AuditStatus auditStatus, WithdrawMethodType withdrawMethodType) {
        Criteria criteria = new Criteria();

        if (StringUtils.isNotBlank(projectOid)) {
            criteria.and("projectOid").is(projectOid);
        }

        if (StringUtils.isNotBlank(oid)) {
            criteria.and("oid").is(oid);
        }

        if (startTime != null && endTime != null) {
            criteria.and("createTime").gte(startTime).lte(endTime);
        }

        if (StringUtils.isNotBlank(search)) {
            java.util.regex.Pattern namePattern = Pattern.compile("^.*" + search + ".*$");
            criteria.and("withdrawUser.name").regex(namePattern);
        }

        if (auditStatus != null) {
            if (auditStatus.equals(AuditStatus.Processed)){
                criteria.and("auditStatus").in(List.of(AuditStatus.Success, AuditStatus.Fail));
            }else {
                criteria.and("auditStatus").is(auditStatus);
            }
        }

        if (withdrawMethodType != null) {
            criteria.and("withdrawMethodType").is(withdrawMethodType);
        }

        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Order.asc("sort"), Sort.Order.desc("createTime")));
        return dbHelper.pages(query, pageable, WithdrawOrder.class);
    }

    @Override
    public boolean updateWithdrawStatus(String id, WithdrawStatus status, String withdrawStatusDesc) {

        Criteria criteria = new Criteria();
        criteria.and("_id").is(id);
        criteria.and("withdrawStatus").is(WithdrawStatus.Accepted);
        Query query = new Query(criteria);

        Update update = new Update();
        update.set("withdrawStatus", status);
        update.set("withdrawStatusDesc", withdrawStatusDesc);
        dbHelper.updateTime(update);
        return mongoTemplate.updateFirst(query, update, WithdrawOrder.class).getMatchedCount() > 0;
    }
}
