/**
 * *
    ODescription:
    GAuthor: sun xia
    @Date: 2023-07-17 14:31:56
    GIastEditors: sun xia
    @IastEditTime: 2023-07-27 14:31:56
 * */
public without sharing class lexRepairAgainAnController{

    @AuraEnabled
    public static InitData initRepair(String recordId){
        InitData res = new InitData();
        try {
            List<Repair__c> reList = [SELECT Id, Name, SAP_Service_Repair_No__c FROM Repair__c WHERE Id = :recordId];
            List<RepairAgainAn__c> raaList = [SELECT Id, Repair__c, RepairAgain_Status__c, ActionOCM__c, Technical_Responsibility__c, 
                                                OCM_Confirmer__c, RepairAgainAn_Approve_status__c, JudgementOSH__c, AnalysisOSH__c, AnalysisReasonOSH__c, ActionOSH__c,
                                                JudgementOSH2__c, AnalysisOSH2__c, AnalysisReasonOSH2__c, ActionOSH2__c, RC_Confirmer__c, RepairLast__c FROM RepairAgainAn__c where Repair__c = :recordId];
            if(reList.size()>0){
                    res.sAPServiceRepairNo = reList[0].SAP_Service_Repair_No__c;      
            }
            if(raaList.size()>0){
                res.repairAgainAnLength = raaList.size();
            }
        }
        catch (Exception e) {
            System.debug('lexRepairAgainAnController1==='+e.getMessage());
        }
        
        
        return res;
    }

    //从修理单获取Id 查有无再修理返品分析
    @AuraEnabled
    public static InitData init(String recordId){
        InitData res = new InitData();
        try {  
            List<Repair__c> reList = [SELECT Id, Name, SAP_Service_Repair_No__c FROM Repair__c WHERE Id = :recordId];
            List<RepairAgainAn__c> raaList = [SELECT Id, Repair__c, RepairAgain_Status__c, ActionOCM__c, Technical_Responsibility__c, 
                                            OCM_Confirmer__c, RepairAgainAn_Approve_status__c, JudgementOSH__c, AnalysisOSH__c, AnalysisReasonOSH__c, ActionOSH__c,
                                            JudgementOSH2__c, AnalysisOSH2__c, AnalysisReasonOSH2__c, ActionOSH2__c, RC_Confirmer__c, RepairLast__c FROM RepairAgainAn__c where Id = :recordId];
            //Repair__c =:recordId AND RepairAgain_Status__c !='取消'
            if(raaList.size()>0){
                res.repairAgainAnLength = raaList.size();
                res.repairAgainStatus = raaList[0].RepairAgain_Status__c;
                res.actionOCM = raaList[0].ActionOCM__c;
                res.oCMConfirmer = raaList[0].OCM_Confirmer__c;
                res.technicalResponsibility = raaList[0].Technical_Responsibility__c;
                res.repairAgainAnApproveStatus = raaList[0].RepairAgainAn_Approve_status__c;
                res.judgementOSH = raaList[0].JudgementOSH__c;
                res.analysisOSH = raaList[0].AnalysisOSH__c;
                res.analysisReasonOSH = raaList[0].AnalysisReasonOSH__c;
                res.actionOSH = raaList[0].ActionOSH__c;
                res.judgementOSH2 = raaList[0].JudgementOSH2__c;
                res.analysisOSH2 = raaList[0].AnalysisOSH2__c;
                res.analysisReasonOSH2 = raaList[0].AnalysisReasonOSH2__c;
                res.actionOSH2 = raaList[0].ActionOSH2__c;
                res.rCConfirmer = raaList[0].RC_Confirmer__c;
                res.repairLast = raaList[0].RepairLast__c;
                res.repairAgainAnName = raaList[0].Name;
            }else{
                res.repairAgainAnLength = 0;
            }

            if(reList.size()>0){
                res.sAPServiceRepairNo = reList[0].SAP_Service_Repair_No__c;      
            }
            System.debug('reList======='+reList);
            System.debug('lexRepairAgainAnController==='+res);
        }
        catch (Exception e) {
            System.debug('lexRepairAgainAnController==='+e.getMessage());
        }
        return res;
    }

    //更新OCSM提交状态
    @AuraEnabled
    public static String updateStatus(String recordId, String updateStatus){
        savepoint sp = Database.setsavepoint();
        RepairAgainAn__c updateRepairAgainAn = new RepairAgainAn__c();
        try {
           updateRepairAgainAn.Id = recordId;
            updateRepairAgainAn.OCSM_Approve_Status__c = updateStatus;
            UPDATE updateRepairAgainAn; 

            //触发审批流
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setobjectid(recordId);
            Approval.ProcessResult submitResult = Approval.process(psr);
            
            return 'SUCCESS';
        }
        catch (Exception e) {
            Database.rollback(sp);
            System.debug('lexRepairAgainAnController===updateStatus'+e.getMessage());
            return e.getMessage();
        }
    }

    //更新再修理返品分析状态
    @AuraEnabled
    public static String updateRepairAgainStatus(String recordId, String updateStatus){
        RepairAgainAn__c updateRepairAgainAn = new RepairAgainAn__c();
        try {
           updateRepairAgainAn.Id = recordId;
            updateRepairAgainAn.RepairAgain_Status__c  = updateStatus;
            UPDATE updateRepairAgainAn; 

            // Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest(); 
            // req.setObjectId(recordId); 
            // // 设置对象ID，可以是要提交审批的记录ID 
            // Approval.ProcessResult result = Approval.process(req); 
            // if (result.isSuccess()) { System.debug('Approval request successfully submitted.'); } 
            // else { System.debug('Approval request submission failed: ' + result.getErrors()[0].getMessage()); }
            return 'SUCCESS';
        }
        catch (Exception e) {
            System.debug('lexRepairAgainAnController===updateRepairAgainStatus'+e.getMessage());
            return 'ERROR';
        }
    }

    //更新OSH审批状态
    @AuraEnabled
    public static String updateRepairAgainAnApproveStatus(String recordId, String updateStatus){
        RepairAgainAn__c updateRepairAgainAn = new RepairAgainAn__c();
        try {
            updateRepairAgainAn.Id = recordId;
            updateRepairAgainAn.RepairAgainAn_Approve_status__c  = updateStatus;
            updateRepairAgainAn.OSH_Detector__c = UserInfo.getUserId();
            UPDATE updateRepairAgainAn; 

            //触发审批流
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setobjectid(recordId);
            Approval.ProcessResult submitResult = Approval.process(psr);

            return 'SUCCESS';
        }
        catch (Exception e) {
            System.debug('lexRepairAgainAnController===updateRepairAgainAnApproveStatus'+e.getMessage());
            return 'ERROR';
        }
    }


    //更新OSH审批状态 二次
    @AuraEnabled
    public static String updateRepairAgainAnApproveStatus2(String recordId, String judgementOSH2){
        RepairAgainAn__c updateRepairAgainAn = new RepairAgainAn__c();
        try {
            updateRepairAgainAn.Id = recordId;
            if(judgementOSH2.equals('故障未再现')){
                updateRepairAgainAn.RepairAgain_Status__c = 'OSH填写完毕';
            }else{
                updateRepairAgainAn.RepairAgainAn_Approve_status2__c = '已提交';
            }
            updateRepairAgainAn.OSH_Detector2__c = UserInfo.getUserId();
            UPDATE updateRepairAgainAn;

            //触发审批流
            Approval.ProcessSubmitRequest psr = new Approval.ProcessSubmitRequest();
            psr.setobjectid(recordId);
            Approval.ProcessResult submitResult = Approval.process(psr);

            return 'SUCCESS';
        }
        catch (Exception e) {
            System.debug('lexRepairAgainAnController===updateRepairAgainAnApproveStatus2'+e.getMessage());
            return 'ERROR';
        }
        
    }

    public class InitData{
        @AuraEnabled
        public Integer repairAgainAnLength;      //再修理返品个数
        @AuraEnabled
        public String repairAgainAnName;
        @AuraEnabled
        public String sAPServiceRepairNo;       //修理.SAP_Service_Repair_No__c
        @AuraEnabled
        public String repairAgainStatus;        //状态
        @AuraEnabled
        public String actionOCM;                //客户对应（OCSM）
        @AuraEnabled
        public String technicalResponsibility;      //技术担当
        @AuraEnabled
        public String oCMConfirmer;             //OCSM确认者
        @AuraEnabled
        public String repairAgainAnApproveStatus;   //OSH审批状态
        @AuraEnabled
        public String judgementOSH;             //结论（OSH）
        @AuraEnabled
        public String analysisOSH;              //技术检测分析（OSH医疗品质保障部填写）
        @AuraEnabled
        public String analysisReasonOSH;        //技术检测分析原因
        @AuraEnabled
        public String actionOSH;                //技术对应
        @AuraEnabled
        public String judgementOSH2;            //结论（OSH）二次
        @AuraEnabled
        public String analysisOSH2;             //技术检测分析（OSH医疗品质保障部填写）二次
        @AuraEnabled
        public String analysisReasonOSH2;       //技术检测分析原因(二次)
        @AuraEnabled
        public String actionOSH2;               //技术对应(二次)
        @AuraEnabled
        public String rCConfirmer;              //
        @AuraEnabled
        public String repairLast;
    }
}