import { LightningElement ,track , wire} from 'lwc'; import { CurrentPageReference } from 'lightning/navigation'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { NavigationMixin } from 'lightning/navigation'; import init from '@salesforce/apex/ProductLimitController.init'; import category1Change from '@salesforce/apex/ProductLimitController.category1Change'; import category2Change from '@salesforce/apex/ProductLimitController.category2Change'; import category3Change from '@salesforce/apex/ProductLimitController.category3Change'; import searchProduct from '@salesforce/apex/ProductLimitController.searchProduct'; import save from '@salesforce/apex/ProductLimitController.save'; import readCsvFile from '@salesforce/apex/ProductLimitController.readCsvFile'; import importSave from '@salesforce/apex/ProductLimitController.importSave'; import searchProductByCategoryList from '@salesforce/apex/ProductLimitController.searchProductByCategoryList'; export default class LexProductLimitApplication extends NavigationMixin(LightningElement) { @track contractId; @track prlId; @track manageCode; @track agencyName; @track contractName; @track contract; @track isEdit = false; @track canEdit = false; @track keyword; @track category1; @track category2; @track category3; @track category4; @track category1OptionList = []; @track category2OptionList = []; @track category3OptionList = []; @track category4OptionList = []; @track productInfoList = []; @track selectedRows = []; @track showSpinner = false; @track showProductDetailSection; @track showSelectApplyType = false; //文件上传 @track showImportSection = false; @track showImportSuccessPop = false; @track fileName = ''; @track filesUploaded = []; @track plrList = []; file; fileContents; fileReader; content; @track carrList = []; //分页 @track currentRecord = []; @track currentPage = 1; @track pageSize = 200; @track pageSizeOptions = [200,500]; @track recordStart = 0; @track recordEnd = 0; //警告 @track warningMsgs = []; @track conCols = [ { label: '产品名称', fieldName: 'Name', wrapText: true, hideDefaultActions: true, initialWidth: 600 }, { label: 'OT CODE', fieldName: 'OTCode', wrapText: true, hideDefaultActions: true }, { label: '规格', fieldName: 'PackingListManual', wrapText: true, hideDefaultActions: true }, { label: 'NMPA状态', fieldName: 'SfdaStatus', wrapText: true, hideDefaultActions: true }, { label: '第一分类', fieldName: 'Category1', wrapText: true, hideDefaultActions: true }, { label: '第二分类', fieldName: 'Category2', wrapText: true, hideDefaultActions: true }, { label: '第三分类', fieldName: 'Category3', wrapText: true, hideDefaultActions: true }, { label: '第四分类', fieldName: 'Category4', wrapText: true, hideDefaultActions: true } ]; @track importCol = [ { label: '合同名称', fieldName: 'contractName', wrapText: true, hideDefaultActions: true, initialWidth: 350 }, { label: '本期协议编号', fieldName: 'termContractNo', wrapText: true, hideDefaultActions: true }, { label: '产品名称', fieldName: 'name', wrapText: true, hideDefaultActions: true, initialWidth: 500 }, { label: 'OT CODE', fieldName: 'otCode', wrapText: true, hideDefaultActions: true }, { label: '规格', fieldName: 'packingListManual', wrapText: true, hideDefaultActions: true }, { label: 'NMPA状态', fieldName: 'sfdaStatus', wrapText: true, hideDefaultActions: true }, { label: '第一分类', fieldName: 'categoryone', wrapText: true, hideDefaultActions: true }, { label: '第二分类', fieldName: 'categorytwo', wrapText: true, hideDefaultActions: true }, { label: '第三分类', fieldName: 'categorythree', wrapText: true, hideDefaultActions: true } ]; //导入的产品限制明细 @track importPlrCol = [ { label: '', type: 'button', typeAttributes: { label: '查看' }, initialWidth: 90, hideDefaultActions: true, wrapText: true }, { label: '名称', fieldName: 'Name', wrapText: true, hideDefaultActions: true }, { label: '合同', fieldName: 'contractName', wrapText: true, hideDefaultActions: true }, { label: '状态', fieldName: 'Status__c', wrapText: true, hideDefaultActions: true } ]; @track options = [ { label: '产品明细', value: 'ProductDetail' }, { label: '产品分类', value: 'ProductSort' } ]; @track typeValue = ''; @track selectedCategroy = []; @track categroyCol = [ { label: '第一分类', fieldName: 'categoryOne', wrapText: true, hideDefaultActions: true }, { label: '第二分类', fieldName: 'categoryTwo', wrapText: true, hideDefaultActions: true }, { label: '第三分类', fieldName: 'categoryThree', wrapText: true, hideDefaultActions: true }, { label: '第四分类', fieldName: 'categoryFour', wrapText: true, hideDefaultActions: true }, { label: '', type: 'button', typeAttributes: { label: '删除' }, initialWidth: 90, hideDefaultActions: true, wrapText: true } ]; get confirmDisable(){ if(this.typeValue == null || this.typeValue == ''){ return true; }else{ return false; } } get isDisable(){ return !this.canEdit; } get canSave(){ if(this.carrList.length > 0){ return false; }else{ return true; } } //是否有警告 get hasWarning() { if (this.warningMsgs == null || this.warningMsgs.length == 0) { return false; } if (this.warningMsgs.length > 0) { return true; } } @wire(CurrentPageReference) getStateParameters(currentPageReference) { console.log('CurrentPageReference'); if (currentPageReference) { var type; this.contractId = currentPageReference.state?.c__contractId; this.prlId = currentPageReference.state?.c__id; type = currentPageReference.state?.c__type; if(type != '2'){ this.showImportSection = false; }else{ this.showImportSection = true; } console.log('contractId:' + this.contractId); console.log('prlId:' + this.prlId); } } connectedCallback(){ //正常申请 if(!this.showImportSection){ if(this.prlId == null && this.contractId == null){ this.showToast('', '请通过经销商合同上的产品限制申请按钮或者主页链接中的产品限制导入链接进入!', 'error'); } if(this.prlId == null || this.prlId == ''){ this.showSelectApplyType = true; }else{ this.initPage(); } } } confirm(){ console.log('typeValue:'+this.typeValue); this.showSelectApplyType = false; if(this.typeValue == 'ProductDetail'){ this.showProductDetailSection = true; }else{ this.showProductDetailSection = false; } this.initPage(); } initPage(){ this.showSpinner = true; init({contractIdPara : this.contractId , plrIdPara : this.prlId}) .then((result) => { console.log("result:"+JSON.stringify(result)); if (result.result == 'Success') { this.manageCode = result.contract.Parent.Management_Code__c; this.agencyName = result.contract.Parent.Name; this.contractName = result.contract.Name; this.contract = result.contract; this.contractId = this.contract.Id; this.isEdit = result.isEdit; this.canEdit = result.canEdit; this.category1 = ''; this.category2 = ''; this.category3 = ''; this.category4 = ''; this.category1OptionList = result.category1OptionList; this.category2OptionList = result.category2OptionList; this.category3OptionList = result.category3OptionList; this.category4OptionList = result.category4OptionList; this.productInfoList = result.proInfoList; if(result.recordTypeName == 'ProductDetail'){ this.showProductDetailSection = true; }else if(result.recordTypeName == 'ProductSort'){ this.showProductDetailSection = false; } for (var i in this.productInfoList) { this.productInfoList[i]['recordId'] = this.productInfoList[i].pro.Id; this.productInfoList[i]['Name'] = this.productInfoList[i].pro.Name__c; this.productInfoList[i]['OTCode'] = this.productInfoList[i].pro.OT_CODE__c; this.productInfoList[i]['Category1'] = this.productInfoList[i].pro.Category1__c; this.productInfoList[i]['Category2'] = this.productInfoList[i].pro.Category2__c; this.productInfoList[i]['Category3'] = this.productInfoList[i].pro.Category3__c; this.productInfoList[i]['Category4'] = this.productInfoList[i].pro.Category4__c; this.productInfoList[i]['SfdaStatus'] = this.productInfoList[i].pro.SFDA_Status__c; this.productInfoList[i]['PackingListManual'] = this.productInfoList[i].pro.Packing_list_manual__c; } this.selectedCategroy = result.productCategoryList; if(this.selectedCategroy == null){ this.selectedCategroy = []; } this.currentPage = 1; this.currentRecord = []; this.showCurrentPageReocrd(); this.getAllChecked(); this.showSpinner = false; }else { console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }); } searchProduct(){ this.showSpinner = true; searchProduct({ category1Para : this.category1 , category2Para : this.category2 , category3Para : this.category3 , category4Para : this.category4 , keywordPara : this.keyword, selectedRowsStr : JSON.stringify(this.selectedRows), contractPara : JSON.stringify(this.contract) }) .then((result)=>{ if (result.result == 'Success') { this.productInfoList = result.proInfoList; for (var i in this.productInfoList) { this.productInfoList[i]['recordId'] = this.productInfoList[i].pro.Id; this.productInfoList[i]['Name'] = this.productInfoList[i].pro.Name__c; this.productInfoList[i]['OTCode'] = this.productInfoList[i].pro.OT_CODE__c; this.productInfoList[i]['Category1'] = this.productInfoList[i].pro.Category1__c; this.productInfoList[i]['Category2'] = this.productInfoList[i].pro.Category2__c; this.productInfoList[i]['Category3'] = this.productInfoList[i].pro.Category3__c; this.productInfoList[i]['Category4'] = this.productInfoList[i].pro.Category4__c; this.productInfoList[i]['SfdaStatus'] = this.productInfoList[i].pro.SFDA_Status__c; this.productInfoList[i]['PackingListManual'] = this.productInfoList[i].pro.Packing_list_manual__c; } this.currentPage = 1; this.currentRecord = []; this.showCurrentPageReocrd(); this.getAllChecked(); this.showSpinner = false; }else{ console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }); } searchProductByCategoryList(){ this.showSpinner = true; console.log('selectedCategroyarr:'+JSON.stringify(this.selectedCategroy)); searchProductByCategoryList({ categoryListJson : JSON.stringify(this.selectedCategroy), contractPara : JSON.stringify(this.contract) }) .then((result)=>{ if (result.result == 'Success') { this.productInfoList = result.proInfoList; for (var i in this.productInfoList) { this.productInfoList[i]['recordId'] = this.productInfoList[i].pro.Id; this.productInfoList[i]['Name'] = this.productInfoList[i].pro.Name__c; this.productInfoList[i]['OTCode'] = this.productInfoList[i].pro.OT_CODE__c; this.productInfoList[i]['Category1'] = this.productInfoList[i].pro.Category1__c; this.productInfoList[i]['Category2'] = this.productInfoList[i].pro.Category2__c; this.productInfoList[i]['Category3'] = this.productInfoList[i].pro.Category3__c; this.productInfoList[i]['Category4'] = this.productInfoList[i].pro.Category4__c; this.productInfoList[i]['SfdaStatus'] = this.productInfoList[i].pro.SFDA_Status__c; this.productInfoList[i]['PackingListManual'] = this.productInfoList[i].pro.Packing_list_manual__c; } this.currentPage = 1; this.currentRecord = []; this.showCurrentPageReocrd(); this.getAllChecked(); this.showSpinner = false; }else{ console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }); } save(){ if(this.typeValue == 'ProductDetail'){ if(this.selectedRows.length < 1){ this.showToast('请选择产品', '', 'error'); return; } }else{ if(this.selectedCategroy.length < 1){ this.showToast('请选择产品分类', '', 'error'); return; } } this.showSpinner = true; save({ selectedRowsStr : JSON.stringify(this.selectedRows), isEditPara : this.isEdit, plrIdPara : this.prlId, contractIdPara : this.contractId, agencyIdPara : this.contract.ParentId, categoryListJson : JSON.stringify(this.selectedCategroy), applyTypePara : this.typeValue }) .then((result)=>{ if (result.result == 'Success') { var url = result.url; const config = { type: 'standard__webPage', attributes: { url : url } }; this[NavigationMixin.Navigate](config); }else{ console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }) } //添加当前分类 addCurrentCategory(){ if(this.category1 == null || this.category1 == ''){ this.showToast('第一分类不能为空!', '', 'error'); return; } var key = this.category1+this.category2+this.category3+this.category4; var selectedCategroyarr = [...this.selectedCategroy]; for(var i = 0; i < this.selectedCategroy.length; i++){ if(this.selectedCategroy[i].recordKey == key){ this.showToast('已添加该分类,请勿重新添加', '', 'error'); return; } } selectedCategroyarr.push({ categoryOne : this.category1, categoryTwo : this.category2, categoryThree : this.category3, categoryFour : this.category4, recordKey : this.category1+this.category2+this.category3+this.category4 }); this.selectedCategroy = selectedCategroyarr; this.searchProductByCategoryList(); } //删除选中分类 deleteCurrentCategroy(event){ const row = event.detail.row; var key = row.recordKey; for(var i = 0; i < this.selectedCategroy.length; i++){ console.log('key:'+key); console.log('recordKey:'+this.selectedCategroy[i].recordKey); if(this.selectedCategroy[i].recordKey == key){ this.selectedCategroy.splice(i,1); } } var selectedCategroyarr = [...this.selectedCategroy]; this.selectedCategroy = selectedCategroyarr; this.searchProductByCategoryList(); } //Radiocchange radioChange(event){ this.typeValue = event.detail.value; // console.log('typeValue:'+event.detail.value); } // 取得所有被勾选的产品id getAllChecked() { this.selectedRows = []; for (var i in this.currentRecord) { if (this.currentRecord[i].check) { this.selectedRows.push(this.currentRecord[i].recordId); } } } checkRows(event){ this.selectedRows = []; const selectedRows = event.detail.selectedRows; for(var i in this.productInfoList){ var count = 0; for (var j in selectedRows) { if(this.productInfoList[i].recordId == selectedRows[j].recordId){ count++; console.log('checkId:' + selectedRows[j].recordId + '---' + this.productInfoList[i].Name); } } if(count == 0){ this.productInfoList[i].check = false; }else{ this.productInfoList[i].check = true; this.selectedRows.push(this.productInfoList[i].recordId); } } // for (var i in this.selectedRows) { // console.log('checkId1:' + this.selectedRows[i]); // for (var i in this.currentRecord) { // if (this.currentRecord[i].recordId == this.selectedRows[i]) { // console.log('currentRecord:' + this.selectedRows[i]); // } // } // } } keywordChange(event){ this.keyword = event.detail.value; } category1Change(event){ this.showSpinner = true; this.category1 = event.detail.value; this.category2 = ''; this.category3 = ''; this.category4 = ''; category1Change({category1Para : this.category1}) .then((result)=>{ if (result.result == 'Success') { this.category2OptionList = result.category2OptionList; this.category3OptionList = result.category3OptionList; this.category4OptionList = result.category4OptionList; this.showSpinner = false; }else { console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }) } category2Change(event){ this.showSpinner = true; this.category2 = event.detail.value; this.category3 = ''; this.category4 = ''; category2Change({category1Para : this.category1 , category2Para : this.category2}) .then((result)=>{ if (result.result == 'Success') { this.category3OptionList = result.category3OptionList; this.category4OptionList = result.category4OptionList; this.showSpinner = false; }else { console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }) } category3Change(event){ this.showSpinner = true; this.category3 = event.detail.value; this.category4 = ''; category3Change({category1Para : this.category1 , category2Para : this.category2 , category3Para : this.category3}) .then((result)=>{ if (result.result == 'Success') { this.category4OptionList = result.category4OptionList; this.showSpinner = false; }else { console.log('Error:' + result.errorMsg); this.showSpinner = false; this.showToast(result.errorMsg, '', 'error'); } }) } category4Change(event){ this.category4 = event.detail.value; } showToast(title, message, variant) { const evt = new ShowToastEvent({ title: title, message: message, variant: variant }); this.dispatchEvent(evt); } //分页 showCurrentPageReocrd() { var startIndex = 0; var endIndex = 0; console.log('len:' + this.productInfoList.length); if (this.productInfoList != null && this.productInfoList.length > 0) { var currentRecord = []; var currentCount = 0; var mu = this.productInfoList.length % this.pageSize; if (this.currentPage == this.totalPage) { if (mu != 0) { currentCount = mu; } else { currentCount = this.pageSize; } } else { currentCount = this.pageSize; } console.log('this.totalPage:' + this.totalPage); console.log('this.pageSize:' + this.pageSize); console.log('mu:' + mu); console.log('currentCount:' + currentCount); startIndex = (this.currentPage - 1) * this.pageSize; endIndex = parseInt(startIndex) + parseInt(currentCount) - 1; console.log('startIndex:' + startIndex); console.log('endIndex:' + endIndex); for (var i in this.currentRecord) { if (this.currentRecord[i].check) { currentRecord.push(this.currentRecord[i]); } } for (var i = startIndex; i <= endIndex; i++) { var count = 0; for (var j in this.currentRecord) { if ( this.productInfoList[i].recordId == this.currentRecord[j].recordId ) { count++; } } if (count == 0) { currentRecord.push(this.productInfoList[i]); } } this.recordStart = startIndex + 1; this.recordEnd = endIndex + 1; this.currentRecord = currentRecord; } else { this.currentPage = 0; this.currentRecord = []; } } get prePage() { return this.currentPage - 1; } get nextPage() { return this.currentPage + 1; } get totalPage() { if ( this.productInfoList.length % this.pageSize == 0 ) { return Math.trunc(this.productInfoList.length / this.pageSize); } else { return (Math.trunc(this.productInfoList.length / this.pageSize) + 1); } } get totalRecords() { return this.productInfoList.length; } //第一页 goFirstPage() { this.currentPage = 1; this.showCurrentPageReocrd(); } //最后一页 goLastPage() { this.currentPage = this.totalPage; this.showCurrentPageReocrd(); } //上一页 prePageClick() { this.currentPage = this.currentPage - 1; this.showCurrentPageReocrd(); } //下一页 nextPageClick() { this.currentPage = this.currentPage + 1; this.showCurrentPageReocrd(); } //page size change pageSizeChange(event) { console.log('pagesize:' + event.detail); this.pageSize = event.detail; this.currentPage = 1; this.currentRecord = []; this.showCurrentPageReocrd(); } //上一页按钮是否disable get previousButtonDisabled() { return this.currentPage == 1 || this.currentPage == 0; } //下一页按钮是否disable get nextButtonDisabled() { return this.currentPage == this.totalPage; } //文件上传 //是否显示附件移除 get attDelBtn() { if ( this.fileName != '' && this.fileName != null && this.fileName != '请选择一个文件上传' ) { return true; } else { return false; } } get disableReadBtn(){ if ( this.fileName != '' && this.fileName != null && this.fileName != '请选择一个文件上传' ) { return false; } else { return true; } } //移除附件 removeAtt() { this.filesUploaded = []; this.fileName = ''; } //附件change事件 attChange(event) { if (event.target.files.length > 0) { this.filesUploaded = event.target.files; this.fileName = event.target.files[0].name; console.log('this.fileName:' + this.fileName); } } //读取CSV readCsvFile(){ if (this.filesUploaded.length > 0) { this.file = this.filesUploaded[0]; if (this.file.size > this.MAX_FILE_SIZE) { window.console.log('文件过大'); return; } this.fileReader = new FileReader(); this.fileReader.onloadend = () => { this.fileContents = this.fileReader.result; let base64 = 'base64,'; this.content = this.fileContents.indexOf(base64) + base64.length; this.fileContents = this.fileContents.substring(this.content); this.saveToFile(); }; this.fileReader.readAsDataURL(this.file); } else { this.fileName = '请选择一个文件上传'; } } //后台读取 saveToFile() { this.showSpinner = true; readCsvFile({ base64Data: encodeURIComponent(this.fileContents)}) .then((result) => { if (result.result == 'Success') { this.carrList = result.carrList; this.warningMsgs = result.warningMsgList; for (var i in this.carrList) { this.carrList[i]['recordId'] = this.carrList[i].product.Id; this.carrList[i]['contractName'] = this.carrList[i].contract.Name; this.carrList[i]['termContractNo'] = this.carrList[i].contract.Term_Contract_No__c; this.carrList[i]['name'] = this.carrList[i].product.Name__c; this.carrList[i]['otCode'] = this.carrList[i].product.OT_CODE__c; this.carrList[i]['categoryone'] = this.carrList[i].product.Category1__c; this.carrList[i]['categorytwo'] = this.carrList[i].product.Category2__c; this.carrList[i]['categorythree'] = this.carrList[i].product.Category3__c; this.carrList[i]['sfdaStatus'] = this.carrList[i].product.SFDA_Status__c; this.carrList[i]['packingListManual'] = this.carrList[i].product.Packing_list_manual__c; } this.showSpinner = false; this.showToast('', result.note, 'success'); } else { this.showSpinner = false; console.log('Error:' + result.errorMsg); this.showToast('读取失败', '请检查您的CSV文件格式及内容', 'error'); } }) .catch((error) => { this.showSpinner = false; this.showToast('读取失败', error, 'error'); }); } importSave(){ this.showSpinner = true; console.log(JSON.stringify(this.carrList)); importSave({carrListPara : JSON.stringify(this.carrList)}) .then((result) => { if (result.result == 'Success') { this.plrList = result.plrList; console.log('plrList:' + JSON.stringify(this.plrList)); for (var i in this.plrList) { this.plrList[i]['contractName'] = this.plrList[i].Agency_Contract__r.Name; } this.showSpinner = false; this.showImportSuccessPop = true; } else { this.showSpinner = false; console.log('Error1:' + result.errorMsg); this.showToast('保存失败', result.errorMsg, 'error'); } }) .catch((error) => { this.showSpinner = false; console.log('Error2:' + JSON.stringify(error)); this.showToast('保存失败', JSON.stringify(error), 'error'); }); } //查看产品限制明细 showPlrDetail(event){ const row = event.detail.row; var plrId= row.Id; console.log('plrId:'+plrId); var urlStr = '/lightning/r/ProductLimitRelation__c/'+plrId+'/view'; window.open(urlStr,'_blank'); } //继续导入 importNew(){ this.showImportSuccessPop = false; this.plrList = []; this.carrList = []; this.filesUploaded = []; this.fileName = ''; this.warningMsgs = []; } }