package cn.com.capcloud.api.action;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.ccui.core.common.json.JackJSON;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultActionFace;
import cn.com.capcloud.core.system.user.UserService;
import cn.com.capcloud.core.util.ReadProperty;
import cn.com.capcloud.api.action.service.NoSessionService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
public class NoSessionAction {
    @Resource
    ReadProperty readProperty;
    @Resource
    private BaseService baseService;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private ApiService apiService;
    @Resource
    private NoSessionService noSessionService;
    @Resource
    private UserService userService;
    @Resource
    private KHCRedisCache khcRedisCache;


    @Resource
    private List<DefaultActionFace> defaultActionFaces;

    private static NoSessionAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.readProperty=this.readProperty;
        action.baseService=this.baseService;
        action.jdbcTemplate=this.jdbcTemplate;
        action.apiService =this.apiService;
        action.defaultActionFaces=this.defaultActionFaces;
        action.userService=this.userService;
    }

    public String myAction(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.noSessionService.getNoSessionUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.noSessionService.executeMyActionMethodByNoSessionCode(jsonStr,retJson,request);
        if(returnMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",JackJSONObject.fromObject(returnMap));
        return JackJSON.toJSONString(retJson);
    }
}
