package cn.com.capcloud.api.action.service;

import cn.com.capcloud.api.action.service.util.ReflectionUtil;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.ccui.core.common.utils.StringUtil;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.util.TblLogNames;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class NoSessionService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private BaseService baseService;
    @Autowired
    private ApiService apiService;
    public String getNoSessionUuid(String jsonStr, JackJSONObject retJson, JackJSONObject dataJson) {
        String nosessionCode=apiService.getJsonValueByKey(jsonStr,retJson,"nosessionCode");
        if("-1".equals(nosessionCode)){
            return "-1";
        }else{
            Map<String,Object> map=findNoSessionInfoByCode(nosessionCode);
            if(map==null){
                retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+" nosessionCode："+nosessionCode);
                return "-1";
            }else{
                return String.valueOf(map.get("uuid"));
            }
        }
    }
    public Map<String,Object> executeMyActionMethodByNoSessionCode(String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        String dsCode=apiService.getJsonValueByKey(jsonStr,retJson,"nosessionCode");
        Map<String,Object> map=findNoSessionInfoByCode(dsCode);
        String className=String.valueOf(map.get("my_action_class"));
        if(StringUtil.isEmpty(className)){
            retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("6010")));
            retJson.put("message",retJson.get("message")+" dsCode："+dsCode);
            return new HashMap<>();
        }else{
            String methodNameStr=apiService.getJsonValueByKey(jsonStr,retJson,"methodName");
            if("-1".equals(methodNameStr)){
                return new HashMap<>();
            }else{
                //此参数也可存储，每次请求校验是否已存在，存在说明该请求是被爬虫拦截到请求参数二次请求。
                String queryLoginPasswordCountSql="SELECT COUNT(1) FROM "+ TblLogNames.TBL_LOG_LOGIN_PASSWORD+" WHERE PASSWORD='"+methodNameStr+"'";
                Integer queryLoginPasswordCount=jdbcTemplate.queryForObject(queryLoginPasswordCountSql,Integer.class);
                if(queryLoginPasswordCount!=0){//已存在说明该请求体是被爬虫拦截到请求参数后使用第三方工具二次请求。
                    retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("4009")));
                    return new HashMap<>();
                }
                Map<String,Object> loginPasswordMap=new HashMap<>();
                loginPasswordMap.put("user_login_name", "nosession_api");
                loginPasswordMap.put("password", methodNameStr);
                baseService.save(TblLogNames.TBL_LOG_LOGIN_PASSWORD,loginPasswordMap);//mysql记录
                methodNameStr=apiService.smDecrypt(methodNameStr);//解析（由于是免登录访问的，需要对请求参数校验，防止爬虫利用参数重复调用）
                JackJSONObject textJson= JackJSONObject.fromObject(methodNameStr);
                String methodName=String.valueOf(textJson.get("text"));
                if(ReflectionUtil.isMethod(className,methodName)){
                    return ReflectionUtil.mySelfMethodRetMap(className,methodName,jsonStr,retJson,request);
                }else{
                    retJson.putAll(JackJSONObject.fromObject(apiService.getReturnDataByCode("6010")));
                    retJson.put("message",retJson.get("message")+" methodName："+methodName+"()  请检查类或方法是否存在");
                    return new HashMap<>();
                }
            }
        }
    }
    public Map<String, Object> findNoSessionInfoByCode(String dsCode) {
        return ApiDao.findNoSessionInfo(null,dsCode,jdbcTemplate);
    }
    public Map<String, Object> findNoSessionByJsonStr(String jsonStr) {
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr);
        String dsCode=String.valueOf(jsonObject.get("nosessionCode"));
        Map<String,Object> map=findNoSessionInfoByCode(dsCode);
        return map;
    }
}

