public without sharing class AssetModifyBelongsController {
    // CHAN-B7J4SE
    /*****************検索用******************/
    public String text1 { get; set; } // 对象
    public String val1 { get; set; }  // 值
    public String RawAccountId  { get; set; }
    public Boolean checkexact { get; set; }  // 20230110 gzw 订单号精确查询用
    public AssetModifyBelongsChange__c ambc { get; set; }
    public static List<SelectOption> textOpts { get; private set; }
    public static Account ir { get; set; }// 20231103 李文涛 lightning升级 
    //public static List<SelectOption> changeReasonOpts { get; private set; }
    static {
        textOpts = new List<SelectOption>();
        textOpts.add(new SelectOption('', '-无-'));
        textOpts.add(new SelectOption('Order_No__c'  , Schema.SObjectType.Asset.fields.Order_No__c.label));
        textOpts.add(new SelectOption('Name'                , Schema.SObjectType.Asset.fields.Name.label));
        textOpts.add(new SelectOption('SerialNumber'        , Schema.SObjectType.Asset.fields.SerialNumber.label));
        textOpts.add(new SelectOption('Installation_Site__c', Schema.SObjectType.Asset.fields.Installation_Site__c.label));
        textOpts.add(new SelectOption('Department_Name__c'  , Schema.SObjectType.Asset.fields.Department_Name__c.label));

        //changeReasonOpts = new List<SelectOption>();
        //changeReasonOpts.add(new SelectOption('医院转赠'  , '医院转赠'));
        //changeReasonOpts.add(new SelectOption('经销商原因', '经销商原因'));
        //changeReasonOpts.add(new SelectOption('集中采购分配到地方', '集中采购分配到地方'));
        //changeReasonOpts.add(new SelectOption('2014年前发货，最终用户修改','2014年前发货，最终用户修改'));
    }

    /*****************画面初始化用********************************/

    public Boolean editAble {get; set;}
    public Boolean spoLinkFlag {get; set;}
    public String statusEdit {get; set;}
    /*****************画面表示Bean******************/

    private List<AssetModifyBelongsChangeDetailInfo> assetModifyBelongsChangeDetailRecords = new List<AssetModifyBelongsChangeDetailInfo>();
    public List<AssetModifyBelongsChangeDetailInfo> assetModifyBelongsChangeDetailRecordsview { get; set; }
    public Integer ConsumableorderdetailsCount {
        get {
            return assetModifyBelongsChangeDetailRecords == null ? 0 : assetModifyBelongsChangeDetailRecords.size();
        }
    }
    public Integer ConsumableorderdetailsviewCount {
        get {
            return assetModifyBelongsChangeDetailRecordsview == null ? 0 : assetModifyBelongsChangeDetailRecordsview.size();
        }
    }
    //变更单Id
    public String Id  { get; set; }

    //CHAN-BC6BV2 XHL AddStart 20190522
    public Boolean isShow { get; set; }
    public List<Attachment> attachments { get; set; }
    //CHAN-BC6BV2 XHL AddEnd 20190522
    // 20231103   Lightning文件修改 Start
	public List<ContentDocument> contents { get; set; }
    // 20231103   Lightning文件修改 End
    //分页功能
    public Integer pageLimit {get; set;}
    public String soql {get; set;}

    public AssetModifyBelongsController(ApexPages.StandardController stdController) {
        
        isShow = false;
        RawAccountId = ApexPages.currentPage().getParameters().get('accountId');
        Id = ApexPages.currentPage().getParameters().get('Id');

        statusEdit = ApexPages.currentPage().getParameters().get('KeyWords');
        System.debug('Id:'+Id+'   accountId:'+RawAccountId+'  KeyWords:'+statusEdit);//20231103 李文涛 
        assetModifyBelongsChangeDetailRecords = new List<AssetModifyBelongsChangeDetailInfo>();
        assetModifyBelongsChangeDetailRecordsview = new List<AssetModifyBelongsChangeDetailInfo>();
        editAble = false;
        spoLinkFlag = false;
        pageLimit = Integer.valueOf(System.Label.orderdetPageLimitsize);

        attachments = new List<Attachment>();
        //update 李文涛 2023/07/13 更新lightning附件上传 start
        if(String.isNotBlank(Id)){
            List<ContentDocumentLink> cdlList = [SELECT ContentDocumentId
                                                       FROM ContentDocumentLink
                                                       WHERE LinkedEntityId = :Id];
            List<ID> fileIDs = new List<ID>();
            for (ContentDocumentLink docLink : cdlList) {
                fileIDs.add(docLink.ContentDocumentId);
            }
            contents=[SELECT Title,OwnerId,ContentModifiedDate from ContentDocument WHERE id IN :fileIDs];
        }
        
        //update 李文涛 2023/07/13 更新lightning附件上传 end
    }

    public boolean getcheckUser() {
        return UserInfo.getProfileId() == system.label.ProfileId_SystemAdmin;


    }
    // 画面初始化
    public void init() {
        checkexact = false;

        if (Id != null && Id != '' && statusEdit == '' && statusEdit == null) {
            editAble = false;
        } else if ((Id == null || Id == '') && (statusEdit == '' || statusEdit == null)) {
            editAble = true;
        } else if (Id != null && Id != '' && statusEdit != '' && statusEdit != null) {
            editAble = true;
        }
        ambc = new AssetModifyBelongsChange__c();
        assetModifyBelongsChangeDetailRecords = new List<AssetModifyBelongsChangeDetailInfo>();
        List<Asset> assetListed = new List<Asset>();
        Map<String, AssetModifyBelongsChangeDetailInfo> MidMap = new Map<String, AssetModifyBelongsChangeDetailInfo>();

        if (Id == NULL || Id == '') {
            soql = this.makeSoql1('', '');
            assetListed = Database.query(soql);
            assetListed = noSubmittedList(assetListed);
            
            if (assetListed.size() > 0) {
                ambc.rawAccount__c = assetListed[0].Account.Id;
            }

            for (Integer i = 0; i < assetListed.size(); i++) {
                assetModifyBelongsChangeDetailRecords.add(new AssetModifyBelongsChangeDetailInfo(assetListed[i]));

                MidMap.put(assetListed[i].Id, new AssetModifyBelongsChangeDetailInfo(assetListed[i]));
            }

            for (Integer i = 0; i < assetModifyBelongsChangeDetailRecords.size(); i++) {
                assetModifyBelongsChangeDetailRecordsview.add(assetModifyBelongsChangeDetailRecords[i]);
            }
        } else {
            List<AssetModifyBelongsChangeDetail__c> changeDetailList = new List<AssetModifyBelongsChangeDetail__c>();
            List<AssetModifyBelongsChange__c>  assetChangeList  = new List<AssetModifyBelongsChange__c>();

            assetChangeList = [select Id, Name, AccountField__c, Change_status__c,
                               AccountField__r.Name,
                               rawAccount__r.Name,
                               SPOLink__c,
                               ChangeReason__c,
                               DetailCnt__c,
                               remark__c,
                    //WLIG-BS7B4T ---20200806---update By rentongxiao---Start
                               share__c,
                    //WLIG-BS7B4T ---20200806---update By rentongxiao---End
                    // WYL 2024/1/25 DB202401065815 start
                        ServiceRequestTrackingNumber__c
                    // WYL 2024/1/25 DB202401065815 end
                               from AssetModifyBelongsChange__c
                               where Id = :Id];
            if (assetChangeList.size() > 0) {
                ambc = assetChangeList[0];
                Id = assetChangeList[0].Id;
            }

            changeDetailList = [select Id, Name, Asset__c, Asset__r.Name,
                                Asset__r.Asset_situation__c,
                                Asset__r.Order_No__c,
                                Asset__r.SerialNumber,
                                Asset__r.Account.Name,
                                Asset__r.Installation_Site__c,
                                Asset__r.CurrentContract__c,
                                Asset__r.Status
                                from AssetModifyBelongsChangeDetail__c
                                where ChangeAndChangeDetail__c = :Id]   ;
            for (Integer i = 0; i < changeDetailList.size(); i++) {
                MidMap.put(changeDetailList[i].Id, new AssetModifyBelongsChangeDetailInfo(changeDetailList[i]));
            }

            for (AssetModifyBelongsChangeDetailInfo ass : MidMap.values()) {
                assetModifyBelongsChangeDetailRecords.add(ass);

            }

            for (AssetModifyBelongsChangeDetailInfo ass : assetModifyBelongsChangeDetailRecords) {
                for (AssetModifyBelongsChangeDetail__c ambcDetail : changeDetailList) {
                    if (ass.ast.Id == ambcDetail.Asset__c) {
                        ass.check = true;
                        assetModifyBelongsChangeDetailRecordsview.add(ass);
                    }
                }


            }

            //searchConsumableorderdetails();
            isShow = true;
            attachments = seekAttachment();
        }
    }
    // 检索
    public void searchConsumableorderdetails() {
        Map<String, AssetModifyBelongsChangeDetailInfo> MidMap = new Map<String, AssetModifyBelongsChangeDetailInfo>();
        List<Asset> asListed = new List<Asset>();
        soql = this.makeSoql1(text1, val1);
        System.debug('soql:'+soql); //20231103 李文涛
		asListed = Database.query(soql);
        asListed = noSubmittedList(asListed);
        for (AssetModifyBelongsChangeDetailInfo bss : assetModifyBelongsChangeDetailRecordsview) {
            if (bss.check == true) {
                MidMap.put(bss.ast.Id, bss);
            }
        }

        for (Integer i = 0; i < asListed.size(); i++) {
            if (!MidMap.containsKey(asListed[i].Id)) {
                MidMap.put(asListed[i].Id, new AssetModifyBelongsChangeDetailInfo(asListed[i]));
            }

        }


        assetModifyBelongsChangeDetailRecordsview.clear();
        for (AssetModifyBelongsChangeDetailInfo bss : MidMap.values()) {
            assetModifyBelongsChangeDetailRecordsview.add(bss);
        }
    }


    public List<Asset> noSubmittedList(List<Asset> asListed){

        // WLIG-BYZ9UF XHL 20210311 Start
        if (String.isNotBlank(RawAccountId)) {
            // 查找科室下所有 状态是 已提交的保有设备所属变更
            List<AssetModifyBelongsChange__c>  assetChangeList  = new List<AssetModifyBelongsChange__c>();

            assetChangeList = [select Id, Name, AccountField__c
                       from AssetModifyBelongsChange__c
                       where rawAccount__c = :RawAccountId And Change_status__c = '已提交'];
            List<String> ambcIdList = new List<String>();
            if (assetChangeList.size() > 0) {
                for (AssetModifyBelongsChange__c ambc :assetChangeList) {
                    ambcIdList.add(ambc.Id);
                }
            }
            List<Asset> noSubmittedList = new List<Asset>();
            if (ambcIdList.size() > 0) {
                Map<String,String> submittedMap = new Map<String,String>();
                List<AssetModifyBelongsChangeDetail__c> changeDetailList = new List<AssetModifyBelongsChangeDetail__c>();
                // 查找 保有设备所属变更明细
                changeDetailList = [select Id, Name, Asset__c,Asset__r.Id 
                                from AssetModifyBelongsChangeDetail__c
                                where ChangeAndChangeDetail__c In:ambcIdList];
                
                if (changeDetailList.size() > 0) {
                    for(AssetModifyBelongsChangeDetail__c ambcDetail:changeDetailList){
                        submittedMap.put(ambcDetail.Asset__r.Id, ambcDetail.Asset__r.Id);
                    }
                    // 排除已提交的保有设备
                    for(Asset asset :asListed){
                        if (!submittedMap.containsKey(asset.Id)) {
                            noSubmittedList.add(asset);
                        }
                    }
                }
                
            } else {
                noSubmittedList = asListed;  
            }

            if (noSubmittedList.size() > 200) {
                Integer num = 0;
                asListed = new List<Asset>();
                for(Asset asset:noSubmittedList){
                    if(num < 200) {
                        asListed.add(asset);
                        num++;
                    } else{
                        break;
                    }
                    
                }
            } else {
               asListed = noSubmittedList;
            }
        }
        
        return asListed;
        // WLIG-BYZ9UF XHL 20210311 End

    }

    private String makeSoql1(String text1, String val1) {
        if (String.isNotBlank(val1)) {
            val1 = val1.trim();
        }
        String soql = 'select Id,name,SerialNumber,Asset_situation__c,Order_No__c,Account.Name,Status,Installation_Site__c,CurrentContract__c from Asset where AccountId = \'' + RawAccountId + '\'';
        if (!String.isBlank(text1)) {
            if (text1 == 'Order_No__c' && checkexact) {
                soql += ' AND (' + text1 + ' = \'' + String.escapeSingleQuotes(val1) + '\' )';
            }else{
                soql += ' AND (' + text1 + ' like \'%' + String.escapeSingleQuotes(val1.replaceAll('%', '\\%')) + '%\' )';
            }
        }
        soql += ' limit 1000';
        return soql;
    }

    // 编辑按钮
    public PageReference setEditAble() {
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/apex/AssetModifyBelongs?Id=' + Id + '&KeyWords=' + statusEdit + '&accountId=' + ambc.rawAccount__c);
        ref.setRedirect(true);
        return ref;
    }

    // 再申请
    public PageReference backOrder() {
        statusEdit = 'Redirect';
        PageReference ref = new Pagereference('/apex/AssetModifyBelongs?Id=' + Id + '&KeyWords=' + statusEdit + '&accountId=' + ambc.rawAccount__c);
        ref.setRedirect(true);
        return ref;
    }

    //删除按钮
    public PageReference DelConsumable() {
        AssetModifyBelongsChange__c ambc = new AssetModifyBelongsChange__c(Id = Id);
        List<AssetModifyBelongsChangeDetail__c> changeDetailList = [Select Id ,
                                                ChangeAndChangeDetail__r.rawAccount__c
                                                from AssetModifyBelongsChangeDetail__c
                                                where ChangeAndChangeDetail__c = :Id];
        String rawAccountIded = '';
        if (changeDetailList.size() > 0) {
            rawAccountIded = changeDetailList[0].ChangeAndChangeDetail__r.rawAccount__c;
        }

        try {
            delete changeDetailList;
            delete ambc;
            //return null;
            return returnRawAccountPage(rawAccountIded);
        } catch (Exception e) {
            ApexPages.addMessages(e);
            return null;
        }
    }

    // 删除“变更申请单”时，跳转到“原客户”页面
    public PageReference returnRawAccountPage(String rawAccountIded) {
        PageReference ref = new Pagereference('/' + rawAccountIded );
        ref.setRedirect(true);
        return ref;
    }
    // CHAN-BC6BV2 XHL AddStart 20190522
    //查找当前"保有设备所属变更申请单"所有的附件
    public List<Attachment> seekAttachment() {
        return [select Id, ParentId, Name, OwnerId, CreatedDate, BodyLength FROM Attachment where ParentId = :Id];
    }
    //跳转到上传附件页面
    public PageReference uploadingAttachment() {
        PageReference ref = new Pagereference('/p/attach/NoteAttach?pid=' + Id + '&retURL=%2Fapex/AssetModifyBelongs?Id=' + Id);

        ref.setRedirect(true);
        return ref;
    }
    //update 李文涛 2023/07/13 更新lightning附件上传 start
    //跳转到上传附件页面（lightning）
    public PageReference uploadingAttachmentLightning() {
        //PageReference ref = new Pagereference('/p/attach/NoteAttach?pid=' + Id + '&retURL=%2Fapex/AssetModifyBelongs?Id=' + Id);
        PageReference ref = new Pagereference('/lightning/n/LexUploadPage?0.recordId='+Id+'');
        
        ref.setRedirect(true);
        return ref;
    }
    //update 李文涛 2023/07/13 更新lightning附件上传 end
    // CHAN-BC6BV2 XHL AddEnd 20190522
    //  重新刷新
    public PageReference returnChangePage() {
        PageReference ref = new Pagereference('/apex/AssetModifyBelongs?Id=' + Id );
        ref.setRedirect(true);
        return ref;
    }

    // 提交按钮
    public PageReference Sorder() {
        PageReference ref = new Pagereference('/' + Id);
        ref.setRedirect(true);

        Savepoint sp = Database.setSavepoint();
        AssetModifyBelongsChange__c  assetModify = new AssetModifyBelongsChange__c();
        assetModify.Id = Id;

        try {

            update assetModify;

        } catch (Exception e) {
            ApexPages.addmessages(e);
        }

        //提交之前验证保有设备是否在原客户下，不在报错。
        List<AssetModifyBelongsChangeDetail__c> getAmBdList = new List<AssetModifyBelongsChangeDetail__c>();
        getAmBdList = [SELECT Id, Name,Asset__r.SerialNumber,AssetNotAccount__c,Asset__r.Account.Name,ChangeAndChangeDetail__r.rawAccount__r.Name
                       FROM AssetModifyBelongsChangeDetail__c 
                       WHERE ChangeAndChangeDetail__c = : Id AND AssetNotAccount__c = true];
        if (getAmBdList.size() > 0) {
            for (AssetModifyBelongsChangeDetail__c ambcd: getAmBdList ) {
                String serialNumber = '保有设备 [ '+  ambcd.Asset__r.SerialNumber +' ]';
                String rawAccountName = ' 不在客户 【 ' + ambcd.ChangeAndChangeDetail__r.rawAccount__r.Name +' 】 下，';
                String accountName =  ' 在客户 【 ' +ambcd.Asset__r.Account.Name +' 】 下，';
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, serialNumber+rawAccountName+accountName+' 无法提交，请确认'));
                    
            }
            return null;
        }
         
        try {
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setObjectId(Id);
            Approval.ProcessResult submitResult = Approval.process(psr);
            //return  returnChangePage();
            return  ref;
        } catch (Exception e) {
            //ApexPages.addmessages(e);
            Database.rollback(sp);
            return  null;
        }

        //return null;
        return  UnabletoEdit();
    }

    // 保存按钮
    public PageReference save() {

        Integer FLG = 0;
        Integer Count = 0;
        for (AssetModifyBelongsChangeDetailInfo CheckCount : assetModifyBelongsChangeDetailRecordsview) {
            FLG = FLG + 1;
            if (CheckCount.check == false) {
                Count = Count + 1;
            }
        }

        if (Count == FLG) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择要修改的保有设备'));
            return null;
        }
        AssetModifyBelongsChange__c  assetModify = new AssetModifyBelongsChange__c ();
        List<AssetModifyBelongsChangeDetail__c> Ins = New List<AssetModifyBelongsChangeDetail__c>();
        //新建变更单时
        Savepoint sp = Database.setSavepoint();
        try {
            if (String.isBlank(ambc.AccountField__c)) {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请选择目标客户'));
                return null;
            }
            if (String.isBlank(Id)) {
                assetModify = upserAassetModify(assetModify, ambc);
                try {
                    assetModify.Change_status__c = '草案中';
                    assetModify.rawAccount__c  = RawAccountId;
                    insert assetModify;
                } catch (Exception e) {
                    ApexPages.addMessages(e);
                }
                Id = assetModify.id;
                Ins = upserAassetModifyDetail(Ins, assetModify, assetModifyBelongsChangeDetailRecordsview);
                if (Ins.size() > 0) {
                    insert Ins;
                    upserAassetModifyByMaxAssetPostingDate(Id);
                    return  UnabletoEdit();
                }

            }
            //修改之后 保存订单
            else if (!String.isBlank(Id)) {
                assetModify = upserAassetModify(assetModify, ambc);
                assetModify.Id = Id;
                update assetModify;
                List<AssetModifyBelongsChangeDetail__c> delIns = [select Id from AssetModifyBelongsChangeDetail__c
                        where ChangeAndChangeDetail__r.Id = :Id];
                if (delIns.size() > 0) {
                    delete delIns;
                }
                Ins = upserAassetModifyDetail(Ins, assetModify, assetModifyBelongsChangeDetailRecordsview);

                if (Ins.size() > 0) {
                    insert Ins;
                    upserAassetModifyByMaxAssetPostingDate(Id);
                    return  UnabletoEdit();
                }
            }

        } catch (Exception e) {
            ApexPages.addMessages(e);
            Database.rollback(sp);
            return  null;
        }
        return UnabletoEdit();
    }
    // 更新保有设备所属变更下 保有设备所属变更明细、经销商1、经销商2
    // WLIG-BUF9R2 XHL -20201020 Start
    public static void upserAassetModifyByMaxAssetPostingDate(String Id) {

        List<AssetModifyBelongsChangeDetail__c> getAmBdList = new List<AssetModifyBelongsChangeDetail__c>();
        getAmBdList = [Select Id, Name, AssetPostingDate__c, AssetOpportunityDealer1__c, AssetOpportunityDealer2__c, ChangeAndChangeDetail__c From AssetModifyBelongsChangeDetail__c Where ChangeAndChangeDetail__c = : Id];
        Map<String, AssetModifyBelongsChangeDetail__c> maxDate = new Map<String, AssetModifyBelongsChangeDetail__c>();
        if (getAmBdList != null && getAmBdList.size() > 0) {
            for (AssetModifyBelongsChangeDetail__c ambd : getAmBdList) {
                String assetModifyBelongsChangeId= ambd.ChangeAndChangeDetail__c;
                assetModifyBelongsChangeId = assetModifyBelongsChangeId.substring(0, 15);
                if (!maxDate.containsKey(assetModifyBelongsChangeId)) {

                    maxDate.put(ambd.ChangeAndChangeDetail__c, ambd);
                } else {
                    AssetModifyBelongsChangeDetail__c getAMBD  = maxDate.get(ambd.ChangeAndChangeDetail__c);
                    if (getAMBD.AssetPostingDate__c < ambd.AssetPostingDate__c) {
                        maxDate.put(ambd.ChangeAndChangeDetail__c, ambd);
                    }
                }
            }
            AssetModifyBelongsChangeDetail__c maxAssetPostingDate = maxDate.get(Id);
            AssetModifyBelongsChange__c  updateAMBC = new AssetModifyBelongsChange__c();
            updateAMBC.Id = Id;
            // 保有设备所属变更明细(收货日最大)
            updateAMBC.MaxAssetPostingDate__c = maxAssetPostingDate.Id;
            // 经销商1
            updateAMBC.MaxAssetPostingDateDealer1__c = maxAssetPostingDate.AssetOpportunityDealer1__c;
            // 经销商2
            updateAMBC.MaxAssetPostingDateDealer2__c = maxAssetPostingDate.AssetOpportunityDealer2__c;

            update updateAMBC;

        }

    }
    // WLIG-BUF9R2 XHL -20201020 End

    //保有设备所属变更赋值
    public static AssetModifyBelongsChange__c upserAassetModify( AssetModifyBelongsChange__c  assetModify, AssetModifyBelongsChange__c ambc) {
        //LZHU-BNR7XZ ---XHL---AddStart---20200420
        Map<String, String> transferMap = BatchIF_Transfer('AssetModifyBelongsChange__c');
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        List<Account> accountList = [select Id, Name, Salesdepartment_Dept__c from Account where Id = :ambc.AccountField__c];
        String salesdepartment = accountList[0].Salesdepartment_Dept__c;
        String ccUserId = NFMUtil.getMapValue(transferMap, 'ModifyBelongsEmail__c', salesdepartment, iflog);
        if (salesdepartment != ccUserId ) {
            Set<String> ccEmailSet = new Set<String>();
            string query = 'select id , Email from user where id = :ccUserId';
            List<user> ccUserList = Database.Query(query);
            for (user ccUser : ccUserList) {
                if (!String.isBlank(ccUser.Email)) {
                    assetModify.ModifyBelongsEmail__c = ccUser.Email;
                } else {
                    assetModify.ModifyBelongsEmail__c = '';
                }
            }
        } else {
            assetModify.ModifyBelongsEmail__c = '';
        }
        //LZHU-BNR7XZ ---XHL---AddEnd---20200420
        assetModify.AccountField__c = ambc.AccountField__c;
        assetModify.SPOLink__c = ambc.SPOLink__c ;
        assetModify.remark__c = ambc.remark__c ;
        assetModify.ChangeReason__c = ambc.ChangeReason__c;
        //WLIG-BS7B4T ---20200806---update By rentongxiao---Start
        assetModify.share__c = ambc.share__c;
        //WLIG-BS7B4T ---20200806---update By rentongxiao---End
        //WYL 2024/1/25 DB202401065815 start
        assetModify.ServiceRequestTrackingNumber__c = ambc.ServiceRequestTrackingNumber__c;
        // WYL 2024/1/25 DB202401065815 end
        return assetModify;
    }

    public static Map<String, String> BatchIF_Transfer(String object_API_Name) {

        Map<String, String> result = new Map<String, String>();
        List<BatchIF_Transfer__c> transferList = [select Table__c,
                                  Column__c,
                                  External_value__c,
                                  Internal_value__c
                                  from BatchIF_Transfer__c
                                  where Dropped_Flag__c = false
                                          and Table__c = :object_API_Name];
        for (BatchIF_Transfer__c t : transferList) {
            result.put(t.Column__c + t.External_value__c, t.Internal_value__c);
        }
        return result;
    }
    //保有设备所属变更明细赋值
    public  List<AssetModifyBelongsChangeDetail__c> upserAassetModifyDetail( List<AssetModifyBelongsChangeDetail__c> Ins, AssetModifyBelongsChange__c  assetModify, List<AssetModifyBelongsChangeDetailInfo> assetModifyBelongsChangeDetailRecordsview) {
        Integer i = 1;
        Integer Roll = 0;

        List<AssetModifyBelongsChange__c> AssetModifyBelongsChange = [SELECT Id, Name FROM AssetModifyBelongsChange__c WHERE id = :assetModify.id];
        for (AssetModifyBelongsChangeDetailInfo ass : assetModifyBelongsChangeDetailRecordsview)  {
            Roll = Roll + 1;

            if (ass.check == true) {
                AssetModifyBelongsChangeDetail__c InsAfterDel = new AssetModifyBelongsChangeDetail__c();
                String str = string.valueOf(i);
                if (str.length() == 1) {
                    str = '0' + str;
                }
                InsAfterDel.Name = AssetModifyBelongsChange[0].Name + '-' + str;
                InsAfterDel.ChangeAndChangeDetail__c = AssetModifyBelongsChange[0].Id;
                InsAfterDel.Asset__c = ass.ast.Id;
                i++;
                Ins.add(InsAfterDel);
            }
        }
        return Ins;
    }
    // 返回不可编辑状态
    public PageReference UnabletoEdit() {
        PageReference ref = new Pagereference('/apex/AssetModifyBelongs?Id=' + Id);
        ref.setRedirect(true);
        return ref;
    }
    // Data Bean
    class AssetModifyBelongsChangeDetailInfo  {
        public Boolean check { get; set; }
        public Boolean oldCheck { get; set; }
        public Boolean canSelect { get; set; }


        public Asset ast { get; set; }
        public AssetModifyBelongsChangeDetail__c cacd { get; set; }



        public AssetModifyBelongsChangeDetailInfo(AssetModifyBelongsChangeDetail__c cacd) {
            check = false;
            oldCheck = false;
            ast = cacd.Asset__r;
            canSelect = true;

        }

        public AssetModifyBelongsChangeDetailInfo(Asset e) {
            check = false;
            oldCheck = false;
            ast = e;
            canSelect = true;
        }

    }
}