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

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.paySetting.paySetting.ProjectPaySettingInfoSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.projectAbout.extend.ProjectOrgPaySettingInfoDaoExtend;
import com.zhongshu.card.server.core.domain.paySetting.ProjectOrgPaySettingInfo;
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.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
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 ProjectOrgPaySettingInfoDaoImpl extends BaseImpl implements ProjectOrgPaySettingInfoDaoExtend {

    @Autowired
    private DBHelper dbHelper;

    @Override
    public Page<ProjectOrgPaySettingInfo> page(Pageable pageable, ProjectPaySettingInfoSearch param) {
        Criteria criteria = buildCriteriaNotOid(param);

        if (ObjectUtils.isNotEmpty(param.getProjectOid())) {
            criteria.and("projectOid").is(param.getProjectOid());
        }

        if (StringUtils.isNotEmpty(param.getBelongOig())) {
            criteria.and("belongOig").is(param.getBelongOig());
        }

        if (param.getChannelType() != null) {
            criteria.and("channelType").is(param.getChannelType());
        }

        if (param.getPaymentChannelType() != null) {
            criteria.and("paymentChannelType").is(param.getPaymentChannelType());
        }

        if (param.getState() != null) {
            criteria.and("state").is(param.getState());
        }

        if (param.getIsDefault() != null) {
            criteria.and("isDefault").is(param.getIsDefault());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (StringUtils.isNotEmpty(param.getName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getName() + ".*$");
            criterias.add(Criteria.where("name").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getBankAccountNumber())) {
            Pattern pattern = Pattern.compile("^.*" + param.getBankAccountNumber() + ".*$");
            criterias.add(Criteria.where("bankAccountNumber").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getBankName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getBankName() + ".*$");
            criterias.add(Criteria.where("bankName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getBankSubName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getBankSubName() + ".*$");
            criterias.add(Criteria.where("bankSubName").is(pattern));
        }
        if (!CollectionUtils.isEmpty(criterias)) {
            criteria.andOperator(criterias.toArray(new Criteria[]{}));
        }
        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, ProjectOrgPaySettingInfo.class);
    }

}
