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.DeviceInfoSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.devices.extend.DeviceInfoDaoExtend;
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
import com.zhongshu.card.server.core.domain.school.Area;
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 DeviceInfoDaoImpl extends BaseImpl implements DeviceInfoDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

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

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

    @Override
    public long count(DeviceInfoSearch param) {
        Criteria criteria = buildFilterCriteria(param);

        Query query = Query.query(criteria);
        return mongoTemplate.count(query, DeviceInfo.class);
    }

    public Criteria buildFilterCriteria(DeviceInfoSearch param) {
        param.setOid("");
        Criteria criteria = buildCriteria(param);

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

        // 设备类型
        if (param.getDeviceType() != null) {
            criteria.and("deviceType").is(param.getDeviceType());
        }

        // 设备型号
        if (param.getDeviceModel() != null) {
            criteria.and("deviceModel").is(param.getDeviceModel());
        }

        if (param.getOnLineState() != null) {
            criteria.and("onLineState").is(param.getOnLineState());
        }

        if (StringUtils.isNotEmpty(param.getAreaId())) {
            criteria.and("area").is(Area.build(param.getAreaId()));
        }

        if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
            criteria.and("projectInfoCode").is(param.getProjectInfoCode());
        }

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

        // 设备绑定的机构
        if (ObjectUtils.isNotEmpty(param.getShopOids())) {
            criteria.and("beLongOid").in(param.getShopOids());
        }

        if (ObjectUtils.isNotEmpty(param.getDeviceIds())) {
            criteria.and("deviceId").in(param.getDeviceIds());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();

        if (StringUtils.isNotEmpty(param.getDeviceId()) && ObjectUtils.isEmpty(param.getDeviceIds())) {
            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 (StringUtils.isNotEmpty(param.getGateWayId())) {
            Pattern pattern = Pattern.compile("^.*" + param.getGateWayId() + ".*$");
            criterias.add(Criteria.where("gateWayId").is(pattern));
        }

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

        // 包含的都算
        if (StringUtils.isNotEmpty(param.getContainAreaId())) {
            Pattern pattern = Pattern.compile("^.*" + param.getContainAreaId() + ".*$");
            criterias.add(Criteria.where("areaPaths").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), Criteria.where("ip").regex(pattern));
        }
        return criteria;
    }

}
