@RestResource(urlMapping = '/SBG204/*')
global with sharing class SBG204Rest {

    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }

    global class GeData {

        public String MaterialNo;         //物料号

        public String RegisterNo;         //注册证号
        public String RegisterNoClass_New; //注册证号经营范围编号(新)
        public String RegisterNoClass_Old; //注册证号经营范围编号(旧)

        public String ValidFrom;          //注册证号效期从
        public String ValidTo;            //注册证号效期至
        public String MedPrdClass;        //医疗器械分类
        public String EffectDate;         //系统维护日

        public String Model;              //产品型号
        public String ProductName;        //产品名称(委托产品名称)
        public String PrdCompanyLicense;  //生产企业许可证号/备案凭证号
        public String PrdCompanyAddr;     //生产企业地址
        public String ClinicalCode;       //型号规格
        public String REG_Name;           // 注册人名称
        public String REG_ADD;            // 注册人住所



        public String AuthorizedCompany;  //委托方企业名称

        public String ApprovedDate;       //委托批准日期
        public String Z3PLAuthorized;    //是否3PL委托产品
        public String AuthorizedCompany_SH;
        public String ApprovedDate_SH;
        public String YXQX;

        public String Other1;
        public String Other2;
        public String Other3;

        public String MaterialGroup;      //物料组
        public String MaterialStatus;     //物料状态
        public String RegisterNoStatus;   //注册证号状态

        public String Unit;               //单位(基本单位)
        public String StroageCondition;   //储存条件
        public String TransCondition;     //运输条件
        public String Z3PLAuthorizedNo;   //3PL业务委托协议号
        public String PrdValidFrom;       //委托产品效期从
        public String PrdValidTo;         //委托产品效期至
        public String BusinessScope;      //经营范围

        public String MaterialStatusType;  // 状态 1:只更新物料状态


        public String APP_NAME_BJ;   //  北京首营审核人
        public String APP_DATE_BJ;   //  北京审核日期
        public String APP_RSLT_BJ;   //  北京首营审核结果
        public String APP_NAME_SH;   //  上海首营审核人
        public String APP_DATE_SH;   //  上海审核日期
        public String APP_RSLT_SH;   //  上海首营审核结果
        public String APP_NAME_GZ;   //  广州首营审核人
        public String APP_DATE_GZ;   //  广州审核日期
        public String APP_RSLT_GZ;   //  广州首营审核结果
        public String APP_NAME_OSH;   //  OSH首营审核人
        public String APP_DATE_OSH;   //  OSH审核日期
        public String APP_RSLT_OSH;   //  OSH首营审核结果
        public String PROD_ADD;       // 生产地址
        public String STRU_COM;      // 结构及组成
        public String ADOPT_RANGE;    // 适用范围


    }

    @HttpPost
    global static void execute() {

        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);

        if (ges == null ) {
            return;
        }

        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }

        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG204', ges.GeData);
        if (String.isBlank(rowData.Log__c) == false) {
            executefuture(rowData.Id);
        }

        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"status": "0", "Message":""}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }

    @future
    global static void executefuture(String rowData_Id) {
        main(rowData_Id);
    }

    global static void main (String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id = :rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'SBG204';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String groupNumber = rowData.MessageGroupNumber__c;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeData> GeDataList = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);

        if (GeDataList == null || GeDataList.size() == 0) {
            return;
        }

        Map<String, String> transferMap = new Map<String, String>();
        List<BatchIF_Transfer__c> transferList = [select Table__c,
                                  Column__c,
                                  External_value__c,
                                  Internal_value__c
                                  from BatchIF_Transfer__c
                                  where Dropped_Flag__c = false
                                          and (Table__c = 'Product_Register__c'
                                                  or Table__c = 'Product2')];
        for (BatchIF_Transfer__c t : transferList) {
            transferMap.put(t.Column__c + t.External_value__c, t.Internal_value__c);
        }

        Savepoint sp = Database.setSavepoint();
        try {

            List<String> productNoList = new List<String>();
            List<String> mD5KeyList = new List<String>();
            List<String> registerNoDelList = new List<String>();
            List<GeData> satisfyGeData = new List<GeData>();
            for (GeData data : GeDataList) {
                //作为产品注册证的唯一标识必须都有值才算正确的产品注册证
                if (String.isNotBlank(data.RegisterNo) && String.isNotBlank(data.ValidFrom)) {
                    mD5KeyList.add(MD5Encrypt(data.RegisterNo,data.ValidFrom));
                } else {
                    iflog.ErrorLog__c += 'RegisterNo/ValidFrom is required,This data is skipped.\n';
                    continue;
                }

                //产品Code的List
                if (String.isNotBlank(data.MaterialNo)) {
                    Pattern pattern = Pattern.compile('^[0-9]*$');  
                    Matcher isNum = pattern.matcher(data.MaterialNo); 
                    if (isNum.matches()) {
                        data.MaterialNo = data.MaterialNo.leftPad(18, '0');
                    }
                    productNoList.add(data.MaterialNo);
                } else {
                    iflog.ErrorLog__c += 'RegisterNo[ ' + data.RegisterNo + ' ] of MaterialNo is required,This data is skipped.\n';
                    continue;
                }

                // 删除注册证关系List
                if (String.isNotBlank(data.RegisterNo)) {
                    registerNoDelList.add(MD5Encrypt(data.RegisterNo,data.ValidFrom));
                }

                satisfyGeData.add(data);

            }

            Map<String, Product2> productMap = new Map<String, Product2>();
            Map<String, Product_Register__c> registerMap = new Map<String, Product_Register__c>();

            // 产品
            List<Product2> productList = [select id, ProductCode from Product2 where ProductCode = :productNoList];
            for (Product2 product : productList) {
                productMap.put(product.ProductCode, product);
            }

            List<Product_Register__c> registerList2 = [select id, Name, MD5EncryptionKey__c from Product_Register__c where MD5EncryptionKey__c = :mD5KeyList];
            for (Product_Register__c register : registerList2) {
                registerMap.put(register.MD5EncryptionKey__c, register);
            }
            List<Product_Register_Link__c> prlinkList = new List<Product_Register_Link__c>();

            prlinkList = [select id,
                          Product2__c,
                          Product2__r.ProductCode,
                          Product_Register__c,
                          Product_Register__r.Name
                          from Product_Register_Link__c
                          where Product_Register__r.MD5EncryptionKey__c in :registerNoDelList];

            Map<String, Product2> upd_productMap = new Map<String, Product2>();
            Map<String, Product_Register__c> ups_registerMap = new Map<String, Product_Register__c>();
            Map<String, Product_Register_Link__c> ins_linkMap = new Map<String, Product_Register_Link__c>();


            for (GeData data : satisfyGeData) {

                Product2 prd = new Product2();
                Product_Register__c pr = new Product_Register__c();


                if (String.isNotBlank(data.MaterialNo)) {

                    if (productMap.containsKey(data.MaterialNo)) {

                        prd = productMap.get(data.MaterialNo);
                        prd.Product_CCode__c = data.Model;
                        String prdCompanyAddr = data.REG_Name;

                        if (String.isNotBlank(prdCompanyAddr)) {
                            if (prdCompanyAddr.indexOf(';') != -1) {
                                String splitPrdCompanyAddr =  prdCompanyAddr.split(';')[0];
                                prd.Factory__c = splitPrdCompanyAddr.indexOf(':') == -1 ?  splitPrdCompanyAddr : splitPrdCompanyAddr.split(':')[1];
                            } else {
                                prd.Factory__c = prdCompanyAddr;
                            }
                        } else {
                            prd.Factory__c = prdCompanyAddr;
                        }
                    } else {

                        iflog.ErrorLog__c += 'OTCode[' + data.MaterialNo + ']不存在\n';
                        continue;
                    }

                    upd_productMap.put(data.MaterialNo, prd);

                    logstr += 'Product:' + data.MaterialNo;
                }
                if (String.isNotBlank(data.RegisterNo) ) {
                    String mD5key = MD5Encrypt(data.RegisterNo,data.ValidFrom);
                    if (registerMap.containsKey(mD5key)) {
                        pr = registerMap.get(mD5key);
                    } 
                    pr.MD5EncryptionKey__c    = mD5key;                  //产品注册证号的唯一标识
                    pr.Name                   = data.RegisterNo;         //注册证号
                    pr.IsFromSPO__c           = true;
                    pr.RegisterNoClass_New__c = data.RegisterNoClass_New; //注册证号分类编号(新)
                    pr.RegisterNoClass_Old__c = data.RegisterNoClass_Old; //注册证号分类编号(旧)
                    pr.ValidFrom__c           = NFMUtil.parseStr2Date(data.ValidFrom);          //注册证号效期从
                    pr.ValidTo__c             = NFMUtil.parseStr2Date(data.ValidTo);            //注册证号效期至
                    pr.Stelsedag__c           = NFMUtil.parseStr2Date(data.EffectDate);         //系统维护日
                    pr.MedPrdClass__c         = data.MedPrdClass;        //医疗器械分类
                    pr.PrdCompanyLicense__c   = data.PrdCompanyLicense;  //生产企业许可证号/备案凭证号
                    pr.PrdCompanyAddr__c      = data.PrdCompanyAddr;     //生产企业地址
                    pr.ClinicalProductCode__c = data.ClinicalCode;       //型号规格
                    pr.RegistrantName__c = data.REG_Name ;               // 注册人名称
                    pr.RegistrantResidence__c = data.REG_ADD ;           // 注册人住所
                    ups_registerMap.put(data.RegisterNo, pr); 
                    logstr += '  Register:' + data.RegisterNo + '\n';
                }
            }
            
            if (upd_productMap.keySet().size() > 0) update upd_productMap.values();
            if (ups_registerMap.keySet().size() > 0) upsert ups_registerMap.values();

            // 删除注册证号下所有物料
            List<Product_Register_Link__c> delList = new List<Product_Register_Link__c>();
            for (Product_Register_Link__c prlc : prlinkList) {

                delList.add(prlc);

            }

            if (delList.size() > 0 )    delete delList;


            // 取得所有注册证ID
            registerList2 = [select id, Name, MD5EncryptionKey__c from Product_Register__c where MD5EncryptionKey__c = :mD5KeyList];
            for (Product_Register__c register : registerList2) {
                registerMap.put(register.MD5EncryptionKey__c, register);
            }

            // 产品和注册证关系表
            for (GeData data : satisfyGeData) {
                if (!productMap.containsKey(data.MaterialNo)) {
                    // 产品不存在的场合
                    continue;
                }

                String prlink = data.MaterialNo + '_' + data.RegisterNo;
                Product_Register_Link__c prl = new Product_Register_Link__c();
                prl.Product2__c = productMap.get(data.MaterialNo).id;
                prl.Product_Register__c = registerMap.get(MD5Encrypt(data.RegisterNo,data.ValidFrom)).id;
                prl.If_Delete__c = false;
                ins_linkMap.put(prlink, prl);

            }

            if (ins_linkMap.keySet().size() > 0) insert ins_linkMap.values();

            logstr += '\nend';
            rowData.retry_cnt__c = 0;

        } catch (Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'SBG204_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'SBG204_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt) {
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt) {
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c + '错误次数已经超过自动收信设定的最大次数，请手动收信';
            }
        }
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
    }
    //将"产品注册证号"与"注册证号效期从"进行MD5加密作为产品注册证的唯一标识
    public static String MD5Encrypt(String RegisterNo,String ValidFrom){
        if (String.isBlank(RegisterNo) || String.isBlank(ValidFrom)) {
            return null;
        }
        String result = '';
        //注册证号效期从
        Date  startOfDay = NFMUtil.parseStr2Date(ValidFrom);
        //时间戳
        Long timestamp = Datetime.newInstanceGmt(startOfDay.year(), startOfDay.Month(),startOfDay.Day(), 0, 0, 0).getTime();
        //MD5转化
        String mD5EncryptionKey = EncodingUtil.convertToHex(Crypto.generateDigest('MD5', Blob.valueOf(RegisterNo+timestamp))).toUpperCase();
        result = mD5EncryptionKey;
        return result;
    }

}