Commit fece6599 by Lin

fix(strategyAnalysis): 优化盯防类型逻辑与数据处理

- 移除不必要的指标类型相关代码,简化组件逻辑,提升可维护性。
- 更新盯防类型选项生成逻辑,确保在没有监控类型数据时返回空数组,避免不必要的默认选项。
- 修改数据请求逻辑,使用getStrategyAnalysisEffect替代getStrategyEffectList,确保数据获取的准确性与一致性。
1 parent 9b925606
...@@ -3,55 +3,57 @@ ...@@ -3,55 +3,57 @@
<div class="card-header"> <div class="card-header">
<span class="card-title">策略类型分布</span> <span class="card-title">策略类型分布</span>
<div class="operator-filter"> <div class="operator-filter">
线上运营人员: 运营人员:
<el-select <el-select size="mini"
:value="operatorFilter"
size="mini"
style="width: 120px;" style="width: 120px;"
@change="handleOperatorChange" :value="operatorFilter"
> @change="handleOperatorChange">
<el-option <el-option v-for="operator in operatorList"
v-for="operator in operatorList"
:key="operator.value" :key="operator.value"
:label="operator.label" :label="operator.label"
:value="operator.value" :value="operator.value">
></el-option> </el-option>
</el-select> </el-select>
</div> </div>
</div> </div>
<div class="chart-container" v-show="hasChartData"> <div v-show="hasChartData" class="chart-container">
<!-- ECharts 容器 --> <!-- ECharts 容器 -->
<div ref="pieChart" class="echart-box"></div> <div ref="pieChart" class="echart-box"></div>
<!-- 自定义图例 --> <!-- 自定义图例 -->
<div class="custom-legend"> <div class="custom-legend">
<div <div v-for="(item, index) in processedPieData"
class="legend-item"
v-for="(item, index) in processedPieData"
:key="index" :key="index"
> class="legend-item">
<div <div class="legend-dot"
class="legend-dot" :style="{ background: item.color }">
:style="{ background: item.color }" </div>
></div> <div class="legend-name">
<div class="legend-name">{{ item.name }}</div> {{ item.name }}
</div>
<div class="legend-right"> <div class="legend-right">
<div class="legend-percent">{{ item.percent }}%</div> <div class="legend-percent">
{{ item.percent }}%
</div>
<span class="legend-sep">|</span> <span class="legend-sep">|</span>
<div class="legend-value">{{ item.value }}</div> <div class="legend-value">
{{ item.value }}
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<div v-show="!hasChartData" class="empty-box"> <div v-show="!hasChartData" class="empty-box">
<img :src="noDataImg" alt="暂无数据"> <img alt="暂无数据" :src="noDataImg">
<div class="empty-text">暂无数据</div> <div class="empty-text">
暂无数据
</div>
</div> </div>
</div> </div>
</template> </template>
<script> <script>
import * as echarts from 'echarts'; import { merchantStaffByRoleList } from "@api/index";
import { merchantStaffByRoleList } from '@api/index'; import * as echarts from "echarts";
import noDataImg from "@/assets/strategy/emepty-strategy.png"; import noDataImg from "@/assets/strategy/emepty-strategy.png";
/** /**
...@@ -59,13 +61,13 @@ import noDataImg from "@/assets/strategy/emepty-strategy.png"; ...@@ -59,13 +61,13 @@ import noDataImg from "@/assets/strategy/emepty-strategy.png";
* @type {Object<number, {name: string, color: string}>} * @type {Object<number, {name: string, color: string}>}
*/ */
const STRATEGY_TYPE_CONFIG = { const STRATEGY_TYPE_CONFIG = {
0: { name: '学习', color: '#409EFF' }, 0: { name: "学习", color: "#409EFF" },
1: { name: '优秀案例分享', color: '#36CBCB' }, 1: { name: "优秀案例分享", color: "#36CBCB" },
2: { name: '带教', color: '#67C23A' }, 2: { name: "带教", color: "#67C23A" },
3: { name: '任务下发', color: '#FFD666' }, 3: { name: "任务下发", color: "#FFD666" },
4: { name: '销售精英到店提拉', color: '#B3E19D' }, 4: { name: "销售精英到店提拉", color: "#B3E19D" },
5: { name: '强力促销', color: '#E6A23C' }, 5: { name: "强力促销", color: "#E6A23C" },
6: { name: '最终盘点与低客单产品购入', color: '#9254DE' } 6: { name: "最终盘点与低客单产品购入", color: "#9254DE" },
}; };
/** /**
...@@ -73,16 +75,16 @@ const STRATEGY_TYPE_CONFIG = { ...@@ -73,16 +75,16 @@ const STRATEGY_TYPE_CONFIG = {
* @param {number} strategyType - 策略类型编号 * @param {number} strategyType - 策略类型编号
* @returns {{name: string, color: string}} * @returns {{name: string, color: string}}
*/ */
const getStrategyTypeConfig = (strategyType) => { function getStrategyTypeConfig(strategyType) {
return STRATEGY_TYPE_CONFIG[strategyType]; return STRATEGY_TYPE_CONFIG[strategyType];
}; }
/** /**
* 转换策略类型分布数据为图表数据格式 * 转换策略类型分布数据为图表数据格式
* @param {Array} apiData - API 返回的原始数据 * @param {Array} apiData - API 返回的原始数据
* @returns {Array} 图表数据 * @returns {Array} 图表数据
*/ */
const transformStrategyData = (apiData) => { function transformStrategyData(apiData) {
if (!Array.isArray(apiData) || apiData.length === 0) { if (!Array.isArray(apiData) || apiData.length === 0) {
return []; return [];
} }
...@@ -93,7 +95,7 @@ const transformStrategyData = (apiData) => { ...@@ -93,7 +95,7 @@ const transformStrategyData = (apiData) => {
// 转换数据格式 // 转换数据格式
return validData return validData
.map(item => { .map((item) => {
const { strategyType, strategyCount = 0, percent } = item; const { strategyType, strategyCount = 0, percent } = item;
const config = getStrategyTypeConfig(strategyType); const config = getStrategyTypeConfig(strategyType);
const calculatedPercent = percent ?? (total > 0 ? Math.round((strategyCount / total) * 100) : 0); const calculatedPercent = percent ?? (total > 0 ? Math.round((strategyCount / total) * 100) : 0);
...@@ -103,25 +105,25 @@ const transformStrategyData = (apiData) => { ...@@ -103,25 +105,25 @@ const transformStrategyData = (apiData) => {
value: strategyCount, value: strategyCount,
percent: calculatedPercent, percent: calculatedPercent,
color: config.color, color: config.color,
strategyType // 保留原始类型用于调试 strategyType, // 保留原始类型用于调试
}; };
}) })
.sort((a, b) => b.value - a.value); // 按数量降序排序 .sort((a, b) => b.value - a.value); // 按数量降序排序
}; }
export default { export default {
name: 'StrategyTypeDistribution', name: "StrategyTypeDistribution",
props: { props: {
// 策略类型分布原始数据(由父组件传递) // 策略类型分布原始数据(由父组件传递)
pieData: { pieData: {
type: Array, type: Array,
default: () => [] default: () => [],
}, },
// 运营人员筛选值(双向绑定) // 运营人员筛选值(双向绑定)
operatorFilter: { operatorFilter: {
type: [String, Number], type: [String, Number],
default: null default: null,
} },
}, },
data() { data() {
return { return {
...@@ -129,10 +131,10 @@ export default { ...@@ -129,10 +131,10 @@ export default {
myChart: null, myChart: null,
// 运营人员列表 // 运营人员列表
operatorList: [ operatorList: [
{ label: '全部', value: null }, { label: "全部", value: null },
], ],
// 处理后的图表数据 // 处理后的图表数据
processedPieData: [] processedPieData: [],
}; };
}, },
computed: { computed: {
...@@ -155,7 +157,7 @@ export default { ...@@ -155,7 +157,7 @@ export default {
*/ */
chartOption() { chartOption() {
return this.getChartOption(); return this.getChartOption();
} },
}, },
watch: { watch: {
// 监听原始数据变化,处理数据并更新图表 // 监听原始数据变化,处理数据并更新图表
...@@ -167,12 +169,12 @@ export default { ...@@ -167,12 +169,12 @@ export default {
}); });
}, },
immediate: true, immediate: true,
deep: true deep: true,
} },
}, },
mounted() { mounted() {
this.initChart(); this.initChart();
window.addEventListener('resize', this.resizeChart); window.addEventListener("resize", this.resizeChart);
// 获取线上运营列表 // 获取线上运营列表
this.getOnlineOpsList(); this.getOnlineOpsList();
// 数据加载由 watch 统一管理,无需在此处调用 // 数据加载由 watch 统一管理,无需在此处调用
...@@ -185,25 +187,25 @@ export default { ...@@ -185,25 +187,25 @@ export default {
} }
// 清理事件监听器 // 清理事件监听器
window.removeEventListener('resize', this.resizeChart); window.removeEventListener("resize", this.resizeChart);
}, },
methods: { methods: {
/** /**
* 获取线上运营列表 * 获取线上运营列表
*/ */
getOnlineOpsList() { getOnlineOpsList() {
merchantStaffByRoleList({ roleName: '线上运营' }).then((res) => { merchantStaffByRoleList({ roleName: "线上运营" }).then((res) => {
if (res) { if (res) {
// 将返回的数据转换为选择器需要的格式 // 将返回的数据转换为选择器需要的格式
this.operatorList = res.map(item => ({ this.operatorList = res.map(item => ({
value: item.uuid, value: item.uuid,
label: item.staffName label: item.staffName,
})); }));
// 在列表前面添加"全部"选项 // 在列表前面添加"全部"选项
if (this.operatorList.length && this.operatorList[0].label !== '全部') { if (this.operatorList.length && this.operatorList[0].label !== "全部") {
this.operatorList.unshift({ this.operatorList.unshift({
value: null, value: null,
label: '全部' label: "全部",
}); });
} }
} }
...@@ -215,7 +217,7 @@ export default { ...@@ -215,7 +217,7 @@ export default {
*/ */
handleOperatorChange(value) { handleOperatorChange(value) {
// 通过 .sync 修饰符自动更新父组件的值 // 通过 .sync 修饰符自动更新父组件的值
this.$emit('update:operatorFilter', value); this.$emit("update:operatorFilter", value);
}, },
/** /**
...@@ -226,36 +228,36 @@ export default { ...@@ -226,36 +228,36 @@ export default {
return { return {
series: [ series: [
{ {
name: '策略总数', name: "策略总数",
type: 'pie', type: "pie",
radius: ['55%', '75%'], radius: ["55%", "75%"],
center: ['50%', '50%'], center: ["50%", "50%"],
avoidLabelOverlap: false, avoidLabelOverlap: false,
itemStyle: { itemStyle: {
borderRadius: 0, borderRadius: 0,
borderColor: '#fff', borderColor: "#fff",
borderWidth: 2, borderWidth: 2,
}, },
label: { label: {
show: true, show: true,
position: 'center', position: "center",
formatter: this.hasChartData ? `策略总数` : '暂无数据', formatter: this.hasChartData ? `策略总数` : "暂无数据",
fontSize: 18, fontSize: 18,
color: '#666666', color: "#666666",
lineHeight: 20 lineHeight: 20,
}, },
emphasis: { emphasis: {
}, },
labelLine: { labelLine: {
show: false show: false,
}, },
data: this.processedPieData.map(item => ({ data: this.processedPieData.map(item => ({
value: item.value, value: item.value,
name: item.name, name: item.name,
itemStyle: { color: item.color } itemStyle: { color: item.color },
})) })),
} },
] ],
}; };
}, },
...@@ -264,14 +266,14 @@ export default { ...@@ -264,14 +266,14 @@ export default {
*/ */
updateChart() { updateChart() {
if (!this.myChart) { if (!this.myChart) {
console.warn('图表实例未初始化'); console.warn("图表实例未初始化");
return; return;
} }
try { try {
this.myChart.setOption(this.chartOption, true); // true 表示不合并,使用新配置 this.myChart.setOption(this.chartOption, true); // true 表示不合并,使用新配置
} catch (error) { } catch (error) {
console.error('图表更新失败:', error); console.error("图表更新失败:", error);
} }
}, },
...@@ -280,7 +282,8 @@ export default { ...@@ -280,7 +282,8 @@ export default {
*/ */
initChart() { initChart() {
const chartDom = this.$refs.pieChart; const chartDom = this.$refs.pieChart;
if (!chartDom) return; if (!chartDom)
return;
this.myChart = echarts.init(chartDom); this.myChart = echarts.init(chartDom);
// 初始时使用空数据,等待API数据加载后再更新 // 初始时使用空数据,等待API数据加载后再更新
...@@ -296,7 +299,7 @@ export default { ...@@ -296,7 +299,7 @@ export default {
} }
}, },
} },
}; };
</script> </script>
...@@ -385,7 +388,7 @@ $bg-white: #fff; ...@@ -385,7 +388,7 @@ $bg-white: #fff;
} }
.legend-sep { .legend-sep {
color: #E4E7ED; color: #e4e7ed;
margin: 0 8px; margin: 0 8px;
} }
} }
...@@ -415,4 +418,3 @@ $bg-white: #fff; ...@@ -415,4 +418,3 @@ $bg-white: #fff;
} }
} }
</style> </style>
...@@ -70,17 +70,6 @@ ...@@ -70,17 +70,6 @@
trigger-icon-type="image" trigger-icon-type="image"
:value="currentWatchType" :value="currentWatchType"
@change="selectWatchType" /> @change="selectWatchType" />
<!-- <span class="v-divider">|</span> -->
<!-- 指标类型 -->
<SelectCustom v-if="false"
label="指标类型"
:options="indicatorTypeOptions"
:trigger-icon="require('@/assets/strategy/icon-strategy-type06.png')"
trigger-icon-type="image"
:value="currentIndicatorType"
@change="selectIndicatorType" />
</div> </div>
</d2-table-header> </d2-table-header>
<d2-table ref="table" <d2-table ref="table"
...@@ -189,7 +178,7 @@ ...@@ -189,7 +178,7 @@
<script> <script>
import dayjs from "dayjs"; import dayjs from "dayjs";
import { getStrategyEffectList, getStrategyOnlineDist, getStrategyTotal, getStrategyTypeDist, getWarningMetricList, getWarningTypeList } from "@/api/earlyWarningManager"; import { getStrategyAnalysisEffect, getStrategyEffectList, getStrategyOnlineDist, getStrategyTotal, getStrategyTypeDist, getWarningTypeList } from "@/api/earlyWarningManager";
import noDataImg from "@/assets/strategy/emepty-strategy.png"; import noDataImg from "@/assets/strategy/emepty-strategy.png";
import { deepClone } from "@/common/util"; import { deepClone } from "@/common/util";
import calendarCustom from "../strategyCentre/compoments/clendarCustom.vue"; import calendarCustom from "../strategyCentre/compoments/clendarCustom.vue";
...@@ -275,9 +264,7 @@ export default { ...@@ -275,9 +264,7 @@ export default {
// 存储原始监控类型数据(接口返回的完整数据) // 存储原始监控类型数据(接口返回的完整数据)
monitorTypeData: [], monitorTypeData: [],
// 当前选中的盯防类型 // 当前选中的盯防类型
currentWatchType: { label: "全部", value: null }, currentWatchType: { label: "", value: null },
// 当前选中的指标类型
currentIndicatorType: { label: "全部", value: null },
// 表格相关数据 // 表格相关数据
searchFormSearch: {}, searchFormSearch: {},
getListApi: null, // 在 mounted 中初始化 getListApi: null, // 在 mounted 中初始化
...@@ -399,14 +386,11 @@ export default { ...@@ -399,14 +386,11 @@ export default {
computed: { computed: {
// 盯防类型选项(自动根据原始数据生成) // 盯防类型选项(自动根据原始数据生成)
watchTypeOptions() { watchTypeOptions() {
const allOption = { label: "全部", value: null };
if (!this.monitorTypeData || this.monitorTypeData.length === 0) { if (!this.monitorTypeData || this.monitorTypeData.length === 0) {
return [allOption]; return [];
} }
return [ return this.monitorTypeData.map((item) => {
allOption,
...this.monitorTypeData.map((item) => {
const iconConfig = WATCH_TYPE_ICON_CONFIG[item.typeCode] || WATCH_TYPE_ICON_CONFIG.default; const iconConfig = WATCH_TYPE_ICON_CONFIG[item.typeCode] || WATCH_TYPE_ICON_CONFIG.default;
return { return {
label: item.typeName, label: item.typeName,
...@@ -414,40 +398,17 @@ export default { ...@@ -414,40 +398,17 @@ export default {
icon: iconConfig.icon, icon: iconConfig.icon,
iconUrl: iconConfig.iconUrl, iconUrl: iconConfig.iconUrl,
}; };
}), });
];
}, },
effectSearchParams() { effectSearchParams() {
return { return {
startDate: this.startDate, startDate: this.startDate,
endDate: this.endDate, endDate: this.endDate,
businessType: this.currentTab.value, businessType: this.currentTab.value,
monitorType: this.currentWatchType.value || null, warningType: this.currentWatchType.value || null,
metricTypeId: this.currentIndicatorType.value || null,
periodType: this.timeRangeType.value, periodType: this.timeRangeType.value,
}; };
}, },
// 指标类型选项(自动根据当前盯防类型生成)
indicatorTypeOptions() {
if (this.currentWatchType.value == null) {
return [{ label: "全部", value: null }];
}
const currentTypeData = this.monitorTypeData.find(
item => item.typeCode === this.currentWatchType.value,
);
if (!currentTypeData || !currentTypeData.rules || currentTypeData.rules.length === 0) {
return [{ label: "全部", value: null }];
}
return [
{ label: "全部", value: null },
...currentTypeData.rules.map(rule => ({
label: rule.rule,
value: rule.id,
})),
];
},
}, },
watch: { watch: {
// 监听业务类型变化,重新获取数据 // 监听业务类型变化,重新获取数据
...@@ -460,19 +421,6 @@ export default { ...@@ -460,19 +421,6 @@ export default {
this.strategyTypeOperatorFilter = null; this.strategyTypeOperatorFilter = null;
} }
}, },
// 监听盯防类型变化,自动重置指标类型为「全部」
"currentWatchType.value": function (newValue, oldValue) {
if (newValue !== oldValue) {
this.currentIndicatorType = { label: "全部", value: null };
}
},
// 监听指标类型变化,触发数据查询
"currentIndicatorType.value": function (newValue, oldValue) {
if (newValue !== oldValue) {
this.$refs.table && this.$refs.table.refreshTable();
}
},
// 监听策略类型分布运营人员筛选变化,防抖请求数据 // 监听策略类型分布运营人员筛选变化,防抖请求数据
strategyTypeOperatorFilter(newValue, oldValue) { strategyTypeOperatorFilter(newValue, oldValue) {
if (newValue !== oldValue && oldValue !== undefined) { if (newValue !== oldValue && oldValue !== undefined) {
...@@ -512,7 +460,9 @@ export default { ...@@ -512,7 +460,9 @@ export default {
*/ */
async wrapStrategyEffectList(params) { async wrapStrategyEffectList(params) {
try { try {
const data = await getStrategyEffectList(params); // const data = await getStrategyEffectList(params);
params.cycleType = params.periodType;
const data = await getStrategyAnalysisEffect(params);
// axios 响应拦截器已返回 data 字段的内容(数组) // axios 响应拦截器已返回 data 字段的内容(数组)
// 需要包装成 d2-table 期望的格式:{ data: [...], total: number } // 需要包装成 d2-table 期望的格式:{ data: [...], total: number }
return { return {
...@@ -539,20 +489,19 @@ export default { ...@@ -539,20 +489,19 @@ export default {
*/ */
async beforeSearchFn(type = 1) { async beforeSearchFn(type = 1) {
try { try {
// 先获取指标类型列表(若已存在数据则不需要再次调用) // 先获取盯防类型列表(若已存在数据则不需要再次调用)
if (!this.monitorTypeData || this.monitorTypeData.length === 0) { if (!this.monitorTypeData || this.monitorTypeData.length === 0) {
await this.getIndicatorTypeList(); await this.getWatchTypeList();
} }
} catch (error) { } catch (error) {
console.error("获取指标类型列表失败,继续执行:", error); console.error("获取盯防类型列表失败,继续执行:", error);
} }
// 构建搜索参数,即使字段为空也先构建(避免首次加载时参数缺失) // 构建搜索参数,即使字段为空也先构建(避免首次加载时参数缺失)
this.searchFormSearch = deepClone({ this.searchFormSearch = deepClone({
startDate: this.startDate, startDate: this.startDate,
endDate: this.endDate, endDate: this.endDate,
businessType: this.currentTab.value, businessType: this.currentTab.value,
monitorType: this.currentWatchType.value || null, warningType: this.currentWatchType.value || null,
metricTypeId: this.currentIndicatorType.value || null,
periodType: this.timeRangeType.value, periodType: this.timeRangeType.value,
}); });
}, },
...@@ -593,93 +542,38 @@ export default { ...@@ -593,93 +542,38 @@ export default {
label: item.label, label: item.label,
value: item.value !== undefined ? item.value : item.label, value: item.value !== undefined ? item.value : item.label,
}; };
if (this.currentWatchType.value != null) {
this.getWarningMetricListData();
}
// watch 会自动处理后续逻辑(重置指标类型、触发查询)
// 使用 $nextTick 确保数据更新后再更新搜索参数
this.$nextTick(() => { this.$nextTick(() => {
// 确保 searchFormSearch 参数已更新
this.searchFormSearch = deepClone({ this.searchFormSearch = deepClone({
startDate: this.startDate, startDate: this.startDate,
endDate: this.endDate, endDate: this.endDate,
businessType: this.currentTab.value, businessType: this.currentTab.value,
monitorType: this.currentWatchType.value || null, warningType: this.currentWatchType.value || null,
metricTypeId: this.currentIndicatorType.value || null,
periodType: this.timeRangeType.value, periodType: this.timeRangeType.value,
}); });
// watch 会自动触发 refreshTable,这里不需要手动调用 this.$refs.table && this.$refs.table.refreshTable();
}); });
console.log("选择盯防类型:", item.label);
},
/**
* 选择指标类型
*/
selectIndicatorType(item) {
// this.currentIndicatorType = {
// label: item.label,
// value: item.value !== undefined ? item.value : null,
// };
// // watch会自动处理后续逻辑(触发查询)
// // 使用 $nextTick 确保数据更新后再更新搜索参数
// this.$nextTick(() => {
// // 确保 searchFormSearch 参数已更新
// this.searchFormSearch = deepClone({
// startDate: this.startDate,
// endDate: this.endDate,
// businessType: this.currentTab.value,
// monitorType: this.currentWatchType.value || null,
// metricTypeId: this.currentIndicatorType.value || null,
// periodType: this.timeRangeType.value,
// });
// // watch 会自动触发 refreshTable,这里不需要手动调用
// });
// console.log("选择指标类型:", item.label);
}, },
/** /**
* 获取盯防类型列表(扁平结构,指标类型在选中盯防类型后动态加载) * 获取盯防类型列表
*/ */
async getIndicatorTypeList() { async getWatchTypeList() {
try { try {
const res = await getWarningTypeList(); const res = await getWarningTypeList();
if (res && res.length) { if (res && res.length) {
this.monitorTypeData = res.map(item => ({ this.monitorTypeData = res.map(item => ({
typeName: item.warningTypeName, typeName: item.warningTypeName,
typeCode: item.warningType, typeCode: item.warningType,
rules: [],
})); }));
this.currentWatchType = { label: "全部", value: null }; const first = this.monitorTypeData[0];
this.currentIndicatorType = { label: "全部", value: null }; this.currentWatchType = {
label: first.typeName,
value: first.typeCode,
};
} else { } else {
console.warn("指标类型列表接口返回为空"); console.warn("盯防类型列表接口返回为空");
}
} catch (error) {
console.error("获取指标类型列表失败:", error);
}
},
// 根据当前盯防类型动态获取指标类型选项
async getWarningMetricListData() {
const warningType = this.currentWatchType.value;
if (warningType == null) {
return;
}
try {
const res = await getWarningMetricList({
businessType: this.currentTab.value,
warningType,
});
const rules = Array.isArray(res) && res.length
? res.map(item => ({
rule: item.categoryExtension.includes("全部") ? item.metricName : (item.categoryExtension + item.metricName),
id: item.metricId,
}))
: [];
const typeIndex = this.monitorTypeData.findIndex(item => item.typeCode === warningType);
if (typeIndex !== -1) {
this.$set(this.monitorTypeData[typeIndex], "rules", rules);
} }
} catch (error) { } catch (error) {
console.error("获取指标列表失败:", error); console.error("获取盯防类型列表失败:", error);
} }
}, },
/** /**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!