liangxiaozhen
2023-08-06 7454e4fe769b5148309b8a932fbf2bc03b41b376
force-app/main/default/lwc/inventoryEditingLWC/inventoryEditingLWC.js
@@ -1,33 +1,76 @@
import { LightningElement, track,api } from 'lwc';
import Product_Number_FIELD from '@salesforce/schema/Dealer_Stock__c.Product_Code__c';
import ONE_FIELD from '@salesforce/schema/Dealer_Stock__c.Product_Code__c';
import TWO_FIELD from '@salesforce/schema/Dealer_Stock__c.Product_Code__c';
import oninit from "@salesforce/apex/LexProductLimitEditController.init";
import save from "@salesforce/apex/LexUpAccountProLimit.upAccount";
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class InventoryEditingLWC extends LightningElement {
    // @track invRecord = {ProductNumber : Product_Number_FIELD,Inventory1: ONE_FIELD,Inventory2 : TWO_FIELD,key : Math.random().toString(36).substring(2, 15)};
    @track invRecords = [];
    toSaveLabel = 'Save';
    @track accountid;
    @track upstring = '';
    @track userPro_Type;
    @track showSpinner = true;
    //获取链接参数
   getQueryString(name) {
      console.log("getQueryString name " + name);
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) {
         return decodeURIComponent(r[2]);
      }
      return null;
   }
    connectedCallback(){
        this.showSpinner = true;
        console.log('  === connectedCallback === ');
        this.inventoryTempRecords();
    }
    inventoryTempRecords(){
        console.log('===>初始化');
        this.invRecord = [];
        // for(var i=0; i < 1 ; i++){
        //     this.invRecords.push({ProductNumber : Product_Number_FIELD,Inventory1: ONE_FIELD,Inventory2 : TWO_FIELD,key : Math.random().toString(36).substring(2, 15)});
        // }
        //获取accountid userPro_Type
      this.accountid = this.getQueryString("accountid");
        this.accountid = this.accountid == null ? "" : this.accountid;
        this.userPro_Type = this.getQueryString("userPro_Type");
      this.userPro_Type = this.userPro_Type == null ? "" : this.userPro_Type;
             //获取数据
             oninit({
                accountid:this.accountid,
                userPro_Type:this.userPro_Type
             }).then((result) => {
                result = JSON.parse(JSON.stringify(result));
                console.log('result'+JSON.stringify(result));
                if(result.status == 'Success'){
                    console.log('11===>'+result.entity.product_Limit);
                    console.log('11===>'+typeof(result.entity.product_Limit));
                    let str =  result.entity.product_Limit;
                    var arr  = str.split(',');
                    console.log('arr==>'+ arr);
                    console.log('arr==>'+ arr.length);
                    console.log('arr==>'+ typeof(arr));
                    for(var i in arr){
                        var arry = arr[i].split('|');
                        this.invRecords.push({
                            productNumber: arry[0],
                            inventory1:arry[1],
                            inventory2:arry[2]
                        });
                    }
                    this.showSpinner = false;
                    console.log('this.invRecords'+JSON.stringify(this.invRecords));
                }else{
                    this.showSpinner = false;
                    console.log("error = " + JSON.stringify(error));
                }
            }).catch((error) => {
                this.showSpinner = false;
            console.log("error = " + JSON.stringify(error));
         });
        // this.inventoryTempRecords();
    }
    addRow(){
        // console.log('=====>进入行项目');
        // const len = this.invRecords.length;
        // this.invRecords.push({ProductNumber : Product_Number_FIELD,Inventory1: ONE_FIELD,Inventory2 : TWO_FIELD,key : Math.random().toString(36).substring(2, 15)});
        let objRow = {
            ProductNumber: '',
            Inventory1: '',
            Inventory2: '',
            productNumber: '',
            inventory1: '',
            inventory2: '',
            id: ++this.keyIndex
        }
        this.invRecords = [...this.invRecords, Object.create(objRow)];
@@ -40,8 +83,64 @@
        this.invRecords = remList;
    }
    handleClick(){
        this.showSpinner = true;
        console.log('保存');
        console.log('this.invRecords'+JSON.stringify(this.invRecords));
        for(var i in this.invRecords){
            console.log('inven'+this.invRecords[i]["inventory1"]);
            console.log('数字'+this.isNum(this.invRecords[i]["inventory1"]));
            if(this.isNum(this.invRecords[i]["inventory1"]) == false || this.isNum(this.invRecords[i]["inventory2"]==false)){
                alert("输入格式不正确,请输入数字!");
                return;
            }
            if(this.invRecords[i]["inventory1"] == ''||this.invRecords[i]["inventory1"] == null ||this.invRecords[i]["inventory2"] == null ||
            this.invRecords[i]["inventory2"] == ''){
                alert('请输入' +this.invRecords[i]["productNumber"] + '库存上下限信息!');
                return;
            }
            if(Number(this.invRecords[i]["inventory1"])>Number(this.invRecords[i]["inventory2"])){
                alert( this.invRecords[i]["productNumber"]  + '库存下限大于上限!');
                return;
            }
        }
            let dataStr = ''
            for(var i in this.invRecords){
                dataStr = dataStr+this.invRecords[i]["productNumber"] +'|'+this.invRecords[i]["inventory1"]+'|'+this.invRecords[i]["inventory2"]+',';
            }
            console.log('dataStr==>'+dataStr);
            this.upstring = dataStr.substring(0, dataStr.lastIndexOf(','));
            console.log('upstring'+ this.upstring);
            save({
                accountId: this.accountid,
                productLimit: this.upstring,
                userPro_Type:this.userPro_Type
            }).then((result) => {
                result = JSON.parse(JSON.stringify(result));
                console.log('result'+JSON.stringify(result));
                if(result.status == 'Success'){
                    const evt = new ShowToastEvent({
                  title: "保存成功",
                  message: result.msg,
                  variant: "Success",
               });
               this.dispatchEvent(evt);
                    this.showSpinner = false;
                }else{
                    const evt = new ShowToastEvent({
                  title: "保存失败",
                  message: result.msg,
                  variant: "Error",
               });
               this.dispatchEvent(evt);
                    this.showSpinner = false;
                }
            }).catch((error) => {
            console.log("error = " + JSON.stringify(error));
                this.showSpinner = false;
         });
        this.inventoryTempRecords();
        console.log( 'Save ==> ' + JSON.stringify(toSaveList));
    }
