<?php

namespace app\api\logic;

use app\common\enum\worker\WorkerAccountLogEnum;
use app\common\logic\BaseLogic;
use app\common\logic\EffectiveLogic;
use app\common\logic\WorkerAccountLogLogic;
use app\common\model\master_worker\MasterWorker;
use app\common\model\orders\RechargeOrder;
use app\common\model\performance\PerformanceRules;
use think\facade\Db;
use think\facade\Log;


/**
 *  师傅业绩逻辑层
 * Class PerformanceLogic
 * @package app\api\logic
 */
class PerformanceLogic extends BaseLogic
{
    /**
     * @param $work
     * @return false|void
     */
    public static function calculatePerformance($work)
    {
        Log::info('calculatePerformance:'.json_encode($work->toArray()));
        //工单已完结，进行结算,结算完成后设置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;
        }
        Log::info('calculatePerformance:'.json_encode($rule->toArray()));
        //师傅金额结算
        if(!$rule->isEmpty()){
            switch ($rule['type']){
                case 0://按照总金额结算
                    $coupon_price = RechargeOrder::where('work_id',$work->id)->sum('coupon_price');
                    $worker_price = $work->work_total-$coupon_price;
                    break;
                case 1://按照结算金额结算
                    $worker_price = $work->work_amount;
                    break;
                default://按照服务总费用结算
                    //搜索所有优惠券
                    $coupon_price = RechargeOrder::where('work_id',$work->id)->sum('coupon_price');
                    $worker_price = $work->service_fee-$coupon_price;
            }
            $work->worker_price = $worker_price;

            $settlement_amount = bcmul($worker_price, $rule['rate']);

            // 缴纳质保金
            $masterWorkerInfo = MasterWorker::where('id', $work->master_worker_id)->findOrEmpty()->toArray();
            if(!empty($masterWorkerInfo) && $masterWorkerInfo['retention_pay_status'] == 2 && ($masterWorkerInfo['earnest_money'] > $masterWorkerInfo['earnest_money_usable'])){
                $retentionData['action'] = WorkerAccountLogEnum::INC;
                $retentionData['worker_id'] = $work->master_worker_id;
                $retentionData['work_id'] = $work->id;
                $amount = bcmul($settlement_amount, bcdiv($masterWorkerInfo['installment_ratio'], 100, 4),2);
                $retentionData['amount'] = $amount;
                $retentionData['remark'] = "分期缴纳质保金：该单实提成--{$settlement_amount},分期每单缴纳比例--{$masterWorkerInfo['installment_ratio']}%,缴纳金额--{$amount}";
                $settlement_amount -= (float)$amount;
                $surplusMoney = RetentionMoneyLogic::add($retentionData);
                if($surplusMoney === false){
                    Log::info('分期缴纳质保金支付失败-'.RetentionMoneyLogic::getError(),$retentionData);
                    $settlement_amount += (float)$amount;
                }else{
                    $settlement_amount += (float)$surplusMoney;
                }
            }

            WorkerAccountLogLogic::addAccountLog($work,$settlement_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
        }
        $work->save();
        //生成保修卡
        EffectiveLogic::receiveEffective($work);
    }
}