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

import cn.com.capcloud.api.action.service.util.DictUtil;
import cn.com.capcloud.ccui.core.common.utils.StringUtil;
import cn.com.capcloud.core.noentitydb.JtPage;
import cn.com.capcloud.core.system.user.User;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.dao.DataAccessException;
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.*;

@Service
public class ShowValueService {

    @Autowired
    private UserService userService;
    @Autowired
    private DataSourceService dsService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public Map<String,Object> valueToShowValue(String jsonStr, HttpServletRequest request, Map<String,Object> map){
        List<Map<String,Object>> fieldList=dsService.getQueryInfo(jsonStr);
        List<Map<String,Object>> dataList=new ArrayList<>();
        dataList.add(map);
        dataList= DictUtil.changeFormDict(fieldList,dataList,jdbcTemplate);
        return dataList.get(0);
    }

    public JtPage valueRewrite(String jsonStr, HttpServletRequest request, JtPage jtPage){
        if(jtPage.getContent().size()==0){
            return jtPage;
        }
        List<Map<String,Object>> fieldList=dsService.getQueryInfo(jsonStr);
        Map<String,Object> reWriteConfigMap=new HashMap<>();
        for (int i = 0; i < fieldList.size(); i++) {//这个循环是提前将dict字典查询出来，避免每次循环都查询数据库
            String isQueryAfterRewriteValue=String.valueOf(fieldList.get(i).get("is_query_after_rewrite_value"));
            String name=String.valueOf(fieldList.get(i).get("name"));
            if(!"null".equals(isQueryAfterRewriteValue)&&isQueryAfterRewriteValue.length()>0&&isQueryAfterRewriteValue.equals("Y")){
                reWriteConfigMap.put(name,fieldList.get(i));
            }
        }
        for (String key : reWriteConfigMap.keySet()) {
            for (int i = 0; i < jtPage.getContent().size(); i++) {
                Map<String,Object> content=jtPage.getContent().get(i);
                Map<String,Object> fieldInfo= (Map<String, Object>) reWriteConfigMap.get(key);
                String queryAfterRewriteSql=String.valueOf(fieldInfo.get("query_after_rewrite_sql"));
                String queryAfterRewriteSqlReturnType=String.valueOf(fieldInfo.get("query_after_rewrite_sql_return_type"));
                queryAfterRewriteSql=queryAfterRewriteSql.replaceAll("&nbsp;"," ");
                queryAfterRewriteSql=queryAfterRewriteSql.replaceAll("&quot;","'");
                for (String fieldName : content.keySet()) {
                    String oldStr="\\$\\{"+fieldName+"\\}";
                    String newStr=content.get(fieldName)+"";
                    queryAfterRewriteSql=queryAfterRewriteSql.replaceAll(oldStr,newStr);
                }
                if(queryAfterRewriteSqlReturnType.equals("1")){
                    try {
                        jtPage.getContent().get(i).put(key,jdbcTemplate.queryForObject(queryAfterRewriteSql,String.class));
                    } catch (DataAccessException e) {
                        jtPage.getContent().get(i).put(key,"");
                    }
                }
                if(queryAfterRewriteSqlReturnType.equals("2")){
                    jtPage.getContent().get(i).put(key,jdbcTemplate.queryForMap(queryAfterRewriteSql));
                }
                if(queryAfterRewriteSqlReturnType.equals("3")){
                    jtPage.getContent().get(i).put(key,jdbcTemplate.queryForList(queryAfterRewriteSql));
                }
            }
        }
        return jtPage;
    }
    public Map<String,Object> valueRewrite(String jsonStr, HttpServletRequest request, Map<String,Object> content){
        List<Map<String,Object>> fieldList=dsService.getQueryInfo(jsonStr);
        Map<String,Object> reWriteConfigMap=new HashMap<>();
        for (int i = 0; i < fieldList.size(); i++) {//这个循环是提前将dict字典查询出来，避免每次循环都查询数据库
            String isQueryAfterRewriteValue=String.valueOf(fieldList.get(i).get("is_query_after_rewrite_value"));
            String name=String.valueOf(fieldList.get(i).get("name"));
            if(StringUtil.isNotEmpty(isQueryAfterRewriteValue)&&isQueryAfterRewriteValue.equals("Y")){
                reWriteConfigMap.put(name,fieldList.get(i));
            }
        }
        for (String key : reWriteConfigMap.keySet()) {
            Map<String,Object> fieldInfo= (Map<String, Object>) reWriteConfigMap.get(key);
            String queryAfterRewriteSql=String.valueOf(fieldInfo.get("query_after_rewrite_sql"));
            String queryAfterRewriteSqlReturnType=String.valueOf(fieldInfo.get("query_after_rewrite_sql_return_type"));
            queryAfterRewriteSql=queryAfterRewriteSql.replaceAll("&nbsp;"," ");
            queryAfterRewriteSql=queryAfterRewriteSql.replaceAll("&quot;","'");
            for (String fieldName : content.keySet()) {
                String oldStr="\\$\\{"+fieldName+"\\}";
                String newStr=content.get(fieldName)+"";
                queryAfterRewriteSql=queryAfterRewriteSql.replaceAll(oldStr,newStr);
            }
            if(queryAfterRewriteSqlReturnType.equals("1")){
                content.put(key,jdbcTemplate.queryForObject(queryAfterRewriteSql,String.class));
            }
            if(queryAfterRewriteSqlReturnType.equals("2")){
                content.put(key,jdbcTemplate.queryForMap(queryAfterRewriteSql));
            }
            if(queryAfterRewriteSqlReturnType.equals("3")){
                content.put(key,jdbcTemplate.queryForList(queryAfterRewriteSql));
            }
        }
        return content;
    }

    // 根据角色过滤数据
    public List<Map<String, Object>> filterDataByRole(List<Map<String, Object>> dataBefore,HttpServletRequest request){
        return filterDataByRole(dataBefore,request,"view_role_id");
    }
    public List<Map<String, Object>> filterDataByRole(List<Map<String, Object>> dataBefore,HttpServletRequest request,String roleFieldName){
        List<Map<String, Object>> dataAfter=new ArrayList<>();
        if(dataBefore.size()>0){
            User user=userService.getUser(request);
            for (int i = 0; i < dataBefore.size(); i++) {
                String roleStr=String.valueOf(dataBefore.get(i).get(roleFieldName));
                if(StringUtil.isEmpty(roleStr)){ // 空则不控制权限
                    dataAfter.add(dataBefore.get(i));
                }else{
                    String[] roleArr=roleStr.split(",");
                    for (int j = 0; j < roleArr.length; j++) {
                        if(!"".equals(roleArr[j])&&userService.isExistsRole(roleArr[j], user.getId())) {
                            dataAfter.add(dataBefore.get(i));
                            break;
                        }
                    }
                }
            }
        }
        return dataAfter;
    }

}

