NotifyCommonController.php 5.7 KB
<?php
/**
 * Created by PhpStorm.
 * User: 29925
 * Date: 2018/7/23
 * Time: 15:19
 */

namespace api\portal\controller;

use api\portal\model\AgencyModel;
use api\portal\model\MemberModel;
use api\portal\model\MoneyDetailModel;
use api\portal\model\OrderModel;
use api\portal\model\RewardModel;
use api\portal\model\UserNeedModel;
use cmf\controller\HomeBaseController;
use think\Db;

class NotifyCommonController extends HomeBaseController
{

    /**
     * 支付回调处理方法(通用)
     * @param where 回调订单的where条件(含订单号,支付金额)
     * @param transaction_id 支付业务参数
     * @param pay_type 支付类型
     * @param title 支付原因
     */
    protected function changeOrderStatus($where, $transaction_id, $pay_type, $title) {
        $order_model = new OrderModel();
        $orderInfo = $order_model->where($where)->find();
        if($orderInfo['status'] == 1) {
            $order_model->startTrans();
            $update = [
                'status' => 2,
                'pay_type' => $pay_type,
                'pay_time' => time(),
                'transaction_id'=>$transaction_id
            ];
            // 根据支付类型修改不同表的数据
            if($orderInfo['type'] == 1) {
                $user_need_model = new UserNeedModel();
                $pay = $order_model->where($where)->update($update);
                $status = $user_need_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
            }
            if($orderInfo['type'] == 2) {
                $agency_model = new AgencyModel();
                $pay = $order_model->where($where)->update($update);
                $status = $agency_model->where(['id'=>$orderInfo['need_id']])->update(['status'=>2,'pay_time'=>time()]);
                // 增加中介报价人数
                $need_id = $agency_model->where(['id'=>$orderInfo['need_id']])->value('need_id');
                $user_need_model = new UserNeedModel();
                $change_people = $user_need_model->where(['id'=>$need_id])->setInc('people');
                if(!$change_people) {
                    return 0;
                }
            }
            if($orderInfo['type'] == 3) {
                $Intermediary = controller('Intermediary','controller');
                $agencyOffer = $Intermediary->agencyOffer($orderInfo['user_id'],$orderInfo['need_id']);
                $update['lower_price'] = $agencyOffer['money'];
                $pay = $order_model->where($where)->update($update);
                $status = 1;
            }
            if(!$pay || !$status) {
                $order_model->rollback();
                return 0;
            }
            // 记录用户金额记录
            $member_model = new MemberModel();
            $user = $member_model->field(['id,type,referee_id'])->where(['id'=>$orderInfo['user_id']])->find();
            if(!$user) {
                $order_model->rollback();
                return 0;
            }
            $money_detail_model = new MoneyDetailModel();
            if($orderInfo['type'] == 1) {
                $order_type = 1;
            }
            if($orderInfo['type'] == 2) {
                $order_type = 2;
            }
            if($orderInfo['type'] == 3) {
                $order_type = 3;
            }
            $insert = [
                'order_id' => $orderInfo['id'],
                'user_id' => $user['id'],
                'user_type' => $user['type'],
                'title' => $title,
                'money' => $orderInfo['money'],
                'order_type' => $order_type,
                'type' => 2,
                'pay_type' => $pay_type,
                'create_time' => time(),
                'update_time' => time()
            ];
            $money_detail_insert = $money_detail_model->insertGetId($insert);
            if(!$money_detail_insert) {
                $order_model->rollback();
                return 0;
            }
            // 增加用户推荐人余额
//            if(!empty($user['referee_id']) && ($orderInfo['type'] == 1 || $orderInfo['type'] == 2)) {
////                $referee_user = $member_model->where(['id'=>$user['referee_id']])->find();
//////                Db::name('Test')->insertGetId(['data'=>json_encode($referee_user)]);
////                if(!empty($referee_user)) {
////                    $reward_model = new RewardModel();
////                    $reward_number = $reward_model->where(['type'=>5,'status'=>['neq',9]])->value('number');
////                    $referee_result = $member_model->where(['id'=>$user['referee_id']])->setInc('money',$reward_number);
////                    if(!$referee_result) {
////                        $order_model->rollback();
////                        return 0;
////                    }
////                    $insert = [
////                        'order_id' => $orderInfo['id'],
////                        'user_id' => $user['id'],
////                        'user_type' => $user['type'],
////                        'title' => '推荐人奖励',
////                        'money' => $reward_number,
////                        'order_type' => 7,
////                        'type' => 1,
////                        'pay_type' => 3,
////                        'create_time' => time(),
////                        'update_time' => time()
////                    ];
////                    $money_detail_insert2 = $money_detail_model->insertGetId($insert);
////                    if(!$money_detail_insert2) {
////                        $order_model->rollback();
////                        return 0;
////                    }
////                }
////            }
            $order_model->commit();
            return 1;
        }
    }

}