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

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.JackJSONObject;
import cn.com.capcloud.ccui.core.common.utils.UUIDUtils;
import cn.com.capcloud.core.cache.BaseConfigStaticCache;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;

public class ApiDao {

    //PDF需要展示的字段
    private static final String SYSTEM_SETTINGS_DS_FORM_PDF_SHOWTYPE="system.settings.ds.form.pdf.show_type";
    /**
     * id 优先级最高，其次是code
     */
    public static Map<String, Object> findNoSessionInfo(String uuid,String nosessionCcode, JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_data_nosession WHERE STATUS =0 ";
        if(uuid!=null){
            sql+=" AND uuid = '"+uuid+"'";
        }else if(nosessionCcode!=null){
            sql+=" AND nosession_code = '"+nosessionCcode+"'";
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if(list.size()==0){
            return null;
        }else{
            return list.get(0);
        }
    }

    /**
     * 生成PDF默认模板HTML
     */
    public static String createOnePdfDefaultHtml(List<Map<String,Object>> fieldList, List<Map<String,Object>> dataList, JdbcTemplate jdbcTemplate) {
        List<String> showTypeArray= Arrays.asList(String.valueOf(BaseConfigStaticCache.get(SYSTEM_SETTINGS_DS_FORM_PDF_SHOWTYPE)).split(","));
        String html="";
        for (int i = 0; i < dataList.size(); i++) {
            for (int j = 0; j < fieldList.size(); j++) {
                if (showTypeArray.contains(String.valueOf(fieldList.get(j).get("show_type")))) {
                    String name=String.valueOf(fieldList.get(j).get("name"));
                    html+="                    <tr>\n";
                    html+="                        <td class=\"table_td_css\">"+fieldList.get(j).get("label_name")+"</td>\n";
                    html+="                        <td>"+dataList.get(i).get(name)+"</td>\n";
                    html+="                    </tr>\n";
                }
            }
        }
        return html;
    }
    public static List<Map<String, Object>> getMenuByParentId(long userId,String parentId,JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_system_menu WHERE STATUS=0 AND menu_status=0 AND menu_parent_id='"+parentId+"' " +
                " AND menu_id IN (SELECT DISTINCT menu_id FROM tbl_system_roleuser ru,tbl_system_menurole mu WHERE ru.role_id=mu.role_id AND ru.user_id="+userId+") " +
                " ORDER BY menu_seq ASC ";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("menu_child",getMenuByParentId(userId,list.get(i).get("menu_id")+"",jdbcTemplate));
        }
        return list;
    }
    public static List<Map<String, Object>> getMenuByParentIdAndRoleId(long roleId,String parentId,JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_system_menu WHERE STATUS=0 AND menu_status=0 AND menu_parent_id='"+parentId+"' " +
                " AND menu_id IN (SELECT DISTINCT menu_id FROM tbl_system_roleuser ru,tbl_system_menurole mu WHERE ru.role_id=mu.role_id AND ru.role_id="+roleId+") " +
                " ORDER BY menu_seq ASC ";
        List<Map<String, Object>> list=jdbcTemplate.queryForList(sql);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).put("menu_child",getMenuByParentIdAndRoleId(roleId,list.get(i).get("menu_id")+"",jdbcTemplate));
        }
        return list;
    }
    /**
     * @param parentUUID 组件id
     */
    public static List<Map<String, Object>> getButtonByParentUUID(String parentUUID,String whereSql, JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_data_button WHERE STATUS=0 AND button_status=0 AND page_parent_uuid='"+parentUUID+"' "+whereSql+"  ORDER BY button_sort ASC ";
        return JackJSON.parseObject(TextUtil.nullToStrFilter(jdbcTemplate.queryForList(sql)), ArrayList.class);
    }

    public static Map<String, Object> getReturnDataByCode(String code, JdbcTemplate jdbcTemplate) {
        String sql="SELECT * FROM tbl_data_api_return WHERE STATUS =0 AND api_return_code = '"+code+"'";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        Map<String, Object> retMap = new HashMap<>();
        if(list.size()==0){
            retMap=new HashMap<>();
            retMap.put("status",code);
            retMap.put("message","no message");
        }else{
            Map<String, Object> map=list.get(0);
            String apiReturnJson=String.valueOf(map.get("api_return_json"));
            if(!"".equals(apiReturnJson)&&!"{}".equals(apiReturnJson)){
                retMap.put("other", JackJSONObject.fromObject(apiReturnJson));
            }
            retMap.put("status",map.get("api_return_code"));
            retMap.put("message",map.get("api_return_message"));
        }
        retMap.put("uuid", UUIDUtils.randomUuidWithoutHyphen());
        return retMap;
    }

}
