<?php

namespace app\api\logic;

use app\common\enum\worker\WorkerAccountLogEnum;
use app\common\logic\BaseLogic;
use app\common\logic\WorkerAccountLogLogic;
use app\common\model\performance\PerformanceRules;
use think\facade\Db;


/**
 *  师傅业绩逻辑层
 * Class PerformanceLogic
 * @package app\api\logic
 */
class PerformanceLogic extends BaseLogic
{
    /**
     * @param $work
     * @return false|void
     */
    public static function calculatePerformance($work)
    {
        //工单已完结，进行结算,结算完成后设置work_pay_status为2，已结算
            $rule = PerformanceRules::whereFindInSet('goods_category_ids',$work->goods_category_id)->findOrEmpty();
            if($rule->isEmpty()){
                $work->work_pay_status = 3;
            }else{
                $work->work_pay_status = 2;
            }
            $work->save();

            //师傅金额结算
            if(!$rule->isEmpty()){
                if($rule['type']==0){
                    $work_price = $work->work_total;
                }else{
                    $work_price = $work->work_amount;
                }
                $settlement_amount = bcmul($work_price, $rule['rate']);
                WorkerAccountLogLogic::addAccountLog($work,$settlement_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
            }
    }
}