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

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.school.CardInfoSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.school.extend.CardInfoDaoExtend;
import com.zhongshu.card.server.core.domain.school.CardInfo;
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 CardInfoDaoImpl extends BaseImpl implements CardInfoDaoExtend {

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Page<CardInfo> page(Pageable pageable, CardInfoSearch param) {
        Criteria criteria = buildCriteriaNotOid(param);

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

        if (param.getCardType() != null) {
            criteria.and("cardType").is(param.getCardType());
        }

        // 卡片是否挂失
        if (param.getCardState() != null) {
            criteria.and("cardState").is(param.getCardState());
        }

        // 是否作废
        if (param.getIsCanceled() != null) {
            criteria.and("isCanceled").is(param.getIsCanceled());
        }

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

        // 模糊搜索
        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.getCode())) {
            Pattern pattern = Pattern.compile("^.*" + param.getCode() + ".*$");
            criterias.add(Criteria.where("code").is(pattern));
        }

        if (StringUtils.isNotEmpty(param.getUserId())) {
            Pattern pattern = Pattern.compile("^.*" + param.getUserId() + ".*$");
            criterias.add(Criteria.where("userId").is(pattern));
        }

        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 (!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.where("userId").regex(pattern),
                    Criteria.where("userName").regex(pattern),
                    Criteria.where("phone").regex(pattern)
            );
        }

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

}
