/*
 * @Author: zhangchunxu
 * @Date: 2023-07-14 14:23:13
 * @LastEditors: zhangchunxu
 * @LastEditTime: 2023-07-20 13:49:56
 * 
 */
/**
     * @Author    lijinhuan
     * @DateTime  2022-12-28T18:25:42+0800
     * 测试类：FrameNumUploadControllerTest 
*/
global class  SetFrameNumManageWebService {
    /**
     * @fuction   [AuraEnabled]
     * @Author    zhangchunxu
     * @DateTime  2023-07-20T13:50:1+0800
     */
    @AuraEnabled
    /**
     * @fuction   [关联数据]
     * @Author    lijinhuan
     * @DateTime  2023-01-10T11:06:10+0800
     */ 
    WebService static String updateFrameNumManag(String Id) {
        Id execBTId = Database.executeBatch(new SetFrameNumManageBatch(Id), 1);
        return '1';
    }
    /**
     * @fuction   [AuraEnabled]
     * @Author    zhangchunxu
     * @DateTime  2023-07-31T13:50:1+0800
     */
    @AuraEnabled
    /**
     * @fuction   [批量删除]
     * @Author    lijinhuan
     * @DateTime  2023-01-10T11:06:10+0800
     */
    WebService static String BatchDelete(List<String> idList) {
        Savepoint sp = Database.setSavepoint();
        try{
            //sf删除
            Database.delete(idList); 
            return 'success';
        }catch(Exception e){
            Database.rollback(sp);
            System.debug('失败原因是 : ' + e.getMessage());
            return '失败原因是 : ' + e.getMessage();
        }
    }
    /**
     * @fuction   [AuraEnabled]
     * @Author    zhangchunxu
     * @DateTime  2023-07-17T13:27:1+0800
     */
    @AuraEnabled
    /**
     * @fuction   [生成机身号管理对象数据]
     * @Author    lijinhuan0
     * @DateTime  2023-02-07T11:06:1+0800
     */
    WebService static String insertFrameNumManag(String Id,String esId) {
        // return Id+';'+esId;
        Map<String,Statu_Achievements_DN_details__c> sandMap = new Map<String,Statu_Achievements_DN_details__c>();
        Map<String,Statu_Achievements_DN_details__c> UpdateSandMap = new Map<String,Statu_Achievements_DN_details__c>();
        Map<String,String> UpdateFIdMap = new Map<String,String>();
        try{
             // 查询明细 在机身号管理中有没，没有生成,有更新 签收单、发货DN明细、保有设备
            for(Statu_Achievements_DN_details__c dn:[SELECT Id,SerialNumber__c,asset__c,
                                                        Statu_Achievements_DN__c,SerialNoorLotNo_Raw__c,TracingCode_Raw__c,
                                                        OTCode_Raw__c,asset__r.Name,QTY__c,Statu_Achievements_DN__r.Name
                                                        FROM Statu_Achievements_DN_details__c
                                                        WHERE Statu_Achievements_DN__c =:Id ]){
                sandMap.put(dn.TracingCode_Raw__c,dn);
            }
            for(FrameNumManage__c f:[SELECT Id,ManagementCode__c  FROM FrameNumManage__c WHERE ManagementCode__c in :sandMap.keySet()]){
                // 更新机身号管理
                UpdateSandMap.put(f.ManagementCode__c,sandMap.get(f.ManagementCode__c));
                UpdateFIdMap.put(f.ManagementCode__c, f.Id);
                sandMap.remove(f.ManagementCode__c);
            }
            if(sandMap.size() > 0 || UpdateSandMap.size() > 0){
                List<FrameNumManage__c> infnmList = new List<FrameNumManage__c>();
                for (Statu_Achievements_DN_details__c s:sandMap.values()) {
                    FrameNumManage__c temp = new FrameNumManage__c();
                    temp.Name = s.TracingCode_Raw__c;
                    temp.ManagementCode__c = s.TracingCode_Raw__c;
                    temp.SerialNumberS__c = s.SerialNoorLotNo_Raw__c+'('+s.TracingCode_Raw__c+')';
                    temp.SerialNumber__c = s.SerialNoorLotNo_Raw__c;
                    temp.Num__c = s.QTY__c;
                    temp.CheckOutNo__c  = s.Statu_Achievements_DN__r.Name.subString(2);//发货DN 的name去掉前两位的 00
 
                    temp.Statu_Achievements_DN_details__c = s.Id;
                    temp.Asset__c = s.asset__c;
                    temp.signInForm__c = esId;

                    temp.Material__c = s.OTCode_Raw__c;
                    temp.MaterialDepict__c = s.asset__r.Name;

                    
                    infnmList.add(temp);
                }
                if(infnmList.size() > 0){
                    insert infnmList;
                    // Database.executeBatch(new SetFrameNumManageBatch(), 100);
                }
                if(UpdateSandMap.size() > 0){
                    List<FrameNumManage__c> upfnmList = new List<FrameNumManage__c>();
                    for (Statu_Achievements_DN_details__c s:UpdateSandMap.values()) {
                        if(UpdateFIdMap.get(s.TracingCode_Raw__c) != null ){
                            FrameNumManage__c temp = new FrameNumManage__c();
                            temp.id = UpdateFIdMap.get(s.TracingCode_Raw__c);
                            temp.Statu_Achievements_DN_details__c = s.Id;
                            temp.Asset__c = s.asset__c;
                            temp.signInForm__c = esId;
                            upfnmList.add(temp);
                        }
                    }
                    if(upfnmList.size() > 0 ){
                        update upfnmList;
                    }
                }
                // return '1';
            }
            return '1';
            // else{
            //     return '数据已经关联，没有需要关联的数据';
            // }
               
        }catch(Exception e){
            // Database.rollback(sp);
            System.debug('失败原因是 : ' + e.getMessage());
            return '失败原因是 : ' + e.getMessage();
        }
    }
}