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.projectAbout.orgFace.OrgUserFaceSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.projectAbout.extend.OrgUserFaceDaoExtend;
import com.zhongshu.card.server.core.domain.org.Department;
import com.zhongshu.card.server.core.domain.projectAbout.OrgUserFace;
import com.zhongshu.card.server.core.domain.school.DictInfo;
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 OrgUserFaceDaoImpl extends BaseImpl implements OrgUserFaceDaoExtend {

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<OrgUserFace> page(Pageable pageable, OrgUserFaceSearch param) {
        Criteria criteria = buildFilterCriteria(param);

        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, OrgUserFace.class);
    }

    private Criteria buildFilterCriteria(OrgUserFaceSearch param) {
        Criteria criteria = buildCriteriaNotOid(param);

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

        // 审核状态
        if (param.getAuditState() != null) {
            criteria.and("auditState").is(param.getAuditState());
        }

        // 是否可用
        if (param.getIsCanUsed() != null) {
            criteria.and("isCanUsed").is(param.getIsCanUsed());
        }

        // 审核用户
        if (StringUtils.isNotEmpty(param.getAuditUserId())) {
            criteria.and("auditUserId").is(param.getAuditUserId());
        }

        // 所属用户
        if (StringUtils.isNotEmpty(param.getUserId())) {
            criteria.and("userId").is(param.getUserId());
        }

        if (StringUtils.isNotEmpty(param.getUserOrgOid())) {
            criteria.and("userOrgOid").is(param.getUserOrgOid());
        }

        if (StringUtils.isNotEmpty(param.getDepartmentId())) {
            criteria.and("department").is(Department.build(param.getDepartmentId()));
        }

        if (ObjectUtils.isNotEmpty(param.getRoleIds())) {
            criteria.and("roleIds").in(param.getRoleIds());
        }

        if (StringUtils.isNotEmpty(param.getPositionId())) {
            criteria.and("position").is(DictInfo.build(param.getPositionId()));
        }

        if (param.getSex() != null) {
            criteria.and("sex").is(param.getSex());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (StringUtils.isNotEmpty(param.getUserName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getUserName() + ".*$");
            criterias.add(Criteria.where("userName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getPhone())) {
            Pattern pattern = Pattern.compile("^.*" + param.getPhone() + ".*$");
            criterias.add(Criteria.where("phone").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getAuditRemark())) {
            Pattern pattern = Pattern.compile("^.*" + param.getAuditRemark() + ".*$");
            criterias.add(Criteria.where("auditRemark").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("userName").regex(pattern), Criteria.where("phone").regex(pattern), Criteria.where("code").regex(pattern));
        }
        return criteria;
    }

}
