/**
 * @description       : 
 * @author            : ChangeMeIn@UserSettingsUnder.SFDoc
 * @group             : 
 * @last modified on  : 07-19-2023
 * @last modified by  : ChangeMeIn@UserSettingsUnder.SFDoc
**/
global without sharing class AssetWebService {

    private static Set<String> assStatus = new Set<String>{'不明','废弃','未使用', '廃棄'};
    @AuraEnabled
    public static string methodName(){
        try {
            
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
    WebService static String createLostReport(String assetId) {
        return createLostReportCheck(assetId, true);
    }

    public static String createLostReportCheck(String assetId, Boolean isWeb) {
        try {
            List<Asset> asList = [SELECT Id
                                       , Account.Management_Code__c
                                       , Status
                                       , Asset_loaner_category__c
                                       , Internal_Asset_number_key__c
                                    FROM Asset
                                   WHERE id = :assetId];
            if (asList.size() == 0) {
                return '2:保有设备不存在,请刷新画面重试';
            }
            else {
                Asset ass = asList[0];
                if (ass.Account.Management_Code__c != System.Label.Account_Asset_FJZ) {
                    return '2:集中管理备品无法通过此路径创建遗失报告';
                }
                else if (String.isBlank(ass.Internal_Asset_number_key__c)) {
                    return '2:此保有设备不是固定资产,无法创建遗失报告';
                } else if (assStatus.contains(ass.Status)) {
                    return '2:此保有设备的状态不符合创建遗失报告的条件,无法创建遗失报告';
                }
            }
            List<LostReport__c> lrList = [SELECT Id, Name
                                            FROM LostReport__c
                                           WHERE Asset__c = :assetId
                                             AND Status__c != '取消提交'];
            if (lrList.size() > 0) {
                List<LostReport_Detail__c> ldList = [SELECT Id
                                                       FROM LostReport_Detail__c
                                                      WHERE LostReport__c = :lrList[0].Id
                                                        AND Asset__c = :assetId
                                                        AND CancelLostReport__c = false];
                if (ldList.size() > 0) {
                    return '2:已经创建遗失报告明细无需再次创建';
                }
            }
            return '1:' + assetId;
        }
        catch(Exception e) {
            return '2:' + e.getMessage();
        }
    }

    /**
     * 转换办事处到省
     * @param str 存放地
     * @return OCSM管理省名字
     */
    WebService static String getOcmMgtProvStr(String str) {
        Map<String, String> ocmMgtProvMap = AssetWebService.getOcmMgtProvMap();
        String res = '';
        System.debug(ocmMgtProvMap);
        System.debug(str);
        if (ocmMgtProvMap.containsKey(str)) {
            res = ocmMgtProvMap.get(str);
        }
        return res;
    }

    /**
     * 从 Label.OCM_Management_Province_Mapping
     * 取出 存放地 → 省 的 Map
     */
    public static Map<String, String> getOcmMgtProvMap() {
        Map<String, String> ocm_Map = new Map<String, String>();
        String OCM_Management_Province_Mapping = System.Label.OCM_Management_Province_Mapping;
        for (String str : OCM_Management_Province_Mapping.split(',')) {
            List<String> strList = str.split(':');
            ocm_Map.put(strList[1].trim(), strList[0].trim());
        }
        return ocm_Map;
    }
}