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.OrganizationSearchParam;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.org.extend.OrganizationDaoExtend;
import com.zhongshu.card.server.core.domain.org.Organization;
import com.zhongshu.card.server.core.service.orgManager.ProjectBindOrgServiceImpl;
import com.zhongshu.card.server.core.util.CommonUtil;
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 OrganizationDaoImpl extends BaseImpl implements OrganizationDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private ProjectBindOrgServiceImpl projectBindOrgServiceImpl;

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

        //
        if (ObjectUtils.isNotEmpty(param.getOid())) {
            criteria.and("oid").is(param.getOid());
        }
        // 状态
        if (param.getState() != null) {
            criteria.and("state").is(param.getState());
        }

        // 类型
        if (param.getAuthType() != null) {
            criteria.and("authType").is(param.getAuthType());
        }

        if (ObjectUtils.isNotEmpty(param.getOids())) {
            criteria.and("oid").in(param.getOids());
        }

        if (ObjectUtils.isNotEmpty(param.getIds())) {
            criteria.and("id").in(param.getIds());
        }

        // 是否是机构数据
        if (param.getIsOrg() != null) {
            criteria.and("isOrg").is(param.getIsOrg());
        }

        if (StringUtils.isNotEmpty(param.getManagerUserId())) {
            Pattern pattern = Pattern.compile("^.*" + param.getManagerUserId() + ".*$");
            criteria.and("managerUserIds").is(pattern);
        }

        if (!CommonUtil.longIsEmpty(param.getStartTime()) && !CommonUtil.longIsEmpty(param.getEndTime())) {
            criteria.and("createTime").gte(param.getStartTime()).and("createTime").lte(param.getEndTime());
        }

        if (StringUtils.isNotEmpty(param.getProjectOid())) {
            List<Organization> list = projectBindOrgServiceImpl.getProjectBindOrgs(param.getProjectOid());
            if (ObjectUtils.isEmpty(list)) {
                return CommonUtil.buildEmptyPage(pageable, Organization.class);
            }
            criteria.and("oid").in(list.stream().map(Organization::getOid).toArray());
        }

        // 模糊搜索
        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.getOrgCode())) {
            Pattern pattern = Pattern.compile("^.*" + param.getOrgCode() + ".*$");
            criterias.add(Criteria.where("orgCode").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getContactEmail())) {
            Pattern pattern = Pattern.compile("^.*" + param.getContactEmail() + ".*$");
            criterias.add(Criteria.where("contactEmail").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getContactName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getContactName() + ".*$");
            criterias.add(Criteria.where("contactName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getContactPhone())) {
            Pattern pattern = Pattern.compile("^.*" + param.getContactPhone() + ".*$");
            criterias.add(Criteria.where("contactPhone").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getCode())) {
            Pattern pattern = Pattern.compile("^.*" + param.getCode() + ".*$");
            criterias.add(Criteria.where("code").is(pattern));
        }
        if (!CollectionUtils.isEmpty(criterias)) {
            criteria.andOperator(criterias.toArray(new Criteria[]{}));
        }

        if (StringUtils.isNotEmpty(param.getKeyWord())) {
            Pattern pattern = Pattern.compile("^.*" + param.getKeyWord() + ".*$");
            criteria.orOperator(Criteria.where("name").regex(pattern), Criteria.where("code").regex(pattern));
        }

        criteria.and("isDelete").is(Boolean.FALSE);
        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, Organization.class);
    }

}
