global class MaintenanceContractSetColunmWebService{

    //1.要求: 将当前合同信息更新到当前合同下的`上一期维修合同`中的`来年合同`中

    ///实现
    ///1.获取当前合同信息 1).记录类型 2)保有设备
    ///2.获取当前合同下的保有设备下的`上一期维修合同`信息   条件1`空白期小于等于365天,条件2`上一期维修合同不为空
    ///3.设置该`上一期维修合同`的`来年合同`为当前合同  条件1` 如果当前合同的记录类型为`服务合同`,则更新`上一期维修合同`中的来年服务合同字段,(维修合同同上


    //用户点击上传SAP按钮,更新'来年服务合同'/'来年维修合同'
    WebService static String updateColunm(String mcid ){
        //当前维修合同
        Maintenance_Contract__c newMcc = [select id,name,RecordTypeId,Contract_Start_Date__c from Maintenance_Contract__c where Id = :mcid];

        //1. 获取当前合同的记录类型
        RecordType rt = [select Name from RecordType where id = :newMcc.RecordTypeId];

        //1. 获取当前合同下的所有符合更改条件的保有设备
        List<Maintenance_Contract_Asset__c> temp = [
        select Id,LastMContract__c,LastMContract_Gap__c from Maintenance_Contract_Asset__c 
        where Maintenance_Contract__c = :mcid
        ];

        if (temp.size() == 0) {
            return '1';
        }

        //1. 获取保有设备下的合同信息
        List<String> lastMcIds = new List<String>();
        for(Maintenance_Contract_Asset__c mcac : temp){
            if (mcac.LastMContract__c != null) {
                if (mcac.LastMContract_Gap__c <= 365) {
                    lastMcIds.add(mcac.LastMContract__c);   
                }   
            }
        }

        List<Maintenance_Contract__c> lastMcc = 
        [select id,name,RecordTypeId,Contract_Start_Date__c,serviceContractNextYear_new__c,contractNextYear_new__c from Maintenance_Contract__c where Id in :lastMcIds];
        
        Map<Id,Maintenance_Contract__c> mmap = new Map<Id,Maintenance_Contract__c>();

        if (rt.Name.equals('服务合同')) {
            for(Maintenance_Contract__c mcc : lastMcc){
                Maintenance_Contract__c mc = new Maintenance_Contract__c();
                mc.Id = mcc.Id;
                mc.serviceContractNextYear_new__c = mcid;
                mmap.put(mcc.Id,mc);
            }
        }else if (rt.Name.equals('维修合同')) {
            for(Maintenance_Contract__c mcc : lastMcc){
                Maintenance_Contract__c mc = new Maintenance_Contract__c();
                mc.Id = mcc.Id;
                mc.serviceContractNextYear_new__c = mcid;
                mc.contractNextYear_new__c = mcid;
                mmap.put(mcc.Id,mc);
            }
        }

        try {
            update mmap.values();
        }
        catch (Exception e) {
            return E.getMessage();
        }

        return '1';
    }





/*
        Map<Id,Maintenance_Contract__c> mmap = new Map<Id,Maintenance_Contract__c>();

        //2. 获取该保有设备的上一期维修合同信息
        if (rt.Name.equals('服务合同')) {
            for (Maintenance_Contract_Asset__c maca : temp) {
                Maintenance_Contract__c  mc = new Maintenance_Contract__c();
                mc.Id = maca.LastMContract__c;
                mc.serviceContractNextYear_new__c = mcid;
                mmap.put(maca.LastMContract__c,mc);
            }    
        }else if (rt.Name.equals('维修合同')) {
            for (Maintenance_Contract_Asset__c maca : temp) {
                Maintenance_Contract__c  mc = new Maintenance_Contract__c();
                mc.Id = maca.LastMContract__c;
                mc.contractNextYear_new__c = mcid;
                mmap.put(maca.LastMContract__c,mc);
            }
        }
        
        try {
            update mmap.values();
        }
        catch (Exception e) {
            return E.getMessage();
        }*/
        
}