// public with sharing class GetDataFromETQForRepairQIS {
public with sharing class NFM402Controller {
    // SYSTEM_SOURCE_REF_NO :修理受付番号 Name
    // ETQ_UPLOAD_STATUS
    // ETQ_COMPLAINT_NUMBER : Complaint_Number__c

    public class ETQData {
        public String SYSTEM_SOURCE_REF_NO;
        public String ETQ_UPLOAD_STATUS;
        public String ETQ_COMPLAINT_NUMBER;
        public Long INTERFACE_RECORD_ID;
        public String ETQ_UPLOAD_MESSAGE;
    }

    // private static String repairName;
    // private static String qisName;

    // private static String endpoint = 'http://161.189.3.104:8088/dojtest/dojInfo/getDojInfoByRefNo';

    public NFM402Controller() {

    }


    public static void sendRequest(List<String> repairIdOrQISId){

        List<Repair__c> repList = [SELECT Id,Name,Complaint_Number__c,INTERFACE_RECORD_ID__c from Repair__c where id in :repairIdOrQISId];
        List<QIS_Report__c> qisList = [SELECT Id,Name,Complaint_Number__c,INTERFACE_RECORD_ID__c from QIS_Report__c where id in :repairIdOrQISId];
        // repairName = repList.get(0).Name;
        // qisName = qisList.get(0).Name;
        String requestJson = null;

        List<String> nameList = new List<String>();
        
        for (Repair__c rpr : repList) {
            nameList.add(rpr.Name);
        }

        for (QIS_Report__c qis : qisList) {
            nameList.add(qis.Name);
        }

        if (nameList.size() > 0) {
            requestJson = '[';
            for (String name : nameList) {
                requestJson += '\"' + name + '\",';
            }
            requestJson = requestJson.substring(0,requestJson.length() - 1);
            requestJson += ']';
        }
        
        System.debug('requestJson++'+requestJson);
        
        // HttpRequest req = new HttpRequest();
        // req.setEndpoint(endpoint);
        // req.setMethod('POST');
        // req.setHeader('Content-Type', 'application/json');
        // req.setBody(requestJson);
        // Http http = new Http();
        // HTTPResponse res = http.send(req);

        // System.debug('res.getStatusCode()'+ res.getStatusCode());
        // String responseBody = res.getBody();

        //------ update by rentongxiao 2020-08-18 Stsrt
        
        String responseBody = NFMUtil.getETQData(requestJson,NFMUtil.NFM402_ENDPOINT);
        System.debug('11111::'+responseBody);
        Integer start = responseBody.indexOf('[');
        Integer theEnd = responseBody.lastIndexOf('],');
        responseBody = responseBody.substring(start,theEnd+1);
        //------ update by rentongxiao 2020-08-18 End
        // // String responseBody = mockjson();
        System.debug('result'+responseBody);
        getResult(responseBody,repList,qisList);

    }


    public static void getResult(String responseBody,List<Repair__c> repList,List<QIS_Report__c> qisList){
        
        Map<String,Repair__c> repMap = new Map<String,Repair__c>();
        Map<String,QIS_Report__c> qisMap = new Map<String,QIS_Report__c>();

        for (Repair__c rpr : repList) {
            repMap.put(rpr.Name,rpr);
        }

        for (QIS_Report__c qis : qisList) {
            qisMap.put(qis.Name,qis);
        }
        System.debug('repMap.values() before++'+repMap);

        // GeneralData genData =  (GeneralData) JSON.deserializeStrict(responseBody, GeneralData.class);
        List<ETQData> genData =  (List<ETQData>) JSON.deserializeStrict(responseBody, List<ETQData>.class);
        System.debug('genData'+genData);
        
        for(ETQData obj : genData){
            String complaintNumber =  obj.ETQ_COMPLAINT_NUMBER;
            String eleKey = obj.SYSTEM_SOURCE_REF_NO;
            Long tempId = obj.INTERFACE_RECORD_ID;
            String status = obj.ETQ_UPLOAD_STATUS;
            String message = obj.ETQ_UPLOAD_MESSAGE;
            if (repMap.containsKey(eleKey)) {
                repMap.get(eleKey).Complaint_Number__c = complaintNumber;
                repMap.get(eleKey).INTERFACE_RECORD_ID__c = tempId;
                repMap.get(eleKey).ETQ_UPLOAD_STATUS__c = status;
                repMap.get(eleKey).ETQ_UPLOAD_MESSAGE__c = message;
            }

            if (qisMap.containsKey(eleKey)) {
                qisMap.get(eleKey).Complaint_Number__c = complaintNumber;
                qisMap.get(eleKey).INTERFACE_RECORD_ID__c = tempId;
                qisMap.get(eleKey).ETQ_UPLOAD_STATUS__c = status;
                qisMap.get(eleKey).ETQ_UPLOAD_MESSAGE__c = message;
            }
        }

        System.debug('repMap.values() after++'+repMap);

        update repMap.values();
        update qisMap.values();
    }

    // public static String mockjson(){

        // SYSTEM_SOURCE_REF_NO
        // ETQ_UPLOAD_STATUS;   
        // ETQ_COMPLAINT_NUMBER

        // String repairName = repList.get(0).Name;
        // String qisName = qisList.get(0).Name;
        // ETQData etq1 = new ETQData();
        // etq1.SYSTEM_SOURCE_REF_NO = 'repairName';
        // etq1.ETQ_UPLOAD_STATUS = '200';
        // etq1.ETQ_COMPLAINT_NUMBER = '111111';

        // ETQData etq2 = new ETQData();
        // etq2.SYSTEM_SOURCE_REF_NO = 'RS-201611-361215';
        // etq2.ETQ_UPLOAD_STATUS = '200';
        // etq2.ETQ_COMPLAINT_NUMBER = '222222';

        // ETQData etq3 = new ETQData();
        // etq3.SYSTEM_SOURCE_REF_NO = qisName;
        // etq3.ETQ_UPLOAD_STATUS = '200';
        // etq3.ETQ_COMPLAINT_NUMBER = '333333';

        // ETQData etq4 = new ETQData();
        // etq4.SYSTEM_SOURCE_REF_NO = 'OCSM-华东-153PA-07322';
        // etq4.ETQ_UPLOAD_STATUS = '200';
        // etq4.ETQ_COMPLAINT_NUMBER = '444444';


        // List<ETQData> etqDatas = new List<ETQData>();
        // etqDatas.add(etq1);
        // etqDatas.add(etq2);
        // etqDatas.add(etq3);
        // etqDatas.add(etq4);
        // [{"SYSTEM_SOURCE_REF_NO":"a0J1000000LmVQU","ETQ_UPLOAD_STATUS":"200","ETQ_COMPLAINT_NUMBER":"111111"},{"SYSTEM_SOURCE_REF_NO":"a0J1000000LmVQU","ETQ_UPLOAD_STATUS":"200","ETQ_COMPLAINT_NUMBER":"222222"}]
        // System.debug('JSON.serialize(etqDatas)++'+JSON.serialize(etqDatas));
        // return JSON.serialize(etqDatas);
    // }

}