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 org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

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


    @Autowired
    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("-1".equals(uuid)){
            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);
    }
}

