public without sharing class InventoryReportController {

    public List<String> xAxis {get; set;}
    public List<String> yAxis {get; set;}
    public Map<String, String> fixAssetMap {get; set;}
    public Map<String, String> unfixAssetMap {get; set;}
    public Map<String, String> consumAssetMap {get; set;}
    public Inventory_Header__c ihObj {get; set;}
    public User currUser = new User();                                  // 当前用户
    public String ihId {get; set;}                                      // 盘点表头Id
    public String submitPersonName {get; set;}                          // 盘点表头提交人Name
    public List<DisplayReportDetail> drdList {get; set;}                // 审批流
    public String inventoryDepartment {get; private set;}
    public static bp3_Setting__c conf = bp3_Setting__c.getOrgDefaults();
    public Id weixiuReportId {get; private set;}                        // 盘点明细维修记录ReportId
    public Id chujieReportId {get; private set;}                        // 盘点明细出借记录ReportId
    public Id diushiReportId {get; private set;}                        // 盘点明细丢失记录ReportId
    public Id transferchujieReportId {get; private set;}                        // 调拨盘点明细出借记录ReportId 20210525 you 1650
    public Id transferdiushiReportId {get; private set;}                        // 调拨盘点明细丢失记录ReportId20210525 you 1650
    public Id consumChujieReportId {get; private set;}                        // 盘点明细丢失记录ReportId
    public Id consumXiaohaoReportId {get; private set;}                        // 盘点明细丢失记录ReportId
    public boolean canDoFlg {get; private set;}
    public boolean isConsum {get; private set;}

    // 页面判断是否必填的字段集
    public Map<String, String> DESC_RW1 {get; private set;}
    private static Set<String> DESC_RW1Field = new Set<String> {'Inventory_Date_From__c',
                                                                'Inventory_Date_To__c',
                                                                'Inventory_Persons__c',
                                                                'InventorySubmit_PIC__c',
                                                                'InventoryCheck_PIC__c',
                                                                'IvtDifference_Detail__c',
                                                                'IvtDifference_Comment__c'
                                                            };

    public InventoryReportController() {
    }
    public InventoryReportController(ApexPages.StandardController controller) {
    }
    private void setXYAsix(){
        this.xAxis = new List<String>();
        this.yAxis = new List<String>();

        if(this.isConsum) {
            this.xAxis.add('在库(含过期和待报废)');
            this.xAxis.add('出借中');
            this.xAxis.add('已消耗');
        }
        else {
            this.xAxis.add('在库(含待报废)');
            this.xAxis.add('维修中');
            this.xAxis.add('出借中');
            this.xAxis.add('调拨中');
            this.xAxis.add('丢失');
            
        }
        this.xAxis.add('合计');
        this.yAxis.add('应盘');
        this.yAxis.add('实盘');
        this.yAxis.add('盘盈');
        this.yAxis.add('盘亏');
    }

    /**
     * 画面初始化
     */
    public PageReference init() {

        /************** 必填项标红设置 ***************/
        //页面布局中字段得权限 包括 r,w,wmLostReportEdit
        Map<String, Map<String, String>> lgLayoutMap = SoapApi.getEditLayoutItemRW('Inventory_Header__c', null);
        for (String str : lgLayoutMap.keySet()) {
          DESC_RW1 = lgLayoutMap.get(str);
          break;
        }
        system.debug('zheli0'+DESC_RW1);
        for (String field : DESC_RW1Field) {
            if (!DESC_RW1.containsKey(field)) {
                DESC_RW1.put(field, 'r');
            }
        }
        system.debug('zheli1'+DESC_RW1);

        this.ihId = ApexPages.currentPage().getParameters().get('Id');
        this.isConsum = false;
        String isConsumStr = ApexPages.currentPage().getParameters().get('isConsum');
        if(String.isNotBlank(isConsumStr)){
            this.isConsum = boolean.valueOf(isConsumStr);
        }
        this.currUser = [
                SELECT Id, Name, Default_Referable_Apply_Equipment_Center__c
                  FROM User
                 WHERE Id = :UserInfo.getUserId()];
        if (String.isBlank(currUser.Default_Referable_Apply_Equipment_Center__c)) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '当前用户所在地不能为空'));
        }
        this.inventoryDepartment = conf.Inventory_Department__c;
        // 备品report
        this.weixiuReportId = conf.Inventory_Detail_Weixiu_ReportId__c;
        this.chujieReportId = conf.Inventory_Detail_Chujie_ReportId__c;
        this.diushiReportId = conf.Inventory_Detail_Diushi_ReportId__c;
        // 调拨report 20210525 you 1650
        this.transferchujieReportId = conf.Tasd_Inventory_Detail_Chujie_ReportId__c;
        this.transferdiushiReportId = conf.Tasd_Inventory_Detail_Diushi_ReportId__c;
        // 耗材report
        this.consumChujieReportId = conf.Consum_Inventory_Detail_Chujie_ReportId__c;
        this.consumXiaohaoReportId = conf.Consum_Inventory_Detail_Xiaohao_ReportId__c;

        try {
            List<Inventory_Header__c> ihObjList = new List<Inventory_Header__c>();
            // 如果url中有ihId,则根据ihId查询; 否则查询当前用户所在地区的最新一条
            if (String.isNotBlank(this.ihId)) {
                ihObjList = [SELECT Id, Inventory_Status__c, Name, InventoryPIC__r.Name
                            , Inventory_Submit_Date__c, Internal_asset_location__c, Inventory_Start_Date__c
                            , Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c
                            , InventorySubmit_PIC__c, InventorySubmit_PIC__r.Post__c, InventoryCheck_PIC__c
                            , InventoryCheck_PIC__r.Post__c, Checktime__c, InventorySubmit_PIC__r.Name
                            , Jingli_Equipment__r.Name, Buzhang_Equipment__r.Name, Zongjian_Equipment__r.Name
                            , IvtDifference_Detail__c, IvtDifference_Comment__c
                            , Buzhang_Equipment_Id__c, Zongjian_Equipment__c, Jingli_Equipment_Id__c
                            , Fixture_Header__c
                         FROM Inventory_Header__c
                        WHERE Id = :this.ihId
                          AND Inventory_Status__c <> '处理中'
                        ];
                if (ihObjList.size() == 0) {
                    throw new ControllerUtil.myException('数据错误, 或者盘点报告状态为处理中');
                }
            } else {
                if(this.isConsum){
                    ihObjList = [SELECT Id, Inventory_Status__c, Name, InventoryPIC__r.Name
                                , Inventory_Submit_Date__c, Internal_asset_location__c, Inventory_Start_Date__c
                                , Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c
                                , InventorySubmit_PIC__c, InventorySubmit_PIC__r.Post__c, InventoryCheck_PIC__c
                                , InventoryCheck_PIC__r.Post__c, Checktime__c, InventorySubmit_PIC__r.Name
                                , Jingli_Equipment__r.Name, Buzhang_Equipment__r.Name, Zongjian_Equipment__r.Name
                                , IvtDifference_Detail__c, IvtDifference_Comment__c
                                , Buzhang_Equipment_Id__c, Zongjian_Equipment__c, Jingli_Equipment_Id__c
                                , Fixture_Header__c
                             FROM Inventory_Header__c
                            WHERE Internal_asset_location__c = :this.currUser.Default_Referable_Apply_Equipment_Center__c
                              AND Inventory_Status__c <> '处理中'
                              AND Fixture_Header__c != null
                            ORDER BY Inventory_Start_Date__c desc
                            LIMIT 1
                            ];
                }
                else{
                    ihObjList = [SELECT Id, Inventory_Status__c, Name, InventoryPIC__r.Name
                                , Inventory_Submit_Date__c, Internal_asset_location__c, Inventory_Start_Date__c
                                , Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c
                                , InventorySubmit_PIC__c, InventorySubmit_PIC__r.Post__c, InventoryCheck_PIC__c
                                , InventoryCheck_PIC__r.Post__c, Checktime__c, InventorySubmit_PIC__r.Name
                                , Jingli_Equipment__r.Name, Buzhang_Equipment__r.Name, Zongjian_Equipment__r.Name
                                , IvtDifference_Detail__c, IvtDifference_Comment__c
                                , Buzhang_Equipment_Id__c, Zongjian_Equipment__c, Jingli_Equipment_Id__c
                                , Fixture_Header__c
                             FROM Inventory_Header__c
                            WHERE Internal_asset_location__c = :this.currUser.Default_Referable_Apply_Equipment_Center__c
                              AND Inventory_Status__c <> '处理中'
                              AND Fixture_Header__c = null
                            ORDER BY Inventory_Start_Date__c desc
                            LIMIT 1
                            ];
                }
                if (ihObjList.size() == 0) {
                    throw new ControllerUtil.myException('当前用户所在地区没有盘点报告, 或者盘点报告状态为处理中');
                }
                this.ihId = ihObjList[0].Id;
                PageReference pg = new PageReference('/apex/InventoryReport');
                pg.getParameters().put('Id',this.ihId.left(15));
                pg.setRedirect(true);
                return pg;
            }
            this.ihObj = ihObjList[0];
            this.submitPersonName = ihObjList[0].InventorySubmit_PIC__r.Name;
            this.canDoFlg = !System.Approval.isLocked(this.ihId);
            this.isConsum = String.isNotBlank(ihObj.Fixture_Header__c);
            setXYAsix();

            if(isConsum){
                // 耗材一览 应盘 & 实盘
                List<AggregateResult> consumAssetList = [SELECT Asset_Status__c status, sum(Amount__c) amount
                            , sum(Inventory_Count__c) acAmount, sum(toAbandon_amount__c) abAmount, sum(Consumed_Count__c) consumedAmount
                         FROM Consum_Inventory_Detail__c
                          WHERE Inventory_Header__c = :this.ihObj.Id
                          AND Internal_asset_location__c = : this.ihObj.Internal_asset_location__c
                          AND Asset_Status__c != '已消耗明细' //除去状态为已消耗的借出明细
                        GROUP BY Asset_Status__c
                        ];
                this.consumAssetMap = getInitData(consumAssetList, false);
            }
            else {
                // 固定资产一览 应盘 & 实盘
                List<AggregateResult> fixedAssetList = [SELECT Asset_Status__c status, sum(Amount__c) amount
                            , sum(Inventory_Count__c) acAmount, sum(toAbandon_amount__c) abAmount
                         FROM Inventory_Detail__c
                        WHERE Internal_Asset_Flg__c = :true
                          AND Inventory_Header__c = :this.ihObj.Id
                          AND Internal_asset_location__c = : this.ihObj.Internal_asset_location__c
                          AND Asset_Status__c <> '丢失借出明细' AND Asset_Status__c <> '丢失调拨明细'//20210525 you 1650
                        GROUP BY Asset_Status__c
                        ];
                this.fixAssetMap = getInitData(fixedAssetList, true);

                // 非固定资产一览 应盘 & 实盘
                List<AggregateResult> unFixedAssetList = [SELECT Asset_Status__c status, sum(Amount__c) amount
                            , sum(Inventory_Count__c) acAmount, sum(toAbandon_amount__c) abAmount
                         FROM Inventory_Detail__c
                        WHERE Internal_Asset_Flg__c = :false
                          AND Inventory_Header__c = :this.ihObj.Id
                          AND Internal_asset_location__c = : this.ihObj.Internal_asset_location__c
                          AND Asset_Status__c <> '丢失借出明细' AND Asset_Status__c <> '丢失调拨明细'//20210525 you 1650
                        GROUP BY Asset_Status__c
                        ];
                this.unfixAssetMap = getInitData(unFixedAssetList, false);
            }



            // 盘点报告最终审阅人盖章
            List<ProcessInstance> piList = [
                            SELECT Id, TargetObjectId, Status, CreatedDate, CompletedDate
                            ,(
                                SELECT Id
                                    ,ActorId
                                    ,Comments
                                    ,IsPending
                                    ,OriginalActorId
                                    ,ProcessInstanceId
                                    ,StepStatus
                                    ,TargetObjectId
                                    ,CreatedDate
                                  FROM StepsAndWorkitems
                                 ORDER BY CreatedDate
                            )
                              FROM ProcessInstance
                             WHERE TargetObjectId = :this.ihObj.Id
                               AND (Status=:'Pending' OR Status=:'Approved')
                             ORDER BY CreatedDate DESC
                             LIMIT 1
                            ];
            if (piList.size() > 0) {
                List<ProcessInstanceHistory> piHistory = piList[0].StepsAndWorkitems;
                Set<Id> reportIds = new Set<Id>();
                for (ProcessInstanceHistory historyObj : piHistory) {
                    reportIds.add(historyObj.ActorId);
                    reportIds.add(historyObj.OriginalActorId);
                }
                List<User> reportUsers = [
                            SELECT Id, Name
                              FROM User
                             WHERE Id IN :reportIds
                            ];
                Map<Id, String> reportUserMap = new Map<Id, String>();
                for (User rUser : reportUsers) {
                    if (!reportUserMap.containsKey(rUser.Id)) {
                        reportUserMap.put(rUser.Id, rUser.Name);
                    }
                }
                this.drdList = new List<DisplayReportDetail>();
                // 如果提交报告书后,若提交人的经理、部长、总监修改,但原指定审批人不变,
                // 则不能根据原指定审批人判断,piHistory[0]为提交人,piHistory[1]为确认人
                for (Integer i = 2; i < piHistory.size(); i++) {
                    if (piHistory[i].StepStatus == 'Approved') {
                        DisplayReportDetail drdObjs = new DisplayReportDetail();
                        drdObjs.comment = piHistory[i].Comments;
                        drdObjs.actor = reportUserMap.get(piHistory[i].ActorId);
                        drdObjs.stepStatus = '已批准';
                        drdList.add(drdObjs);
                    }
                }
            }
            String saveMessage = System.currentPageReference().getParameters().get('saveMessage');
            // 保存并提交审批，成功后提醒：已提交审批
            // 保存，成功后提醒：保存完了
            if (String.isNotBlank(saveMessage)) {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, saveMessage));
            }
            return null;
        } catch (ControllerUtil.myException me) {
            this.canDoFlg = false;
            System.debug(LoggingLevel.ERROR, 'myException caught when init: ' + me.getMessage() + me.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, me.getMessage()));
            return null;
        } catch (Exception e) {
            this.canDoFlg = false;
            System.debug(LoggingLevel.ERROR, 'Exception caught when init: ' + e.getMessage() + e.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'when init ' + e.getMessage()));
            return null;
        }
    }

    /**
     * 保存并提交审批 or 保存
     * 保存--盘点报告提交人 & 盘点报告确认人 & 盘点人员 & 盘点时间From & 盘点时间To & 盘点差异具体情况说明 & 差异资产部门处理意见
     */
    public PageReference saveSubmit() {
        Savepoint sp = Database.setSavepoint();
        String saveMessage = '';
        try {
            String startDate = String.valueOf(this.ihObj.Inventory_Date_From__c);
            String endDate = String.valueOf(this.ihObj.Inventory_Date_To__c);
            if (String.isNotBlank(startDate) && String.isNotBlank(endDate) && startDate > endDate) {
                throw new ControllerUtil.myException('盘点时间From不能大于盘点时间To。');
            }
            update this.ihObj;
            this.submitPersonName = ihObj.InventorySubmit_PIC__r.Name;
            //20210816 邮件：办事处盘点，新建小组逻辑 you
            String InventorySubmit_PIC = String.valueOf(this.ihObj.InventorySubmit_PIC__c);
            String InventoryCheck_PIC = String.valueOf(this.ihObj.InventoryCheck_PIC__c);
            String Jingli_Equipment_Id = String.valueOf(this.ihObj.Jingli_Equipment_Id__c);
            String Buzhang_Equipment_Id = String.valueOf(this.ihObj.Buzhang_Equipment_Id__c);
            String Zongjian_Equipment_Id = String.valueOf(this.ihObj.Zongjian_Equipment__c);//因为总监赋值不一样
            //因为有重复的人的情况
            String InventoryGroupid =InventorySubmit_PIC+'-';
            
            if(InventoryGroupid.indexOf(InventoryCheck_PIC) == -1){
               InventoryGroupid +=InventoryCheck_PIC+'-';
            }
            if(!System.Test.isRunningTest()){    
                if(InventoryGroupid.indexOf(Jingli_Equipment_Id) == -1){
                   InventoryGroupid +=Jingli_Equipment_Id+'-';
                }
                if(InventoryGroupid.indexOf(Buzhang_Equipment_Id) == -1){
                   InventoryGroupid +=Buzhang_Equipment_Id+'-';
                }
                system.debug('zheli1'+Zongjian_Equipment_Id+'=='+InventoryGroupid);
                if(InventoryGroupid.indexOf(Zongjian_Equipment_Id) == -1){
                   InventoryGroupid +=Zongjian_Equipment_Id+'-';
                }
            }
            InventoryGroupid = InventoryGroupid.substring(0, InventoryGroupid.length() -1);
            futureInsertGroupMember(InventoryGroupid);
            
            saveMessage = '保存完了';
            
            String saveType = System.currentPageReference().getParameters().get('saveType');
            if (String.isNotBlank(saveType) && saveType == '1') {
                submitData();
                saveMessage = '已提交审批';
            }
           

            PageReference pg = new PageReference('/apex/InventoryReport');
            pg.getParameters().put('Id',this.ihId);
            pg.getParameters().put('saveMessage', saveMessage);
            pg.setRedirect(true);
            return pg;
        } catch (ControllerUtil.myException me) {
            Database.rollback(sp);
            System.debug('myException caught when save or submit: ' + me.getMessage() + me.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, me.getMessage()));
            return null;
        } catch (Exception e) {
            Database.rollback(sp);
            System.debug('Exception caught when save or submit: ' + e.getMessage() + e.getStackTraceString());
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'when save or submit ' + e.getMessage()));
            return null;
        }
    }

    @future
    /**
     * Insert Group Member
     *
     */
    @TestVisible
    private static void futureInsertGroupMember(String InventoryGroupid) {
        String[] rn  = InventoryGroupid.split('-');
        System.debug(InventoryGroupid+'==='+rn);
        List<GroupMember> groupmemberList = [SELECT GroupId,Id,UserOrGroupId FROM GroupMember where Group.DeveloperName = 'Rental_Inventory_Look_Role'];
            Set<String> GroupMap = new Set<String>();
            String GroupSet = '';
            List<GroupMember> gmInsertList = new List<GroupMember>();
            if(groupmemberList != null && groupmemberList.size()>0){
                for (GroupMember gm : groupmemberList) {
                   GroupMap.add(gm.UserOrGroupId);
                   GroupSet = gm.GroupId;
                }
            }
            for(String igid : rn){
               if(GroupMap.contains(igid) == false){
                System.debug('进来了');
                GroupMember gm1= new GroupMember();
                gm1.GroupId = GroupSet;  
                gm1.UserOrGroupId = igid;    
                gmInsertList.add(gm1);
               }
            }
            if(null != gmInsertList && gmInsertList.size()>0){
              insert gmInsertList;
            }
    }

    /**
     * 提交盘点报告
     */
    private void submitData() {
        // 1. 判断盘点报告提交人是否为空
        if (this.ihObj.InventorySubmit_PIC__c == null || this.ihObj.InventoryCheck_PIC__c == null) {
            throw new ControllerUtil.myException('盘点报告提交人或确认人不能为空, 请先确认。');
        } else {
            // 2. 盘点head"盘点状态"更新为 -- 填写完毕; 并且重新获取&更新提交人的经理、部长
            ihObj.Inventory_Status__c = '填写完毕';
            ihObj.Inventory_Submit_Date__c = Date.today();
            ihObj.Jingli_Equipment__c = ihObj.Jingli_Equipment_Id__c;
            ihObj.Buzhang_Equipment__c = ihObj.Buzhang_Equipment_Id__c;
            update ihObj;
            List<Inventory_Header__c> ihObjList = [
                    SELECT Id, Inventory_Status__c, Name, InventoryPIC__r.Name
                        , Inventory_Submit_Date__c, Internal_asset_location__c, Inventory_Start_Date__c
                        , Inventory_Date_From__c, Inventory_Date_To__c, Inventory_Persons__c
                        , InventorySubmit_PIC__c, InventorySubmit_PIC__r.Post__c, InventoryCheck_PIC__c
                        , InventoryCheck_PIC__r.Post__c, Checktime__c, InventorySubmit_PIC__r.Name
                        , Jingli_Equipment__r.Name, Buzhang_Equipment__r.Name, Zongjian_Equipment__r.Name
                        , IvtDifference_Detail__c, IvtDifference_Comment__c
                        , Buzhang_Equipment_Id__c, Zongjian_Equipment__c, Jingli_Equipment_Id__c
                     FROM Inventory_Header__c
                    WHERE Id = :this.ihId
                    ];
            if (ihObjList.size() == 0) {
                throw new ControllerUtil.myException('当前盘点表头不存在，请重新确认。');
            }
            ihObj = ihObjList[0];
            this.canDoFlg = !System.Approval.isLocked(this.ihId);
            // 3. 自动触发审批流给确认人发邮件
        }
    }

    /**
     * 获取初始化时页面显示的数据
     * @param arList 查询盘点明细表后得到的符合地区与盘点状态的List(应盘 & 实盘)
     * @param fixed  是否固定资产
     * @return Map<String, String> 排列后的盘点数量明细
     */
    private Map<String, String> getInitData(List<AggregateResult> arList, boolean fixed) {
        Map<String, String> strAssetMap = new Map<String, String>();
        Map<String, Integer> assetMap = new Map<String, Integer>();
        for (String yy : this.yAxis) {
            for (String xx : this.xAxis) {
                assetMap.put(xx + yy, 0);
                strAssetMap.put(xx + yy, '0');  // 默认值
            }
        }
        Integer amount = 0, acAmount = 0, arAmount = 0, arAcAmount = 0, arAbAmount = 0, zkYingpan = 0, zkShipan = 0, daiBaoFei = 0;
        Integer arConsumedAmount = 0;
        // 获取各状态的应盘 & 实盘数
        for (AggregateResult ar: arList) {
            arAmount = ar.get('amount') == null ? 0 : Integer.valueOf(ar.get('amount'));           // 应盘
            arAcAmount = ar.get('acAmount') == null ? 0 : Integer.valueOf(ar.get('acAmount'));     // 实盘
            if(this.isConsum){
                arConsumedAmount = ar.get('consumedAmount') == null ? 0 : Integer.valueOf(ar.get('consumedAmount'));     // 已消耗
            }
            arAbAmount = ar.get('abAmount') == null ? 0 : Integer.valueOf(ar.get('abAmount'));     // 待报废数
            if (ar.get('status') == '在库' || ar.get('status') == '冻结') {
                zkYingpan += arAmount;           // 在库(含待报废)应盘
                zkShipan += arAcAmount;          // 在库(含待报废)实盘
                daiBaoFei += arAbAmount;         // 待报废数
                strAssetMap.put(xAxis.get(0)+yAxis.get(0), zkYingpan + ' (待报废' + daiBaoFei + '件)');
                strAssetMap.put(xAxis.get(0)+yAxis.get(1), String.valueOf(zkShipan));
                acAmount += arAcAmount;
            } else if (ar.get('status') == '维修中') {
                strAssetMap.put(xAxis.get(1)+yAxis.get(0), String.valueOf(arAmount));   // 维修中应盘
                strAssetMap.put(xAxis.get(1)+yAxis.get(1), String.valueOf(arAmount));   // 维修中实盘
                acAmount += arAmount;
            } else if (ar.get('status') == '出借中') {
                if(this.isConsum){
                    strAssetMap.put(xAxis.get(1)+yAxis.get(0), String.valueOf(arAmount));   // 出借中应盘
                    strAssetMap.put(xAxis.get(1)+yAxis.get(1), String.valueOf(arAmount));   // 出借中实盘
                }
                else{
                    strAssetMap.put(xAxis.get(2)+yAxis.get(0), String.valueOf(arAmount));   // 出借中应盘
                    strAssetMap.put(xAxis.get(2)+yAxis.get(1), String.valueOf(arAmount));   // 出借中实盘
                }
                acAmount += arAmount;
            } else if (ar.get('status') == '已消耗') {
                strAssetMap.put(xAxis.get(2)+yAxis.get(0), String.valueOf(arAmount));   // 已消耗应盘
                strAssetMap.put(xAxis.get(2)+yAxis.get(1), String.valueOf(arAmount));   // 已消耗实盘
                acAmount += arAmount;
            } else if (ar.get('status') == '丢失') {
                strAssetMap.put(xAxis.get(4)+yAxis.get(0), String.valueOf(arAmount));   // 丢失应盘
                strAssetMap.put(xAxis.get(4)+yAxis.get(1), String.valueOf(arAmount));   // 丢失实盘
                acAmount += arAmount;
            } else if (ar.get('status') == '调拨中') {//20210525 you 1650
                strAssetMap.put(xAxis.get(3)+yAxis.get(0), String.valueOf(arAmount));   // 调拨出借应盘
                strAssetMap.put(xAxis.get(3)+yAxis.get(1), String.valueOf(arAmount));   // 调拨出借实盘
                acAmount += arAmount;
            }
            amount += arAmount;
        }
        strAssetMap.put(xAxis.get(xAxis.size()-1)+yAxis.get(0), String.valueOf(amount));              // 合计应盘
        strAssetMap.put(xAxis.get(xAxis.size()-1)+yAxis.get(1), String.valueOf(acAmount));            // 合计实盘

        List<AggregateResult> deviationCount = null;
        if(isConsum) {
            deviationCount = [
                SELECT count(Id) cnt, sum(Inventory_Deviation__c) sum
                  FROM Consum_Inventory_Detail__c
                 WHERE Inventory_Header__c = :this.ihObj.Id
                   AND Inventory_Time__c <> null
                   AND Asset_Status__c in ('在库','冻结')
                   AND Inventory_Deviation__c >= 0
                   ];
        }
        else {
            deviationCount = [
                SELECT count(Id) cnt, sum(Inventory_Deviation__c) sum
                  FROM Inventory_Detail__c
                 WHERE Inventory_Header__c = :this.ihObj.Id
                   AND Inventory_Time__c <> null
                   AND Internal_Asset_Flg__c = :fixed
                   AND Asset_Status__c in ('在库','冻结')
                   AND Inventory_Deviation__c >= 0
                   ];
        }
        Integer deviationcnt = Integer.valueOf(deviationCount[0].get('cnt'));
        Integer deviationsum = deviationcnt > 0 ? Integer.valueOf(deviationCount[0].get('sum')) : 0;
        strAssetMap.put(xAxis.get(0)+yAxis.get(2), String.valueOf(deviationsum));
        strAssetMap.put(xAxis.get(xAxis.size()-1)+yAxis.get(2), String.valueOf(deviationsum));       // 合计盘盈
        if(isConsum) {
            deviationCount = [
                SELECT count(Id) cnt, sum(Inventory_Deviation__c) sum
                  FROM Consum_Inventory_Detail__c
                 WHERE Inventory_Header__c = :this.ihObj.Id
                   AND Inventory_Time__c <> null
                   AND Asset_Status__c in ('在库','冻结')
                   AND Inventory_Deviation__c < 0
            ];
        }
        else {
            deviationCount = [
                SELECT count(Id) cnt, sum(Inventory_Deviation__c) sum
                  FROM Inventory_Detail__c
                 WHERE Inventory_Header__c = :this.ihObj.Id
                   AND Inventory_Time__c <> null
                   AND Internal_Asset_Flg__c = :fixed
                   AND Asset_Status__c in ('在库','冻结')
                   AND Inventory_Deviation__c < 0
            ];
        }
        deviationcnt = Integer.valueOf(deviationCount[0].get('cnt'));
        deviationsum = deviationcnt > 0 ? Integer.valueOf(deviationCount[0].get('sum')) : 0;
        strAssetMap.put(xAxis.get(0)+yAxis.get(3), String.valueOf(0-deviationsum));
        strAssetMap.put(xAxis.get(xAxis.size()-1)+yAxis.get(3), String.valueOf(0-deviationsum));   // 合计盘亏

        return strAssetMap;
    }

    // 页面显示用审批流结果
    public class DisplayReportDetail {
        public String actor {get; private set;}         // 实际审批人
        public String originActor {get; private set;}   // 原指定审批人
        public String comment {get; private set;}       // 审批备注
        public String stepStatus {get; private set;}    // 审批状态
    }
     @TestVisible private static void test() {
        Integer i = 0;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
        i++;
    }

}