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.PermissSettingListSearch;
import com.zhongshu.card.server.core.dao.BaseImpl;
import com.zhongshu.card.server.core.dao.devices.PermissSettingDao;
import com.zhongshu.card.server.core.dao.devices.extend.PermissSettingListDaoExtend;
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.PermissSettingList;
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 java.util.ArrayList;
import java.util.List;

/**
 * @Author TRX
 * @CreateDate: 2023/4/12
 * @Version: 1.0
 */
public class PermissSettingListDaoImpl extends BaseImpl implements PermissSettingListDaoExtend {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private DBHelper dbHelper;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private PermissSettingDao permissSettingDao;

    @Override
    public Page<PermissSettingList> page(Pageable pageable, PermissSettingListSearch param) {
        // 时段筛选出来的规则信息
        Criteria criteria = buildFilterCriteria(param);
        if (ObjectUtils.isEmpty(param.getPermissSettingIds())) {
            return CommonUtil.buildEmptyPage(pageable, PermissSettingList.class);
        }
        criteria.and("permissSettingId").in(param.getPermissSettingIds());
        Sort sort = buildSort(param);
        Query query = Query.query(criteria);
        query.with(sort);
        return dbHelper.pages(query, pageable, PermissSettingList.class);
    }

    @Override
    public long countNumber(PermissSettingListSearch param) {
        Criteria criteria = buildFilterCriteria(param);
        // 时段配置
        if (ObjectUtils.isEmpty(param.getPermissSettingIds())) {
            return 0;
        }
        criteria.and("permissSettingId").in(param.getPermissSettingIds());
        Query query = Query.query(criteria);
        return mongoTemplate.count(query, PermissSettingList.class);
    }

    @Override
    public boolean isExit(PermissSettingListSearch param) {
        Criteria criteria = buildFilterCriteria(param);
        if (ObjectUtils.isEmpty(param.getPermissSettingIds())) {
            return false;
        }
        criteria.and("permissSettingId").in(param.getPermissSettingIds());
        Query query = Query.query(criteria);
        return mongoTemplate.exists(query, PermissSettingList.class);
    }

    private Criteria buildFilterCriteria(PermissSettingListSearch param) {
        Criteria criteria = buildCriteriaNotOid(param);

        // 所属项目信息
        if (StringUtils.isNotEmpty(param.getProjectOid())) {
            criteria.and("projectOid").is(param.getProjectOid());
        }

        // 模糊搜索
        List<Criteria> criterias = new ArrayList<>();
        if (param.getSex() != null) {
            criterias.add(Criteria.where("sex").in(List.of(param.getSex())));
        }
        if (ObjectUtils.isNotEmpty(param.getDataIds())) {
            criterias.add(Criteria.where("dataIds").in(param.getDataIds()));
        }
        Criteria criteriaUser = new Criteria();
        criteriaUser.orOperator(criterias.toArray(new Criteria[]{}));


        List<Criteria> criteriaDevices = new ArrayList<>();

        if (ObjectUtils.isNotEmpty(param.getDeviceIds())) {
            criteriaDevices.add(Criteria.where("deviceIds").in(param.getDeviceIds()));
        }
        if (ObjectUtils.isNotEmpty(param.getDeviceTypes())) {
            criteriaDevices.add(Criteria.where("deviceTypes").in(param.getDeviceTypes()));
        }

        Criteria criteriaDe = new Criteria();
        criteriaDe.orOperator(criteriaDevices.toArray(new Criteria[]{}));
        criteria.andOperator(criteriaUser, criteriaDe);

        return criteria;
    }
}
