<?php

namespace app\common\logic;

use app\common\enum\worker\WorkerAccountLogEnum;
use app\common\model\financial\MasterSettlementDetails;
use app\common\model\master_worker\MasterWorker;
use app\common\model\master_worker\MasterWorkerAccountLog;
use think\Exception;

class WorkerAccountLogLogic extends BaseLogic
{
    /**
     * @param $work //工单对象
     * @param $change_amount //变动数量
     * @param $change_type //变动类型
     * @param $action //动作 1-增加 2-减少
     * @return bool|void
     * @throws Exception
     */
    public static function addAccountLog($work,$changeAmount,$changeType,$action, string $remark = '',  array $extra = [],int $appoint_worker_id = 0)
    {
        $worker = MasterWorker::findOrFail($appoint_worker_id?:$work->master_worker_id);
        if($worker->isEmpty()) {
            throw new Exception('工程师不存在');
        }

        $changeObject = WorkerAccountLogEnum::getChangeObject($changeType);
        if(!$changeObject) {
            throw new Exception('结算错误');
        }

        switch ($changeObject) {
            // 用户余额
            case WorkerAccountLogEnum::UM:
                $left_amount = $worker->user_money;
                break;
            // 其他
        }

        $data = [
            'sn' => generate_sn(MasterWorkerAccountLog::class, 'sn', 20),
            'title'=>$work->title,
            'worker_id' => $appoint_worker_id?:$work->master_worker_id,
            'change_object' => $changeObject,
            'change_type' => $changeType,
            'action' => $action,
            'left_amount' => $left_amount,
            'change_amount' => $changeAmount,
            'work_sn' => $work->work_sn,
            'remark' => $remark,
            'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
        ];
        MasterWorkerAccountLog::create($data);

        $worker->user_money = $left_amount + ($action==1?$changeAmount:-$changeAmount);
        $worker->save();

        return true;
    }
    public static function masterAccountLog(int $worker_id,$changeAmount,$changeType,$action, string $title = '', string $remark = '',  array $extra = [])
    {
        $worker = MasterWorker::findOrFail($worker_id);
        if($worker->isEmpty()) {
            throw new Exception('工程师不存在');
        }

        $changeObject = WorkerAccountLogEnum::getChangeObject($changeType);
        if(!$changeObject) {
            throw new Exception('结算错误');
        }

        switch ($changeObject) {
            // 用户余额
            case WorkerAccountLogEnum::UM:
                $left_amount = $worker->user_money;
                break;
            // 其他
        }
        $worker->user_money = $left_amount + ($action==1?$changeAmount:-$changeAmount);
        $worker->save();
        $data = [
            'sn' => generate_sn(MasterWorkerAccountLog::class, 'sn', 20),
            'title'=>$title,
            'worker_id' => $worker_id,
            'change_object' => $changeObject,
            'change_type' => $changeType,
            'action' => $action,
            'left_amount' => $worker->user_money,
            'change_amount' => $changeAmount,
            'work_sn' => '',
            'remark' => $remark,
            'extra' => $extra ? json_encode($extra, JSON_UNESCAPED_UNICODE) : '',
        ];
        return MasterWorkerAccountLog::create($data);
    }
}