// FixtureSetHandlerTest
public without sharing class FixtureSetHandler extends Oly_TriggerHandler {
    private Map<Id, Fixture_Set__c> newMap;
    private Map<Id, Fixture_Set__c> oldMap;
    private List<Fixture_Set__c> newList;
    private List<Fixture_Set__c> oldList;

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

    protected override void beforeInsert() {
        beforeSetValue();
    }
    protected override void beforeUpdate() {
        beforeSetValue();
        upToAsset();// 20230719 ljh DB202306212211 p9
    }

    private void beforeSetValue() {
        for (Fixture_Set__c nObj : newList) {
            nObj.Loaner_code__c = nObj.Name;
        }
    }
    /**
     * @Author: [lijinhuan]
     * @description: ±¸Æ·ÀàÐÍIºÍ±¸Æ·ÀàÐÍII·Åµ½±£ÓÐÉè±¸ÉÏÏÔÊ¾
     * @return {*}
     */
    private void upToAsset() {
        Map<String,Fixture_Set__c> fsMap= new Map<String,Fixture_Set__c>();
        // Set<String> ModelNoSet = new  Set<String>();
        for (Fixture_Set__c nObj : newList) {
            Fixture_Set__c oObj;
            if (Trigger.isUpdate) {
                oObj = oldMap.get(nObj.Id);
            }
            // ·¢Éú¸Ä±ä
            if( (String.isNotBlank(nObj.Loaner_categoryI__c) && nObj.Loaner_categoryI__c != oObj.Loaner_categoryI__c)
                || (String.isBlank(nObj.Loaner_categoryI__c) && String.isNotBlank(oObj.Loaner_categoryI__c))
                || (String.isNotBlank(nObj.Loaner_categoryII__c) && nObj.Loaner_categoryII__c != oObj.Loaner_categoryII__c)
                || (String.isBlank(nObj.Loaner_categoryII__c) && String.isNotBlank(oObj.Loaner_categoryII__c))
            ){
                if(String.isNotBlank(nObj.Fixture_Set_Body_Model_No__c)){
                    String model = nObj.Fixture_Set_Body_Model_No__c.toUpperCase();
                    fsMap.Put(model,nObj);
                }
                
            }
            // if(Trigger.isDelete && String.isNotBlank(oObj.Fixture_Set_Body_Model_No__c ) && (String.isNotBlank(oObj.Loaner_categoryI__c)||String.isNotBlank(oObj.Loaner_categoryII__c))){
            //     ModelNoSet.add(oObj.Fixture_Set_Body_Model_No__c);
            // }
        }
        // if(fsMap.size() > 0 || ModelNoSet.size() > 0){
        if(fsMap.size() > 0){
            // ²éÑ¯Õâ¸öÅäÌ×µÄ²úÆ·,¸³Öµ¸ø²úÆ·
            List<Product2>  Product2List = [SELECT Id,Fixture_Model_No_T__c,Loaner_categoryI__c,Loaner_categoryII__c 
                                                 FROM Product2 
                                                // WHERE Fixture_Model_No_T__c IN :fsMap.keySet() OR Fixture_Model_No_T__c IN :ModelNoSet ];
                                                WHERE Fixture_Model_No_T__c IN :fsMap.keySet() ];
            if(Product2List.size() > 0){
                Map<String,Product2> updateMap = new Map<String,Product2>();
                for(Product2 pro:Product2List){
                    Product2 p2 = new Product2();
                    p2.Id = pro.Id;
                    p2.Loaner_categoryI__c = fsMap.get(pro.Fixture_Model_No_T__c.toUpperCase()).Loaner_categoryI__c;
                    p2.Loaner_categoryII__c = fsMap.get(pro.Fixture_Model_No_T__c.toUpperCase()).Loaner_categoryII__c;
                    if(((String.isNotBlank(p2.Loaner_categoryI__c) && p2.Loaner_categoryI__c.equals(pro.Loaner_categoryI__c)) || (String.isBlank(p2.Loaner_categoryI__c)&&String.isBlank(pro.Loaner_categoryI__c)))
                        && ((String.isNotBlank(p2.Loaner_categoryII__c) && p2.Loaner_categoryII__c.equals(pro.Loaner_categoryII__c)) || (String.isBlank(p2.Loaner_categoryII__c)&&String.isBlank(pro.Loaner_categoryII__c)))){
                        continue;
                    }
                    updateMap.put(pro.Id,p2);
                }
                if(updateMap.size() > 0){
                    update updateMap.values();
                }
            }
        }
    }
}