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.UserCountSearchParam;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.org.extend.UserCountDaoExtend;
import com.zhongshu.card.server.core.domain.org.UserAccount;
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 UserCountDaoImpl extends BaseImpl implements UserCountDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Override
    public Page<UserAccount> page(Pageable pageable, UserCountSearchParam param) {
        Criteria criteria = buildCriteria(param);

        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);

        return dbHelper.pages(query, pageable, UserAccount.class);
    }

    @Override
    public long countNumber(UserCountSearchParam param) {
        Criteria criteria = buildCriteria(param);
        Query query = Query.query(criteria);
        long count = mongoTemplate.count(query, UserAccount.class);
//        List<AggregationOperation> operations = new ArrayList<>(2);
//        AggregationOperation match = Aggregation.match(criteria);
//        operations.add(match);
//
//        AggregationOperation group = Aggregation.group("state").count().as("count");
//        operations.add(group);
//
//        Aggregation groupAggregation = Aggregation.newAggregation(operations);
//        List<Map> countObjs = mongoTemplate.aggregate(groupAggregation, BusinessInfo.class, Map.class).getMappedResults();
//        if (countObjs.size() > 0) {
//            Object obj = countObjs.get(0).get("count");
//            int number = CommonUtil.getBigDecimalByObj(obj).intValue();
//        }

        return count;
    }

    public Criteria buildCriteria(UserCountSearchParam param) {
        Criteria criteria = new Criteria();

        if (ObjectUtils.isNotEmpty(param.getUserId())) {
            criteria.and("userId").is(param.getUserId());
        }

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

        if (param.getUserType() != null) {
            criteria.and("userType").is(param.getUserType());
        }

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

        if (!CommonUtil.longIsEmpty(param.getStartTime()) && !CommonUtil.longIsEmpty(param.getEndTime())) {
            criteria.and("createTime").gte(param.getStartTime()).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.getLoginName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getLoginName() + ".*$");
            criterias.add(Criteria.where("loginName").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("phone").regex(pattern));
        }
        criteria.and("isDelete").is(Boolean.FALSE);
        return criteria;
    }

}
