<?php

namespace app\common\service;

use app\common\model\Worker as WorkerModel;
use app\common\model\WorkerIncome as WorkerIncomeModel;

class WorkerIncomeService extends BaseService
{
    public function add($id, $value, $title, $remark = '')
    {
        if (empty($value)) {
            return false;
        }

        $worker   = WorkerModel::where('id', $id)->find();
        $income = new WorkerIncomeModel();
        $income->save([
            'workerid'     => $id,
            'title'        => $title,
            'origin_value' => $worker['income'],
            'value'        => $value,
            'remark'       => $remark,
            'createtime'   => time(),
            'yeartime'     => date("Y"),
            'monthtime'    => date("Ym"),
        ]);

        //修改收益
        $worker->income += $value;
        if ($value > 0) {
            $worker->income_total += $value;
        }
        $worker->save();
    }
}