将一个关系中的值乘以每个记录的另一个关系的总和

时间:2018-05-26 13:11:32

标签: javascript laravel eloquent

我想在这里计算一些东西。 所以基本上我正在做的是获取他们项目的所有配置文件,以及对这些项目的热切负载关系出价和计时器。每个项目都有一个出价,它有一个值,每个项目都有多个计时器。

我想要做的是为每个项目计算定时器的started_at和stopped_at之间的差异,并将其作为小时数的总和,然后乘以同一个项目的出价值,然后将它全部归还为总和。 基本上我想得到这个人赚取的总金额(对于每个项目,所有计时器的持续时间以小时为单位*出价值)。

目前我正在尝试计算javascript中的所有内容,这是我到目前为止所做的:

            calculateTotalAmountEarned(projects) {
            let totalAmount = 0;
            let totalHours = 0;
            let total = moment.duration(0);
            let value = 0;
            projects.forEach((project) => {
                project.timers.forEach((timer) => {
                    if(timer.stopped_at) {
                        let started = moment(timer.started_at);
                        let stopped = moment(timer.stopped_at);
                        let time = moment.duration(stopped.diff(started));

                        total.add(time)
                    }
                    totalHours = +(total.asHours()).toFixed(0);
                })
                project.bids.forEach((bid) => {
                    value = bid.value
                })

                totalAmount = totalHours * value
            })

            return totalAmount
        },

现在它返回所有项目中所有计时器的总和乘以单个项目的单个出价值。

我现在得到的是:55小时,这是个人资料的所有项目的所有计时器的持续时间,乘以14,这是单个出价的价值。

示例输入和预期输出: 项目1:3个定时器,每个定时器持续1小时 - >总和3小时,出价值是14 项目2与项目1等相同

预期结果是(项目1 = 3 * 14)+(项目2 => 3 * 14)等所有项目等等

