@isTest
private class AccountTargetTabControllerTest {

    static testMethod void myUnitTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        // layout作成
        Target_account_manage__c layout = new Target_account_manage__c();
        layout.Name = 'GI-Case';
        layout.department_class__c = '消化科';
        layout.field1__c = '{"label":"CV-290","api":"CV290"}';
        layout.field2__c = '{"label":"CV-170","api":"CV170"}';
        layout.field3__c = '{"label":"EU-ME2","api":"EUME2"}';
        insert layout;

        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'AccountCaseHistoryTestHp1', OwnerId = hpOwner.Id);
        insert hp;
        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c in ('消化科')];

        // 病例数作る
        Account_Number_of_target__c ac1 = new Account_Number_of_target__c(
            Account__c = dc1s[0].Id,
            Account_HP__c = hp.Id,
            OCM_Period__c = '146P',
            OCM_Year__c = '2013年度'
        );
        insert ac1;

        PageReference page = new PageReference('/apex/AccountTargetTab?id=' + ac1.Id + '&accid=' + dc1s[0].Id + '&t=146P');
        System.Test.setCurrentPage(page);
        AccountTargetTabController controller = new AccountTargetTabController();
        controller.init();

        System.assertNotEquals(null, controller.history);
        // update
        controller.history.rec.Additional_comment__c = 'aaaaa';
        controller.save();

        page = new PageReference('/apex/AccountTargetTab?id=&accid=' + dc1s[0].Id + '&t=');
        System.Test.setCurrentPage(page);
        controller = new AccountTargetTabController();
        controller.init();
        // insert
        controller.history.rec.OCM_Year__c = '2014年度';
        controller.save();

        List<Account_Number_of_target__c> acs = [select Id from Account_Number_of_target__c where Account__c = :dc1s[0].Id];
        System.assertEquals(3, acs.size());
    }

    private static integer FIELDMAX = 10;

    // 病例数設定に項目が正しいかどうか
    @isTest(SeeAllData=true)
    static void makeSqlTest() {
        List<Target_account_manage__c> layoutList = Target_account_manage__c.getall().values();
        for (Target_account_manage__c layout : layoutList) {
            // 病例数のみチェック
            if (layout.Name.startsWith('目标客户管理-')) {
                String soql = 'select Id';
                for (Integer i = 1; i <= FIELDMAX; i++) {
                    String strI = 'field' + i + '__c';
                    String fieldStr = String.valueOf(layout.get(strI));

                    if (String.isBlank(fieldStr) == false) {
                        Map<String, Object> m = (Map<String, Object>) JSON.deserializeUntyped(fieldStr);
                        String api = String.valueOf(m.get('api'));

                        if (String.isBlank(api) == false) {
                            // soql += ', Target_amount_' + api + '__c';
                            // soql += ', OPD_' + api + '__c';
                            // soql += ', OPD_amount_' + api + '__c';
                        }
                    }
                }
                soql += ' from Account_Number_of_target__c limit 1';
                List<Account_Number_of_target__c> tmpList = Database.query(soql);
                // ExceptionがなければOK
                System.assertEquals(1, tmpList.size());
            }
        }
    }
}