/**********************************************************************
 * 
 *
 * @url: /services/apexrest/rest
 * @data:
 * author :kkbes
 *  {
        
    }
*************************************************************************/

public with sharing class LexSearchProductController {


   
    public class  InitData{
            @AuraEnabled
            public String Asset_Model_No;
            @AuraEnabled
            public String OT_CODE_no_link;
            @AuraEnabled
            public String Name;
            @AuraEnabled
            public String suitDepartment;
            @AuraEnabled
            public String Alternative_Products;
            @AuraEnabled
            public String SFDA_Status;
            @AuraEnabled
            public Decimal Intra_Trade_List_RMB;
            @AuraEnabled
            public Decimal Foreign_Trade_List_US;
            @AuraEnabled
            public Decimal Packing_list_manual;


            @AuraEnabled
            public string VenderName;


            @AuraEnabled
            public Decimal Intra_Trade_Gurantee_RMB;
            @AuraEnabled
            public String GuranteeType;
            @AuraEnabled
            public boolean Estimation_Entry_Possibility;
            @AuraEnabled
            public boolean CostFlag;
            @AuraEnabled
            public Decimal Foreign_Trade_Cost_US;
            @AuraEnabled 
            public Decimal Intra_Trade_Gurantee_USD;
            @AuraEnabled
            public Decimal Intra_Trade_Service_USD;
            @AuraEnabled
            public decimal Maintenance_Price_Year_USD;



            public InitData(Integer i, PricebookEntry pbe){
            this.Asset_Model_No=pbe.Product2.Asset_Model_No__c;
            this.OT_CODE_no_link=pbe.Product2.OT_CODE_no_link__c;
            this.Name=pbe.Product2.Name;
            this.suitDepartment=pbe.Product2.suitDepartment__c;
            this.Alternative_Products=pbe.Product2.Alternative_Products__c;
            this.SFDA_Status=pbe.Product2.SFDA_Status__c;
            this.Intra_Trade_List_RMB=pbe.Product2.Intra_Trade_List_RMB__c;
            this.Foreign_Trade_List_US=pbe.Product2.Foreign_Trade_List_US__c;
            this.Packing_list_manual = pbe.Product2.Packing_list_manual__c;
            this.Intra_Trade_Gurantee_USD = pbe.Product2.Intra_Trade_Foreign_RMB__c;



            //需要的隐藏数据
            this.Intra_Trade_Gurantee_RMB = pbe.Product2.Intra_Trade_Gurantee_RMB__c;
            this.GuranteeType=pbe.Product2.GuranteeType__c;
             if(pbe.Product2.Estimation_Entry_Possibility__c=='×' ){
                    this.Estimation_Entry_Possibility = true;
                }else{
                    this.Estimation_Entry_Possibility = false;
                }
            if(pbe.Product2.VenderName__c==null||
                    pbe.Product2.VenderName__c.trim().length() == 0){
                    this.VenderName =' 无 ';
                }
            else{
                    this.VenderName = pbe.Product2.VenderName__c;
            }
            if(pbe.Product2.Intra_Trade_Cost_RMB__c > 0 ){
                    this.CostFlag = false;
                }
            else{
                    this.CostFlag = true;
            }
            this.Foreign_Trade_Cost_US = pbe.Product2.Foreign_Trade_Cost_US__c;
            this.Intra_Trade_Service_USD = pbe.Product2.NoDiscount_Foreign__c;
            this.Maintenance_Price_Year_USD = pbe.Product2.Repair_Contract_USD__c;

    }
}
     public class ProductLine {
            @AuraEnabled
            public Integer idx;
            @AuraEnabled
            public PricebookEntry pbe;
            @AuraEnabled
            public Decimal Intra_Trade_Cost_RMB;
            @AuraEnabled
            public Decimal Foreign_Trade_Cost_US;
            @AuraEnabled
            public String NameCode;
            @AuraEnabled
            public String Sales_Possibility;
            @AuraEnabled
            public String Bsscategory;
            @AuraEnabled
            public String Product_Id;
            @AuraEnabled
            public String PricebookEntryId;
            @AuraEnabled
            public Decimal Packing_list_manual;
            
            // 多年保修 start
            @AuraEnabled
            public string  GuranteeType;
            @AuraEnabled
            public Decimal Entend_gurantee_period_all;
            @AuraEnabled
            public Decimal Intra_Trade_Gurantee_RMB;
            @AuraEnabled
            public Decimal Intra_Trade_Service_RMB;
             //2021/01/06 liying start
            @AuraEnabled
            public Decimal NoDiscount_Foreign;
            //2021/01/06 liying end
            @AuraEnabled
            public string VenderName;
            @AuraEnabled
            public decimal Maintenance_Price_Year;

            //外贸多年保 取产品主数据上的外贸金额以及维修合同报价 精琢技术 wql 2021/01/04 start
            @AuraEnabled 
            public Decimal Intra_Trade_Gurantee_USD;
            @AuraEnabled
            public Decimal Intra_Trade_Service_USD;
            @AuraEnabled
            public decimal Maintenance_Price_Year_USD;
            //外贸多年保 取产品主数据上的外贸金额以及维修合同报价 精琢技术 wql 2021/01/04 end

            //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 Start
            //是否登录报价
            @AuraEnabled
            public boolean Estimation_Entry_Possibility;
            //成本是否为空
            @AuraEnabled
            public boolean CostFlag;

            //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 end

            //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 start
            @AuraEnabled
            public boolean CanNotCancelledGurantee;
            //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 end

            //增加阿西赛多逻辑 精琢技术 wql 2020/12/29 start
            @AuraEnabled
            public boolean Is_DangerousChemicals;
            //增加阿西赛多逻辑 精琢技术 wql 2020/12/29 end


        
            public ProductLine(Integer i, PricebookEntry pbe) {
                this.idx = i;
                this.pbe = pbe;
                this.Intra_Trade_Cost_RMB = pbe.Product2.Intra_Trade_Cost_RMB__c;
                this.Foreign_Trade_Cost_US = pbe.Product2.Foreign_Trade_Cost_US__c;
                this.NameCode = pbe.Product2.ProductCode;
                this.Sales_Possibility = pbe.Product2.Sales_Possibility__c;
                this.Bsscategory = pbe.Product2.BSSCategory__c;
                this.Product_Id = pbe.Product2Id;
                this.PricebookEntryId = pbe.Id;
                this.Packing_list_manual = pbe.Product2.Packing_list_manual__c;

                // 多年保修 start
                this.GuranteeType = pbe.Product2.GuranteeType__c;
                this.Entend_gurantee_period_all = pbe.Product2.Entend_gurantee_period_all__c;
                this.Intra_Trade_Gurantee_RMB = pbe.Product2.Intra_Trade_Gurantee_RMB__c;
                this.Intra_Trade_Service_RMB= pbe.Product2.Intra_Trade_Service_RMB__c;

                 //2021/01/06 liying start
                this.NoDiscount_Foreign=pbe.Product2.NoDiscount_Foreign__c;
                //2021/01/06 liying end
                this.Maintenance_Price_Year = pbe.Product2.Maintenance_Price_Year__c;
                
                //外贸多年保 取产品主数据上的外贸金额以及维修合同报价 精琢技术 wql 2021/01/04 start 
                this.Intra_Trade_Gurantee_USD = pbe.Product2.Intra_Trade_Foreign_RMB__c;
                this.Intra_Trade_Service_USD = pbe.Product2.NoDiscount_Foreign__c;
                this.Maintenance_Price_Year_USD = pbe.Product2.Repair_Contract_USD__c;
                //外贸多年保 取产品主数据上的外贸金额以及维修合同报价 精琢技术 wql 2021/01/04 end


                if(pbe.Product2.VenderName__c==null||
                    pbe.Product2.VenderName__c.trim().length() == 0){
                    this.VenderName =' 无 ';
                }else{
                    this.VenderName = pbe.Product2.VenderName__c;
                }
                // 多年保修 end

                //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 start
                this.CanNotCancelledGurantee = pbe.Product2.CanNotCancelledGurantee__c;
                //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 end
                //增加阿西赛多逻辑 精琢技术 wql 2020/12/29 start
                this.Is_DangerousChemicals = pbe.Product2.Is_DangerousChemicals__c;
                //增加阿西赛多逻辑 精琢技术 wql 2020/12/29 end


                //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 Start
                //是否登录报价
                System.debug('aaaaaa:'+pbe.Product2.Estimation_Entry_Possibility__c);
                if(pbe.Product2.Estimation_Entry_Possibility__c=='×' ){
                    this.Estimation_Entry_Possibility = true;
                }else{
                    this.Estimation_Entry_Possibility = false;
                }
                //成本是否为空
                system.debug('bbbbb:'+pbe.Product2.Intra_Trade_Cost_RMB__c);
                if(pbe.Product2.Intra_Trade_Cost_RMB__c > 0 ){
                    this.CostFlag = false;
                }else{
                    this.CostFlag = true;
                }
                //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 end
            }
   
    }


   
    @AuraEnabled
    public static List<initData> getData(String SearchName,String trade){
        List<ProductLine> cl =new List<ProductLine>();
        String x = '×';
        String searchSql = 'select Id,Product2Id,Product2.Name,Product2.ProductCode,Product2.StorageStatus__c,Product2.OT_CODE_no_link__c, Product2.Alternative_Products__c, '
                        + 'Product2.Foreign_Trade_Cost_US__c, Product2.Foreign_Trade_List_US__c, Product2.Intra_Trade_Cost_RMB__c, Product2.Intra_Trade_List_RMB__c, '
                        + 'Product2.Asset_Model_No__c, Product2.Serial_Lot_No__c, Product2.Sales_Possibility__c, Product2.Estimation_Entry_Possibility__c, '
                        + 'Product2.SFDA_Status__c, Product2.Qty_Unit__c, Product2.BSSCategory__c, Product2.Asset_loaner_category__c, Product2.Packing_list_manual__c '
                        // 多年保修 start
                        + ' ,Product2.Extend_new_product_gurantee_MD__c , Product2.Extend_new_product_gurantee__c , Product2.Entend_gurantee_period_all__c , '
                        + '  Product2.Intra_Trade_Gurantee_RMB__c  '
                        //外贸多年保 取产品主数据上的外贸金额及报价 精琢技术 wql 2021/01/04 start
                        + ',Product2.Intra_Trade_Foreign_RMB__c,Product2.NoDiscount_Foreign__c,Product2.Repair_Contract_USD__c '
                        //外贸多年保 取产品主数据上的外贸金额及报价 精琢技术 wql 2021/01/04 end
                        //+ ' , Product2.If_Consumable__c '
                        //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 start 阿西赛多 2020/12/29 start 
                        + ' , Product2.Intra_Trade_Service_RMB__c,Product2.CanNotCancelledGurantee__c,Product2.Is_DangerousChemicals__c '
                        //增加产品不可取消多年保逻辑 精琢技术 wql 2020/09/02 end 阿西赛多 2020/12/29 end
                        + ' , Product2.GuranteeType__c , product2.VenderName__c , product2.Maintenance_Price_Year__c '
                        // 多年保修 end
                        // 20221020 ljh SWAG-CJ98AJ start
                        + ' , Product2.suitDepartment__c '
                        // 20221020 ljh SWAG-CJ98AJ end
                        //SFDC停止预警（预计消耗到期日） lt 20210929 add start
                        + ' , Product2.Estimated_ConsumptionDueDate__c '
                        //SFDC停止预警（预计消耗到期日） lt 20210929 add end

                        + 'FROM PricebookEntry ';
                        //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 Start

                        //注释原来逻辑
        String whereStr = 'Where Product2.Asset_Model_No__c != null And Product2.Manual_Entry__c = false And Product2.Estimation_Entry_Possibility__c !=' + '\'' + x + '\' '
                        // String whereStr = 'Where Product2.Asset_Model_No__c != null And Product2.Manual_Entry__c = false '
                        //HWAG-BLWB9F 询价报价：多年保产品未登录计提金额时不可报价 精琢技术 2020/03/03 end
                        + 'AND CurrencyIsoCode =' + '\'' + String.escapeSingleQuotes(trade) + '\' '
                        + 'AND IsActive = true ';
        String whereSql = '';
        if (SearchName != null && SearchName != '') {
            String likename = String.escapeSingleQuotes(SearchName);
            String prdcd = String.escapeSingleQuotes(SearchName);
            if (likename.indexOf('*') >= 0) {
                // TODO escapeは考量してないけど、問題ないと思います。一文字の _ の対応はいらないと思います。
                likename = likename.replace('*', '%');
            } else {
                // default両側あいまい検索
                likename = '%' + likename + '%';
            }
            whereSql += 'and (Product2.Name Like ' + '\'' + likename + '\' or Product2.ProductCode = ' + '\'' + prdcd + '\' or Product2.Asset_Model_No__c Like ' + '\'' + likename + '\') ';
        }
        // Celon询价修改 2022-04-24 ssm start
        // celon = String.isNotBlank(celon) ? celon : null;
        whereSql += ' and Product2.Celon_Product__c = null ';
        // Celon询价修改 2022-04-24 ssm end

        searchSql = searchSql + whereStr + whereSql;

        //20211123 lt update 450 原500
        searchSql += ' order by Product2.Asset_Model_No__c Limit 450';
        System.debug('searchSql::::::' + searchSql);
        System.debug('whereStr::::::' + whereStr);
        System.debug('whereSql::::::' + whereSql);
        List<PricebookEntry> pbes = Database.query(searchSql);
        
        List<initData> res=new List<initData>();
        for (Integer i = 0; i < pbes.size(); i++) {
            initData pl = new initData(i, pbes[i]);
            res.add(pl);
        }

        return res;
        }
}