文件
mashangyoujiu/Public/Retail/js/goods-batch.js

187 行
5.5 KiB
JavaScript
可执行文件

/**
* 药品批量操作管理类
*/
class GoodsBatchManager {
constructor(vueInstance) {
this.vue = vueInstance;
}
/**
* 显示批量操作对话框
*/
showBatchOperate() {
if (!this.vue.checkedList.length) {
return this.vue.$message.info('请先选择需要修改的药品');
}
this.vue.changeForm.key = 'status';
this.vue.submitLoading = false;
this.vue.showBatchOperate = true;
}
/**
* 选择批量修改的字段
*/
selectedChangeKey(val) {
this.resetFormValue(val);
this.vue.changeForm.delivery_template_id = '';
if (['goods_info', 'goods_plu'].includes(val)) {
this.openBatchEditDialog(val);
}
}
/**
* 重置表单值
*/
resetFormValue(val) {
switch (val) {
case 'status':
this.vue.changeForm.value = 1;
break;
case 'retail_state':
case 'online_state':
case 'vip_retail_state':
case 'vip_online_state':
case 'guide_royalty_state':
case 'refund_state':
case 'exchange_state':
this.vue.changeForm.value = 0;
break;
case 'delivery_type':
this.vue.changeForm.value = [];
if (!this.vue.deliveryTemplateList.length) {
this.getDeliveryTemplateList();
}
break;
case 'goods_class_id':
case 'fictitious_num':
this.vue.changeForm.value = '';
break;
}
}
/**
* 打开批量编辑对话框
*/
openBatchEditDialog(val) {
const ids = this.vue.checkedList.map(item => item.id).join(',');
const urlMap = {
'goods_info': { title: '批量修改药品', url: `updateGoods?id=${ids}` },
'goods_plu': { title: '批量修改药品PLU', url: `updateGoodsPLU?id=${ids}` },
};
GoodsUtils.openDialog({
title: urlMap[val].title,
content: `/index.php/Retail/Goods/${urlMap[val].url}`,
end: () => {
this.vue.getData();
}
});
this.vue.showBatchOperate = false;
}
/**
* 批量操作药品
*/
async batchOperateGoods(close) {
const validation = GoodsUtils.validateBatchOperation(this.vue.changeForm, this.vue.checkedList);
if (!validation.valid) {
return this.vue.$message.warning(validation.message);
}
const goodsIds = this.vue.checkedList.map(item => item.id).join(',');
this.vue.submitLoading = true;
if (this.vue.changeForm.key === 'goods_class_id') {
await this.verifyGoodsChangeClass(goodsIds, close);
} else {
await this.submitChangeGoods(goodsIds, close);
}
}
/**
* 验证药品分类变更
*/
async verifyGoodsChangeClass(goodsIds, close) {
try {
const res = await GoodsUtils.apiRequest('Retail/LabelInventory/verifyGoodsChangeClass', {
class_id: this.vue.changeForm.value,
operate_type: 2,
goods_ids: goodsIds,
});
if (res.status == 1) {
await this.submitChangeGoods(goodsIds, close);
} else {
this.vue.submitLoading = false;
this.handleVerificationError(res);
}
} catch (error) {
this.vue.submitLoading = false;
this.vue.$message.error('验证失败');
}
}
/**
* 处理验证错误
*/
handleVerificationError(res) {
if (res.data && res.data.length > 0) {
let str = `<div><p class="danger">${res.message}</p>`;
res.data.forEach((item, index) => {
str += `<p><span>${index + 1}. </span><span>${item.goods_name}</span></p>`;
});
str += `</div>`;
this.vue.$alert(str, '错误提示', {
dangerouslyUseHTMLString: true,
type: 'danger',
}).catch(() => {});
} else {
this.vue.$message.error(res.message);
}
}
/**
* 提交变更药品
*/
async submitChangeGoods(goodsIds, close) {
try {
const res = await GoodsUtils.apiRequest('Retail/Goods/batchOperateGoods', {
...this.vue.changeForm,
goodsIds: goodsIds,
});
this.vue.submitLoading = false;
if (res.status == 1) {
if (close) {
this.vue.showBatchOperate = false;
this.vue.checkedList = [];
this.vue.getData();
}
this.vue.$message.success(res.message);
} else {
this.vue.$message.error(res.message);
}
} catch (error) {
this.vue.submitLoading = false;
this.vue.$message.error('操作失败');
}
}
/**
* 获取配送模板列表
*/
async getDeliveryTemplateList() {
try {
const res = await GoodsUtils.apiRequest("/index.php/Retail/SystemDelivery/getStoreFreightTem");
if (res.code == 0) {
this.vue.deliveryTemplateList = res.data;
}
} catch (error) {
this.vue.$message.error('获取配送模板失败');
}
}
}
window.GoodsBatchManager = GoodsBatchManager;