/**
 * Ver1.0 2022-05-17 ssm
 * 
 * OBSAP项目-报价委托功能修改
 * 
 * after update:
 *     报价委托已完成时，查询标准任务里关联的未完成任务，并更新状态
 */
public without sharing class QuoteIraiHandler extends Oly_TriggerHandler  {

    private Map<Id, QuoteIrai__c> newMap;
    private Map<Id, QuoteIrai__c> oldMap;
    private List<QuoteIrai__c> newList;
    private List<QuoteIrai__c> oldList;

    public QuoteIraiHandler() {
        this.newMap = (Map<Id, QuoteIrai__c>) Trigger.newMap;
        this.oldMap = (Map<Id, QuoteIrai__c>) Trigger.oldMap;
        this.newList = (List<QuoteIrai__c>) Trigger.new;
        this.oldList = (List<QuoteIrai__c>) Trigger.old;
    }

    protected override void afterUpdate() {
        task_changeto_finish();
    }

    // 报价委托已完成时，查询标准任务里关联的未完成任务，并更新状态
    private void task_changeto_finish() {
        List<String> ids = new List<String>();
        // 循环获得已完成的报价委托
        for (QuoteIrai__c irai : this.newList) {
            QuoteIrai__c irai_old = this.oldMap.get(irai.Id);
            if (irai_old != null && irai.QuoteIrai_Status__c != irai_old.QuoteIrai_Status__c && irai.QuoteIrai_Status__c == '更新完毕') {
                ids.add(irai.Id);
            }
        }
        if (ids.size() > 0) {
            List<Task> tasks = [select Id, Status, QuoteIraiId__c from Task where Status != '完了' and QuoteIraiId__c in :ids];
            if (tasks != null && tasks.size() > 0) {
                for (Task task : tasks) {
                    task.Status = '完了';
                }

                update tasks;
            }
        }
    }
}