public class FixtureRemindController {
    public Map<String, Integer> eastMap {set;get;} // 华东
    public Map<String, Integer> eastConsumMap {set;get;} // 华东耗材
    public Map<String, Integer> southMap {set;get;} // 南方
    public Map<String, Integer> northMap {set;get;} // 北方
    public Map<String, Integer> totalMap {set;get;} // 合计
    public Boolean hasEastMap {set;get;} // 华东
    public Boolean hasEastConsumMap {set;get;} // 华东耗材
    public Boolean hasSouthMap {set;get;} // 南方
    public Boolean hasNorthMap {set;get;} // 北方
    public Boolean hasTotalMap {set;get;} // 合计
    public Boolean hasAuthority {set;get;}

    public FixtureRemindUtil frUtil{get;set;}
    public FixtureRemindSnapshot__c snap{get;set;}

    public FixtureRemindController(){
        this.snap = new FixtureRemindSnapshot__c();
        snap.Date__c = System.today();
        //User currentUser = [SELECT UserRole.Name, ProfileId FROM User WHERE Id=: UserInfo.getUserId()];
        User currentUser = [SELECT UserRole.Name, ProfileId FROM User WHERE Id= '00510000001OOJx'];
        this.hasAuthority     = false;
        this.hasNorthMap      = false;
        this.hasSouthMap      = false;
        this.hasEastMap       = false;
        this.hasTotalMap      = false;
        this.hasEastConsumMap = false;
        //20210519 you WLIG-C2J9AA  || currentUser.UserRole.Name == '备品运营部'
        if (System.Label.ProfileId_EquCenAdmin.contains(currentUser.ProfileId) || currentUser.UserRole.Name == '备品运营部'){
            this.hasAuthority     = true;
            this.hasNorthMap      = true;
            this.hasSouthMap      = true;
            this.hasEastMap       = true;
            this.hasTotalMap      = true;
            this.hasEastConsumMap = true;
        }
        else if(currentUser.UserRole.Name == '备品中心北方管理成员') {
            this.hasAuthority     = true;
            this.hasNorthMap = true;
        }
        else if(currentUser.UserRole.Name == '备品中心南方管理成员') {
            this.hasAuthority     = true;
            this.hasSouthMap      = true;
        }
        else if(currentUser.UserRole.Name == '备品中心华东管理成员') {
            this.hasAuthority     = true;
            this.hasEastMap       = true;
            this.hasEastConsumMap = true;
        }
        this.frUtil = new FixtureRemindUtil();
        this.eastMap = this.frUtil.eastMap;
        this.eastConsumMap = this.frUtil.eastConsumMap;
        this.southMap = this.frUtil.southMap;
        this.northMap = this.frUtil.northMap;
        this.totalMap = this.frUtil.totalMap;
    }
    public void init() {
        if(!hasAuthority){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, '您所在的Role无权查看数量'));
            return;
        }
        this.frUtil.init();
    }
    public void refreshTable(){
        if(snap.Time__c == null){
            this.frUtil.init();
            return;
        }
        else if(snap.Date__c != null) {
            this.frUtil.resetAllMaps();
            List<FixtureRemindSnapshot__c> frsList = [

                SELECT Id
                     , Region__c
                     , Date__c
                     , Time__c
                     , Consum_Dai_Chu_Ku_Zhi_Shi_Cnt__c
                     , Consum_Dai_Fa_Huo_Cnt__c
                     , Consum_Dai_Fen_Pei_Cnt__c
                     , Consum_Dai_Que_Ren_Shi_Yong_Biao_Cnt__c
                     , Consum_Dai_Shou_Huo_Cnt__c
                     , Consum_Dao_Huo_NG_Dai_Que_Ren_Cnt__c
                     , Dai_Bao_Xiu_Cnt__c
                     , Dai_CDS_Cnt__c
                     , Dai_Chu_Ku_Qian_Jian_Ce_Cnt__c
                     , Dai_Chu_Ku_Zhi_Shi_Cnt__c
                     , Dai_Fa_Huo_Cnt__c
                     , Dai_Fen_Pei_Cnt__c
                     , Dai_Hui_Shou_Hou_Jian_Ce_Cnt__c
                     , Dai_Qian_Pin_Que_Ren_Cnt__c
                     , Dai_Que_Ren_Qian_Shou_Cnt__c
                     , Dai_Shang_Jia_Cnt__c
                     , Dai_Xia_Jia_Cnt__c
                     , Pai_Dui_Zhong_Cnt__c
                     , Qian_Pin_Zhong_Cnt__c
                     , Zan_Ding_Fen_Pei_Cnt__c
                FROM FixtureRemindSnapshot__c
                WHERE Date__c = : snap.Date__c
                AND Time__c = : snap.Time__c
                //add by rentx 20210722 WLIG-C4VDUX
                AND Rental_ApplyRecordtype__c != '办事处' 
                //add by rentx 20210722 WLIG-C4VDUX
                
            ];

            Boolean northLoaded = false;
            Boolean southLoaded = false;
            Boolean eastLoaded = false;
            for(FixtureRemindSnapshot__c frs:frsList){
                switch on frs.Region__c{
                    when '北方' {
                        loadMap(this.northMap, frs);
                        northLoaded = true;
                    }
                    when '南方' {
                        loadMap(this.southMap, frs);
                        southLoaded = true;
                    }
                    when '华东' {
                        this.eastConsumMap.put('待分配件数', valueOf(frs.Consum_Dai_Fen_Pei_Cnt__c));
                        this.eastConsumMap.put('待出库指示件数', valueOf(frs.Consum_Dai_Chu_Ku_Zhi_Shi_Cnt__c));
                        this.eastConsumMap.put('待发货件数', valueOf(frs.Consum_Dai_Fa_Huo_Cnt__c));
                        this.eastConsumMap.put('到货NG待确认数量',valueOf(frs.Consum_Dao_Huo_NG_Dai_Que_Ren_Cnt__c));
                        this.eastConsumMap.put('待确认试用表数量', valueOf(frs.Consum_Dai_Que_Ren_Shi_Yong_Biao_Cnt__c));
                        this.eastConsumMap.put('待收货件数', valueOf(frs.Consum_Dai_Shou_Huo_Cnt__c));
                        loadMap(this.eastMap, frs);
                        eastLoaded = true;
                    }
                }
            }
            String regions = '';
            if(this.hasEastMap && !eastLoaded){
                regions +=' 华东 ';
            }
            if(this.hasNorthMap && !northLoaded){
                regions +=' 北方 ';
            }
            if(this.hasSouthMap && !southLoaded){
                regions +=' 南方 ';
            }
            if(String.isNotBlank(regions)){
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, '该时间' + regions + '没有快照'));
            }

            for(String key : this.totalMap.keySet()){
                Integer total = this.eastMap.get(key) + this.northMap.get(key) + this.southMap.get(key);
                this.totalMap.put(key, total);
            }
        }
    }
    private void loadMap(Map<String, Integer> fixtureMap, FixtureRemindSnapshot__c frs){
        fixtureMap.put('待分配件数', valueOf(frs.Dai_Fen_Pei_Cnt__c));
        fixtureMap.put('暂定分配件数', valueOf(frs.Zan_Ding_Fen_Pei_Cnt__c));
        fixtureMap.put('排队中件数', valueOf(frs.Pai_Dui_Zhong_Cnt__c));
        fixtureMap.put('待出库指示件数', valueOf(frs.Dai_Chu_Ku_Zhi_Shi_Cnt__c));
        fixtureMap.put('待下架件数', valueOf(frs.Dai_Xia_Jia_Cnt__c));
        fixtureMap.put('待出库前检测件数', valueOf(frs.Dai_Chu_Ku_Qian_Jian_Ce_Cnt__c));
        fixtureMap.put('待发货件数', valueOf(frs.Dai_Fa_Huo_Cnt__c));
        fixtureMap.put('待确认签收单数', valueOf(frs.Dai_Que_Ren_Qian_Shou_Cnt__c));
        fixtureMap.put('待欠品确认件数', valueOf(frs.Dai_Qian_Pin_Que_Ren_Cnt__c));
        fixtureMap.put('待CDS件数', valueOf(frs.Dai_CDS_Cnt__c));
        fixtureMap.put('待回收后检测件数', valueOf(frs.Dai_Hui_Shou_Hou_Jian_Ce_Cnt__c));
        fixtureMap.put('待上架件数', valueOf(frs.Dai_Shang_Jia_Cnt__c));
        fixtureMap.put('欠品中件数', valueOf(frs.Qian_Pin_Zhong_Cnt__c));
        fixtureMap.put('待报修件数', valueOf(frs.Dai_Bao_Xiu_Cnt__c));
    }

    private Integer valueOf(Decimal d) {
        if(d == null) {
            return 0;
        }
        return Integer.valueOf(d);
    }
}