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

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.org.OrganizationRelationSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.org.extend.OrganizationRelationDaoExtend;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.domain.org.OrganizationRelation;
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.MongoTemplate;
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 OrganizationRelationDaoImpl extends BaseImpl implements OrganizationRelationDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Override
    public Page<OrganizationRelation> page(Pageable pageable, OrganizationRelationSearch param) {
        Criteria criteria = new Criteria();

        // 主数据id
        if (StringUtils.isNotEmpty(param.getMainId())) {
            criteria.and("mainOrganization").is(Organization.build(param.getMainId()));
        }

        // 关联的机构数据id
        if (StringUtils.isNotEmpty(param.getRelId())) {
            criteria.and("relOrganization").is(Organization.build(param.getRelId()));
        }

        if (StringUtils.isNotEmpty(param.getMainOid())) {
            criteria.and("mainOid").is(param.getMainOid());
        }

        if (StringUtils.isNotEmpty(param.getRelOid())) {
            criteria.and("relOid").is(param.getRelOid());
        }
        if (StringUtils.isEmpty(param.getRelOid()) && ObjectUtils.isNotEmpty(param.getOids())) {
            criteria.and("relOid").in(param.getOids());
        }

        // 绑定类型
        if (param.getRelationType() != null) {
            criteria.and("relationType").is(param.getRelationType());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (StringUtils.isNotEmpty(param.getMainName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getMainName() + ".*$");
            criterias.add(Criteria.where("mainName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getRelName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getRelName() + ".*$");
            criterias.add(Criteria.where("relName").is(pattern));
        }
        if (!CollectionUtils.isEmpty(criterias)) {
            criteria.andOperator(criterias.toArray(new Criteria[]{}));
        }
        criteria.and("isDelete").is(Boolean.FALSE);
        Sort sort = Sort.by(Sort.Order.desc("createTime"));
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, OrganizationRelation.class);
    }

}
