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

import com.github.microservice.components.data.mongo.mongo.helper.DBHelper;
import com.zhongshu.card.client.model.devices.DeviceBindSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.devices.extend.DeviceBindDaoExtend;
import com.zhongshu.card.server.core.domain.devices.DeviceBind;
import com.zhongshu.card.server.core.domain.school.Area;
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 DeviceBindDaoImpl extends BaseImpl implements DeviceBindDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

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

        if (param.getDeviceType() != null) {
            criteria.and("deviceType").is(param.getDeviceType());
        }
        // 在线状态
        if (param.getOnLineState() != null) {
            criteria.and("onLineState").is(param.getOnLineState());
        }
        // 所属区域
        if (StringUtils.isNotEmpty(param.getAreaId())) {
            criteria.and("area").is(Area.build(param.getAreaId()));
        }
        // 所属项目code
        if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
            criteria.and("projectInfoCode").is(param.getProjectInfoCode());
        }
        // 所属学校oid
        if (StringUtils.isNotEmpty(param.getSchoolInfoOid())) {
            criteria.and("schoolInfoOid").is(param.getSchoolInfoOid());
        }
        // 所属商户oid
        if (StringUtils.isNotEmpty(param.getShopInfoOid())) {
            criteria.and("shopInfoOid").is(param.getShopInfoOid());
        }
        // 所属商户oid集合
        if (ObjectUtils.isNotEmpty(param.getShopInfoOids())) {
            criteria.and("shopInfoOid").in(param.getShopInfoOids());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (StringUtils.isNotEmpty(param.getDeviceId())) {
            Pattern pattern = Pattern.compile("^.*" + param.getDeviceId() + ".*$");
            criterias.add(Criteria.where("deviceId").is(pattern));
        }
        if (StringUtils.isNotEmpty(param.getDeviceName())) {
            Pattern pattern = Pattern.compile("^.*" + param.getDeviceName() + ".*$");
            criterias.add(Criteria.where("deviceName").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("deviceId").regex(pattern), Criteria.where("deviceName").regex(pattern), Criteria.where("gateWayId").regex(pattern));
        }
        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, DeviceBind.class);
    }

}
