<?php

namespace Modules\Jiesuan\Services;

use Illuminate\Support\Facades\DB;

class CompanyUserMoneyService
{
    /**
     * 公司管理员用户金额详情
     * @param $companyUid
     * @return null | object
     * <pre>
     * {
     *      'company_uid' : 1, // 公司管理员uid
     *      'total_income' : 1, // 总收入,
     *      'total_dakuan' :1, //已打款金额
     *      'total_tuikuan' : 1, // 总退款金额
     *      'tixian_money' : 1, // 审核中提现金额
     *      'yue_money' : 1, // 当前余额
     * }
     * </pre>
     */
    public static function userMoneyInfo($companyUid)
    {
        $moneyInfo = DB::table('company_user_money')
            ->where('company_uid', $companyUid)
            ->select('total_income', 'total_dakuan', 'total_tuikuan', 'company_uid')
            ->first();
        if(!$moneyInfo) {
            return (object)[
                'company_uid' => $companyUid,
                'total_income' => 0,
                'total_dakuan' => 0,
                'total_tuikuan' => 0,
                'tixian_money' => 0,
                'yue_money' => 0,
            ];
        }

        $tixian_money = DB::table('tixian_records')
            ->where('company_uid', $companyUid)
            ->whereIn('status', [1, 3])
            ->sum('tixian_money');

        $moneyInfo->tixian_money = $tixian_money;
        $moneyInfo->yue_money = intval(($moneyInfo->total_income - $moneyInfo->total_tuikuan - $moneyInfo->total_dakuan - $tixian_money) * 100) / 100;
        return $moneyInfo;
    }

    /**
     * 用户金额变动日志记录
     * @param $companyUid
     * @param $type
     * @param $before
     * @param $after
     */
    public static function log($companyUid, $type, $before, $after) {
        $now = date('Y-m-d H:i:s');
        $field = [
            'total_income' => '总充值',
            'total_dakuan' => '总打款',
            'total_tuikuan' => '总退款',
            'tixian_money' => '提现金额',
            'yue_money' => '余额'
        ];
        foreach ($field as $key=>$val) {
            $des[] = sprintf('%s变动:[%s元-->%s元]', $val, $before->{$key}, $after->{$key});
        }
        DB::table('company_user_money_change_logs')
            ->insert([
                'type' => $type,
                'company_uid' => $companyUid,
                'created_at' => $now,
                'log' => \json_encode([
                    'desc' => join(', ', $des),
                    'before' => [
                        'total_income' => $before->total_income,
                        'total_dakuan' => $before->total_dakuan,
                        'total_tuikuan' => $before->total_tuikuan,
                        'tixian_money' => $before->tixian_money,
                        'yue_money' => $before->yue_money,
                    ],
                    'after' => [
                        'total_income' => $after->total_income,
                        'total_dakuan' => $after->total_dakuan,
                        'total_tuikuan' => $after->total_tuikuan,
                        'tixian_money' => $after->tixian_money,
                        'yue_money' => $after->yue_money,
                    ],
                    'created_at' => $now,
                ], JSON_UNESCAPED_UNICODE)
            ]);
    }

}
