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

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.visitor.VisitorMainSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.visitor.extend.VisitorMainDaoExtend;
import com.zhongshu.card.server.core.domain.visitor.VisitorMain;
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 VisitorMainDaoImpl extends BaseImpl implements VisitorMainDaoExtend {

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private MongoTemplate mongoTemplate;

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

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

    @Override
    public Long count(VisitorMainSearch param) {
        Criteria criteria = buildFilterCriteria(param);
        Query query = Query.query(criteria);
        return mongoTemplate.count(query, VisitorMain.class);
    }

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

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

        // 是否已审核
        if (param.getIsReViewed() != null) {
            criteria.and("isReViewed").is(param.getIsReViewed());
        }

        if (StringUtils.isNotEmpty(param.getSceneComponentId())) {
            criteria.and("sceneComponentId").is(param.getSceneComponentId());
        }

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

        if (ObjectUtils.isNotEmpty(param.getFinalStates())) {
            criteria.and("finalState").in(param.getFinalStates());
        }

        if (StringUtils.isNotEmpty(param.getVisitorUserId())) {
            criteria.and("visitorUserId").is(param.getVisitorUserId());
        }

        if (param.getIsCancel() != null) {
            criteria.and("isCancel").is(param.getIsCancel());
        }

        // 是否失效
        if (param.getIsOverTime() != null) {
            criteria.and("isOverTime").is(param.getIsOverTime());
        }

        if (param.getIsClearFace() != null) {
            criteria.and("isClearFace").is(param.getIsClearFace());
        }

        if (param.getIsLeave() != null) {
            criteria.and("isLeave").is(param.getIsLeave());
        }

        if (param.getIsOverTimeLeave() != null) {
            criteria.and("isOverTimeLeave").is(param.getIsOverTimeLeave());
        }

        if (StringUtils.isNotEmpty(param.getApplyUserId())) {
            criteria.and("applyUserId").is(param.getApplyUserId());
        }

        // 微信小程序的openId
        if (StringUtils.isNotEmpty(param.getOpenId())) {
            criteria.and("openId").is(param.getOpenId());
        }

        if (param.getApplyType() != null) {
            criteria.and("applyType").is(param.getApplyType());
        }

        // 类型
        if (ObjectUtils.isNotEmpty(param.getVisitorTypes())) {
            criteria.and("visitorType").in(param.getVisitorTypes());
        }
        // 能使用的设备
        if (ObjectUtils.isNotEmpty(param.getDeviceIds())) {
            criteria.and("deviceIds").in(param.getDeviceIds());
        }

        // 是否审核成功
        if (param.getReViewSuccess() != null) {
            criteria.and("reViewSuccess").is(param.getReViewSuccess());
        }

        // 预约到访时间
        if (!CommonUtil.longIsEmpty(param.getVisitorStartTime()) && !CommonUtil.longIsEmpty(param.getVisitorEndTime())) {
            criteria.and("startTime").gte(param.getVisitorStartTime()).lte(param.getVisitorEndTime());
        } else if (!CommonUtil.longIsEmpty(param.getVisitorStartTime()) && CommonUtil.longIsEmpty(param.getVisitorEndTime())) {
            criteria.and("startTime").gte(param.getVisitorStartTime());
        } else if (CommonUtil.longIsEmpty(param.getVisitorStartTime()) && !CommonUtil.longIsEmpty(param.getVisitorEndTime())) {
            criteria.and("startTime").lte(param.getVisitorEndTime());
        }

        // 实际到访时间
        if (!CommonUtil.longIsEmpty(param.getStartFirstVisitTime()) && !CommonUtil.longIsEmpty(param.getEndFirstVisitTime())) {
            criteria.and("firstVisitTime").gte(param.getStartFirstVisitTime()).lte(param.getEndFirstVisitTime());
        } else if (!CommonUtil.longIsEmpty(param.getStartFirstVisitTime()) && CommonUtil.longIsEmpty(param.getEndFirstVisitTime())) {
            criteria.and("firstVisitTime").gte(param.getStartFirstVisitTime());
        } else if (CommonUtil.longIsEmpty(param.getStartFirstVisitTime()) && !CommonUtil.longIsEmpty(param.getEndFirstVisitTime())) {
            criteria.and("firstVisitTime").lte(param.getEndFirstVisitTime());
        }

        // 模糊搜索
        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.getPhone())) {
            Pattern pattern = Pattern.compile("^.*" + param.getPhone() + ".*$");
            criterias.add(Criteria.where("phone").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getCardNumber())) {
            Pattern pattern = Pattern.compile("^.*" + param.getCardNumber() + ".*$");
            criterias.add(Criteria.where("cardNumber").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getCarNo())) {
            Pattern pattern = Pattern.compile("^.*" + param.getCarNo() + ".*$");
            criterias.add(Criteria.where("carNo").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getVisitorUserName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getVisitorUserName() + ".*$");
            criterias.add(Criteria.where("visitorUserName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getVisitorUserPhone())) {
            Pattern pattern = Pattern.compile("^.*" + param.getVisitorUserPhone() + ".*$");
            criterias.add(Criteria.where("visitorUserPhone").is(pattern));
        }
        if (!CollectionUtils.isEmpty(criterias)) {
            criteria.andOperator(criterias.toArray(new Criteria[]{}));
        }
        return criteria;
    }

}
