yudao-init/src/store/modules/home.js
‘937886381’ 51938926d3 修改bug
2024-06-27 09:28:04 +08:00

745 lines
20 KiB
JavaScript

import axios from "@/utils/request";
import { deepClone } from "../../utils";
/* 状态 */
const state = {
copilot: {
/* 产量驾驶舱 */
yield: {
ftoInvest: null,
chipInvest: null,
chipOutput: null,
stdOutput: null,
bipvOutput: null,
},
/* 能源驾驶舱 */
energy: {
stockDOData: [],
waterList: {},
gasList: [],
elecList:{}
},
/* 效率驾驶舱 */
efficiency: {
chipOee: {
current: [],
previous: [],
target:[]
},
transformRate: {
current: [],
previous: [],
target:[]
},
chipRate: {
target: [],
current: [],
previous: [],
outputNumber:[],
},
stdRate: {
target: [],
current: [],
previous: [],
outputNumber:[]
},
},
},
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;
},
SET_COPILOT_INFO: (state, { type, payload }) => {
switch (type) {
case "yield":
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;
break;
case "energy":
state.copilot.energy.stockDOData = payload.stockDOData;
state.copilot.energy.gasList = payload.gasList;
state.copilot.energy.waterList = payload.waterList;
state.copilot.energy.elecList = payload.elecList;
break;
case "efficiency":
state.copilot.efficiency.chipOeeRate = payload.chipOeeRate;
state.copilot.efficiency.transformRate = payload.transformRate;
state.copilot.efficiency.chipRate = payload.chipRate;
state.copilot.efficiency.stdRate = payload.stdRate;
break;
}
},
};
const actions = {
/** 初始化首页数据 */
async initHome({ commit }) {
const dataArr = await getHomeInfo();
console.log('dataArr', dataArr);
let preData = dataArr.previousProdOutputOutDO
let preFtoData = dataArr.previousProdOutputFtoDO
// const targetArr = await getHomeInfo();
// factoryListResponse, targetListResponse, prodOutputFtoListRes,preData,preFtoData
const payload = splitCurrentAndPrevious(dataArr.prodOutputOutDO, dataArr.prodTargetOutputDO,dataArr.prodOutputFtoDO,preData,preFtoData);
commit("SET_HOME_INFO", payload);
},
/** 初始化驾驶舱数据 */
async initCopilot({ commit }, { period,source,than}) {
// if (source == "energy") return;
const fetcher = {
yield: getCopilotYield,
energy: getCopilotEnergy,
efficiency: getCopilotEfficiency,
}[source];
const handler = {
yield: splitCurrentAndPrevious,
energy: splitCurrentAndPreviousB,
efficiency: splitCurrentAndPreviousA,
}[source]
// 获取产量数据
let { data: factoryList, type } = await fetcher(period,than)
// let targetList = null;
// if (source === "yield" || source === "efficiency") {
// // 获取目标数据
// let { data } = await fetcher(period,than, true)
// targetList = data
// }
// if (source == "energy") {
// let factoryData = factoryList
// const payload = handler(factoryData)
// commit("SET_COPILOT_INFO", { type, payload });
// } else {
console.log('factoryList',factoryList)
let factoryData = factoryList.prodOutputOutDO ? factoryList.prodOutputOutDO : factoryList.prodOutputRateDO
let preData = factoryList.previousProdOutputOutDO ? factoryList.previousProdOutputOutDO : factoryList.previousProdOutputRateDO
let preFtoData = factoryList.previousProdOutputFtoDO ? factoryList.previousProdOutputFtoDO : []
let prodOutputFtoDO = factoryList.prodOutputFtoDO ? factoryList.prodOutputFtoDO : []
let targetData = factoryList.prodTargetOutputDO ? factoryList.prodTargetOutputDO : factoryList.prodTargetRateDO
const payload = handler(factoryData,targetData,prodOutputFtoDO,preData,preFtoData)
commit("SET_COPILOT_INFO", { type, payload });
// }
},
};
export default {
namespaced: true,
state,
mutations,
actions,
};
function splitCurrentAndPreviousB(factoryListResponse) {
let factoryArr = [
{
id: 0,
name: '瑞昌中建材光电材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 1,
name: '邯郸中建材光电材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 2,
name: '中建材株洲光电材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 3,
name: '佳木斯中建材光电材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 4,
name: '成都中建材光电材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 5,
name: '凯盛光伏材料有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
{
id: 6,
name: '蚌埠兴科玻璃有限公司',
stockData: [],
waterData: [],
elsData: [],
gasData: [],
},
]
if (factoryListResponse) {
for (let i in factoryListResponse.stockDO) {
const index = factoryArr.findIndex(item => item.id == factoryListResponse.stockDO[i].factory)
if (index != -1) {
factoryListResponse.stockDO[i].stockInfo.forEach(ele => {
factoryArr[index].stockData.push(ele)
});
}
}
let stockDOData = []
factoryArr.forEach((ele, index) => [
stockDOData[index] = [],
ele.stockData.forEach((item) => {
let obj = {}
obj.name = item.glassType == 0 ? '玻璃芯片' : item.glassType == 1 ? '标准组件' : item.glassType == 2 ? 'BIPV' : '定制组件'
obj.value = item.stockNumber
stockDOData[index].push(obj)
}),
])
let gasList = []
// if (factoryListResponse.gasDO) {
factoryListResponse.gasDO.forEach((ele) => {
gasList[ele.factory] = ele.totalEnergyValue
})
// }
// console.log(factoryListResponse.gasDO);
let waterObj = Object.groupBy(factoryListResponse.waterDO, ({ groupName }) => groupName)
let waterList = {
times: [],
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
}
for (let i in waterObj) {
waterList.times.push(i)
waterObj[i].forEach((ele, index) => {
waterList[ele.factory].push(ele.totalEnergyValue)
})
}
let elecObj = Object.groupBy(factoryListResponse.elecDO, ({ groupName }) => groupName)
let elecList = {
times: [],
0: [],
1: [],
2: [],
3: [],
4: [],
5: [],
6: [],
}
for (let i in elecObj) {
elecList.times.push(i)
elecObj[i].forEach((ele, index) => {
elecList[ele.factory].push(ele.totalEnergyValue)
})
}
return {
stockDOData,
waterList,
gasList,
elecList,
};
} else {
let stockDOData = Array[7].fill([])
let waterList = {}
return {
stockDOData,
waterList,
gasList,
elecList
};
}
}
function splitCurrentAndPreviousA(factoryListResponse, targetListResponse, prodOutputFtoListRes,preData,preFtoData) {
console.log('工厂',preData);
// 初始数据
const { chipOeeRate, transformRate, chipRate, stdRate } = initA();
// factoryListResponse = [
// {
// factory: 0,
// oee: 0.8,
// previousYearOee: 0.7,
// componentConversionEfficiency: 0.8,
// previousYearComponentConversionEfficiency: 0.7,
// glassType: 0,
// yieldRate: 0.8,
// previousYearYieldRate: 0.7,
// chipYieldRate: 0.38,
// componentYieldRate: 0.73,
// },
// {
// factory: 1,
// oee: 0.8,
// previousYearOee: 0.7,
// componentConversionEfficiency: 0.8,
// previousYearComponentConversionEfficiency: 0.7,
// glassType: 1,
// yieldRate: 0.8,
// previousYearYieldRate: 0.7,
// chipYieldRate: 0.38,
// componentYieldRate: 0.73,
// },
// {
// factory: 2,
// oee: 0.8,
// previousYearOee: 0.7,
// componentConversionEfficiency: 0.8,
// previousYearComponentConversionEfficiency: 0.7,
// glassType: 1,
// yieldRate: 0.8,
// previousYearYieldRate: 0.7,
// chipYieldRate: 0.38,
// componentYieldRate: 0.73,
// },
// {
// factory: 3,
// oee: 0.8,
// previousYearOee: 0.7,
// componentConversionEfficiency: 0.8,
// previousYearComponentConversionEfficiency: 0.7,
// glassType: 0,
// yieldRate: 0.8,
// previousYearYieldRate: 0.7,
// chipYieldRate: 0.38,
// componentYieldRate: 0.73,
// },
// ];
if (preData) {
for (const factory of preData) {
const fId = getPreFactoryId(factory)
console.log('factory',factory.previousYearOee);
// chipInvest.previous[fId] = factory.previousYearInputNumber;
// chipOeeRate.current[fId] = factory.oee;
if (factory.previousGlassType === 0) {
chipOeeRate.previous[fId] = factory.previousYearOee;
}
// chipOeeRate.previous[fId] = factory.previousYearOee;
// 转化效率
if (factory.previousGlassType === 1) {
transformRate.previous[fId] = factory.previousYearComponentConversionEfficiency;
}
// transformRate.previous[fId] =factory.previousYearComponentConversionEfficiency ;
// 芯片良率 与 标准组件良率
if (![0, 1].includes(factory.previousGlassType)) continue;
const _t = [chipRate, stdRate][factory.previousGlassType]
// _t.current[fId] = factory.yieldRate ;
_t.previous[fId] = factory.previousYearYieldRate ;
// }
// debugger;
}
}
if (factoryListResponse) {
for (const factory of factoryListResponse) {
const fId = getFactoryId(factory);
// 获取目标值
if (targetListResponse) {
const {
chipYieldRate,
componentYieldRate,
chipOee,
componentConversionEfficiency,
componentYield
} = getFactoryTargetValueA(targetListResponse, fId)
stdRate.target[fId] = componentYieldRate
chipRate.target[fId] = chipYieldRate
chipOeeRate.target[fId] = chipOee
transformRate.target[fId] = componentConversionEfficiency
}
// 芯片OEE
if (factory.glassType === 0) {
chipOeeRate.current[fId] = factory.oee;
}
// chipOeeRate.previous[fId] = factory.previousYearOee;
// 转化效率
if (factory.glassType === 1) {
transformRate.current[fId] = factory.componentConversionEfficiency;
}
// transformRate.previous[fId] = factory.previousYearComponentConversionEfficiency ;
// 芯片良率 与 标准组件良率
if (![0, 1].includes(factory.glassType)) continue;
const _t = [chipRate, stdRate][factory.glassType]
_t.current[fId] = factory.yieldRate ;
// _t.previous[fId] = factory.previousYearYieldRate ;
}
console.log('chipOeeRate',stdRate);
// console.log('chipOeeRate',chipOeeRate);
return {
chipOeeRate,
transformRate,
chipRate,
stdRate,
};
}
}
function splitCurrentAndPrevious(factoryListResponse, targetListResponse, prodOutputFtoListRes,preData,preFtoData) {
console.log('prodOutputFtoListRes',preFtoData);
// 初始数据
const { chipInvest, ftoInvest, chipOutput, stdOutput, bipvOutput } = init()
if (prodOutputFtoListRes.length !== 0 ) {
for (const factory of prodOutputFtoListRes) {
console.log(factory);
const fId = getFactoryId(factory);
// const preFId = getPreFactoryId(factory);
// if (fId) {
ftoInvest.current[fId] = factory.chipInput;
// }
// if (preFId) {
// ftoInvest.previous[fId] = factory.previousYearChipInput;
// }
}
}
if (preFtoData) {
for (const factory of preFtoData) {
console.log(factory);
const fId = getPreFactoryId(factory);
// const preFId = getPreFactoryId(factory);
// if (fId) {
ftoInvest.previous[fId] = factory.previousYearChipInput;
// }
// if (preFId) {
// ftoInvest.previous[fId] = factory.previousYearChipInput;
// }
}
}
if (preData) {
for (const factory of preData) {
const fId = getPreFactoryId(factory);
if (factory.previousGlassType === 0) {
chipInvest.previous[fId] = factory.previousYearInputNumber
}
// chipInvest.previous[fId] = factory.previousYearInputNumber;
if (![0, 1, 2].includes(factory.previousGlassType)) continue;
const _t = [chipOutput, stdOutput, bipvOutput][factory.previousGlassType];
// if (fId) {
// _t.current[fId] = factory.outputNumber;
// }
// if (preFId) {
_t.previous[fId] = factory.previousYearOutputNumber
// }
// debugger;
}
}
// console.log('ftoInvest',ftoInvest)
if (factoryListResponse) {
for (const factory of factoryListResponse) {
const fId = getFactoryId(factory);
console.log('factory.inputNumber', factory, fId);
if (factory.glassType === 0) {
chipInvest.current[fId] = factory.inputNumber
}
// const preFId = getPreFactoryId(factory);
// 获取目标值
if (targetListResponse) {
const { chipYield, componentYield, bipvProductOutput } =
getFactoryTargetValue(targetListResponse, fId);
chipOutput.target[fId] = chipYield;
stdOutput.target[fId] = componentYield;
bipvOutput.target[fId] = bipvProductOutput;
}
// 芯片投入
// 产出数据, 0 - (玻璃)芯片产出, 1 - 标准组件产出, 2 - BIPV产出
// 因为后端写的垃圾数据,所以这里要做一下判断
if (![0, 1, 2].includes(factory.glassType)) continue;
const _t = [chipOutput, stdOutput, bipvOutput][factory.glassType];
_t.current[fId] = factory.outputNumber;
}
console.log('chipInvest',ftoInvest);
return {
chipInvest,
// ftoInvest,
ftoInvest,
chipOutput,
stdOutput,
bipvOutput,
};
}
}
function getFactoryId(factory) {
return factory.factory;
}
function getPreFactoryId(factory) {
return factory.previousFactory;
}
function getFactoryTargetValue(targetList, factoryId) {
const target = targetList.find((item) => item.factory === factoryId);
if (target) {
return {
// 自带模拟数据了.... random_default
chipYield: target.chipYield ?? 0,
componentYield: target.componentYield ?? 0,
bipvProductOutput: target.bipvProductOutput ?? 0,
};
}
return {
chipYield: 0,
componentYield: 0,
bipvProductOutput: 0,
};
}
/**
* 获取 芯片良率目标值 和 标准组件良率目标值
* @param {*} targetList 目标值列表
* @param {*} factoryId 工厂ID
* @returns
*/
function getFactoryTargetValueA(targetList, factoryId) {
const target = targetList.find((item) => item.factory === factoryId)
if (target) {
return {
chipYieldRate: target.chipYieldRate ?? 0,
componentYieldRate: target.componentYieldRate ?? 0,
componentYield: target.componentYield ?? 0,
// componentYield: target.componentYield ?? 0,
chipOee: target.chipOee ?? 0,
};
}
return {
chipYieldRate:0,
componentYieldRate:0,
componentYield: 0,
chipOee:0,
};
}
/**
*
* @returns 初始化 效率模块里 初始状态值
*/
function initA() {
const t_ = {
current: Array(7).fill(0),
previous: Array(7).fill(0),
};
// 芯片OEE
// 转化效率
// 标准组件良率
const stdRate = {
...deepClone(t_),
target: Array(7).fill(0),
};
// 芯片良率
const transformRate = deepClone(stdRate);
const chipRate = deepClone(stdRate);
const chipOeeRate = deepClone(stdRate);
return {
chipOeeRate,
transformRate,
chipRate,
stdRate,
};
}
/**
*
* @returns 初始化 产量模块里 初始状态值
*/
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, // FTO投入
chipOutput, // 芯片产出
stdOutput, // 标准组件产出
bipvOutput, // BIPV产出
};
}
function random_default(min = 0, max = 1) {
// return 0;
let a = Math.floor(Math.random() * max);
while (a < min) {
a = Math.floor(Math.random() * max);
}
return a;
}
/* 接口 */
async function getHomeInfo() {
const { code, data } = await axios.post("ip/prod-output/cockpitData", {
factorys: [],
date: 4,
mode:0
});
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;
}
function getUrl(copilot_module) {
let url = {
// 对比数据的 URL
comparison: "",
// 目标数据的 URL
// target: "",
};
switch (copilot_module) {
case "yield":
url.comparison = "ip/prod-output/cockpitData";
// url.target = "/ip/prod-target/query-by-date";
break;
case "energy":
url.comparison = "ip/prod-output/cockpitComprehensiveDataMonitor";
break;
case "efficiency":
url.comparison = "ip/prod-output/cockpitDataRate";
// 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);
const { code, data } = await axios.post(
url.comparison,
{
...params,
}
);
if (code == 0) {
return data;
}
console.warn("[doFetch] failed, code: ", code);
return null;
}
/**
*
* @param {*} period 日周月年1,2,3,4
* @param {*} target 是否获取目标数据
* @returns
*/
function getCopilotYield(period,than,target = false) {
return getCopilotData("yield", period,than, target);
}
function getCopilotEnergy(period, target = false) {
return getCopilotData("energy", period,than, target);
}
function getCopilotEfficiency(period,than, target = false) {
return getCopilotData("efficiency", period,than, target);
}
/**
*
* @param {*} period 周期: 日周月年
* @param {*} target 是否获取目标数据:默认 否
* @returns
*/
async function getCopilotData(copilot_module, period,than, target = false) {
if (!copilot_module) copilot_module = "yield";
// 请求参数,直接一次性获取所有工厂
let queryParams = {
factorys: [],
date: 4,
mode:0
};
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;
}
switch (than) {
case "同比":
queryParams.mode = 0;
break;
case "环比":
queryParams.mode = 1;
break;
}
return {
data: await doFetch(copilot_module, target ? true : false, queryParams),
type: copilot_module,
};
}