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

import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.noentitydb.BaseService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

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

@Service
public class NoSessionService {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private BaseService baseService;
    @Resource
    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("null".equals(className)||className.length()==0){
            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 tbl_data_login_password_log 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("tbl_data_login_password_log",loginPasswordMap);//mysql记录
                methodNameStr=apiService.smDecrypt(methodNameStr);//解析（由于是免登录访问的，需要对请求参数校验，防止爬虫利用参数重复调用）
                JackJSONObject textJson= JackJSONObject.fromObject(methodNameStr);
                String methodName=String.valueOf(textJson.get("text"));
                if(ApiDao.isMethod(className,methodName)){
                    return ApiDao.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;
    }
}
