global without sharing class AssetMaintainHeaderWebService {
    public AssetMaintainHeaderWebService() {

    }
    WebService static String submitApply(String amhId) {
        List<AssetMaintainHeader__c> amhList = [select id, Status__c
                                            from AssetMaintainHeader__c
                                            where id = :amhId
                                            FOR UPDATE];
        if (amhList.size() <= 0) {
            return '维护单不存在。';
        }
        AssetMaintainHeader__c amh = amhList[0];
        if(amh.Status__c != '草案中'){
            return '请确认维护单状态，已经提交过的申请，不能重复提交';
        }
        List<String> errList = AssetMaintainManualBatch.checkAllDetailAndAsset(amhId, AssetMaintainManualBatch.Operation.FROZEN).errorList;
        if(errList.size()>0){
            return String.join(errList,'\n');
        }
        amh.Status__c = '填写完毕';
        amh.Submit_Person__c = UserInfo.getUserId();
        Savepoint sp = Database.setSavepoint();
        try {
            List<AssetMaintainDetail__c> amdList = [
                SELECT Asset__c
                     , MaintainCount__c
                  FROM AssetMaintainDetail__c
                 WHERE AssetMaintainHeader__c =: amh.Id
            ];
            setMaintainConuntToAsset(amdList);
            update amh;
        }
        catch (Exception ex) {
            Database.rollback(sp);
            return ex.getMessage();
        }
        return '1';
    }
    WebService static String abandon(String amhId) {
        Savepoint sp = Database.setSavepoint();
        try {
            List<AssetMaintainHeader__c> amhList = [select id
                                                         , Status__c
                                                         , Batch_Processing__c
                                                from AssetMaintainHeader__c
                                                where id = :amhId
                                                FOR UPDATE];
            if (amhList.size() <= 0) {
                return '维护单不存在。';
            }
            AssetMaintainHeader__c amh = amhList[0];
            if(amh.Status__c != '已批准'){
                return '请确认维护单状态，只有已批准的申请才可以废弃';
            }
            if(amh.Batch_Processing__c) {
                return '此维护单有Batch正在执行，请稍候';
            }
            amh.Batch_Processing__c = true;
            update amh;
            Boolean needCheck = true;
            AssetMaintainManualBatch amBatch = new AssetMaintainManualBatch(amhId, needCheck, AssetMaintainManualBatch.Operation.ABANDON);
            Database.executeBatch(amBatch, Integer.valueOf(System.Label.AssetMaintainManualBatchSize));
        }
        catch (Exception ex) {
            Database.rollback(sp);
            return ex.getMessage();
        }
        return '1';
    }
    WebService static String deleteApply(String amhId) {
        List<AssetMaintainHeader__c> amhList = [select id, Status__c
                                            from AssetMaintainHeader__c
                                            where id = :amhId
                                            FOR UPDATE];

        if (amhList.size() <= 0) {
            return '维护单不存在。';
        }

        AssetMaintainHeader__c amh = amhList[0];
        if (amh.Status__c == '申请中') {
            return '需要调回/驳回申请后再删除';
        }
        else if (amh.Status__c != '草案中') {
            return '维护单状态为【' + amh.Status__c + '】，不可以删除';
        }
        else {
            Savepoint sp = Database.setSavepoint();
            try {
                delete amh;
            } catch (Exception ex) {
                Database.rollback(sp);
                return ex.getMessage();
            }
        }

        return '1';
    }
    /**
    @description 把维护数写到asset
    */
    static private void setMaintainConuntToAsset(List<AssetMaintainDetail__c> amdList){
        Set<Id> assetIdSet = new Set<Id>();
        for (AssetMaintainDetail__c amd: amdList) {
            assetIdSet.add(amd.Asset__c);
        }
        Map<Id, Asset> assetMap = new Map<Id, Asset>([
            SELECT Id
                 , MaintainCount_For_Processing__c
              FROM Asset
             WHERE Id IN:assetIdSet
               FOR UPDATE
        ]);
        for (AssetMaintainDetail__c amd: amdList) {
            if(assetMap.containsKey(amd.Asset__c)) {
                Asset ass = assetMap.get(amd.Asset__c);
                ass.MaintainCount_For_Processing__c = intValueOf(ass.MaintainCount_For_Processing__c) + intValueOf(amd.MaintainCount__c);
                assetMap.put(ass.Id, ass);
            }
        }
        Oly_TriggerHandler.bypass('AssetHandler');
        update assetMap.values();
        Oly_TriggerHandler.clearBypass('AssetHandler');
    }
    static private Integer intValueOf(Decimal d) {
        if(d == null || d < 0) {
            return 0;
        }
        return Integer.valueOf(d);
    }
}