package cn.com.capcloud.api.action;

import cn.com.capcloud.api.action.service.ApiDao;
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.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.lucene.LuceneIndexManager;
import cn.com.capcloud.core.lucene.SearchParam;
import cn.com.capcloud.core.util.ReadProperty;
import cn.com.capcloud.core.cache.BaseConfigStaticCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.noentitydb.DefaultActionFace;
import cn.com.capcloud.core.noentitydb.JtPage;
import cn.com.capcloud.core.system.user.User;
import cn.com.capcloud.core.system.user.UserService;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import org.joda.time.DateTime;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
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 javax.transaction.Transactional;

import java.io.IOException;
import java.util.*;

@Component
public class ApiAction {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ApiAction.class);
    //自动填充用户信息(KEY)
    private static final String SYSTEM_SETTINGS_DS_FORM_AUTO_USER="system.settings.ds.form.auto.user.json";
    @Resource
    ReadProperty readProperty;
    @Resource
    private BaseService baseService;
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private ApiService apiService;
    @Resource
    private UserService userService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private LuceneIndexManager luceneIndexManager;


    @Resource
    private List<DefaultActionFace> defaultActionFaces;

    private static ApiAction 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;
        action.luceneIndexManager=this.luceneIndexManager;
    }

    private DefaultActionFace get(String actionClassName) {
        DefaultActionFace defaultActionFace=null;
        for(int i=0;i<action.defaultActionFaces.size();i++) {
            if(action.defaultActionFaces.get(i).getClass().getName().indexOf(actionClassName)>=0) {
                defaultActionFace=action.defaultActionFaces.get(i);
            }
        }
        return defaultActionFace;
    }
    public String page(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String pageCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"pageCode");
        if("-1".equals(pageCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> returnMap = action.apiService.getPageInfo(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String list(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        action.apiService.getDataSourceInfo(jsonStr,uuid,retJson,dataJson);
        if(action.apiService.getFormInfo(jsonStr,uuid,retJson,dataJson,request)=="-1"){
            return JackJSON.toJSONString(retJson);
        }else{
            String formStr=String.valueOf(dataJson.get("form"));
            if(!"null".equals(formStr)){
                User user=action.userService.getUser(request);
                String sql="SELECT "+ BaseConfigStaticCache.get(SYSTEM_SETTINGS_DS_FORM_AUTO_USER) +" FROM tbl_system_user WHERE id="+user.getId();
                List<Map<String,Object>> userList=jdbcTemplate.queryForList(sql);
                dataJson.getJackJSONObject("form").put("user",userList.get(0));
            }
        }
        if(action.apiService.getQueryInfo(jsonStr,uuid,retJson,dataJson,request)=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",dataJson);
        return JackJSON.toJSONString(retJson);
    }
    public String showAll(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        String sql;
        String buildSql=String.valueOf(queryMap.get("build_sql"));
        if(existsAction&&!"null".equals(buildSql)&& buildSql.length() >0){
            sql = ApiDao.mySelfMethodRetStr(actionClass,buildSql,jsonStr,retJson,request);
        }else{
            sql = actionImpl.buildSql(jsonStr,retJson,request);
        }
        if("-1".equals(sql)){
            return JackJSON.toJSONString(retJson);
        }
        String powerSql;
        String buildPowerSql=String.valueOf(queryMap.get("build_power_sql"));
        if(existsAction&&!"null".equals(buildPowerSql)&& buildPowerSql.length() >0){
            powerSql = ApiDao.mySelfMethodRetStr(actionClass,buildPowerSql,jsonStr,retJson,request);
        }else{
            powerSql = actionImpl.buildPowerSql(jsonStr,retJson,request);
        }
        if("-1".equals(powerSql)){
            return JackJSON.toJSONString(retJson);
        }
        sql=sql+powerSql;
        String sqlCount;
        String buildSqlCount=String.valueOf(queryMap.get("build_sql_count"));
        if(existsAction&&!"null".equals(buildSqlCount)&& buildSqlCount.length() >0){
            sqlCount = ApiDao.mySelfMethodRetStr(actionClass,buildSqlCount,jsonStr,retJson,request);
        }else{
            sqlCount = sql;//改成如下，如果count影响效率可重写buildSqlCount，在该方法优化sql即可。
        }
        if("-1".equals(sqlCount)){
            return JackJSON.toJSONString(retJson);
        }
        actionImpl.buildSqlCount(jsonStr,retJson,request);
        String orderBy=action.apiService.getOrderBySql(jsonStr,retJson);
        if(orderBy=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> pageMap=new HashMap<>();
        action.apiService.getLimitPage(jsonStr,pageMap);
        logger.debug(sql+orderBy);
        Integer page=Integer.valueOf(String.valueOf(pageMap.get("page")));
        Integer pageSize=Integer.valueOf(String.valueOf(pageMap.get("pageSize")));
        //查询选中的内容
        JackJSONObject jsonObject = JackJSONObject.fromObject(jsonStr).getJackJSONObject("queryParam");
        String isLucene=String.valueOf(dsMap.get("is_lucene"));
        JtPage jtPage=null;
        if(isLucene.equals("Y")){
            String tableName=String.valueOf(dsMap.get("ds_table_name"));
            String dsUUID=String.valueOf(dsMap.get("uuid"));
            List<Map<String,Object>> fieldList=action.apiService.getQueryInfo(jsonStr);
            List<SearchParam> listSear=ApiDao.getLuceneSearchParam(tableName,fieldList, jsonStr,jdbcTemplate,retJson);
            if(listSear.size()>0){
                try {
                    jtPage=action.luceneIndexManager.searchMultipleQuery(listSear,(page+1),pageSize,tableName,dsUUID);
                    jtPage.setNumber(jtPage.getNumber()-1);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                jtPage=action.baseService.query(sql+orderBy,page,pageSize,sqlCount);
            }
        }else{
            jtPage=action.baseService.query(sql+orderBy,page,pageSize,sqlCount);
        }

        jtPage = actionImpl.showAllDesensitize(jsonStr,retJson,request,jtPage);
        if(jtPage==null){
            return JackJSON.toJSONString(retJson);
        }
        String selectTableDataArray = String.valueOf(jsonObject.get("selectTableDataArray"));
        if(!selectTableDataArray.equals("null")&&selectTableDataArray.length()>0){
            String tableName=String.valueOf(dsMap.get("ds_table_name"));
            String selectDataSql="select * from "+tableName +" where id in("+selectTableDataArray+") "+orderBy;
            List<Map<String, Object>> selectContent=jdbcTemplate.queryForList(selectDataSql);
            jtPage.setSelectContent(selectContent);
        }
        action.apiService.valueRewrite(jsonStr,request,jtPage);
        String showAllAfter=String.valueOf(queryMap.get("show_all_after"));
        if(existsAction&&!"null".equals(showAllAfter)&& showAllAfter.length() >0){
            jtPage = ApiDao.mySelfMethodRetJtPage(actionClass,showAllAfter,jsonStr,retJson,request,jtPage);
        }else{
            jtPage = actionImpl.showAllAfter(jsonStr,retJson,request,jtPage);
        }
        if(jtPage==null){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",jtPage);
        return JackJSON.toJSONString(retJson);
    }
    @Transactional
    public String save(String jsonStr, HttpServletRequest request){

    	User user=userService.getUser(request);
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String dsUuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(dsUuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String formCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"formCode");
        if("-1".equals(formCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> formMap=action.apiService.getForm(dsUuid,formCode,retJson);
        if(formMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(dsUuid);
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        String isTree=String.valueOf(formMap.get("is_tree"));
        String saveBefore=String.valueOf(formMap.get("save_before"));
        String saveAfter=String.valueOf(formMap.get("save_after"));
        String startBpm=String.valueOf(formMap.get("start_bpm"));
        String updateBpm=String.valueOf(formMap.get("update_bpm"));
        String bpmModelID=String.valueOf(formMap.get("bpm_model_id"));
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        JackJSONArray saveData=JackJSONObject.fromObject(jsonStr).getJackJSONArray("saveData");
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        if(isTree.equals("Y")){//是否tree表
            String idField=String.valueOf(formMap.get("t_id"));
            String pIdField=String.valueOf(formMap.get("t_p_id"));
            String levelField=String.valueOf(formMap.get("t_level"));
            for (int i = 0; i < saveData.size(); i++) {
                String id=String.valueOf(saveData.getJackJSONObject(i).get(idField));
                Integer level=0;
                String levelStr=String.valueOf(saveData.getJackJSONObject(i).get(levelField));
                String pId=String.valueOf(saveData.getJackJSONObject(i).get(pIdField));
                String querySql="select "+levelField+" from "+tableName +" where "+idField+"='"+pId+"'";
                try {
                    level=jdbcTemplate.queryForObject(querySql,Integer.class);
                    level=level+1;//因为查询出的是上级部门层级，需加1
                } catch (DataAccessException e) {
                    level=1;//查不到默认一级
                }
                saveData.getJackJSONObject(i).put(levelField,level);
                if(!"null".equals(id)&&id.length()>0){//刷新下级的level
                    Map<String,Object> map=new HashMap<>();
                    map.put(pIdField,id);
                    map.put("pIdField",pIdField);
                    map.put(levelField,level+1);
                    map.put("levelField",levelField);
                    map.put("idField",idField);
                    map.put("tableName",tableName);
                    action.apiService.refreshChildLevel(map);
                }
            }
        }
        DateTime d = new DateTime();
        if(existsAction&&!"null".equals(saveBefore)&& saveBefore.length() >0){
            saveData = ApiDao.mySelfMethodRetListMap(actionClass,saveBefore,jsonStr,retJson,request,saveData);
        }else{
            saveData = actionImpl.saveBefore(jsonStr,retJson,request,saveData);
        }
        if(saveData.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        String pkFieldName = String.valueOf(dsMap.get("ds_pk_id"));
        String pkId=String.valueOf(saveData.getJackJSONObject(0).get(pkFieldName));
        //保存之前后端也验证数据（验证一些值是否唯一）
        String validate=action.apiService.saveBeforeValidate(jsonStr,saveData,retJson,request);
        if(validate=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        //新增数据
        if("null".equals(pkId)|| pkId.length() ==0){
            for (int i = 0; i < saveData.size(); i++) {
                String uuid=String.valueOf(saveData.getJackJSONObject(i).get("uuid"));
                if("null".equals(uuid)|| uuid.length() ==0){
                    uuid=UUID.randomUUID().toString().replaceAll("-","");
                    saveData.getJackJSONObject(i).put("uuid", uuid);
                }
                saveData.getJackJSONObject(i).remove(dsMap.get("ds_pk_id"));
                saveData.getJackJSONObject(i).put("status",0);
                saveData.getJackJSONObject(i).put("data_source","form");
                saveData.getJackJSONObject(i).put("form_version",formMap.get("form_version"));
            }
            Map<String, Object> retSaveMap=null;
            for (int i = 0; i < saveData.size(); i++) {//todo 虽然有批量保存实现，但不建议在前端使用批量插入，可以批量修改
                if(i==0){
                    retSaveMap=action.baseService.save(tableName,saveData.getJackJSONObject(i),user);
                }else{
                    action.baseService.save(tableName,saveData.getJackJSONObject(i),user);
                }
                action.apiService.cudLogSave("create",retSaveMap.get(pkFieldName)+"",dsMap,request,saveData,new JackJSONArray());
            }
            saveData.set(0,retSaveMap);//将旧的值覆盖一下
        }else{//update数据
            String ids="";
            for (int i = 0; i < saveData.size(); i++) {
                saveData.getJackJSONObject(i).put("update_user",user.getUserName());
                saveData.getJackJSONObject(i).put("update_time",d.toString("yyyy-MM-dd HH:mm:ss"));
                saveData.getJackJSONObject(i).put("update_date",d.toString("yyyy-MM-dd"));
                ids+=saveData.getJackJSONObject(i).get(pkFieldName)+",";
            }
            ids=ids.substring(0,ids.length()-1);
            String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
            if(!"-1".equals(queryCode)){
                Map<String, Object> queryMap=action.apiService.getQuery(dsUuid,queryCode,retJson);
                if(queryMap.size()>0){
                    String sql;
                    String buildSql=String.valueOf(queryMap.get("build_sql"));
                    if(existsAction&&!"null".equals(buildSql)&& buildSql.length() >0){
                        sql = ApiDao.mySelfMethodRetStr(actionClass,buildSql,jsonStr,retJson,request);
                    }else{
                        sql = actionImpl.buildSql(jsonStr,retJson,request);
                    }
                    if("-1".equals(sql)){
                        return JackJSON.toJSONString(retJson);
                    }
                    String powerSql;
                    String buildPowerSql=String.valueOf(queryMap.get("build_power_sql"));
                    if(existsAction&&!"null".equals(buildPowerSql)&& buildPowerSql.length() >0){
                        powerSql = ApiDao.mySelfMethodRetStr(actionClass,buildPowerSql,jsonStr,retJson,request);
                    }else{
                        powerSql = actionImpl.buildPowerSql(jsonStr,retJson,request);
                    }
                    if("-1".equals(powerSql)){
                        return JackJSON.toJSONString(retJson);
                    }
                    sql=sql+powerSql+" and "+pkFieldName+" in("+ids+")";//todo 这块先默认都是字符串，且在MySQL是加单引号是不影响的，如果更换数据库影响请动态判断
                    System.out.println(sql);
                    //Map<String,Object> returnMap = action.baseService.findOne(tableName, pkId,pkFieldId);
                    List<Map<String,Object>> returnList = action.jdbcTemplate.queryForList(sql);
                    if(returnList.size()!=saveData.size()){
                        retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("4031")));
                        return JackJSON.toJSONString(retJson);
                    }
                }
            }
            JackJSONArray oldSaveData=new JackJSONArray();
            if(saveData.size()==1){
                String sql="select * from "+tableName+" where "+pkFieldName+"='"+saveData.getJackJSONObject(0).get(pkFieldName)+"'";
                oldSaveData= JackJSONArray.fromObject(action.jdbcTemplate.queryForList(sql));
                String enableHistory = String.valueOf(dsMap.get("enable_history"));
                if(enableHistory.equals("Y")){
                    JackJSONObject historyJson=oldSaveData.getJackJSONObject(0);
                    String historyUUID=String.valueOf(historyJson.get("uuid"));
                    historyJson.put("is_history","Y");
                    historyJson.put("status","2"); // 0有效，1删除，2历史
                    historyJson.put("page_parent_uuid", historyUUID);
                    historyJson.remove("id");
                    historyJson.remove("uuid");
                    action.baseService.save(tableName, historyJson);
                }
            }
            action.baseService.update(tableName, (List) saveData);
            action.apiService.cudLogSave("update",ids,dsMap,request,saveData,oldSaveData);
        }
        String isLucene=String.valueOf(dsMap.get("is_lucene"));
        if(isLucene.equals("Y")){//更新全文索引
            for (int i = 0; i < saveData.size(); i++) {
                String dsUUID=String.valueOf(dsMap.get("uuid"));
                String dataUUID=String.valueOf(saveData.getJackJSONObject(i).get("uuid"));
                luceneIndexManager.updateIndex(tableName,dsUUID,dataUUID);//对单条数据索引更新
            }
        }
        String enableJob=String.valueOf(dsMap.get("enable_job"));
        String jobClassPath=String.valueOf(dsMap.get("job_class_path"));
        if(enableJob.equals("Y")&&!jobClassPath.equals("null")&&jobClassPath.length()>0){
            JackJSONObject rowData=saveData.getJackJSONObject(0);
            String cronExpression=String.valueOf(rowData.get("cron_expression"));
            if(!cronExpression.equals("null")&&cronExpression.length()>0){
                apiService.executeJobByMethodName(jobClassPath,dsMap,rowData,"start");
            }else{
                apiService.executeJobByMethodName(jobClassPath,dsMap,rowData,"stop");
            }
        }
        if(existsAction&&!"null".equals(saveAfter)&& saveAfter.length() >0){
            saveData = ApiDao.mySelfMethodRetListMap(actionClass,saveAfter,jsonStr,retJson,request,saveData);
        }else{
            saveData = actionImpl.saveAfter(jsonStr,retJson,request,saveData);
        }
        if(saveData.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        dataJson.put("saveData",saveData);
        retJson.put("data",dataJson);
        return JackJSON.toJSONString(retJson);
    }

    public String insertOne(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        User user=userService.getUser(request);
        String dsUuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(dsUuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String formCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"formCode");
        if("-1".equals(formCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> formMap=action.apiService.getForm(dsUuid,formCode,retJson);
        if(formMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        JackJSONArray saveData=JackJSONObject.fromObject(jsonStr).getJackJSONArray("saveData");
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(dsUuid);
        saveData.getJackJSONObject(0).put("data_source","form");
        saveData.getJackJSONObject(0).put("form_version",formMap.get("form_version"));
        action.baseService.save(String.valueOf(dsMap.get("ds_table_name")), (List) saveData,user);
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data","");
        return JackJSON.toJSONString(retJson);
    }

    public String findOne(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        /**
         * 如果不是使用默认的主键当查询条件，请注意前端传过来的查询条件要唯一的。
         */
        String pkFieldId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkFieldId"));
        if("null".equals(pkFieldId)||pkFieldId.length()==0){
            pkFieldId=String.valueOf(dsMap.get("ds_pk_id"));
        }
        Map<String,Object> returnMap = new HashMap<>();
        String isLucene=String.valueOf(dsMap.get("is_lucene"));
        if(isLucene.equals("Y")){
            String pkId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkId"));
            String tableName=String.valueOf(dsMap.get("ds_table_name"));
            returnMap = action.baseService.findOne(tableName, pkId,pkFieldId);
        }else{
            String sql;
            String buildSql=String.valueOf(queryMap.get("build_sql"));
            if(existsAction&&!"null".equals(buildSql)&& buildSql.length() >0){
                sql = ApiDao.mySelfMethodRetStr(actionClass,buildSql,jsonStr,retJson,request);
            }else{
                sql = actionImpl.buildSql(jsonStr,retJson,request);
            }
            if("-1".equals(sql)){
                return JackJSON.toJSONString(retJson);
            }
            String powerSql;
            String buildPowerSql=String.valueOf(queryMap.get("build_power_sql"));
            if(existsAction&&!"null".equals(buildPowerSql)&& buildPowerSql.length() >0){
                powerSql = ApiDao.mySelfMethodRetStr(actionClass,buildPowerSql,jsonStr,retJson,request);
            }else{
                powerSql = actionImpl.buildPowerSql(jsonStr,retJson,request);
            }
            if("-1".equals(powerSql)){
                return JackJSON.toJSONString(retJson);
            }
            String pkId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkId"));
            sql=sql+powerSql+" and "+pkFieldId+"='"+pkId+"'";//todo 这块先默认都是字符串，且在MySQL是加单引号是不影响的，如果更换数据库影响请动态判断
            System.out.println(sql);
            try {
                returnMap = action.jdbcTemplate.queryForMap(sql);
            } catch (Exception e) {
                if(!pkFieldId.equals("page_parent_uuid")){
                    retJson.putAll(JSONObject.fromObject(action.apiService.getReturnDataByCode("4031")));
                    return JackJSON.toJSONString(retJson);
                }else{
                    returnMap=new HashMap<>();
                }
            }
        }
        /*
         * retSaveMap.put("process_instance_id", String.valueOf(processInfo.getProcessInstanceId()));//流程实例ID
            	retSaveMap.put("process_definition_id", String.valueOf(processInfo.getProcessDefinitionId()));//流程图ID

            	Task task=processService.getFirstTask(processInfo.getProcessInstanceId());
            	retSaveMap.put("process_task_id", String.valueOf(task.getId()));
         */
        //如果有工作流字段，且工作流字段有值，则获取下一节点信息，并取出按钮

        returnMap=JackJSON.parseObject(ApiDao.nullToStrFilter(returnMap), HashMap.class);
        if(returnMap.size()>0){
            String findOneAfter=String.valueOf(queryMap.get("find_one_after"));
            if(existsAction&&!"null".equals(findOneAfter)&& findOneAfter.length() >0){
                returnMap = ApiDao.mySelfMethodRetMap(actionClass,findOneAfter,jsonStr,retJson,request,returnMap);
            }else{
                returnMap = actionImpl.findOneAfter(jsonStr,retJson,request,returnMap);
            }
            if(returnMap.size()==0){
                return JackJSON.toJSONString(retJson);
            }
            String valueToShowValue=String.valueOf(JackJSONObject.fromObject(jsonStr).get("valueToShowValue"));
            if(valueToShowValue.equals("Y")){//字典转换
                action.apiService.valueToShowValue(jsonStr,request,returnMap);
            }
        }

        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String del(String jsonStr, HttpServletRequest request){
        //注：formCode为必须参数，因为在form组件里需要用到一些配置
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> queryMap=action.apiService.getQuery(uuid,queryCode);
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        JackJSONArray pkIds=JackJSONObject.fromObject(jsonStr).getJackJSONArray("pkIds");
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        String formCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"formCode");
        if("-1".equals(formCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> formMap=action.apiService.getForm(uuid,formCode,retJson);
        if(formMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        String isTree=String.valueOf(formMap.get("is_tree"));
        String pIdField=String.valueOf(formMap.get("t_p_id"));
        String pkFieldName = String.valueOf(dsMap.get("ds_pk_id"));
        if(isTree.equals("Y")){
            for (int i = 0; i < pkIds.size(); i++) {
                String queryCountSql="SELECT count(1) FROM "+tableName+" WHERE STATUS=0 AND "+pIdField+"='"+pkIds.getJackJSONObject(i).get("pkId")+"'";
                Integer count=action.jdbcTemplate.queryForObject(queryCountSql,Integer.class);
                if(count>0){
                    retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("7011")));
                    retJson.put("message",retJson.get("message")+" 原因是当前有子数据");
                    return JackJSON.toJSONString(retJson);
                }
            }
        }
        String ids="";
        for (int i = 0; i < pkIds.size(); i++) {
            ids+=pkIds.getJackJSONObject(i).get("pkId")+",";
        }
        ids=ids.substring(0,ids.length()-1);
        String sql;
        String buildSql=String.valueOf(queryMap.get("build_sql"));
        if(existsAction&&!"null".equals(buildSql)&& buildSql.length() >0){
            sql = ApiDao.mySelfMethodRetStr(actionClass,buildSql,jsonStr,retJson,request);
        }else{
            sql = actionImpl.buildSql(jsonStr,retJson,request);
        }
        if("-1".equals(sql)){
            return JackJSON.toJSONString(retJson);
        }
        String powerSql;
        String buildPowerSql=String.valueOf(queryMap.get("build_power_sql"));
        if(existsAction&&!"null".equals(buildPowerSql)&& buildPowerSql.length() >0){
            powerSql = ApiDao.mySelfMethodRetStr(actionClass,buildPowerSql,jsonStr,retJson,request);
        }else{
            powerSql = actionImpl.buildPowerSql(jsonStr,retJson,request);
        }
        if("-1".equals(powerSql)){
            return JackJSON.toJSONString(retJson);
        }
        sql=sql+powerSql+" and "+pkFieldName+" in("+ids+")";//todo 这块先默认都是字符串，且在MySQL是加单引号是不影响的，如果更换数据库影响请动态判断
        System.out.println(sql);
        //Map<String,Object> returnMap = action.baseService.findOne(tableName, pkId,pkFieldId);
        List<Map<String,Object>> returnList = action.jdbcTemplate.queryForList(sql);
        if(returnList.size()!=pkIds.size()){
            retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("4031")));
            return JackJSON.toJSONString(retJson);
        }
        String deleteBefore=String.valueOf(queryMap.get("delete_before"));
        String beforeRet;
        if(existsAction&&!"null".equals(deleteBefore)&& deleteBefore.length() >0){
            beforeRet= ApiDao.mySelfMethodRetStr(actionClass,deleteBefore,jsonStr,retJson,request);
        }else{
            beforeRet=actionImpl.deleteBefore(jsonStr,retJson,request);
        }
        if("-1".equals(beforeRet)){//自定义的方法需要返回一个字符串，返回null则抛错误信息
            return JackJSON.toJSONString(retJson);
        }
        String buildDeleteSql=String.valueOf(queryMap.get("build_delete_sql"));
        String deleteSql;
        if(existsAction&&!"null".equals(buildDeleteSql)&& buildDeleteSql.length() >0){
            deleteSql= ApiDao.mySelfMethodRetStr(actionClass,buildDeleteSql,jsonStr,retJson,request);
        }else{
            deleteSql=actionImpl.buildDeleteSql(jsonStr,retJson,request);
        }
        if("-1".equals(deleteSql)){
            return JackJSON.toJSONString(retJson);
        }
        action.jdbcTemplate.execute(deleteSql);
        JackJSONArray oldDeleteData=new JackJSONArray();
        if(pkIds.size()==1){
            String querySQL="select * from "+dsMap.get("ds_table_name")+" where "+pkFieldName+"='"+pkIds.getJackJSONObject(0).get("pkId")+"'";
            oldDeleteData= JackJSONArray.fromObject(action.jdbcTemplate.queryForList(querySQL));
        }
        action.apiService.cudLogSave("delete",ids,dsMap,request,new JackJSONArray(),oldDeleteData);
        String isLucene=String.valueOf(dsMap.get("is_lucene"));
        if(isLucene.equals("Y")){//对单条数据索引删除
            for (int i = 0; i < oldDeleteData.size(); i++) {
                String dataUUID=String.valueOf(oldDeleteData.getJackJSONObject(i).get("uuid"));
                try {//对单条数据索引删除
                    luceneIndexManager.deleteIndexByUuid(tableName,dataUUID);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String enableJob=String.valueOf(dsMap.get("enable_job"));
        String jobClassPath=String.valueOf(dsMap.get("job_class_path"));
        if(enableJob.equals("Y")&&!jobClassPath.equals("null")&&jobClassPath.length()>0){
            apiService.executeJobByMethodName(jobClassPath,dsMap,oldDeleteData.getJackJSONObject(0),"stop");
        }
        String deleteAfter=String.valueOf(queryMap.get("delete_after"));
        String afterRet;
        if(existsAction&&!"null".equals(deleteAfter)&& deleteAfter.length() >0){
            afterRet= ApiDao.mySelfMethodRetStr(actionClass,deleteAfter,jsonStr,retJson,request);
        }else{
            afterRet=actionImpl.deleteAfter(jsonStr,retJson,request);
        }
        if("-1".equals(afterRet)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        return JackJSON.toJSONString(retJson);
    }

    public String showAllForTree(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String treeCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"treeCode");
        if("-1".equals(treeCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> returnMap = action.apiService.getAllTree(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }

    public String showAllForOutDict(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String oDictCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"oDictCode");
        if("-1".equals(oDictCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> returnMap = action.apiService.getAllOutDict(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }

    public String myAction(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.executeMyActionMethod(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);
    }

    public String login(String jsonStr, HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        JackJSONObject jsonObject=JackJSONObject.fromObject(jsonStr);
        jsonObject.put("methodName","login");//固定login方法
        jsonStr =JackJSON.toJSONString(jsonObject);
        Map<String,Object> returnMap=action.apiService.executeMyActionMethod(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String exportExcelData(String jsonStr,HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        String sql;
        String buildExcelSql=String.valueOf(queryMap.get("build_excel_sql"));
        String buildSql = String.valueOf(queryMap.get("build_sql"));
        if(existsAction&&!"null".equals(buildExcelSql)&& buildExcelSql.length() >0){
            sql = ApiDao.mySelfMethodRetStr(actionClass,buildExcelSql,jsonStr,retJson,request);
        } else if (existsAction && !"null".equals(buildSql) && buildSql.length() > 0){
            sql = ApiDao.mySelfMethodRetStr(actionClass, buildSql, jsonStr, retJson, request);
        }else{
            sql = actionImpl.buildExcelSql(jsonStr,retJson,request);
        }
        if("-1".equals(sql)){
            return JackJSON.toJSONString(retJson);
        }
        String powerSql;
        String buildPowerSql = String.valueOf(queryMap.get("build_power_sql"));
        if (existsAction && !"null".equals(buildPowerSql) && buildPowerSql.length() > 0) {
            powerSql = ApiDao.mySelfMethodRetStr(actionClass, buildPowerSql, jsonStr, retJson, request);
        } else {
            powerSql = actionImpl.buildPowerSql(jsonStr, retJson, request);
        }
        if ("-1".equals(powerSql)) {
            return JackJSON.toJSONString(retJson);
        }
        sql = sql + powerSql;
        String orderBy=action.apiService.getOrderBySql(jsonStr,retJson);
        if(orderBy=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        List<Map<String, Object>> dataList = new ArrayList<>();
        if(!sql.equals("")){
            dataList = jdbcTemplate.queryForList(sql);
        }
        dataList=JackJSON.parseObject(ApiDao.nullToStrFilter(dataList), ArrayList.class);
        JtPage jtPage=new JtPage();
        jtPage.setContent(dataList);
        jtPage = actionImpl.showAllDesensitizeExportExcel(jsonStr,retJson,request,jtPage);
        if(jtPage==null){
            return JackJSON.toJSONString(retJson);
        }
        String showAllAfterExportExcel=String.valueOf(queryMap.get("show_all_after_export_excel"));
        if(existsAction&&!"null".equals(showAllAfterExportExcel)&& showAllAfterExportExcel.length() >0){
            jtPage = ApiDao.mySelfMethodRetJtPage(actionClass,showAllAfterExportExcel,jsonStr,retJson,request,jtPage);
        }else{
            jtPage = actionImpl.showAllAfterExportExcel(jsonStr,retJson,request,jtPage);
        }
        if(jtPage==null){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.exportExcelDataByXlsx(jsonStr,retJson,jtPage);
        if(returnMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String exportWordData(String jsonStr, HttpServletRequest request){
        User user=action.userService.getUser(request);
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        /**
         * 如果不是使用默认的主键当查询条件，请注意前端传过来的查询条件要唯一的。
         */
        String pkFieldId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkFieldId"));
        if("null".equals(pkFieldId)||pkFieldId.length()==0){
            pkFieldId=String.valueOf(dsMap.get("ds_pk_id"));
        }
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        String dsTableName=apiService.getDsTableName(dsMap,queryMap,user);
        String pkId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkId"));
        Map<String,Object> returnMap;
        if(tableName.equals(dsTableName)){
            returnMap = action.baseService.findOne(tableName, pkId,pkFieldId);
        }else{
            returnMap = action.jdbcTemplate.queryForMap("select * from "+dsTableName+" _khc where _khc."+pkFieldId+"='"+pkId+"'");
        }
        returnMap=JackJSON.parseObject(ApiDao.nullToStrFilter(returnMap), HashMap.class);
        if(returnMap.size()>0){
            apiService.valueToShowValue(jsonStr,request,returnMap);
            String findOneAfterExportWord=String.valueOf(queryMap.get("find_one_after_export_word"));
            if(existsAction&&!"null".equals(findOneAfterExportWord)&& findOneAfterExportWord.length() >0){
                returnMap = ApiDao.mySelfMethodRetMap(actionClass,findOneAfterExportWord,jsonStr,retJson,request,returnMap);
            }else{
                returnMap = actionImpl.findOneAfterExportWord(jsonStr,retJson,request,returnMap);
            }
            if(returnMap.size()==0){
                return JackJSON.toJSONString(retJson);
            }
        }
        returnMap = apiService.createWord(jsonStr,retJson,request,returnMap);
        if(returnMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    //单条导出
    public String exportExcelOneData(String jsonStr, HttpServletRequest request){
        User user=action.userService.getUser(request);
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        /**
         * 如果不是使用默认的主键当查询条件，请注意前端传过来的查询条件要唯一的。
         */
        String pkFieldId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkFieldId"));
        if("null".equals(pkFieldId)||pkFieldId.length()==0){
            pkFieldId=String.valueOf(dsMap.get("ds_pk_id"));
        }
        String tableName=String.valueOf(dsMap.get("ds_table_name"));
        String dsTableName=apiService.getDsTableName(dsMap,queryMap,user);
        String pkId=String.valueOf(JackJSONObject.fromObject(jsonStr).get("pkId"));
        Map<String,Object> returnMap;
        if(tableName.equals(dsTableName)){
            returnMap = action.baseService.findOne(tableName, pkId,pkFieldId);
        }else{
            returnMap = action.jdbcTemplate.queryForMap("select * from "+dsTableName+" _khc where _khc."+pkFieldId+"='"+pkId+"'");
        }
        returnMap=JackJSON.parseObject(ApiDao.nullToStrFilter(returnMap), HashMap.class);
        returnMap=apiService.valueRewrite(jsonStr,request,returnMap);
        if(returnMap.size()>0){
            apiService.valueToShowValue(jsonStr,request,returnMap);
            String findOneAfterExportWord=String.valueOf(queryMap.get("find_one_after_export_excel"));
            if(existsAction&&!"null".equals(findOneAfterExportWord)&& findOneAfterExportWord.length() >0){
                returnMap = ApiDao.mySelfMethodRetMap(actionClass,findOneAfterExportWord,jsonStr,retJson,request,returnMap);
            }else{
                returnMap = actionImpl.findOneAfterExportExcel(jsonStr,retJson,request,returnMap);
            }
            if(returnMap.size()==0){
                return JackJSON.toJSONString(retJson);
            }
        }
        returnMap = apiService.createExcel(jsonStr,retJson,request,returnMap);
        if(returnMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String exportExcelTemplate(String jsonStr,HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.exportExcelTemplate(action.apiService.getQueryInfo(jsonStr),queryMap);
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    //旧版导入是上传附件并解析数据然后导入库
    public String importExcelData(HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(retJson,request);
        String queryCode=action.apiService.getRequestValueByKey(request,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.importExcelData(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    //新版导入是先上传附件拿到附件信息再请求此接口传附件信息然后根据文件信息找到文件再解析数据导入库
    public String importExcelDataNew(String jsonStr, HttpServletRequest request){

        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        String fileArray=action.apiService.getJsonValueByKey(jsonStr,retJson,"fileArray");
        if("-1".equals(fileArray)){
            return JackJSON.toJSONString(retJson);
        }
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        Map<String,Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        String importExcelDataBefore=String.valueOf(queryMap.get("import_excel_data_before"));
        Map<String,Object> returnMap;
        if(existsAction&&!"null".equals(importExcelDataBefore)&& importExcelDataBefore.length() >0){
            returnMap = ApiDao.mySelfMethodRetMap(actionClass,importExcelDataBefore,jsonStr,retJson,request);
        }else{
            returnMap = actionImpl.importExcelDataBefore(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String importJsonData(String jsonStr, HttpServletRequest request){

        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        String importJsonStr=action.apiService.getJsonValueByKey(jsonStr,retJson,"importJsonStr");
        if("-1".equals(importJsonStr)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.importJsonData(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String updateJsonData(String jsonStr, HttpServletRequest request){

        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        String queryCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"queryCode");
        if("-1".equals(queryCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> queryMap=action.apiService.getQuery(uuid,queryCode,retJson);
        if(queryMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        String importJsonStr=action.apiService.getJsonValueByKey(jsonStr,retJson,"updateJsonStr");
        if("-1".equals(importJsonStr)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.updateJsonData(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
    public String upload(HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        Map<String,Object> returnMap;
        String uploadFileToType=request.getParameter("upload_file_to_type");
        if(uploadFileToType==null||uploadFileToType.equals("")||uploadFileToType.length()==0){
            uploadFileToType=readProperty.getDefaultUploadFileToType();

        }
        if(uploadFileToType.equals("minio")){
            returnMap=action.apiService.uploadToMinio(retJson,request);
        }else{
            returnMap=action.apiService.uploadToService(retJson,request);
        }
        if(returnMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        apiService.fileLogSave(returnMap,request);
        Map<String,Object> retMap=action.apiService.getReturnDataByCode("ok");
        retJson= JackJSONObject.fromObject(retMap);
        retJson.put("data",returnMap);
        return JackJSON.toJSONString(retJson);
    }

    /**
     * 生成PDF，对应一条数据
     */
    public String createOnePdf(String jsonStr,HttpServletRequest request){
        JackJSONObject retJson=new JackJSONObject();
        JackJSONObject dataJson=new JackJSONObject();
        String uuid=action.apiService.getDataSourceUuid(jsonStr,retJson,dataJson);
        if(uuid=="-1"){
            return JackJSON.toJSONString(retJson);
        }
        String formCode=action.apiService.getJsonValueByKey(jsonStr,retJson,"formCode");
        if("-1".equals(formCode)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> formMap=action.apiService.getForm(uuid,formCode,retJson);
        if(formMap.size()==0){
            return JackJSON.toJSONString(retJson);
        }
        Map<String, Object> dsMap=action.apiService.findDataSourceByUuid(uuid);
        DefaultActionFace actionImpl;
        Boolean existsAction=false;
        String actionClass=String.valueOf(dsMap.get("my_action_class"));
        if ("null".equals(actionClass)|| actionClass.length() == 0) {
            actionImpl=this.get(DefaultAction.class.getName());
        }else{
            actionImpl=this.get(String.valueOf(dsMap.get("my_action_class")));
            if(actionImpl==null){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("6010")));
                return JackJSON.toJSONString(retJson);
            }
            existsAction=true;
        }
        String createPdf=String.valueOf(formMap.get("create_pdf"));
        String beforeRet;
        if(existsAction&&!"null".equals(createPdf)&& createPdf.length() >0){
            beforeRet=ApiDao.mySelfMethodRetStr(actionClass,createPdf,jsonStr,retJson,request);
        }else{
            beforeRet=actionImpl.createPdf(jsonStr,retJson,request);
        }
        if("-1".equals(beforeRet)){
            return JackJSON.toJSONString(retJson);
        }
        Map<String,Object> returnMap=action.apiService.createOnePdf(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",returnMap);
        return JackJSON.toJSONString(retJson);
    }
}
