<?php
/**
 * +------------------------------------------------------------
 * | 财务 finance/controller/Behavior.php
 * +------------------------------------------------------------
 * | @author: bsh (844783437@qq.com)
 * | @create_time: 2018-04-28 19:14:25
 * +------------------------------------------------------------
 * | @copyright: CIM信息聚合系统 (https://cimxx.com)
 * +------------------------------------------------------------
 * | @last_modified_by: bsh
 * | @last_modified_time: 2018-04-28 19:14:25
 * +------------------------------------------------------------
 * | @todo:
 * +------------------------------------------------------------
 */

namespace app\finance\controller;

use think\facade\Hook;
use think\Db;

class Behavior extends Module
{
    //置顶行为
    function top($order)
    {
        $data = json_decode($order['extend'], 1);
        return Hook::exec('app\\index\\behavior\\Top', $data);
    }

    //红包行为
    function redpack($order)
    {
        $data = json_decode($order['extend'], 1);
        $data = array_merge($data, ['money' => $order['money']]);
        return Hook::exec('app\\index\\behavior\\Redpack', $data);
    }

    //聚合行为
    function goods($order)
    {
        $order_id = $order['id'];
        $user_id = $order['user_id'];
        $data = model('app\finance\model\Goods')->where('order_id', '=', $order_id)->select();
        if (!empty($data)) {
            $final_user_money = 0;
            $final_user_credit = 0;
            $final_money = 0;
            $behavior = [];
            foreach ($data as $k => $v) {
                $final_user_money += $v['user_money'];
                $final_user_credit += $v['user_credit'];
                $final_money += $v['money'];
                if (!empty($v['extend'])) $behavior[$v['behavior']] = object_to_array($v['extend']);
            }
            Db::startTrans();
            try {
                if (!empty($final_money)) {
                    if (!empty($final_user_money)) {
                        model('app\user\model\User')->where('id', '=', $user_id)->setDec('money', $final_user_money);
                    }
                    if (!empty($final_user_credit)) {
                        model('app\user\model\User')->where('id', '=', $user_id)->setDec('credit', $final_user_credit);
                    }
                } else if (!empty($final_user_money)) {
                    if (!empty($final_user_credit)) {
                        model('app\user\model\User')->where('id', '=', $user_id)->setDec('credit', $final_user_credit);
                    }
                }
                if (!empty($behavior)) {
                    foreach ($behavior as $k => $v) {
                        if (class_exists('app\index\behavior\\' . ucfirst($k))) {
                            Hook::exec('app\\index\\behavior\\' . ucfirst($k), array_merge($order, $v));
                        }
                    }
                }
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
            }
        }
        return json(['code' => 1, 'msg' => '']);
    }

    //充值置顶行为
    function recharge_top($order)
    {
        $behavior = $order['behavior'];
        //创建置顶扣款订单
        $attr = [
            'user_id' => $order['user_id'],
            'balance' => '0',
            'type' => '1',
            'payment' => '1',
            'money' => $order['money'],
            'memo' => '置顶信息扣款',
            'behavior' => $behavior,
            'extend' => $order['extend']
        ];
        $api = new Api();
        $create_order = $api->order($attr, 0);
        if ($create_order['code'] == 1) {
            //进行支付操作
            return $api->pay(['order' => $attr['order']], 0);
        } else {
            return ['code' => 0, 'msg' => '置顶失败:' . $create_order['msg']];
        }
    }
}