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

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

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private MongoTemplate mongoTemplate;

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

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

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

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

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

        if (param.getTipType() != null) {
            criteria.and("tipType").is(param.getTipType());
        }

        if (StringUtils.isNotEmpty(param.getFromUserId())) {
            criteria.and("fromUserId").is(param.getFromUserId());
        }

        if (StringUtils.isNotEmpty(param.getToUserId())) {
            criteria.and("toUserId").is(param.getToUserId());
        }

        if (param.getIsRead() != null) {
            criteria.and("isRead").is(param.getIsRead());
        }

        if (StringUtils.isNotEmpty(param.getBusDataId())) {
            criteria.and("busDataId").is(param.getBusDataId());
        }

        if (param.getBusFunctionType() != null) {
            criteria.and("busFunctionType").is(param.getBusFunctionType());
        }

        if (StringUtils.isNotEmpty(param.getOpenId())) {
            criteria.and("openId").is(param.getOpenId());
        }

        if (StringUtils.isNotEmpty(param.getBusStateStr())) {
            criteria.and("busStateStr").is(param.getBusStateStr());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (StringUtils.isNotEmpty(param.getTitle())) {
            Pattern pattern = Pattern.compile("^.*" + param.getTitle() + ".*$");
            criterias.add(Criteria.where("title").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getContent())) {
            Pattern pattern = Pattern.compile("^.*" + param.getContent() + ".*$");
            criterias.add(Criteria.where("content").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getFromUserName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getFromUserName() + ".*$");
            criterias.add(Criteria.where("fromUserName").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getToUserName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getToUserName() + ".*$");
            criterias.add(Criteria.where("toUserName").is(pattern));
        }
        if (!CollectionUtils.isEmpty(criterias)) {
            criteria.andOperator(criterias.toArray(new Criteria[]{}));
        }
        return criteria;
    }

}
