buli
2023-07-11 a68c8c52c2820052c2ae3856876f74a385354583
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { LightningElement, wire, api, track } from 'lwc';
import oninit from "@salesforce/apex/LexProductLimitEditController.init";
import save from "@salesforce/apex/LexUpAccountProLimit.upAccount";
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { NavigationMixin } from "lightning/navigation";
export default class LexProductLimitEdit extends NavigationMixin(LightningElement) {
    @track invRecords = [];
    @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 === ');
        //获取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() {
        let objRow = {
            productNumber: '',
            inventory1: '',
            inventory2: '',
            id: ++this.keyIndex
        }
        this.invRecords = [...this.invRecords, Object.create(objRow)];
    }
    removeRow(event) {
        const indexPos = event.currentTarget.name;
        let remList = [];
        remList = this.invRecords;
        remList.splice(indexPos, 1);
        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('inven1' + this.invRecords[i]["inventory1"]);
            console.log('数字1' + this.isNum(this.invRecords[i]["inventory1"]));
            console.log('inven2' + this.invRecords[i]["inventory2"]);
            console.log('数字2' + this.isNum(this.invRecords[i]["inventory2"]));
            if (this.invRecords[i]["inventory1"] == '' || this.invRecords[i]["inventory1"] == null || this.invRecords[i]["inventory2"] == null || this.invRecords[i]["inventory2"] == '') {
                this.showMyToast('保存失败', '输入格式不正确,请输入数字', 'error');
                this.showSpinner = false;
                return;
            }
            if( this.isNum(this.invRecords[i]["inventory1"]) == false || this.isNum(this.invRecords[i]["inventory2"]) == false){
                this.showMyToast('保存失败', '输入格式不正确,请输入数字', 'error');
                this.showSpinner = false;
                return;
 
            }
            if (Number(this.invRecords[i]["inventory1"]) > Number(this.invRecords[i]["inventory2"])) {
                this.showMyToast('保存失败', this.invRecords[i]["productNumber"] + '库存下限大于上限', 'error');
                this.showSpinner = false;
                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') {
                this.showMyToast('保存成功', '', 'success');
                this.showSpinner = false;
                // setTimeout(this.toLexToppage(),5000 );  
                setTimeout(() => {
                    this.toLexToppage();
                }, 500);
 
            } else {
                this.showMyToast('保存失败', result.msg, 'error');
                this.showSpinner = false;
 
            }
        }).catch((error) => {
            console.log("error = " + JSON.stringify(error));
            this.showSpinner = false;
        });
 
        this.inventoryTempRecords();
        console.log('Save ==> ' + JSON.stringify(toSaveList));
    }
 
    handleProductChange(event) {
        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;
            console.log('===>invrecords' + JSON.stringify(this.invRecords));
        }
        else if (event.target.name == 'inventory2') {
            this.invRecords[event.currentTarget.dataset.index].inventory2 = event.target.value;
            console.log('===>invrecords' + JSON.stringify(this.invRecords));
        }
    }
 
    //数字验证
    isNum(num) {
        var reNum = /^[0-9]+$/;
        return (reNum.test(num));
    }
    showMyToast(title, message, variant) {
        console.log('show custom message');
        var iconName = '';
        var content = '';
        if (variant == 'success') {
            iconName = 'utility:check';
        } else {
            iconName = 'utility:error';
        }
        if (message != '') {
            content = '<h2><strong>' + title + '<strong/></h2><h5>' + message + '</h5>';
        } else {
            content = '<h2><strong>' + title + '<strong/></h2>';
        }
        this.template.querySelector('c-common-toast').showToast(variant, content, iconName, 10000);
    }
    toLexToppage() {
        this[NavigationMixin.Navigate]({
            type: 'standard__webPage',
            attributes: {
                url: '/lextoppage'
            }
        });
    }
}