SpecialAuthenticationServiceImpl.java 3.99 KB
package com.objecteye.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.objecteye.entity.PageResult;
import com.objecteye.pojo.SpecialAuthenticationUrlConfig;
import com.objecteye.service.ISpecialAuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
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.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class SpecialAuthenticationServiceImpl implements ISpecialAuthenticationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分页查询
     *
     * @param requestMap 请求参数
     * @return 结果集
     */
    @Override
    public PageResult findByPage(Map<String, Object> requestMap) {
        Integer pagevolume = (Integer) requestMap.get("pagevolume");
        Integer currentpage = (Integer) requestMap.get("currentpage");

        List<SpecialAuthenticationUrlConfig> specialAuthenticationUrlConfigs = mongoTemplate
                .find(new Query().limit(pagevolume).skip((currentpage - 1) * pagevolume), SpecialAuthenticationUrlConfig.class);
        long total = mongoTemplate
                .count(new Query().limit(pagevolume).skip((currentpage - 1) * pagevolume), SpecialAuthenticationUrlConfig.class);
        total = (long) Math.ceil((double) total / pagevolume);
        return new PageResult<>(total, specialAuthenticationUrlConfigs);
    }

    /**
     * 添加特殊地址权限要求
     *
     * @param requestMap 请求参数
     * @return 结果集
     */
    @Override
    public JSONObject addConfig(Map<String, Object> requestMap) {
        SpecialAuthenticationUrlConfig urlConfig = JSON.parseObject(JSON.toJSONString(requestMap), SpecialAuthenticationUrlConfig.class);
        urlConfig = mongoTemplate.insert(urlConfig);
        JSONObject resultObj = new JSONObject();
        if (urlConfig.getId() == null) {
            resultObj.put("error", "数据创建失败");
        } else {
            resultObj.put("id", urlConfig.getId());
        }
        return resultObj;
    }

    /**
     * 更新配置信息
     *
     * @param requestMap 请求参数
     * @return 结果集
     */
    @Override
    public JSONObject updateConfig(Map<String, Object> requestMap) {
        SpecialAuthenticationUrlConfig urlConfig = JSON.parseObject(JSON.toJSONString(requestMap), SpecialAuthenticationUrlConfig.class);
        long count = mongoTemplate.count(Query.query(Criteria.where("id").is(urlConfig.getId())), SpecialAuthenticationUrlConfig.class);
        JSONObject resultObj = new JSONObject();
        if (count == 0) {
            resultObj.put("error", "数据不存在");
        } else {
            mongoTemplate.save(urlConfig);
        }
        return resultObj;
    }

    /**
     * 删除配置信息
     *
     * @param requestMap 请求参数
     * @return 结果集
     */
    @Override
    public JSONObject deleteConfig(Map<String, Object> requestMap) {
        SpecialAuthenticationUrlConfig urlConfig = JSON.parseObject(JSON.toJSONString(requestMap), SpecialAuthenticationUrlConfig.class);
        mongoTemplate.remove(Query.query(Criteria.where("id").is(urlConfig.getId())), SpecialAuthenticationUrlConfig.class);
        return new JSONObject();
    }

    /**
     * 校验相同配置信息是否存在
     *
     * @param requestMap 请求参数
     * @return 结果集
     */
    @Override
    public JSONObject checkConfig(Map<String, Object> requestMap) {
        SpecialAuthenticationUrlConfig urlConfig = JSON.parseObject(JSON.toJSONString(requestMap), SpecialAuthenticationUrlConfig.class);
        long count = mongoTemplate.count(Query.query(Criteria.where("id").is(urlConfig.getId())), SpecialAuthenticationUrlConfig.class);
        JSONObject resultObj = new JSONObject();
        resultObj.put("count", count);
        return resultObj;
    }
}