@isTest
public  class lexConsumabletargetformControllerTest {
    @isTest
    static void Test1(){
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        Date today = Date.today();
        Integer year = today.year();
        Integer month = today.month();
        String fiscalYearValue = '';
        if(month<4){
            fiscalYearValue = 'FY'+String.valueOf(year);
        }else{
            fiscalYearValue = 'FY'+String.valueOf(year+1);
        }
        RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
        User currentUser = [SELECT Alias FROM User WHERE Id = :UserInfo.getUserId()];
        String userAlias = currentUser.Alias;
        Account hospital = new Account(Name = 'テスト病院');
        hospital.RecordTypeId = recByoin.id;
        hospital.Site = 'テスト病院部門';
        hospital.Alias_Name2__c = 'テスト病院別名';
        hospital.ET_owner__c = UserInfo.getUserId();
        hospital.ET_Product_Leader__c = userAlias;
        insert hospital;

        List<ConsumableTargetManage__c> CTMAList = new List<ConsumableTargetManage__c>();
        ConsumableTargetManage__c CTMAobj = new ConsumableTargetManage__c();
        CTMAobj.Category3__c = '基干';
        CTMAobj.Category4__c = '异物钳';
        CTMAobj.TargetNumber__c = 2;
        CTMAobj.StrategicDepartment__c = null;
        CTMAobj.Hospital__c = hospital.Id;
        CTMAobj.PlannedNewOpeningTime__c = Date.today();
        CTMAobj.OCM_Year__c = fiscalYearValue;
        if(CTMAobj.Category3__c=='ENG耗材'){
            CTMAobj.productType__c = '外科耗材';
        }else {
            CTMAobj.productType__c = 'ET耗材';
        }
        CTMAobj.ConsumableTargetManageKey__c = CTMAobj.Hospital__c+'-'+CTMAobj.Category3__c+'-'+CTMAobj.Category4__c+'-'+CTMAobj.OCM_Year__c;
        CTMAList.add(CTMAobj);
        String SubmitManageApprovedata = JSON.serialize(CTMAList);
        String rsualt = lexConsumabletargetformController.SubmitApprovals(SubmitManageApprovedata);
        lexConsumabletargetformController.selectAllConsumableTargetManagecopy(fiscalYearValue);
        List<Map<String,String>> options = lexConsumabletargetformController.getPicklistValues('ConsumableTargetManage__c','productType__c',true);
        List<ConsumableTargetManage__c> rtn = lexConsumabletargetformController.selectAllConsumableTargetManage(fiscalYearValue,options[1].get('value'));
        Set<String> mySet = new Set<String>();
        for (ConsumableTargetManage__c value : rtn) {
            mySet.add(value.H_Salesdepartment_HP__c+'-'+fiscalYearValue+'-'+value.Category3__c+'-'+value.Category4__c);
        }
        
        List<String> arrays = new List<String>();
        arrays.addAll(mySet);
        String ETASPKeyListstr = JSON.serialize(arrays);
        lexConsumabletargetformController.selectETASP(ETASPKeyListstr);
        lexConsumabletargetformController.selectSDepartmentByHospital(hospital.Id);
        List<Address_Level__c> sh = lexConsumabletargetformController.selectState_Master();
        lexConsumabletargetformController.selectCityMasterByStateMaste(sh[0].Id);
        lexConsumabletargetformController.selectConsumableTargetManageByCondition(fiscalYearValue,'ET耗材','新开','','','','');
        ConsumableData a= new ConsumableData();
        List<ConsumableData> ALIST = new List<ConsumableData>();
        a.id = rtn[0].Id;
        a.consumableTargetManageKey = rtn[0].ConsumableTargetManageKey__c;
        a.hospital = rtn[0].Hospital__c;
        a.category3 = rtn[0].Category3__c;
        a.category4 = rtn[0].Category4__c;
        a.issuesAndPlans = rtn[0].IssuesAndPlans__c;
        a.actualUsageNum1 = 1;
        a.actualUsageNum2 = 1;
        a.actualUsageNum3 = 1;
        a.actualUsageNum4 = 1;
        a.actualUsageNum5 = 1;
        a.actualUsageNum6 = 1;
        a.actualUsageNum7 = 1;
        a.actualUsageNum8 = 1;
        a.actualUsageNum9 = 1;
        a.actualUsageNum10 = 1;
        a.actualUsageNum11= 1;
        a.actualUsageNum12= 1;
        ALIST.add(a);
        String ALISTSTR = JSON.serialize(ALIST);
        lexConsumabletargetformController.saveIms(ALISTSTR,fiscalYearValue);
    
    }
    @isTest
    static void myUnitTest() {
        //医院
        Date today = Date.today();
        Integer year = today.year();
        Integer month = today.month();
        String fiscalYearValue = '';
        if(month<4){
            fiscalYearValue = 'FY'+String.valueOf(year);
        }else{
            fiscalYearValue = 'FY'+String.valueOf(year+1);
        }
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
        if (rectHp.size() == 0) {
            return;
        } 
        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', Work_Location__c = '北京', 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);
        
        User thisUser = [select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ){
            insert hpOwner;
            // 省
            Address_Level__c al = new Address_Level__c();
            al.Name = '广东省';
            al.Level1_Code__c = 'CN-99';
            al.Level1_Sys_No__c = '999999';
            insert al;
            // 市
            Address_Level2__c al2 = new Address_Level2__c();
            al2.Level1_Code__c = 'CN-99';
            al2.Level1_Sys_No__c = '999999';
            al2.Level1_Name__c = '广东省';
            al2.Name = '深圳市';
            al2.Level2_Code__c = 'CN-9999';
            al2.Level2_Sys_No__c = '9999999';
            al2.Address_Level__c = al.id;
            insert al2;
            RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
            Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = thisUser.Id);
            hp.Name = 'test hospital';
            hp.RecordTypeId = recByoin.id;
            hp.FSE_GI_Main_Leader__c = thisUser.Id;
            hp.FSE_SP_Main_Leader__c = thisUser.Id;
            hp.State_Master__c = al.id;
            hp.ET_owner__c = UserInfo.getUserId();
            hp.City_Master__c = al2.id;
            insert hp;
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '深圳';
            mp1.Province__c = '广州省';
            mp1.Window1__c = hpOwner.Id;
            mp1.ET_DataWindow__c = hpOwner.Id;
            mp1.ET_BusinessWindow__c = hpOwner.Id;
            mp1.ET_Assistant1__c = hpOwner.Id;
            mp1.ET_Assistant2__c = hpOwner.Id;
            mp1.ENG_DataWindow__c = hpOwner.Id;
            mp1.ENG_BusinessWindow__c = hpOwner.Id;
            insert mp1;
            // List<ConsumableTargetManage__c> CTMAList = new List<ConsumableTargetManage__c>();
            ConsumableTargetManage__c CTMAobj = new ConsumableTargetManage__c();
            CTMAobj.Category3__c = '基干';
            CTMAobj.Category4__c = '异物钳';
            CTMAobj.TargetNumber__c = 2;
            CTMAobj.StrategicDepartment__c = null;
            CTMAobj.Hospital__c = hp.Id;
            CTMAobj.PlannedNewOpeningTime__c = Date.today();
            CTMAobj.OCM_Year__c = fiscalYearValue;
            if(CTMAobj.Category3__c=='ENG耗材'){
                CTMAobj.productType__c = '外科耗材';
            }else {
                CTMAobj.productType__c = 'ET耗材';
            }
            CTMAobj.ConsumableTargetManageKey__c = CTMAobj.Hospital__c+'-'+CTMAobj.Category3__c+'-'+CTMAobj.Category4__c+'-'+CTMAobj.OCM_Year__c;
            // CTMAList.add(CTMAobj);
            insert CTMAobj;
            // 共享计划创建
            String rowCause = Schema.ConsumableTargetManage__Share.RowCause.OCSMToIMSUser__c;
            ConsumableTargetManage__Share tmsshare = new ConsumableTargetManage__Share();
            tmsshare.RowCause= rowCause;//手动
            tmsshare.ParentId = CTMAobj.id;
            tmsshare.UserOrGroupId = hpOwner.Id;
            tmsshare.AccessLevel = 'Read';//编辑
            insert tmsshare;
        }
    }
    @isTest
    static void myUnitTest2() {
        //医院
        Date today = Date.today();
        Integer year = today.year();
        Integer month = today.month();
        String fiscalYearValue = '';
        if(month<4){
            fiscalYearValue = 'FY'+String.valueOf(year);
        }else{
            fiscalYearValue = 'FY'+String.valueOf(year+1);
        }
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and DeveloperName = 'Agency'];
        if (rectHp.size() == 0) {
            return;
        } 
        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', Work_Location__c = '北京', 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);
        
        User thisUser = [select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ){
            insert hpOwner;
            // 省
            Address_Level__c al = new Address_Level__c();
            al.Name = '广东省';
            al.Level1_Code__c = 'CN-99';
            al.Level1_Sys_No__c = '999999';
            insert al;
            // 市
            Address_Level2__c al2 = new Address_Level2__c();
            al2.Level1_Code__c = 'CN-99';
            al2.Level1_Sys_No__c = '999999';
            al2.Level1_Name__c = '广东省';
            al2.Name = '深圳市';
            al2.Level2_Code__c = 'CN-9999';
            al2.Level2_Sys_No__c = '9999999';
            al2.Address_Level__c = al.id;
            insert al2;
            RecordType recByoin = [select id from RecordType where IsActive = true and SobjectType= 'Account' and Name='病院' limit 1];
            Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'hp', OwnerId = thisUser.Id);
            hp.Name = 'test hospital';
            hp.RecordTypeId = recByoin.id;
            hp.FSE_GI_Main_Leader__c = thisUser.Id;
            hp.FSE_SP_Main_Leader__c = thisUser.Id;
            hp.State_Master__c = al.id;
            hp.ET_owner__c = UserInfo.getUserId();
            hp.City_Master__c = al2.id;
            insert hp;
            OCM_Management_Province__c mp1 = new OCM_Management_Province__c();
            mp1.Name = '深圳';
            mp1.Province__c = '广州省';
            mp1.Window1__c = hpOwner.Id;
            mp1.ET_DataWindow__c = hpOwner.Id;
            mp1.ET_BusinessWindow__c = hpOwner.Id;
            mp1.ENG_Assistant1__c = hpOwner.Id;
            mp1.ENG_Assistant2__c = hpOwner.Id;
            mp1.ENG_DataWindow__c = hpOwner.Id;
            mp1.ENG_BusinessWindow__c = hpOwner.Id;
            insert mp1;
            // List<ConsumableTargetManage__c> CTMAList = new List<ConsumableTargetManage__c>();
            ConsumableTargetManage__c CTMAobj = new ConsumableTargetManage__c();
            CTMAobj.Category3__c = 'ENG耗材';
            CTMAobj.Category4__c = 'PK';
            CTMAobj.TargetNumber__c = 2;
            CTMAobj.StrategicDepartment__c = null;
            CTMAobj.Hospital__c = hp.Id;
            CTMAobj.PlannedNewOpeningTime__c = Date.today();
            CTMAobj.OCM_Year__c = fiscalYearValue;
            if(CTMAobj.Category3__c=='ENG耗材'){
                CTMAobj.productType__c = '外科耗材';
            }else {
                CTMAobj.productType__c = 'ET耗材';
            }
            CTMAobj.ConsumableTargetManageKey__c = CTMAobj.Hospital__c+'-'+CTMAobj.Category3__c+'-'+CTMAobj.Category4__c+'-'+CTMAobj.OCM_Year__c;
            // CTMAList.add(CTMAobj);
            insert CTMAobj;
            // 共享计划创建
            String rowCause = Schema.ConsumableTargetManage__Share.RowCause.OCSMToIMSUser__c;
            ConsumableTargetManage__Share tmsshare = new ConsumableTargetManage__Share();
            tmsshare.RowCause= rowCause;//手动
            tmsshare.ParentId = CTMAobj.id;
            tmsshare.UserOrGroupId = hpOwner.Id;
            tmsshare.AccessLevel = 'Read';//编辑
            insert tmsshare;
        }
    }
    public class ConsumableData {
        String id;
        String consumableTargetManageKey;
        String hospital;
        String category3;
        String category4;
        String issuesAndPlans;
        Integer actualUsageNum1;
        Integer actualUsageNum2;
        Integer actualUsageNum3;
        Integer actualUsageNum4;
        Integer actualUsageNum5;
        Integer actualUsageNum6;
        Integer actualUsageNum7;
        Integer actualUsageNum8;
        Integer actualUsageNum9;
        Integer actualUsageNum10;
        Integer actualUsageNum11;
        Integer actualUsageNum12;
    }
}