global without sharing class RentalApplyEquipmentSetWebService {

    //借出备品一览单位重新分配
    WebService static String rentalApplyEquipmentSetReassign(Id raesID) {

        Savepoint sp = Database.setSavepoint();
        try{

            //ToDo IndexFromUniqueKey__c要清除吗
            //applyUser__c loginuser?
            List<Rental_Apply_Equipment_Set__c> raess = [Select Id, Loaner_centre_mail_address__c, Fixture_Set__c,
                    Rental_Apply__c, Irreplaceable_flag__c, Same_Accessory_flag__c, Received_Confirm__c, IndexFromUniqueKey__c,
                    Rental_Num__c, Groupby_SortInt__c, Old_Rental_Apply__c
                    From Rental_Apply_Equipment_Set__c
                    Where id = :raesID AND Cancel_Select__c = false
                    for update];
            if (raess.isEmpty()) {
                return '没有检索到可以重新分配的借出备品配套一览';
            }

            if (raess[0].Received_Confirm__c != 'NG') {
                return '这个按钮只能重新分配现场收到确认结果NG的一览';
            }
            
            // Lock申请书
            List<Rental_Apply__c> ras = [SELECT Id
                                          FROM Rental_Apply__c
                                         WHERE Id = :raess[0].Rental_Apply__c
                                         FOR UPDATE];
            if (ras.isEmpty()) {
                return '没有检索到备品借出申请';
            }
            Rental_Apply__c ra = ras[0];
            // // 20211125 ljh 排队 重新分配的配套自动带有“插队”标识 add Is_Body__c
            List<Rental_Apply_Equipment_Set_Detail__c> raesds = [Select Id,StockDown__c, Rental_Apply__c,
                    Fixture_Set_Detail__c, IndexFromUniqueKey__c, Rental_Apply_Equipment_Set__c,Is_Body__c, 
                    Rental_Num__c, IndexFromUniqueKey_Text__c
                    From Rental_Apply_Equipment_Set_Detail__c
                    Where Rental_Apply_Equipment_Set__c = :raesID
                    // RAESD_SortInt_F__c -> IndexFromUniqueKey_Text__c(备品配套明细.SortInt__c)
                    // 1应该是主体
                    AND Cancel_Select__c = false order by IndexFromUniqueKey_Text__c ASC nulls last];

            if (raesds.isEmpty()) {
                return '没有明细或者全部取消了不能重新分配';
            }
            // 防止一览更新后发送全部归还邮件   OLY_OCM-409
            ra.NotSendMessageFlag__c = true;
            update ra;

            Rental_Apply_Equipment_Set__c craes = raess[0];
            //OLY_OCM-163 只设置NG_Select_Again__C和Cancel_Select__c取消理由取消时间不设值
            craes.Cancel_Select__c = true;
            craes.NG_Select_Again__C = true;
            // craes.Cancel_Reason__c = '重新分配';
            // craes.Loaner_cancel_Remarks__c = '申请者收货NG';
            update craes;

            Rental_Apply_Equipment_Set__c raes = new Rental_Apply_Equipment_Set__c();
            raes.Rental_Apply__c = craes.Rental_Apply__c;
            raes.Fixture_Set__c = craes.Fixture_Set__c;
            raes.Canceled__c = craes.Id;
            raes.Canceled_Id__c = craes.Id;
            raes.IndexFromUniqueKey__c = craes.IndexFromUniqueKey__c;
            raes.Rental_Num__c = craes.Rental_Num__c;
            raes.Same_Accessory_flag__c = craes.Same_Accessory_flag__c;
            raes.Groupby_SortInt__c = craes.Groupby_SortInt__c;
            raes.Old_Rental_Apply__c = craes.Old_Rental_Apply__c; // OLY_OCM-251 分单后的新申请单Name是从其下一览处取，所以一览的源申请单也要clone
            raes.Received_NG_ReAssign_Text__c = true;

            insert raes;

            Rental_Apply_Equipment_Set_Detail__c firstraesd;

            List<Rental_Apply_Equipment_Set_Detail__c> cloneraesd = new List<Rental_Apply_Equipment_Set_Detail__c>();

            for (Rental_Apply_Equipment_Set_Detail__c raesd : raesds) {
                Rental_Apply_Equipment_Set_Detail__c raesdc = new Rental_Apply_Equipment_Set_Detail__c();
                raesdc.Rental_Apply_Equipment_Set__c = raes.Id;
                raesdc.Rental_Apply__c = raesd.Rental_Apply__c;
                raesdc.Fixture_Set_Detail__c = raesd.Fixture_Set_Detail__c;
                raesdc.IndexFromUniqueKey__c = raesd.IndexFromUniqueKey__c;
                raesdc.IndexFromUniqueKey_Text__c = raesd.IndexFromUniqueKey_Text__c;
                raesdc.Rental_Num__c  = raesd.Rental_Num__c ;
                // 20211125 ljh 排队 重新分配的配套自动带有“插队”标识 add start
                if(raesd.Is_Body__c){
                    raesdc.Allow_Adjust_Queue_Flag__c = true;
                }
                // 20211125 ljh 排队 重新分配的配套自动带有“插队”标识 add end
                //cancel重新分配的不算追加附属品 272チケット
                raesdc.Create_State__c = 0;
                // 20180126 MTG时确定,NG重新分配时下面两个字段不设值,为了扫描主机二维码时,不检索出来NG重新分配的数据
                //raesdc.Canceled__c = raesd.Id;
                //raesdc.Canceled_Id__c = raesd.Id;
                if (firstraesd == null) {
                    firstraesd = raesdc;
                }
                cloneraesd.add(raesdc);
            }

            // update raesds;

            insert cloneraesd;
            raes.First_RAESD__c = firstraesd.Id;
            update raes;
            // 一览更新完后恢复触发WorkFlow OLY_OCM-409
            ra.NotSendMessageFlag__c = false;
            update ra;
        }
        catch (Exception e) {
            System.debug(e.getStackTraceString());
            Database.rollback(sp);
            return e.getMessage();
        }

        return '1';
    }


}