"projects":[  
     {  
        "id":1,
        "timers":[  
           {  
              "id":1,
              "name":"dfdsf",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-23 11:48:22",
              "stopped_at":"2018-05-23 11:48:25",
              "created_at":"2018-05-23 11:48:22",
              "updated_at":"2018-05-23 11:48:25",
           },
           {  
              "id":2,
              "name":"dfdsf343",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-23 11:48:28",
              "stopped_at":"2018-05-23 11:48:30",
              "created_at":"2018-05-23 11:48:28",
              "updated_at":"2018-05-23 11:48:30",
           },
           {  
              "id":3,
              "name":"dfdsf343zzxccz",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-23 11:48:33",
              "stopped_at":"2018-05-23 11:48:35",
              "created_at":"2018-05-23 11:48:33",
              "updated_at":"2018-05-23 11:48:35",
           },
           {  
              "id":4,
              "name":"dfdsf343zzxcczfgfg",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-23 11:48:37",
              "stopped_at":"2018-05-23 11:48:40",
              "created_at":"2018-05-23 11:48:37",
              "updated_at":"2018-05-23 11:48:40",
           },
           {  
              "id":5,
              "name":"asdfsadf",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-24 10:44:51",
              "stopped_at":"2018-05-24 11:12:55",
              "created_at":"2018-05-24 10:44:51",
              "updated_at":"2018-05-24 11:12:55",
           },
           {  
              "id":7,
              "name":"twqetqwtq",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-24 11:13:45",
              "stopped_at":"2018-05-24 12:08:22",
              "created_at":"2018-05-24 11:13:45",
              "updated_at":"2018-05-24 12:08:22",
           },
           {  
              "id":9,
              "name":"asd",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-24 12:08:29",
              "stopped_at":"2018-05-24 12:42:13",
              "created_at":"2018-05-24 12:08:29",
              "updated_at":"2018-05-24 12:42:13",
           },
           {  
              "id":11,
              "name":"1234",
              "project_id":1,
              "profile_id":2,
              "started_at":"2018-05-24 13:23:34",
              "stopped_at":"2018-05-26 08:30:41",
              "created_at":"2018-05-24 13:23:34",
              "updated_at":"2018-05-26 08:30:41",
           }
        ],
        "bids":[  
           {  
              "id":1,
              "value":"34",
              "created_at":"2018-05-22 12:25:37",
              "updated_at":"2018-05-22 12:25:55"
           }
        ],

好的,这是一个项目的相关信息 - 它的计时器和出价 - 总是有一个出价。预期输出是每个定时器相加并乘以出价值后,以小时为单位的stopped_at和started_at之间的差值。对于每个其他项目以及所有总结为最终结果的内容重复此操作。我希望你理解我的解释

1 个答案:

答案 0 :(得分:0)

希望这是你正在寻找的。

它使用.reduce函数将项目缩减为总数。

(或者,如果我正确理解了问题,通过在total回调中移动totalHoursvalueproject.forEach的声明,您应该能够使用totalAmount = totalHours * value替换totalAmount += totalHours * value以使用原始函数获取正确的值)



let projects = [{
  "id": 1,
  "timers": [{
      "id": 1,
      "name": "dfdsf",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-23 11:48:22",
      "stopped_at": "2018-05-23 11:48:25",
      "created_at": "2018-05-23 11:48:22",
      "updated_at": "2018-05-23 11:48:25",
    },
    {
      "id": 2,
      "name": "dfdsf343",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-23 11:48:28",
      "stopped_at": "2018-05-23 11:48:30",
      "created_at": "2018-05-23 11:48:28",
      "updated_at": "2018-05-23 11:48:30",
    },
    {
      "id": 3,
      "name": "dfdsf343zzxccz",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-23 11:48:33",
      "stopped_at": "2018-05-23 11:48:35",
      "created_at": "2018-05-23 11:48:33",
      "updated_at": "2018-05-23 11:48:35",
    },
    {
      "id": 4,
      "name": "dfdsf343zzxcczfgfg",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-23 11:48:37",
      "stopped_at": "2018-05-23 11:48:40",
      "created_at": "2018-05-23 11:48:37",
      "updated_at": "2018-05-23 11:48:40",
    },
    {
      "id": 5,
      "name": "asdfsadf",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-24 10:44:51",
      "stopped_at": "2018-05-24 11:12:55",
      "created_at": "2018-05-24 10:44:51",
      "updated_at": "2018-05-24 11:12:55",
    },
    {
      "id": 7,
      "name": "twqetqwtq",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-24 11:13:45",
      "stopped_at": "2018-05-24 12:08:22",
      "created_at": "2018-05-24 11:13:45",
      "updated_at": "2018-05-24 12:08:22",
    },
    {
      "id": 9,
      "name": "asd",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-24 12:08:29",
      "stopped_at": "2018-05-24 12:42:13",
      "created_at": "2018-05-24 12:08:29",
      "updated_at": "2018-05-24 12:42:13",
    },
    {
      "id": 11,
      "name": "1234",
      "project_id": 1,
      "profile_id": 2,
      "started_at": "2018-05-24 13:23:34",
      "stopped_at": "2018-05-26 08:30:41",
      "created_at": "2018-05-24 13:23:34",
      "updated_at": "2018-05-26 08:30:41",
    }
  ],
  "bids": [{
    "id": 1,
    "value": "34",
    "created_at": "2018-05-22 12:25:37",
    "updated_at": "2018-05-22 12:25:55"
  }],
}]

let total = projects.reduce((total, project) => {
  // Get the total time 
  let totalHours = project.timers.reduce((totalTime, timer) => {
      let started = moment(timer.started_at);
      let stopped = moment(timer.stopped_at);
      let m = moment.duration(stopped.diff(started));
      return totalTime + Math.round(m.asHours())
  }, 0);

  // Multiply by the bid value, and add to the total
  return total + totalHours * project.bids[0].value;
}, 0);

console.log("Total is", total)

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
&#13;
&#13;
&#13;