package cn.com.capcloud.api.action.myself.system;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.MongoService;
import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DBCheck;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.noentitydb.JtPage;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;

/**
 * 如果数据存储在MongoDB里，可以指定Action为此类
 */

@Component
public class MongoAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiService apiService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private MongoService mongoService;
    private static MongoAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.mongoTemplate=this.mongoTemplate;
        action.mongoService=this.mongoService;
    }

    @Override
    public JtPage showAllAfter(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JtPage jtPage) {
        if(DBCheck.isMongoDB()){
            jtPage=mongoService.showAll(jsonStr,retJson,request,jtPage);
        }else{
            jtPage=super.showAllAfter(jsonStr,retJson,request,jtPage);
        }
        return jtPage;
    }
    // saveDemo 保存和修改调用示例
    @Override
    public JackJSONArray saveBefore(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        if(DBCheck.isMongoDB()){
            return action.mongoService.save(jsonStr,retJson,request,array);
        }else{
            return super.saveBefore(jsonStr,retJson,request,array);
        }
    }

}

