var echartBar = require('./fn-cpc-ws-factory-dashboard-bar.js');
var echartDoughnut = require('./fn-cpc-ws-factory-dashboard-doughnut.js');
var echartLine = require('./fn-cpc-ws-factory-dashboard-line.js');
var dayjs = require('../../Plugins/dayjs/dayjs.min.js');

/**
 * 自定义业务数据特殊处理,通过  params={_rmt,_mkey,_resField}
 * @param {结果} result
 * @param {参数} params
 */
function doBusiness(result, params) {
    let dataT = result.data;
    //#region 工单、产品统计数据处理
    dataT.summary = {
        taskCount0: 0,
        taskCount1: 0,
        taskCount2: 0,
        taskCount3: 0,
        taskCount4: 0,
        taskCount5: 0,
        taskCount6: 0,
    };
    if (dataT.task.length > 0) {
        dataT.result1 = dataT.task.filter(temp => temp.status === 1);
        dataT.result2 = dataT.task.filter(temp => temp.status === 2);
        if (dataT.result2.length > 0 && dataT.result1.length > 0) {
            dataT.summary = {
                consumeNum: (dataT.result1[0].consumeNum || 0) + (dataT.result2[0].consumeNum || 0),
                taskCount1: dataT.result1[0].count,//进行中工单
                taskCount2: dataT.result2[0].count,//已完成工单
                defectiveNum: (dataT.result1[0].defectiveNum || 0) + (dataT.result2[0].defectiveNum || 0),
                genuineNum: (dataT.result1[0].genuineNum || 0) + (dataT.result2[0].genuineNum || 0),
                materialLib: (dataT.result1[0].materialLib || 0) + (dataT.result2[0].materialLib || 0),
                materialNum: (dataT.result1[0].materialNum || 0) + (dataT.result2[0].materialNum || 0),
                productClib: (dataT.result1[0].productClib || 0) + (dataT.result2[0].productClib || 0),
                productNum: (dataT.result1[0].productNum || 0) + (dataT.result2[0].productNum || 0),
                productZlib: (dataT.result1[0].productZlib || 0) + (dataT.result2[0].productZlib || 0),
            }
        } else if (dataT.result1.length > 0) {
            dataT.summary = dataT.result1[0];
            dataT.summary.taskCount2 = 0;
            dataT.summary.taskCount1 = dataT.result1[0].count;
        } else if (dataT.result2.length > 0 && dataT.result1.length === 0) {
            dataT.summary = dataT.result2[0];
            dataT.summary.taskCount2 = dataT.result2[0].count;
            dataT.summary.taskCount1 = 0;
        }

        //status 0 待分配/待接收 1 进行中 2 已完工 3 已延期 4 已暂停 5 已关闭 6 已拒绝
        dataT.summary.taskCount0 = dataT.task.filter(temp => temp.status === 0)[0]?.count || 0;
        dataT.summary.taskCount3 = dataT.task.filter(temp => temp.status === 3)[0]?.count || 0;
        dataT.summary.taskCount4 = dataT.task.filter(temp => temp.status === 4)[0]?.count || 0;
        dataT.summary.taskCount5 = dataT.task.filter(temp => temp.status === 5)[0]?.count || 0;
        dataT.summary.taskCount6 = dataT.task.filter(temp => temp.status === 6)[0]?.count || 0;

        delete dataT.summary.count;
    }
    delete dataT.result1;
    delete dataT.result2;
    //#region 工单、产品统计数据处理

    //#region  工厂、工单用户处理  百分图

    dataT.doughnut = echartDoughnut;

    let yshU = (dataT.factoryUser.filter(temp => temp.status == 1).map(item => item.count) || []);
    let yshUser = 0;
    if (yshU && yshU.length > 0) {
        yshUser = yshU.reduce((prev, curr) => prev + curr) || 0;
    }

    let dshU = (dataT.factoryUser.filter(temp => temp.status == 0).map(item => item.count) || []);
    let dshUser = 0;
    if (dshU && dshU.length > 0) {
        dshUser = dshU.reduce((prev, curr) => prev + curr) || 0;
    }
    dataT.summary.factoryCount1 = yshUser || 0;
    dataT.summary.factoryCount0 = dshUser || 0;
    dataT.doughnut.data.datasets[0].data = [yshUser, dshUser, dataT.summary.taskCount0, dataT.summary.taskCount4];
    //labels: ["已加入成员", "待审核成员", "待审核工单", "已暂停工单"],

    //#endregion 工厂、工单用户处理


    //任务数据
    let productTaskD = dataT.productTask.map(temp => {
        temp.startDateV = dayjs(temp.startDate).valueOf();
        return temp;
    })
    let _currentChartValues = params._currentChartValues.map(temp => {
        return { startDate: dayjs(temp.split('|')[0]).valueOf(), endDate: dayjs(temp.split('|')[1]).valueOf() }
    });
    //dataT.productTaskD = productTaskD;
    //dataT._currentChartValues = _currentChartValues;
    //#region  工单柱状数据
    dataT.bar = echartBar;
    dataT.bar.data.labels = params._currentChartRange.map(temp => `${temp}${params._currentChartUnit || ''}`);
    dataT.bar.data.datasets[0].data = _currentChartValues.map(temp => {
        //生产下达数 productNum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        //return values.length;
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.productNum || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.bar.data.datasets[1].data = _currentChartValues.map(temp => {
        //正品生产数 genuineNum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return tempC.genuineNum || 0; }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.bar.data.datasets[2].data = _currentChartValues.map(temp => {
        //次品生产数 defectiveNum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.defectiveNum || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.bar.data.datasets[3].data = _currentChartValues.map(temp => {
        //正品回库数 productZlib
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return tempC.productZlib || 0; }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });

    dataT.bar.data.datasets[4].data = _currentChartValues.map(temp => {
        //次品回库数 productClib
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.productClib || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });

    //#endregion 

    //#region  工单曲线数据
    dataT.line = echartLine;
    dataT.line.data.labels = params._currentChartRange.map(temp => `${temp}${params._currentChartUnit || ''}`);

    dataT.line.data.datasets[0].data = _currentChartValues.map(temp => {
        //出库量 consumePnum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.consumePnum || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.line.data.datasets[1].data = _currentChartValues.map(temp => {
        //使用量 materialNum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.materialNum || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.line.data.datasets[2].data = _currentChartValues.map(temp => {
        //消耗量 consumeNum
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.consumeNum || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    dataT.line.data.datasets[3].data = _currentChartValues.map(temp => {
        //回库量 consumeLib
        let values = productTaskD.filter(tempC => (temp.startDate <= tempC.startDateV && tempC.startDateV <= temp.endDate));
        if (values.length > 0) {
            return values.map(tempC => { return (tempC.consumeLib || 0) }).reduce((prev, curr) => prev + curr) || 0;
        }
        return 0;
    });
    //#endregion 


    delete dataT.factoryUser;
    //delete dataT.productTask;
    delete dataT.task;

    result.data = dataT;
    return result;
}
//业务特殊处理
exports.doBusiness = doBusiness;
文档更新时间: 2022-09-05 22:32   作者:admin