@isTest 
public class QuoteTrialControllerTest {
    private static Product2 product = null;
    private static Pricebook2 pricebook = null;


    private static  Opportunity opprtunity = null;

    static final String RC_HOSPITAL = '病院';
    static final String RC_OPP = '引合';
    static final String RC_SENRYAKUKASHITSUBUNRUI = '戦略科室分類 消化科';
    static final String RC_BYOUIN = '病院';
    static final String RC_SHINRYOUKA = '診療科 その他';
    static final String RC_HANBAOITEN = '販売店';
    static final String RC_KEIYAKU = '契約';

    static {
        product = new Product2( Name = 'テスト商品');
        insert product;
        system.debug('##product.Id=[' + product.Id + ']' );

        pricebook = ControllerUtil.getStandardPricebook();
        system.debug('##pricebook.Id=[' + pricebook.Id + ']' );
    }

    private static PricebookEntry buildPB( String input) {
        PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id);

        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = input;
        insert entry;
        return entry;
    }

    public static Opportunity buildOppInstance( String inputTrade, String CurrencyIsoCode) {
        Opportunity target = new Opportunity( Name = 'aiueo', StageName = 'contact', CloseDate = Date.today());
        target.Trade__c = inputTrade;
        target.owner_not_automatically_update__c = true;
        target.CurrencyIsoCode = CurrencyIsoCode;
        target.Wholesale_Price__c = 9000;
        return target;
    }

    private static QuoteLineItem insertQuoteLineItem( Quote input, Pricebookentry entry) {
        QuoteLineItem target = new QuoteLineItem();
        target.Name__c = 'テスト商品';
        target.QuoteId = input.Id;
        target.Quantity = 1;
        target.Cost_Subtotal__c = 0;
        target.Cost__c = 200;
        //        target.TotalPrice = 100;
        target.UnitPrice = 10;
        target.PricebookEntryId = entry.Id;
        insert target;
        return target;
    }


    private static String quoteNo = 'textQutote01';
    public static String trade = '内貿';
    public static String CurrencyIso = 'CNY';

    private static Boolean isUnitPrice = true;
    private static Boolean isOfferAmount = true;
    private static Boolean isTotalPrice = true;
    private static Boolean isDiscountRate = true;
    private static Boolean isDiscountAmount = true;
    private static Boolean isTradingPrice = true;
    private static Boolean isContractDetail = true;

    private static Date offerExpireDate = Date.today();
    private static String clientName = 'test client';
    private static String offerComment = 'test comment';
    private static Date offerPrintDate = Date.today();
    private static String agentName = 'test agent name';


    public class GeDatass {
          public  String Id;//产品编码(产品代码)
          public String JxsType;
          public String Categoryc;
          public String GuaranteeDiscountcInput;
          public String NormalDiscountcInput;
          public String PromotionNocEqual;
          public String typess;
          public String PromotionNoc;
          public String Name;
          public String itemIdStr;
          public String IsTempItems;
          public String itemCounts;
          public String ParamIdStr;
          public String GuaranteeDiscountc;
          public String NormalDiscountc;
          public String Descriptionc;
          public Boolean ifContainNodc;
          public Boolean ifFixc;
          public Double PriceCNYc;
          public Double Total;
          public Double HeTongTotal;
          public Boolean ifNecessaryc;
          public Decimal maxCounts;
          public Double ListPriceTotalc;
          public Double sumNod;
          public String CompareId;
          public String Trade;
          public String sumNoDiscountTotal;
          public Double GuaranteeDiscountHMoneyc;
          public Double NormalDiscountHMoneyc;
      }

      public class Discount {
          public  String Id;//产品编码(产品代码)
          public String AssetModelNoc;
          public String Namec;
          public Integer Quantity;
          public String IGuranteeTypecd;
          public String PromotionNoc;
          public String Name;
          public Double ListPrice;
          public Double AgencyUnitPricec;
          public Double AgencySubtotalc;
          public Double NoDiscountTotalc;
          public String ParamIdStr;
          public String TypeName;
          public String UUID;
          public String ismatch;
          public String PromotionId;
          public Integer UseCountc;
          public String Product2c;
          public Double ServicePricec;
          public Boolean ifFixc;
          public Boolean ifNecessaryc;
          public String CompareId;
          public Double DiscountcInput;
          public String Categoryc;
          public String NormalDiscountcInput;
          public String GuaranteeDiscountcInput;
          public Boolean multiYearWarrantyc;
      }


    static testMethod void TestInit01() {
        String userId = UserInfo.getUserId();
        User u1 = [SELECT Id, Quote_Correct__c, Quote_Special_Operation__c, Cost_Referable__c FROM User WHERE Id = :userId];
        u1.Quote_Correct__c = false;
        u1.Quote_Special_Operation__c = true;
        u1.Cost_Referable__c = true;
        update u1;

        // PricebookEntry
        PricebookEntry entryUSD = buildPB( 'USD');
        PricebookEntry entryCNY = buildPB( 'CNY');

        // 引合作成
        Opportunity opp = buildOppInstance( trade, CurrencyIso);
        opp.Trade__c = trade;
        opp.owner_not_automatically_update__c = true;
        opp.Sales_Root__c = 'AAA';
        opp.Pricebook2Id = pricebook.id;
        insert opp;

        //見積データ作成
        Quote quote = new Quote( Name = 'テスト見積');
        quote.OpportunityId = opp.Id;
        quote.Pricebook2Id = pricebook.id;
        quote.Quote_No__c = quoteNo;

        quote.Unit_Price__c = isUnitPrice;
        quote.Offer_Amount__c = isOfferAmount;
        quote.TOTAL__c = isTotalPrice;
        quote.Discount__c = isDiscountRate;
        quote.Pricing__c = isDiscountAmount;
        quote.Preferential_Trading_Price__c = isTradingPrice;
        quote.Contract__c = isContractDetail;
        quote.Quote_Comment__c = offerComment;
        insert quote;
        quote = [SELECT Id, OpportunityId, Quote_no__c, Quote_No_Auto__c, CurrencyIsoCode FROM Quote WHERE Id = :quote.id];
        System.assertEquals( CurrencyIso, quote.CurrencyIsoCode);

        // 見積品目作成
        QuoteLineItem item01 = insertQuoteLineItem( quote, entryCNY);
        QuoteLineItem item02 = insertQuoteLineItem( quote, entryCNY);

        //创建促销方案 / 记录类型=促销方案
        PromotionHead__c head1 = new PromotionHead__c();
        head1.Name = '202';
        head1.PromotionNo__c = '202';
        head1.Category__c = '固定价格';
        head1.if_Contain_Nod__c = true;
        head1.RecordTypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'PromotionHead__c' and DeveloperName = 'Promotion'].id;
        head1.DateFrom__c = Date.today().addDays(-1);
        head1.DateTo__c = Date.today().addDays(1);
        head1.Description__c = '（202）153P-GI-001-05 奥辉（AXEON）-M1层以下 胃肠（CF-LV1I）标套（MX192适用）';
        head1.Memo__c = 'CV-V1(A)/GIF-LV1/CF-LV1I/MX192彩色液晶显示器/TC-A4/MU-1/MB-155';
        head1.Price_CNY__c = 254000.00;
        head1.Price_USD__c = 20000.00;
        insert head1;


        //创建促销方案 / 记录类型=一般产品
        PromotionHead__c head2 = new PromotionHead__c();
        head2.Name = '203';
        head2.PromotionNo__c = '203';
        head2.Category__c = '固定价格';
        head2.if_Contain_Nod__c = true;
        head2.RecordTypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'PromotionHead__c' and DeveloperName = 'NormalProduct'].id;
        head2.DateFrom__c = Date.today().addDays(-1);
        head2.DateTo__c = Date.today().addDays(1);
        head2.Description__c = '（202）153P-GI-001-05 奥辉（AXEON）-M1层以下213-LV1I）标套（MX192适用）';
        head2.Memo__c = 'CV-V1(A)/GIF-LV1/CF-LV1I/MX192彩色液晶显示器/TC-A4/前21 -155';
        head2.Price_CNY__c = 254000.00;
        head2.Price_USD__c = 20000.00;
        insert head2;

        //创建促销方案 / 记录类型=特约固定
        PromotionHead__c head3 = new PromotionHead__c();
        head3.Name = '203';
        head3.PromotionNo__c = '203';
        head3.Category__c = '固定价格';
        head3.if_Contain_Nod__c = true;
        head3.RecordTypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'PromotionHead__c' and DeveloperName = 'Authorizer'].id;
        head3.DateFrom__c = Date.today().addDays(-1);
        head3.DateTo__c = Date.today().addDays(1);
        head3.Description__c = '（202）153P-GI-001-05 奥辉（AXEON）-M1层以下213-LV1I）标套（MX192适用）';
        head3.Memo__c = 'CV-V1(A)/GIF-LV1/CF-LV1I/MX192彩色液晶显示器/TC-A4/前21 -155';
        head3.Price_CNY__c = 254000.00;
        head3.Price_USD__c = 20000.00;
        insert head3;


        //创建促销方案产品
        PromotionProduct__c pro1 = new PromotionProduct__c();
        pro1.Name = 'TEST1';
        pro1.PromotionHead__c = head1.Id;
        // pro1.Product__c = product.Id;
        pro1.Quantity__c = 2;
        insert pro1;

        //创建 测试_报价促销方案  
        PromotionSales__c sales1 = new PromotionSales__c();
        sales1.Name = 'test1';
        sales1.PromotionNo__c  = '203';
        sales1.if_Contain_Nod__c = true;
        // sales1.ParamIdStr__c = quote.Id;
        insert sales1;

        //创建 测试_报价促销产品
        PromotionSalesProducts__c salesPro = new PromotionSalesProducts__c();
        // salesPro.ParamIdStr__c = quote.Id;
        insert salesPro;

        //保存所选方案，价格政策和折扣政策，JsonStr 是要保存的数据的转成的json字符串,ParamIdStr 保存方案表的id
        List<GeDatass> testList = new List<GeDatass>();
        GeDatass data = new GeDatass();
        data.Id      = head1.Id;
        data.JxsType = 'testType';
        data.Categoryc = 'testcategory';
        data.GuaranteeDiscountcInput = 'testinput';
        data.NormalDiscountcInput = 'testnormal';
        data.PromotionNocEqual ='testequal';
        data.typess = 'testtypess';
        data.PromotionNoc = 'testpromotion';
        data.Name = '202';
        data.itemIdStr = 'test12';
        data.IsTempItems = 'testtemp';
        data.itemCounts = 'testitemcounts';
        data.ParamIdStr = quote.Id;
        data.GuaranteeDiscountc = 'testguaranteediscount';
        data.NormalDiscountc = 'testnormal';
        data.Descriptionc = 'testdesc';
        data.ifContainNodc = true;
        data.ifFixc = true;
        data.PriceCNYc = 12.21;
        data.Total     = 22.22;
        data.HeTongTotal = 222.22;
        testList.add(data);
        String jsonStr = JSON.serialize(testList);


        //保存所选产品和未选产品，JsonStr 是要保存的数据的转成的json字符串,ParamIdStrs 保存产品表的id
        List<Discount> disList = new List<Discount>();
        Discount dis = new Discount();
        dis.Id = 'a4S1m0000004r4AEAQ';
        dis.AssetModelNoc = 'testno';
        dis.Namec = 'testname';
        dis.Quantity = 12;
        dis.IGuranteeTypecd = 'testtype';
        dis.PromotionNoc = 'testpromotion';
        dis.Name = 'testname';
        dis.ListPrice = 12.22;
        dis.AgencyUnitPricec = 14.44;
        dis.AgencySubtotalc = 23.32;
        dis.NoDiscountTotalc = 99.32;
        dis.ParamIdStr = quote.Id;
        dis.TypeName = 'testtypename';
        dis.UUID = '12achuvqwh';
        dis.ismatch = 'test';
        dis.PromotionId = head1.Id;
        dis.multiYearWarrantyc = true;
        disList.add(dis);
        String jsonStr1 = JSON.serialize(disList);
        //几个政策Id集合
        List<String> jiaGe=new List<String>();
        jiaGe.add('321654');
        jiaGe.add('234234');
        jiaGe.add('231636');
        //1.获取报价数据
        QuoteTrialController.GetQuoteData(quote.Id);
        //2.获取待选择产品
        QuoteTrialController.GetPromotionDefalut(quote.Id);
        //3.获取促销方案
        QuoteTrialController.GetPromotionPromotionSearch(1,1,'','','');
        //4.获取固定资产
        QuoteTrialController.GetNormalProductSearch(1,1,'','','');
        //5.获取特约折扣
        QuoteTrialController.GetAuthorizerSearch(1,1,'','','','');
        //6.产品对应的Item
        QuoteTrialController.GetSearchProductById(head1.Id);
        //7.获取所有PromotionSales__c表中的数据
        QuoteTrialController.selectAllDataProduct(quote.Id);
        //8.获取所有PromotionSalesProducts__c表中的数据
        QuoteTrialController.selectAllDataDiscount(quote.Id);
        //9.保存所选方案，价格政策和折扣政策，JsonStr 是要保存的数据的转成的json字符串,ParamIdStr 保存方案表的id
        Double ContractPrice=12.12;
        Double DealerFinalPriceFc=100000.00;
        Double Agent1Agent2Pricec=10000.0;
        Double Agent1Agent2Pricec1=null;
        QuoteTrialController.saveAllDataProduct(jsonStr,(String)quote.Id,jsonStr1,(String)item01.QuoteId,'販売店',ContractPrice,(String)quote.OpportunityId,DealerFinalPriceFc,Agent1Agent2Pricec);
        QuoteTrialController.saveAllDataProduct(jsonStr,(String)quote.Id,jsonStr1,(String)item01.QuoteId,'販売店',ContractPrice,(String)quote.OpportunityId,DealerFinalPriceFc,Agent1Agent2Pricec1);
        QuoteTrialController.saveAllDataProduct(jsonStr,(String)quote.Id,jsonStr1,(String)item01.QuoteId,'OCM直接販売',ContractPrice,(String)quote.OpportunityId,DealerFinalPriceFc,Agent1Agent2Pricec);
        QuoteTrialController.selectUniqueKey((String)quote.Id);
        //10.判断所处数据是否为空
        QuoteTrialController.GetParamToId('123');
        QuoteTrialController.GetParamToId('');
        //11.测试清空表中数据，非正式环境
        QuoteTrialController.deleteAlltable();
        //12.保存所选产品和未选产品，JsonStr是要保存的数据的转成的json字符串,ParamIdStrs 保存产品表的id
        // QuoteTrialController.saveAllDataDiscount(jsonStr1,quote.Id);
        //13.测试清空表中数据，非正式环境
        QuoteTrialController.deleteAllDiscounttable();
        //14.获取经销商姓名
        QuoteTrialController.GetAgencyRName();
        //15.根据保存的Id查询 Header 表的数据
        QuoteTrialController.selectUpdateFiexedpriceData(jiaGe);
        QuoteTrialController.selectUpdateQuoteLineItemData(jiaGe);
    }

}