public without sharing class Consum_ApplyUtil {

        //借出备品配套一览状態
    public enum CaStatus {
            Qu_Xiao,
            Shan_Chu,
            Wan_Liao,
            Yi_Hui_Shou,
            Yi_Hui_Ji,
            Yi_Yuan_Yi_Zhuang_Ji_Que_Ren,
            Shen_Qing_Zhe_Yi_Shou_Huo,
            Yi_Chu_Ku,
            Chu_Ku_Qian_Yi_Jian_Ce,
            Yi_Xia_Jia,
            Yi_Chu_Ku_Zhi_Shi,
            Yi_Fen_Pei,
            Qian_Pin_Zhong,
            Cao_An_Zhong,
            Tian_Xie_Wan_Bi,
            Shen_Qing_Zhong,
            Yi_Pi_Zhun,
            Bu_Pi_Zhun
            //申请状态没有待分配
            // Dai_Fen_Pei

    }

    //借出备品配套一览状態
    public enum CaesStatus {
            Cao_An_Zhong,
            Qu_Xiao,
            Yi_Hui_Ku,
            Hui_Shou_Hou_Yi_Jian_Ce,
            Hui_Shou_Hou_Jian_Ce_NG,
            Hui_Shou_Hou_Yi_CDS,
            Yi_Hui_Shou,
            Yi_Hui_Ji,
            Yi_Yuan_Yi_Zhuang_Ji_Que_Ren,
            Shen_Qing_Zhe_Yi_Shou_Huo,
            Shen_Qing_Zhe_Shou_Huo_NG,
            Yi_Chu_Ku,
            Chu_Ku_Qian_Yi_Jian_Ce,
            Yi_Xia_Jia,
            Yi_Chu_Ku_Zhi_Shi,
            Yi_Fen_Pei,
            Dai_Fen_Pei,
            Pai_Dui_Zhong,
            Zan_Ding_Fen_Pei,
            Qian_Pin_Zhong

    }

    //借出备品配套一览明细状態
    public enum CaesdStatus {
            Qu_Xiao,
            Fei_Qi,
            Yi_Hui_Ku,
            Dai_Shang_Jia,
            Hui_Shou_Hou_Yi_CDS,
            Yi_Hui_Shou,
            Yi_Hui_Ji,
            Yi_Yuan_Yi_Zhuang_Ji_Que_Ren,
            Shen_Qing_Zhe_Yi_Shou_Huo,
            Shen_Qing_Zhe_Shou_Huo_NG,
            Yi_Chu_Ku,
            Chu_Ku_Qian_Yi_Jian_Ce,
            Yi_Xia_Jia,
            Yi_Chu_Ku_Zhi_Shi,
            Yi_Fen_Pei,
            Dai_Fen_Pei,
            Qian_Pin_Zhong,
            Yi_Xiao_Hao,
            Diu_Shi,
            Dai_Fei_Qi,
            Yi_Zhi_Bao_Fei_Qu,
            Pai_Dui_Zhong,
            Zan_Ding_Fen_Pei,
            Dai_Xiu_Li,
            Xiu_Li_Zhong
    }


    //备品借出申请
    //RA_Status__c
    public static Map<Integer, String> CaStatusMap = new Map<Integer, String> {
            CaStatus.Qu_Xiao.ordinal()                        => '取消',
            CaStatus.Shan_Chu.ordinal()                       => '删除',
            CaStatus.Wan_Liao.ordinal()                       => '完了', //全部已回库的意思
            CaStatus.Yi_Hui_Shou.ordinal()                    => '已回收',
            CaStatus.Yi_Hui_Ji.ordinal()                      => '已回寄', //old:'归还发送完了',
            CaStatus.Yi_Yuan_Yi_Zhuang_Ji_Que_Ren.ordinal()   => '医院已装机确认', //old:'医院收到确认完了',
            CaStatus.Shen_Qing_Zhe_Yi_Shou_Huo.ordinal()      => '申请者已收货', //old:'申请者收到确认完了',
            CaStatus.Yi_Chu_Ku.ordinal()                      => '已出库', //old:'出库完了',
            CaStatus.Chu_Ku_Qian_Yi_Jian_Ce.ordinal()         => '出库前已检测', //old:'出库前检查完了',
            CaStatus.Yi_Xia_Jia.ordinal()                     => '已下架', //old:'下架完了',
            CaStatus.Yi_Chu_Ku_Zhi_Shi.ordinal()              => '已出库指示', //old:'出库指示完了',
            CaStatus.Yi_Fen_Pei.ordinal()                     => '已分配',
            CaStatus.Qian_Pin_Zhong.ordinal()                 => '欠品中', //欠品确认中
            CaStatus.Cao_An_Zhong.ordinal()                   => '草案中',
            CaStatus.Tian_Xie_Wan_Bi.ordinal()                => '填写完毕',
            CaStatus.Shen_Qing_Zhong.ordinal()                => '申请中',
            CaStatus.Yi_Pi_Zhun.ordinal()                     => '已批准', //old:'批准完了',
            CaStatus.Bu_Pi_Zhun.ordinal()                     => '不批准' //old:'批准完了',
            //申请状态没有待分配
            // CaStatus.Dai_Fen_Pei.ordinal()                   => '待分配'
    };

    //借出备品配套一览状態
    //RAES_Status__c
    public static Map<Integer, String> CaesStatusMap = new Map<Integer, String> {
            CaesStatus.Cao_An_Zhong.ordinal()                 => '草案中',
            CaesStatus.Qu_Xiao.ordinal()                      => '取消',
            CaesStatus.Yi_Hui_Ku.ordinal()                    => '已回库', //old:'上架完了',
            CaesStatus.Hui_Shou_Hou_Yi_Jian_Ce.ordinal()      => '回收后已检测', //old:'归还后检查完了',
            CaesStatus.Hui_Shou_Hou_Jian_Ce_NG.ordinal()      => '回收后检测NG',   //old:'回收后检查NG'
            CaesStatus.Hui_Shou_Hou_Yi_CDS.ordinal()          => '回收后已CDS', //old:'归还后CDS完了',
            CaesStatus.Yi_Hui_Shou.ordinal()                  => '已回收',
            CaesStatus.Yi_Hui_Ji.ordinal()                    => '已回寄', //old:'归还发送完了',
            CaesStatus.Yi_Yuan_Yi_Zhuang_Ji_Que_Ren.ordinal() => '医院已装机确认', //old:'医院收到确认完了',
            CaesStatus.Shen_Qing_Zhe_Yi_Shou_Huo.ordinal()    => '申请者已收货', //old:'申请者收到确认完了',
            CaesStatus.Shen_Qing_Zhe_Shou_Huo_NG.ordinal()    => '申请者收货NG',
            CaesStatus.Yi_Chu_Ku.ordinal()                    => '已出库', //old:'出库完了',
            CaesStatus.Chu_Ku_Qian_Yi_Jian_Ce.ordinal()       => '出库前已检测', //old:'出库前检查完了',
            CaesStatus.Yi_Xia_Jia.ordinal()                   => '已下架', //old:'下架完了',
            CaesStatus.Yi_Chu_Ku_Zhi_Shi.ordinal()            => '已出库指示', //old:'出库指示完了',
            CaesStatus.Yi_Fen_Pei.ordinal()                   => '已分配',
            CaesStatus.Dai_Fen_Pei.ordinal()                  => '待分配',
            CaesStatus.Pai_Dui_Zhong.ordinal()                => '排队中',
            CaesStatus.Zan_Ding_Fen_Pei.ordinal()             => '暂定分配',
            CaesStatus.Qian_Pin_Zhong.ordinal()               => '欠品中'
    };

    //ヒストリ状態
    //RAESD_Status_F__c
    public static Map<Integer, String> raesdStatusMap = new Map<Integer, String> {
            CaesdStatus.Qu_Xiao.ordinal()                       => '取消',
            CaesdStatus.Fei_Qi.ordinal()                        => '废弃',
            CaesdStatus.Yi_Hui_Ku.ordinal()                     => '已回库', //old:'上架完了',
            CaesdStatus.Dai_Shang_Jia.ordinal()                 => '待上架',
            CaesdStatus.Hui_Shou_Hou_Yi_CDS.ordinal()           => '回收后已CDS', //old:'归还后CDS完了',
            CaesdStatus.Yi_Hui_Shou.ordinal()                   => '已回收',
            CaesdStatus.Yi_Hui_Ji.ordinal()                     => '已回寄', //old:'归还发送完了',
            CaesdStatus.Yi_Yuan_Yi_Zhuang_Ji_Que_Ren.ordinal()  => '医院已装机确认', //old:'医院收到确认完了',
            CaesdStatus.Shen_Qing_Zhe_Shou_Huo_NG.ordinal()     => '申请者收货NG',
            CaesdStatus.Shen_Qing_Zhe_Yi_Shou_Huo.ordinal()     => '申请者已收货', //old:'申请者收到确认完了',
            CaesdStatus.Yi_Chu_Ku.ordinal()                     => '已出库', //old:'出库完了',
            CaesdStatus.Chu_Ku_Qian_Yi_Jian_Ce.ordinal()        => '出库前已检测', //old:'出库前检查完了',
            CaesdStatus.Yi_Xia_Jia.ordinal()                    => '已下架', //old:'下架完了',
            CaesdStatus.Yi_Chu_Ku_Zhi_Shi.ordinal()             => '已出库指示', //old:'出库指示完了',
            CaesdStatus.Yi_Fen_Pei.ordinal()                    => '已分配',
            CaesdStatus.Dai_Fen_Pei.ordinal()                   => '待分配',
            CaesdStatus.Qian_Pin_Zhong.ordinal()                => '欠品中',
            CaesdStatus.Yi_Xiao_Hao.ordinal()                   => '已消耗',
            CaesdStatus.Diu_Shi.ordinal()                       => '丢失',
            CaesdStatus.Dai_Fei_Qi.ordinal()                    => '待废弃',
            CaesdStatus.Yi_Zhi_Bao_Fei_Qu.ordinal()             => '待移至报废区',
            CaesdStatus.Pai_Dui_Zhong.ordinal()                 => '排队中',
            CaesdStatus.Zan_Ding_Fen_Pei.ordinal()              => '暂定分配',
            CaesdStatus.Dai_Xiu_Li.ordinal()                    => '待修理',
            CaesdStatus.Xiu_Li_Zhong.ordinal()                  => '修理中'
    };

    public enum CUCUNTYPE {
        You_Xiao_Ku_Cun,
        Yi_Guo_Qi_Ku_Cun,
        You_Xiao_Qi_Ku_Cun
    }

    public static List<SelectOption> getPlickList(String objApi, String fieldApi) {
        Schema.DescribeFieldResult fieldResult = Schema.getGlobalDescribe().get(objApi).getDescribe().fields.getMap().get(fieldApi).getDescribe();
        List<SelectOption> pickListValuesList= new List<SelectOption>();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        pickListValuesList.add(new SelectOption('', system.label.StartTrading_None));
        for( Schema.PicklistEntry pickListVal : ple){
            pickListValuesList.add(new SelectOption(pickListVal.getValue(), pickListVal.getLabel()));
        }
        return pickListValuesList;
    }

    public static void withoutUpsertCaesd(List<Consum_Apply_Equipment_Set_Detail__c> sobj) {
        if (!sobj.isEmpty()) upsert sobj UniqueKey__c;
    }
    public static List<sObject> withoutQueryList(String sqlStr) {
        return Database.query(sqlStr);
    }
    public static List<sObject> withoutQueryListWithConditionList(String sqlStr, List<String> conditionList) {
        return Database.query(sqlStr);
    }
    public static void withoutUpsertCaes(List<Consum_Apply_Equipment_Set__c> sobj) {
        if (!sobj.isEmpty()) upsert sobj UniqueKey__c;
    }

    public static void delCAESD_excludedUpserted(List<Consum_Apply_Equipment_Set__c> pList, List<Consum_Apply_Equipment_Set_Detail__c> upserted) {
        // ToDo 削除時、一部データのkeyを削除対象から外す
        // 一部データとは = Not(申請中) のこと
        List<Consum_Apply_Equipment_Set_Detail__c> dels = [select Id
                from Consum_Apply_Equipment_Set_Detail__c
               where Consum_Apply_Equipment_Set__c IN :pList
                 and Cancel_Select__c = False
                 and Id Not IN :upserted];
        if (!dels.isEmpty()) delete dels;
    }

    public static void delCAES_excludedUpserted(Consum_Apply__c ra, List<Consum_Apply_Equipment_Set__c> upserted) {
        List<Consum_Apply_Equipment_Set__c> dels = [select Id
                from Consum_Apply_Equipment_Set__c
               where Consum_Apply__c = :ra.Id
                 and Cancel_Select__c = False
                 and Id Not IN :upserted];
        if (!dels.isEmpty()) delete dels;
    }

    public static void withoutUpsert(List<Sobject> sobjs) {
        if (!sobjs.isEmpty()) upsert sobjs;
    }

    public static  Map<Date, OlympusCalendar__c> getOlympusCalendarMAp(Date minDate, Date maxDate) {
        Map<String, Schema.SObjectField> fsMap = OlympusCalendar__c.sObjectType.getDescribe().fields.getMap();
        List<String> allFiledList = new List<String>();
        allFiledList.addAll(fsMap.keySet());
        String soql = 'SELECT '
                  + String.join(allFiledList, ',')
                   + ' FROM OlympusCalendar__c'
                  + ' WHERE Date__c >= :minDate'
                     + ' AND Date__c <= :maxDate';
        List<OlympusCalendar__c> calendarList = Database.query(soql);
        Map<Date, OlympusCalendar__c> calendarMap = new Map<Date, OlympusCalendar__c>();
        for (OlympusCalendar__c calendar : calendarList) {
            calendarMap.put(calendar.Date__c, calendar);
        }
        return calendarMap;
    }

    /**
    * @description Get 開始日 + 何日後 的 平日 (只考虑 周六 周日)
    * @param pd 開始日
    * @param i 何日後(平日,+-可能)
    * @return 日付
    **/
    public static  Date getWD_addday(Date pd, Integer i) {
        if (null == pd) pd = System.today();
        if (null == i) i = 0;
        Date d = Date.newInstance(pd.year(), pd.month(), pd.day());
        // Day number of week (1 = Monday, ..., 7 = Sunday)
        Integer dayNumOfWeek = Integer.valueOf(Datetime.newInstance(d.year(), d.month(), d.day()).format('u'));
        if (dayNumOfWeek == 6) {
            if (i < 0) {
                d = d.addDays(2);
            }
            else {
                d = d.addDays(-1);
            }
        }
        else if (dayNumOfWeek == 7) {
            if (i < 0) {
                d = d.addDays(1);
            }
            else {
                d = d.addDays(-2);
            }
        }

        // 这里开始 已经没有 周六 周日 了
        dayNumOfWeek = Integer.valueOf(Datetime.newInstance(d.year(), d.month(), d.day()).format('u'));
        Integer targetAddWeek = Math.rint(i / 5).intValue();
        Integer dateMod = Math.mod(i, 5);
        Integer targetDayNumOfWeek = dayNumOfWeek + dateMod;
        // 这里 targetDayNumOfWeek 一定变成 1 ~ 5 之间
        if (targetDayNumOfWeek <= 0) {
            targetDayNumOfWeek += 5;
            targetAddWeek--;
        }
        else if (targetDayNumOfWeek > 5) {
            targetDayNumOfWeek -= 5;
            targetAddWeek++;
        }
        else {
            // 1, 2 ,3 ,4, 5 就是 平日了
        }

        d = d.addDays(targetAddWeek * 7);
        d = d.addDays(targetDayNumOfWeek - dayNumOfWeek);
        return d;
    }

    public static Map<CUCUNTYPE, Map<String, Integer>> getAssetKucun(List<Asset> assetList, Map<String, Date> dayMap) {
        return getAssetKucun1(assetList, dayMap, false, false);
    }

    public static Map<CUCUNTYPE, Map<String, Integer>> getAssetKucun1(List<Asset> assetList, Map<String, Date> dayMap, Boolean isSnum, Boolean haveTime) {
        Map<CUCUNTYPE, Map<String, Integer>> aMap = new Map<CUCUNTYPE, Map<String, Integer>>();
        Map<String, Integer> assetyouxiaoqiMap = new Map<String, Integer>();
        Map<String, Integer> assetguoqiMap = new Map<String, Integer>();
        Map<String, Integer> assetyouxiaoMap = new Map<String, Integer>();
        for (Asset ass : assetList) {
            String key;
            if (isSnum) {
                key = ass.GroupKey_F__c;
            }
            else {
                key = ass.Fixture_Model_No_F__c;
            }
            if (assetyouxiaoMap.containsKey(key) == false) {
                assetyouxiaoMap.put(key, 0);
            }
            if (ass.Consumable_Guaranteen_end__c != null) {
                if (assetyouxiaoqiMap.containsKey(key) == false) {
                    assetyouxiaoqiMap.put(key, 0);
                    assetguoqiMap.put(key, 0);
                }
                if (dayMap == null
                    || dayMap.containsKey(key) == false
                    || ass.Consumable_Guaranteen_end__c > dayMap.get(key)) {
                    assetyouxiaoqiMap.put(key, assetyouxiaoqiMap.get(key) + Integer.valueOf(ass.You_Xiao_Ku_Cun__c));
                }
                else {
                    assetguoqiMap.put(key, assetguoqiMap.get(key) + Integer.valueOf(ass.You_Xiao_Ku_Cun__c));
                }
            }
            assetyouxiaoMap.put(key, assetyouxiaoMap.get(key) + Integer.valueOf(ass.You_Xiao_Ku_Cun__c));
        }
        aMap.put(CUCUNTYPE.You_Xiao_Qi_Ku_Cun, assetyouxiaoqiMap);
        aMap.put(CUCUNTYPE.Yi_Guo_Qi_Ku_Cun, assetguoqiMap);
        aMap.put(CUCUNTYPE.You_Xiao_Ku_Cun, assetyouxiaoMap);
        return aMap;
    }

    public static List<Sobject> queryTempList(String sqlStr, List<SObject> tempList) {
        return Database.query(sqlStr);
    }

}