/**
 * wangweipeng     2021/09/02
 * update          2021/11/10
 * 用于把ods__c表的数据同步到 user表中
 */
public with sharing class OdsToUserHandler extends Oly_TriggerHandler{
    private Map<Id, ODS__c> newMap;
    private Map<Id, ODS__c> oldMap;
    private List<ODS__c> newList;
    private List<ODS__c> oldList;


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

    protected override void beforeInsert() {
        
    }

    protected override void beforeUpdate() {
        
    }
    protected override void afterInsert() {
        UpsertUser();
    }

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


    //新增或修改user表数据
    //ods如果修改，user表数据也跟着修改
    public void UpsertUser(){
        Map<Id,ODS__c> updateMap = new Map<Id,ODS__c>();
        if(newList != null && newList.size() > 0){
            //用于存放触发trigger的ods数据的员工编号
            List<String> employeeNoList = new List<String>();
            for (ODS__c os : newList) {
                //20220407 LLIU-CCY9TA you 临时更新几个user
                if(String.isNotBlank(os.Employee_No__c) && System.Label.OdsToUserEmployee_No.contains(os.Employee_No__c)){
                   if(String.isNotBlank(os.Employee_No__c) && os.Job_Type__c != '劳务-派遣' && os.Job_Type__c != '劳务-临时' && os.Job_Type__c != '无' && String.isNotBlank(os.Job_Type__c) && String.isNotBlank(os.Stay_or_not__c) && String.isNotBlank(os.Email__c))
                   {
                        //判断email、员工编码、名称、Stay_or_not和入职日期是否为空
                        if(os.Email__c != null && String.isNotBlank(os.Employee_No__c) && String.isNotBlank(os.Stay_or_not__c) &&String.isNotBlank(os.Name__c) && os.Hire_Date__c != null)
                        {
                            //如果为update，那么判断值是否发生变化
                            //判断值数据发生变化
                            if(Trigger.isUpdate){
                                //统括本部、本部、部、课、员工编码、入职日期、电子邮件、手机号码、职位、职种、负责产品（主）、销售工作内容、工作地、在职/已离职、离职日期
                                //以上字段的值是否发生变换
                                //如果发生变化，那么更新user表数据，user表更新contact表数据
                                if(os.Category3__c != oldMap.get(os.id).Category3__c || os.Category4__c != oldMap.get(os.id).Category4__c 
                                    || os.Category5__c != oldMap.get(os.id).Category5__c || os.Category6__c != oldMap.get(os.id).Category6__c 
                                    || os.Employee_No__c != oldMap.get(os.id).Employee_No__c || os.Hire_Date__c != oldMap.get(os.id).Hire_Date__c 
                                    || os.Email__c != oldMap.get(os.id).Email__c || os.MobilePhone__c != oldMap.get(os.id).MobilePhone__c 
                                    || os.Post__c != oldMap.get(os.id).Post__c || os.Job_Category__c != oldMap.get(os.id).Job_Category__c 
                                    || os.Product_specialist_incharge_product__c != oldMap.get(os.id).Product_specialist_incharge_product__c 
                                    || os.Sales_Speciality__c != oldMap.get(os.id).Sales_Speciality__c || os.Work_Location__c != oldMap.get(os.id).Work_Location__c
                                    || os.Stay_or_not__c != oldMap.get(os.id).Stay_or_not__c || os.LeaveDate__c != oldMap.get(os.id).LeaveDate__c 
                                    || os.Name__c != oldMap.get(os.Id).Name__c || os.Null_Update__c != oldMap.get(os.Id).Null_Update__c
                                    // SWAG-CBP5DY的历史数据使用空更新处理 thh 20220224 start
                                    || (System.Label.onlyupdate == '1' && UserInfo.getUserId() == '00510000005sEEMAA2'))
                                    // SWAG-CBP5DY的历史数据使用空更新处理 thh 20220224 end
                                    {
                                    employeeNoList.add(os.Employee_No__c.trim());
                                }
                            }else{
                                employeeNoList.add(os.Employee_No__c.trim());
                            }
                        }else{
                            System.debug('--------------Email__c:'+os+'----Employee_No__c:'+os.Employee_No__c+'-----Stay_or_not__c:'+os.Stay_or_not__c+'-------Name__c:'+os.Name__c+'--------Hire_Date__c:'+os.Hire_Date__c+' 为空');
                        }
                   }
                }
                
            }

            System.debug(employeeNoList.size()+'----------------------ods员工编号--'+employeeNoList);
            //判断是否有需要更新的数据
            if(employeeNoList.size() > 0){
                ControllerUtil.updateFutureOdsByUser(employeeNoList);
            }
        }
    }
}