public without sharing class NFM207Handler extends Oly_TriggerHandler {
    private Map<Id, QIS_Report__c> newMap;
    private Map<Id, QIS_Report__c> oldMap;
    private List<QIS_Report__c> newList;
    private List<QIS_Report__c> oldList;
    private List<String> qisIdList;

    public NFM207Handler() {
        this.newMap = (Map<Id, QIS_Report__c>) Trigger.newMap;
        this.oldMap = (Map<Id, QIS_Report__c>) Trigger.oldMap;
        this.newList = (List<QIS_Report__c>) Trigger.new;
        this.oldList = (List<QIS_Report__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        updateAssistantNo();
        //setUpProduct();
    }
    protected override void beforeUpdate() {
        updateAssistantNo();
    }
    protected override void afterInsert() {
        afterInsertQIS_Report();
    }
    protected override void afterUpdate() {
        updateQIS_Report();
    }


    //2019年4月2日 start 废弃不用了
    //从保有设备新建QIS，QIS来源为“保有设备”且主机/耗材选择为"耗材"时，且耗材栏为空时设置耗材数据
    //private void setUpProduct() {
    //    list<id> assetIdlist = new List<id>();
    //    for (QIS_Report__c qisReport : newList) {
    //        if (qisReport.Generation_Source__c != null &&
    //                qisReport.Generation_Source__c.equals('保有设备') &&
    //                qisReport.capital_or_consumable__c != null &&
    //                qisReport.capital_or_consumable__c.equals('耗材') &&
    //                qisreport.nonyushohin__c != null
    //           ) {
    //            assetIdlist.add(qisreport.nonyushohin__c);
    //        }
    //    }
    //    Map<id, asset> assetMap = new Map<id, Asset>();
    //    if (assetIdlist.size() > 0) {
    //        assetMap = new Map<id, Asset>(
    //            [select id, Product2Id, Asset_Owner__c, SerialNumber, Order_No__c,
    //             InstallDate, Posting_Date__c, Asset_day__c
    //             from asset
    //             where id in: assetIdlist]
    //        );
    //    }
    //    for (QIS_Report__c qisReport : newList) {
    //        if (qisReport.Generation_Source__c != null &&
    //                qisReport.Generation_Source__c.equals('保有设备') &&
    //                qisReport.capital_or_consumable__c != null &&
    //                qisReport.capital_or_consumable__c.equals('耗材') &&
    //                qisreport.nonyushohin__c != null
    //           ) {
    //            asset tempAsset = assetMap.get(qisreport.nonyushohin__c);
    //            if (tempAsset == null) {
    //                tempAsset = new asset();
    //            }
    //            if (qisReport.consumable__c == null) {
    //                qisReport.consumable__c = tempAsset.Product2Id;
    //            }
    //            if (qisReport.Lots_No__c == null) {
    //                qisReport.Lots_No__c = tempAsset.SerialNumber;
    //            }

    //            if (qisReport.sell__c == null) {
    //                string tempSell = null;
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('病院資産')){
    //                    tempSell = '购买';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('Olympus')){
    //                    tempSell = '备品';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('リース会社')){
    //                    tempSell = '其他';
    //                }
    //                if(tempAsset.Asset_Owner__c !=null
    //                    && tempAsset.Asset_Owner__c.equals('经销商资产')){
    //                    tempSell = '购买';
    //                }
                    
    //                qisReport.sell__c = tempSell;
    //            }
    //            if (qisReport.Instration_date_consumable__c == null) {
    //                Date tempdate = null;
    //                if (qisReport.InstallDate_text__c != null) {
    //                    tempdate = qisReport.InstallDate_text__c;
    //                }
    //                else if (tempAsset.InstallDate != null) {
    //                    tempdate = tempAsset.InstallDate;
    //                }
    //                else if (tempAsset.Posting_Date__c != null) {
    //                    tempdate = tempAsset.Posting_Date__c;
    //                }
    //                else if (tempAsset.Asset_day__c != null) {
    //                    tempdate = tempAsset.Asset_day__c;
    //                }
    //                qisReport.Instration_date_consumable__c =  tempdate;
    //            }

    //            if (qisReport.contract_number_ET__c == null) {
    //                qisReport.contract_number_ET__c = tempAsset.Order_No__c;
    //            }
            
    //        }
            
    //    }
    //}
    //2019年4月2日 end
    private void updateAssistantNo() {

        List<String> accIdList = new List<String>();
        for (QIS_Report__c qis : newList) {
            if (Trigger.isInsert
                    ||  (Trigger.isUpdate && (qis.Department_Class__c != oldMap.get(qis.Id).Department_Class__c))
                    ||  (Trigger.isUpdate && (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ))) {
                // 新建或者战略科室发生变化发生时,重新获取助理,对于老数据当我们点击“发送QIS到SPO”是会触发。
                accIdList.add(qis.Department_Class__c);
            }
        }

        Map<Id, Account> accountMap = new Map<Id, Account>();
        Map<String, OCM_Management_Province__c> provinceMap = new Map<String, OCM_Management_Province__c>();

        if (accIdList.size() > 0) {
            List<Account> accList = [select id,
                                     OCM_Management_Province_HP__c,
                                     OCM_man_province_HP__c,
                                     Department_Class_Label__c
                                     from Account
                                     where id = :accIdList];
            for (Account acc : accList) {
                accountMap.put(acc.id, acc);
            }

            List<OCM_Management_Province__c> provinceList = [select id,
                                             Name,
                                             GI_assistant__c,
                                             SP_assistant__c,
                                             Energy_assistant__c
                                             from OCM_Management_Province__c];
            for (OCM_Management_Province__c province : provinceList) {
                provinceMap.put(province.Name, province);
            }
        }

        for (QIS_Report__c qis : newList) {
            if (Trigger.isInsert ||
                    (Trigger.isUpdate && (qis.Department_Class__c != oldMap.get(qis.Id).Department_Class__c))
                    ||  (Trigger.isUpdate && (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ))) {
                String p = '';
                if (accountMap.get(qis.Department_Class__c) != null) {
                    p = accountMap.get(qis.Department_Class__c).OCM_man_province_HP__c;
                }
                if (qis.Salesdepartment__c == '7.能量') {
                    qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).Energy_assistant__c;
                } else {
                    //QIS中战略科室名的战略科室分类
                    String c = '';
                    if (accountMap.get(qis.Department_Class__c) != null) {
                        c = accountMap.get(qis.Department_Class__c).Department_Class_Label__c;
                    }
                    if (c == '消化科' || c == '呼吸科' || c == 'ET') {
                        qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).GI_assistant__c;
                    } else {
                        qis.BusinessAssistantNo__c = provinceMap.get(p) == null ? null : provinceMap.get(p).SP_assistant__c;
                    }
                }
            }
        }
    }

    private void afterInsertQIS_Report() {

        qisIdList = new List<String>();
        for (QIS_Report__c qis : newList) {
            if (qis.IsSendQIS__c == true) {
                NFM207Controller.isRunning = true;
                qisIdList.add(qis.Id);
            }
        }
        qisIdListMethod();
    }

    private void updateQIS_Report() {
        qisIdList = new List<String>();
        // CHAN-BC6BW9 LHJ 20190520 Start
        List<String> taskList = new List<String>();
        // CHAN-BC6BW9 LHJ 20190520 End

        for (QIS_Report__c qis : newList) {
            if (oldMap.get(qis.Id).get('IsSendQIS__c') != qis.IsSendQIS__c && qis.IsSendQIS__c == true ) {
                NFM207Controller.isRunning = true;
                qisIdList.add(qis.Id);
            }
            // CHAN-BC6BW9 LHJ 20190520 Start
            if (oldMap.get(qis.Id).get('QIS_Status__c') != qis.QIS_Status__c 
                    && oldMap.get(qis.Id).get('QIS_Status__c') == '草案中' ) {
                taskList.add(qis.ID);
            }
            // CHAN-BC6BW9 LHJ 20190520 End
        }
        // CHAN-BC6BW9 LHJ 20190520 Start
        if (taskList != null && taskList.size() > 0) {
            List<Task> lstTask = [select id, status from task where whatid in :taskList and status != '完了'];

            if (lstTask != null && lstTask.size() > 0) {
                List<Task> updTask = new List<Task>();
                for(Task tsk : lstTask) {
                    tsk.status = '完了';
                    updTask.add(tsk);
                }
                if (updTask.size() > 0) {
                    update updTask;
                }
            }
        }
        // CHAN-BC6BW9 LHJ 20190520 End

        qisIdListMethod();

    }
    private void qisIdListMethod() {
        if (qisIdList.size() > 0) {
            // MessageGroupNumber の採番
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM207';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
            System.debug(Logginglevel.DEBUG, 'NFM207_' + iflog.Name + ' start');                  // callout の中 end のlogを出します
            NFM207Controller.callout(iflog.Id, qisIdList);
        }
    }
}