package com.weaver.seconddev.njwebservice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.njwebservice.entity.BillingApplicationResItem;
import com.weaver.seconddev.njwebservice.util.DocUtil;
import com.weaver.verupgrade.workflow.workflow.WorkflowAllComInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("demoService")
public class DemoService {
    private final static Logger log = LoggerFactory.getLogger(DemoService.class);

    private static final String HOST = "http://10.6.24.64:20600/papi/openapi";

    @Autowired
    private DocUtil docUtil;

//    @Autowired
//    private DepartmentComInfo departmentComInfo;

    @Autowired
    private WorkflowAllComInfo workflowAllComInfo;



    public String demo(String[] detailTables){

        String detailTablesStr = String.join(",", detailTables);
//         //实现自定义webservice接口逻辑

        //String tips = SecondUtil.validationParam("OAAdmin", "123456", "100003460000005676", "2022050353");

        //String formId = workflowAllComInfo.getFormId("100003460000005676");

        String workcodeId = "100003460000005676";

        // 获取明细表表名
//        List<Map<String,Object>> list = new ArrayList<>();
//        RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
//        String poolname = CONN_TYPE.workflow.getType();
//
//        String formSql = "";
//
//        String sql = "SELECT * FROM form_table WHERE \n" +
//                " form_id IN ( SELECT id FROM sub_form WHERE form_id IN ( SELECT form_id FROM e10_core_business.dbo.form_table WHERE table_name = 'formtable_main_3540'))";
//        rs.executeSql(sql,poolname);
//        while (rs.next()){
//            Map<String,Object> map = new HashMap<>();
//            map.put("id", Util.getLongValue(rs.getString("id")));
//            map.put("table_type", Util.getIntValue(rs.getString("table_type")));
//            map.put("form_name", Util.null2String(rs.getString("form_name")));
//            list.add(map);
//
//        }


//        String token = "";
//        try {
//            token = AccessTokenUtil.getToken();
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//
////        log.error("base64:"+base64);
////
//        int base64Length = base64.length();
//
//        // 计算原始文件大小
//        int originalSize = (base64Length * 3) / 4;
//
//        // 如果 Base64 字符串末尾有填充字符 '='，需要减去对应的字节数
//        if (base64.endsWith("==")) {
//            originalSize -= 2; // 每两个 '=' 表示填充了 1 个字节
//        } else if (base64.endsWith("=")) {
//            originalSize -= 1; // 每 1 个 '=' 表示填充了 1 个字节
//        }
//
////
//        JSONObject params = new JSONObject();
//        params.put("userid", 100001700000063120L);
//        params.put("base64", base64);
//        params.put("name","测试.png");
//        params.put("size",String.valueOf(originalSize));
//        params.put("lastModified",System.currentTimeMillis());
////
////
////
//        String url = HOST + "/api/file/v2/common/upload/base64?access_token=" + token;
//        HttpRequest request = HttpRequest.post(url)
//                .header(Header.CONTENT_TYPE, ContentType.MULTIPART.toString())
//                .form("userid", 100001700000063120L)  // 添加表单字段
//                .form("base64", base64)
//                .form("name", "测试.png")  // 添加表单字段
//                .form("size", String.valueOf(originalSize))
//                .form("lastModified", System.currentTimeMillis());
//
//        // 发送请求并获取响应
//        HttpResponse response = request.execute();
//
//        // 获取响应状态码
//        int statusCode = response.getStatus();
//
//        // 获取响应体
//        String body = response.body();

//        log.error("base64:"+base64);
//        String docId = docUtil.createdocByBase642("测试.png", base64, "", "100001700000063120", "");


//        RestTemplate restTemplate = new RestTemplate();

//        try {
//            HttpHeaders headers = new HttpHeaders();
//            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//
//            // Create the parameters and add the file
//            MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
//            param.add("userid", "811711878160941057");
//            param.add("name", "测试.png");
//            param.add("size", String.valueOf(originalSize));
//            param.add("module", "workflow");
//            //param.add("access_token", token);
//            param.add("base64", base64);
//            // Create the request entity using the parameters and headers
//            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(param, headers);
//            // Send the request
//            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
//            return response.toString();
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        return detailTablesStr;
     }

    public static void main(String[] args) {

        BillingApplicationResItem build = BillingApplicationResItem.builder()
                .billNo("1")
                .dueDate("2024-10-01")
                .money("134")
                .receiveAccountName("3432")
                .build();

        BillingApplicationResItem build1 = BillingApplicationResItem.builder()
                .billNo("1")
                .dueDate("2024-10-02")
                .money("1344")
                .receiveAccountName("3432")
                .build();

        BillingApplicationResItem[] param = {
                build,
                build1
        };

        String detailTables = "";
        detailTables = "[[";
        for(int i=0;i<param.length;i++){
            detailTables += JSON.toJSONString(param[i]);
            detailTables += ",";
        }
        detailTables = detailTables.substring(0, detailTables.length()-1)+"]]";

        System.out.println(detailTables);

        JSONArray jsonArrays = JSON.parseArray(detailTables);

        // 获取第一个 JSONArray
        JSONArray innerArray = jsonArrays.getJSONArray(0);

        // 创建 String[] 数组
        String[] resultArray = new String[innerArray.size()];

        for (int i = 0; i < innerArray.size(); i++) {
            JSONObject jsonObject = innerArray.getJSONObject(i);
            resultArray[i] = jsonObject.toJSONString();
        }

        String detailTablesStr = String.join(",", detailTables);

        System.out.println("新"+detailTablesStr);

//        String detailTablesStr = "[[" + String.join(",", resultArray) + "]]";
//        System.out.println("转换为 JSON 字符串:"+detailTablesStr);
//
//
//        JSONArray outerArray = JSON.parseArray(detailTablesStr);
//
//        // 遍历外部 JSONArray
//        for (int i = 0; i < outerArray.size(); i++) {
//            JSONArray t = outerArray.getJSONArray(i);
//            System.out.println(t);
//        }




    }
}