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.permiss.DevicePermissSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.devices.extend.DevicePermissDaoExtend;
import com.zhongshu.card.server.core.dao.org.DepartmentDao;
import com.zhongshu.card.server.core.dao.org.RoleDao;
import com.zhongshu.card.server.core.domain.devices.permiss.DevicePermiss;
import com.zhongshu.card.server.core.domain.org.Department;
import com.zhongshu.card.server.core.domain.org.Role;
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 DevicePermissDaoImpl extends BaseImpl implements DevicePermissDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    RoleDao roleDao;

    @Autowired
    DepartmentDao departmentDao;

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

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

        // 机构ID
        if (ObjectUtils.isNotEmpty(param.getUserOid())) {
            criteria.and("userOid").is(param.getUserOid());
        }

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

        // 在这个机构的状态，是否可用
        if (param.getState() != null) {
            criteria.and("state").is(param.getState());
        }

        // 机构类型
        if (param.getAuthType() != null) {
            criteria.and("authType").is(param.getAuthType());
        }

        // 职位
        if (StringUtils.isNotEmpty(param.getPositionId())) {
            criteria.and("position.id").is(param.getPositionId());
        }

        // 角色
        List<String> roleIds = param.getRoleIds();
        if (!CollectionUtils.isEmpty(roleIds)) {
            List<Role> roles = roleDao.findByIdIn(roleIds);
            criteria.and("roles").in(roles);
        }

        // 部门
        List<String> departmentIds = param.getDepartmentIds();
        if (!CollectionUtils.isEmpty(departmentIds)) {
            List<Department> roles = departmentDao.findByIdIn(departmentIds);
            criteria.and("departments").in(roles);
        }

        // 搜索项目的用户
        if (StringUtils.isNotEmpty(param.getProjectOid())) {
            criteria.and("projectOid").is(param.getProjectOid());
        }

        if (param.getIsUsed() != null) {
            criteria.and("isUsed").is(param.getIsUsed());
        }

        if (param.getDeviceType() != null) {
            criteria.and("deviceType").is(param.getDeviceType());
        }

        // 设备ID
        if (StringUtils.isNotEmpty(param.getDeviceId())) {
            criteria.and("deviceId").is(param.getDeviceId());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
//        // 用户名称
//        if (StringUtils.isNotEmpty(param.getName())) {
//            Pattern pattern = Pattern.compile("^.*" + param.getName() + ".*$");
//            criterias.add(Criteria.where("name").is(pattern));
//        }
//        if (!CollectionUtils.isEmpty(criterias)) {
//            criteria.andOperator(criterias.toArray(new Criteria[]{}));
//        }

        // 关键字搜索
        if (StringUtils.isNotEmpty(param.getDeviceKeyWord())) {
            Pattern pattern = Pattern.compile("^.*" + param.getDeviceKeyWord() + ".*$");
            criteria.orOperator(
                    Criteria.where("deviceId").regex(pattern),
                    Criteria.where("deviceName").regex(pattern)
            );
        }

        if (StringUtils.isNotEmpty(param.getUserKeyWord())) {
            Pattern pattern = Pattern.compile("^.*" + param.getUserKeyWord() + ".*$");
            criteria.orOperator(
                    Criteria.where("name").regex(pattern),
                    Criteria.where("code").regex(pattern)
            );
        }

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

}
