//测试类 InventoryTemperatureHumidityCtlTest

public class InventoryTemperatureHumidityController {

	public String bieCunFangDi { get; set; } 
    public Temperature_And_Humidity_Check__c tahcObj { get; set; }
    public String temperatureHumidityZone { get; set; }     //温湿度区
    public String YYYYMM { get; set; }

	public PageReference init() {
		Datetime now = Datetime.now();
        YYYYMM = formatYYYYMM(now.format('YYYYMM'));

        if (String.isBlank(temperatureHumidityZone)) {
            temperatureHumidityZone = 'A区';
        }

		User currUser = [
                Select Id, Name,Dept__c, Default_Referable_Apply_Equipment_Center__c, JingliEquipmentId__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, '当前用户所在地不能为空'));
            return null;
        }

        bieCunFangDi = currUser.Default_Referable_Apply_Equipment_Center__c;
        getTAHCObj();
        return null;
	}

    public void changeZone() {
        getTAHCObj();
    }

    public void getTAHCObj() {

        /*String QueryString = 'SELECT SUM(You_Xiao_Ku_Cun__c) sumQuantity FROM Asset WHERE Internal_asset_location__c = :bieCunFangDi and Delete_Flag__c = False and Quantity > 0 and Asset_Owner__c = \'Olympus\' and Temperature_And_Humidity_Zone__c =: temperatureHumidityZone and ' + FixtureUtil.getAssetSoqlBase();
        AggregateResult[] sumResults = Database.query(QueryString);*/

        Integer sumAsset = 0;
        AggregateResult[] sumResults1 = [Select sum(Amount__c) sumQuantity
                                        From Inventory_Detail__c
                                        WHERE Inventory_Header__r.Inventory_Status__c <> '已批准' 
                                        AND Inventory_Header__r.Internal_asset_location__c =: bieCunFangDi
                                        AND Inventory_Header__r.Inventory_Start_Date__c = LAST_N_DAYS:30
                                        AND Asset_Status__c IN ('在库', '冻结')
                                        AND Asset__r.Temperature_And_Humidity_Zone__c =: temperatureHumidityZone];

        for (AggregateResult ar : sumResults1) {
            if (ar.get('sumQuantity') != null) {
                sumAsset += Integer.valueOf(ar.get('sumQuantity'));
            }
        }

        AggregateResult[] sumResults2 = [Select sum(Amount__c) sumQuantity
                                        From Consum_Inventory_Detail__c
                                        WHERE Inventory_Header__r.Inventory_Status__c <> '已批准' 
                                        AND Inventory_Header__r.Internal_asset_location__c =: bieCunFangDi
                                        AND Inventory_Header__r.Inventory_Start_Date__c = LAST_N_DAYS:30
                                        AND Asset_Status__c IN ('在库', '冻结')
                                        AND Asset__r.Temperature_And_Humidity_Zone__c =: temperatureHumidityZone];

        for (AggregateResult ar : sumResults2) {
            if (ar.get('sumQuantity') != null) {
                sumAsset += Integer.valueOf(ar.get('sumQuantity'));
            }
        }

        List<Temperature_And_Humidity_Check__c> tahcTemp = [
                    SELECT Id, Temperature_And_Humidity_Zone__c, Asset_Count__c, Internal_asset_location__c, Inventory_Time__c, Temperature_Storage_Information__c,
                        Humidity_Storage_Information__c, Temperature_Check_Result__c, Temperature_Note__c, Humidity_Check_Result__c, Humidity_Note__c
                    FROM Temperature_And_Humidity_Check__c 
                    WHERE Internal_asset_location__c =: bieCunFangDi AND Inventory_Time__c =: YYYYMM AND Temperature_And_Humidity_Zone__c =: temperatureHumidityZone];

        if (tahcTemp.isEmpty()) {
            tahcObj = new Temperature_And_Humidity_Check__c();
            tahcObj.Inventory_Time__c = YYYYMM;
            tahcObj.Internal_asset_location__c = bieCunFangDi;
            tahcObj.Asset_Count__c = sumAsset;
            tahcObj.Temperature_And_Humidity_Zone__c = temperatureHumidityZone;
            tahcObj.Temperature_Storage_Information__c = FixtureUtil.zoneTemperatureMap.get(temperatureHumidityZone);
            tahcObj.Humidity_Storage_Information__c = FixtureUtil.zoneHumidityMap.get(temperatureHumidityZone);
        } else {
            tahcObj = tahcTemp[0];
        }
    }

    //温湿度区 SelectOption
    public List<SelectOption> gettemperatureHumidityZoneOps() {

        List<SelectOption> opList = new List<selectOption>();
        List<Schema.PicklistEntry> pick_list_values = Temperature_And_Humidity_Check__c.Temperature_And_Humidity_Zone__c.getDescribe().getPicklistValues();

        for (Schema.PicklistEntry a : pick_list_values) {
            opList.add(new selectOption(a.getValue(), a.getLabel()));
        }
        return opList;
    }

    public String formatYYYYMM(String YYYYMM) {
        return YYYYMM.substring(0, 4) + '年' + YYYYMM.substring(4, 6) + '月';
    }
    
    public PageReference save() {
        if (String.isBlank(tahcObj.Temperature_Check_Result__c)) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请填写温度检查结果'));
            return null;
        }
        if (String.isBlank(tahcObj.Humidity_Check_Result__c)) {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error, '请填写湿度检查结果'));
            return null;
        }
        insert tahcObj;

        return null;
    }
}