@@ -50,7 +149,6 @@
        if (event.target.name == 'productNumber') {
            this.invRecords[event.currentTarget.dataset.index].productNumber = event.target.value;
            console.log('===>invrecords'+JSON.stringify(this.invRecords));
        }
        else if (event.target.name == 'inventory1') {
            this.invRecords[event.currentTarget.dataset.index].inventory1 = event.target.value;
@@ -58,39 +156,11 @@
        else if (event.target.name == 'inventory2') {
            this.invRecords[event.currentTarget.dataset.index].inventory2 = event.target.value;
        }
    //     let foundelement = this.invRecords.find(ele => ele.key == event.target.dataset.id);
    //     console.log('=====>foundelement'+foundelement);
    //     foundelement.productNumber = event.target.value;
    //     this.invRecords = [this.invRecords];
    //     console.log(' ==>第一行' +   foundelement.productNumber);
    //     console.log(' ==>第一行' +  JSON.stringify(this.invRecords));
    // }
    // handleinv1Change(event){
    //     let foundelement = this.invRecords.find(ele => ele.key == event.target.dataset.id);
    //     foundelement.Inventory1 = event.target.value;
    //     this.invRecords = [this.invRecords];
    //     // if( foundelement.Inventory1)
    //     console.log(' ==> 第二' +  JSON.stringify(this.invRecords));
    // }
    // handleinv2Change(event){
    //     let foundelement = this.invRecords.find(ele => ele.key == event.target.dataset.id);
    //     foundelement.Inventory2 = event.target.value;
    //     this.invRecords = [this.invRecords];
    //     console.log(' ==>第三' +  JSON.stringify(this.invRecords));
    }
    
    //数字验证
//数字验证
isNum(num){
    var reNum =/^[0-9]+$/;
    return (reNum.test(num));
    }
}