yudao-init/src/store/modules/home.js

300 lines
7.4 KiB
JavaScript
Raw Normal View History

2024-04-18 17:01:10 +08:00
import axios from "@/utils/request";
2024-04-25 17:07:44 +08:00
import { deepClone } from "../../utils";
2024-04-24 16:31:27 +08:00
2024-04-18 17:01:10 +08:00
/* 状态 */
const state = {
copilot: {
/* 产量驾驶舱 */
2024-04-25 17:07:44 +08:00
yield: {
ftoInvest: null,
chipInvest: null,
chipOutput: null,
stdOutput: null,
bipvOutput: null,
},
2024-04-18 17:01:10 +08:00
/* 能源驾驶舱 */
energy: {},
/* 效率驾驶舱 */
efficiency: {},
},
home: {
ftoInvest: null,
chipInvest: null,
chipOutput: null,
stdOutput: null,
bipvOutput: null,
},
};
const mutations = {
SET_HOME_INFO: (state, payload) => {
state.home.ftoInvest = payload.ftoInvest;
state.home.chipInvest = payload.chipInvest;
state.home.chipOutput = payload.chipOutput;
state.home.stdOutput = payload.stdOutput;
state.home.bipvOutput = payload.bipvOutput;
},
2024-04-25 17:07:44 +08:00
SET_COPILOT_INFO: (state, { type, payload }) => {
switch (type) {
2024-04-24 16:31:27 +08:00
case "yield":
2024-04-25 17:07:44 +08:00
state.copilot.yield.ftoInvest = payload.ftoInvest;
state.copilot.yield.chipInvest = payload.chipInvest;
state.copilot.yield.chipOutput = payload.chipOutput;
state.copilot.yield.stdOutput = payload.stdOutput;
state.copilot.yield.bipvOutput = payload.bipvOutput;
2024-04-24 16:31:27 +08:00
break;
case "energy":
state.copilot.energy = payload.data;
break;
case "efficiency":
state.copilot.efficiency = payload.data;
break;
}
},
2024-04-18 17:01:10 +08:00
};
const actions = {
/** 初始化首页数据 */
async initHome({ commit }) {
const dataArr = await getHomeInfo();
const targetArr = await getHomeTarget();
2024-04-25 17:07:44 +08:00
const payload = splitCurrentAndPrevious(dataArr, targetArr);
commit("SET_HOME_INFO", payload);
2024-04-18 17:01:10 +08:00
},
/** 初始化驾驶舱数据 */
2024-04-25 17:07:44 +08:00
async initCopilot({ commit }, { period, source }) {
const fetcher = {
yield: getCopilotYield,
2024-04-28 15:18:46 +08:00
energy: getCopilotEnergy,
efficiency: getCopilotEfficiency,
2024-04-25 17:07:44 +08:00
}[source];
2024-04-26 17:05:26 +08:00
// 获取产量数据
2024-04-25 17:07:44 +08:00
let { data: factoryList, type } = await fetcher(period);
2024-04-26 17:05:26 +08:00
let targetList = null;
2024-04-28 15:18:46 +08:00
if (source === "yield" || source === "efficiency") {
2024-04-26 17:05:26 +08:00
// 获取目标数据
let { data } = await fetcher(period, true);
targetList = data;
}
const payload = splitCurrentAndPrevious(factoryList, targetList);
2024-04-25 17:07:44 +08:00
commit("SET_COPILOT_INFO", { type, payload });
2024-04-24 16:31:27 +08:00
},
2024-04-18 17:01:10 +08:00
};
export default {
namespaced: true,
state,
mutations,
actions,
};
2024-04-25 17:07:44 +08:00
// utils function
function splitCurrentAndPrevious(factoryListResponse, targetListResponse) {
const { chipInvest, ftoInvest, chipOutput, stdOutput, bipvOutput } = init();
if (factoryListResponse) {
for (const factory of factoryListResponse) {
const fId = getFactoryId(factory);
// 获取目标值
if (targetListResponse) {
const { chipYield, componentYield, bipvProductOutput } =
getFactoryTargetValue(targetListResponse, fId);
chipOutput.target[fId] = chipYield;
stdOutput.target[fId] = componentYield;
bipvOutput.target[fId] = bipvProductOutput;
}
// 芯片投入
chipInvest.current[fId] = factory.inputNumber;
chipInvest.previous[fId] = factory.previousYearInputNumber;
// FTO投入
ftoInvest.current[fId] = factory.chipInput;
ftoInvest.previous[fId] = factory.previousYearChipInput;
// 产出数据, 0 - (玻璃)芯片产出, 1 - 标准组件产出, 2 - BIPV产出
2024-04-26 15:44:04 +08:00
// 因为后端写的垃圾数据,所以这里要做一下判断
2024-04-26 17:05:26 +08:00
if (![0, 1, 2].includes(factory.glassType)) continue;
2024-04-25 17:07:44 +08:00
const _t = [chipOutput, stdOutput, bipvOutput][factory.glassType];
_t.current[fId] = factory.outputNumber;
_t.previous[fId] = factory.previousYearOutputNumber;
2024-04-26 15:44:04 +08:00
// debugger;
2024-04-25 17:07:44 +08:00
}
return {
chipInvest,
ftoInvest,
chipOutput,
stdOutput,
bipvOutput,
};
}
}
function getFactoryId(factory) {
return factory.factory;
}
function getFactoryTargetValue(targetList, factoryId) {
const target = targetList.find((item) => item.factory === factoryId);
if (target) {
return {
// 自带模拟数据了.... random_default
chipYield: target.chipYield ?? random_default(),
componentYield: target.componentYield ?? random_default(),
bipvProductOutput: target.bipvProductOutput ?? random_default(),
};
}
return {
chipYield: random_default(),
componentYield: random_default(),
bipvProductOutput: random_default(),
};
}
function init() {
const t_ = {
current: Array(7).fill(0),
previous: Array(7).fill(0),
};
// 芯片投入
const chipInvest = deepClone(t_);
// FTO投入
const ftoInvest = deepClone(t_);
// 芯片产出
const chipOutput = {
...deepClone(t_),
target: Array(7).fill(0),
};
// 标准组件产出
const stdOutput = deepClone(chipOutput);
// BIPV产出
const bipvOutput = deepClone(chipOutput);
return {
chipInvest,
ftoInvest,
chipOutput,
stdOutput,
bipvOutput,
};
}
function random_default() {
return 0;
let a = Math.floor(Math.random() * 1000);
while (a < 600) {
a = Math.floor(Math.random() * 1000);
}
return a;
}
/* 接口 */
async function getHomeInfo() {
const { code, data } = await axios.post("/ip/prod-output/query-by-date", {
factorys: [],
date: 4,
});
if (code == 0) {
return data;
}
console.warn("getHomeInfo failed, code: ", code);
return null;
}
async function getHomeTarget() {
const { code, data } = await axios.post("/ip/prod-target/query-by-date", {
factorys: [],
date: 4,
});
if (code == 0) {
return data;
}
console.warn("getHomeTarget failed, code: ", code);
return null;
}
2024-04-28 15:18:46 +08:00
function getUrl(copilot_module) {
let url = {
// 对比数据的 URL
comparison: "",
// 目标数据的 URL
target: "",
};
switch (copilot_module) {
case "yield":
url.comparison = "/ip/prod-output/query-by-date";
url.target = "/ip/prod-target/query-by-date";
break;
case "energy":
break;
case "efficiency":
url.comparison = "/ip/prod-output/query-Rate-List";
url.target = "/ip/prod-target/query-rate-target";
break;
}
return url;
}
async function doFetch(copilot_module = "yield", fetch_target, params) {
const url = getUrl(copilot_module);
2024-04-26 17:05:26 +08:00
const { code, data } = await axios.post(
2024-04-28 15:18:46 +08:00
fetch_target ? url.target : url.comparison,
2024-04-26 17:05:26 +08:00
{
...params,
}
);
2024-04-25 17:07:44 +08:00
if (code == 0) {
return data;
}
2024-04-28 15:18:46 +08:00
console.warn("[doFetch] failed, code: ", code);
2024-04-25 17:07:44 +08:00
return null;
}
2024-04-28 15:18:46 +08:00
function getCopilotYield(period, target = false) {
return getCopilotData("yield", period, target);
}
function getCopilotEnergy(period, target = false) {
return getCopilotData("energy", period, target);
}
function getCopilotEfficiency(period, target = false) {
return getCopilotData("efficiency", period, target);
}
2024-04-26 17:05:26 +08:00
/**
*
* @param {*} period 周期 日周月年
* @param {*} target 是否获取目标数据默认
* @returns
*/
2024-04-28 15:18:46 +08:00
async function getCopilotData(copilot_module, period, target = false) {
if (!copilot_module) copilot_module = "yield";
2024-04-25 17:07:44 +08:00
// 请求参数,直接一次性获取所有工厂
let queryParams = {
factorys: [],
date: 4,
};
switch (period) {
case "日":
queryParams.date = 1;
break;
case "周":
queryParams.date = 2;
break;
case "月":
queryParams.date = 3;
break;
case "年":
queryParams.date = 4;
break;
default:
queryParams.date = 1;
break;
}
2024-04-26 17:05:26 +08:00
return {
2024-04-28 15:18:46 +08:00
data: await doFetch(copilot_module, target ? true : false, queryParams),
type: copilot_module,
2024-04-26 17:05:26 +08:00
};
2024-04-25 17:07:44 +08:00
}