package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.service.ApiDao;
import cn.com.capcloud.api.action.service.util.TextUtil;
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.api.action.service.ApiService;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.stereotype.Component;

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

@Component
public class DataPageComponentAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiService apiService;
    private static DataPageComponentAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.apiService =this.apiService;
    }

    /**
     * 根据数据源编码查询form和query组件
     */
    public Map<String,Object> findComponentData(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> map=new HashMap<>();
        String dsCode = String.valueOf(json.get("ds_code"));
        String querySql="SELECT query_code AS 'key',query_code AS 'value' FROM tbl_data_query WHERE status=0 and page_parent_uuid=(SELECT uuid FROM tbl_data_source WHERE status=0 and ds_code='"+dsCode+"')";
        String formSql="SELECT form_code AS 'key',form_code AS 'value' FROM tbl_data_form WHERE status=0 and page_parent_uuid=(SELECT uuid FROM tbl_data_source WHERE status=0 and ds_code='"+dsCode+"')";
        this.getJdbcTemplate().queryForList(querySql);
        this.getJdbcTemplate().queryForList(formSql);
        String dataQueryStr= TextUtil.nullToStrFilter(this.getJdbcTemplate().queryForList(querySql));
        String dataFormStr= TextUtil.nullToStrFilter(this.getJdbcTemplate().queryForList(formSql));
        map.put("message","复制成功");
        map.put("queryData", JackJSON.parseObject(dataQueryStr, ArrayList.class));
        map.put("formData",JackJSON.parseObject(dataFormStr, ArrayList.class));
        return map;
    }

    @Override
    public JackJSONArray saveBefore(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        return super.saveBefore(jsonStr, retJson, request, array);
    }
}

