quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
 
namespace app\shop\model\plus\release;
 
use app\common\library\easywechat\AppWx;
use app\common\library\easywechat\AppMp;
use app\common\model\app\AppMp as AppMpModel;
use app\common\model\app\AppOpen as AppOpenModel;
use app\common\model\app\AppWx as AppWxModel;
use app\common\model\plus\release\Setting;
use app\common\service\message\MessageService;
use app\common\service\order\OrderService;
use app\common\library\easywechat\WxPay;
use app\common\model\plus\release\Cash as CashModel;
use app\shop\model\user\User as UserModel;
use app\shop\service\order\ExportService;
use app\common\library\fbpay\FbPay;
use app\shop\model\user\UserAuth;
use app\shop\model\shop\FbCashApply;
 
/**
 * 提现明细模型
 */
class Cash extends CashModel
{
    /**
     * 获取器:申请时间
     */
    public function getAuditTimeAttr($value)
    {
        return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
    }
 
    /**
     * 获取器:打款方式
     */
    public function getPayTypeAttr($value)
    {
        return ['text' => $this->payType[$value], 'value' => $value];
    }
 
    /**
     * 获取提现列表
     */
    public function getList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
    {
        $model = $this;
        // 构建查询规则
        $model = $model->alias('cash')
            ->with(['user'])
            ->field('cash.*, release.real_name, release.mobile, user.nickName, user.avatarUrl')
            ->join('user', 'user.user_id = cash.user_id')
            ->join('release_supply_user release', 'release.user_id = cash.user_id')
            ->order(['cash.create_time' => 'desc']);
        // 查询条件
        if ($user_id > 0) {
            $model = $model->where('cash.user_id', '=', $user_id);
        }
        if (!empty($search)) {
            $model = $model->where('release.real_name|repair.mobile', 'like', '%' . $search . '%');
        }
        if ($apply_status > 0) {
            $model = $model->where('cash.apply_status', '=', $apply_status);
        }
        if ($pay_type > 0) {
            $model = $model->where('cash.pay_type', '=', $pay_type);
        }
        // 获取列表数据
        return $model->paginate(15);
    }
 
    /**
     * 提现审核
     */
    public function submit($param)
    {
        $data = ['apply_status' => $param['apply_status']];
        if ($param['apply_status'] == 30) {
            $data['reject_reason'] = $param['reject_reason'];
        }
        // 更新申请记录
        $data['audit_time'] = time();
        self::update($data, ['id' => $param['id']]);
        // 提现驳回:解冻资金
        if ($param['apply_status'] == 30) {
            SupplyUser::backFreezeMoney($param['user_id'], $param['money']);
        }
 
        // 发送模板消息
       // (new MessageService)->cash($this);
        return true;
    }
 
    /**
     * 确认已打款
     */
    public function money()
    {
        $this->startTrans();
        try {
            // 更新申请状态
            $data = ['apply_status' => 40, 'audit_time' => time()];
            self::update($data, ['id' => $this['id']]);
 
            // 更新分销商累积提现佣金
            SupplyUser::totalMoney($this['user_id'], $this['money']);
 
            // 记录资金明细
            Capital::add([
                'user_id' => $this['user_id'],
                'flow_type' => 20,
                'money' => -$this['money'],
                'describe' => '申请提现',
            ]);
            // 发送模板消息
           //(new Message)->withdraw($this);
            // 事务提交
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 分销商提现:微信支付企业付款
     */
    public function wechatPay()
    {
        $settlement = Setting::getItem('settlement');
        if($settlement['wechat_type'] == 0){
            return $this->wechatPay0();
        }else {
            return $this->wechatPay1();
        }
    }
 
    /**
     * 分销商提现:微信支付企业付款
     */
    public function wechatPay0()
    {
        // 微信用户信息
        $user = UserModel::detail($this['user_id']);
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = '供应方提现付款';
        // 微信支付api:企业付款到零钱
        $open_id = '';
        $app = [];
        if($user['reg_source'] == 'mp'){
            $app = AppMp::getWxPayApp($user['app_id']);
            $open_id = $user['mpopen_id'];
        }else if($user['reg_source'] == 'wx'){
            $app = AppWx::getWxPayApp($user['app_id']);
            $open_id = $user['open_id'];
        }
 
        if($open_id == ''){
            $this->error = '未找到用户open_id';
            return false;
        }
 
        $WxPay = new WxPay($app);
        // 请求付款api
        if ($WxPay->transfers($orderNO, $open_id, $this['money'], $desc)) {
            // 确认已打款
            $this->money();
            return true;
        }
        return false;
    }
    
     /**
     * 商家转账到零钱
     */
    public function wechatPay1()
    {
        // 微信用户信息
        $user = UserModel::detail($this['user_id']);
        // 生成付款订单号
        $orderNO = OrderService::createOrderNo();
        // 付款描述
        $desc = '余额提现付款';
        // 微信支付api:企业付款到零钱
        $open_id = '';
        $app_id = '';
        if ($user['reg_source'] == 'mp') {
            $open_id = $user['mpopen_id'];
            $wxConfig = AppMpModel::getAppMpCache($app_id);
            $app_id = $wxConfig['mpapp_id'];
        } else if ($user['reg_source'] == 'wx') {
            $open_id = $user['open_id'];
            $wxConfig = AppWxModel::getAppWxCache($app_id);
            $app_id = $wxConfig['wxapp_id'];
        } else if ($user['reg_source'] == 'app') {
            $open_id = $user['appopen_id'];
            $wxConfig = AppOpenModel::getAppOpenCache($app_id);
            $app_id = $wxConfig['openapp_id'];
        }
 
        if ($open_id == '') {
            $this->error = '未找到用户open_id';
            return false;
        }
 
        $pars = [];
        $pars['appid'] = $app_id;//直连商户的appid
        $pars['out_batch_no'] = 'sjzz'.date('Ymd').mt_rand(1000, 9999);//商户系统内部的商家批次单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
        $pars['batch_name']   = $desc;//该笔批量转账的名称
        $pars['batch_remark'] = $desc;//转账说明,UTF8编码,最多允许32个字符
        $pars['total_amount'] = intval($this['money'] * 100);//转账总金额 单位为“分”
        $pars['total_num']    = 1;//转账总笔数
        $pars['transfer_detail_list'][0]  = [
            'out_detail_no'=>'Dh'.$orderNO,
            'transfer_amount'=>$pars['total_amount'],
            'transfer_remark'=> $desc,
            'openid' => $open_id
        ];//转账明细列表
        //获取token
        $wxPay = new WxPay(null);
        $res = $wxPay->wechatTrans($pars, $user['app_id']);
        $resArr = json_decode($res,true);
        if(isset($resArr['batch_id'])){
            $this->save([
                'batch_id' => $resArr['batch_id']
            ]);
            // 确认打款
            $this->money();
            return true;
        }else{
            $this->error = $resArr['message'];
            return false;
        }
    }
 
    /**
     * 提现:分账功能
     */
    public function fbPay()
    {
        // 用户认证的信息
        $userAuth = UserAuth::detail($this['user_id']);
        if(empty($userAuth)){
            $this->error = '该用户未实名认证';
            return false;
        }elseif(!empty($userAuth) && empty($userAuth["account_id"])){
            $this->error = '该用户实名认证还未审核通过';
            return false;
        }
        //获取向付呗申请的记录
        $where["cash_id"] = $this['id'];
        $apply = FbCashApply::detail($where);
        if(empty($apply)){
            //只生成一次订单 否则造成多次打同一笔款
            $merchant_order_sn = OrderService::createOrderNo();
            // 更新订单号:用于回调 更新打款状态
            $save_data = [
                'merchant_order_sn' => $merchant_order_sn,
                'cash_id' => $this['id'],
                'type' => 'agent',
                'user_id' => $this['user_id'],
            ];
            $model =new FbCashApply();
            $model->save($save_data);
        }else{
            $merchant_order_sn = $apply['merchant_order_sn'];
        }
 
        // 分账接收方ID
        $data["account_id"] = $userAuth["account_id"];
        // 生成付款订单号
        $data["merchant_order_sn"] = $merchant_order_sn;
        //打款金额
        $data["amount"]=$this['real_money'];
 
        //如果是付呗接口,配置参数 by yj
        $app = FbPay::getFbPayApp($userAuth["app_id"], 'wx');
        $FbPay = new FbPay($app);
        // 请求分账api
        if ($FbPay->withdraw($data)) {
            return true;
        }
        return false;
    }
 
    /*
     *统计提现总数量
     */
    public function getAgentOrderTotal()
    {
        return $this->count('id');
    }
 
    /*
    * 统计提现待审核总数量
    */
    public function getAgentApplyTotal($apply_status)
    {
        return $this->where('apply_status', '=', $apply_status)->count();
    }
 
    /**
     * 导出提现
     */
    public function exportList($user_id = null, $apply_status = -1, $pay_type = -1, $search = '')
    {
        $model = $this;
        // 构建查询规则
        $model = $model->alias('cash')
            ->with(['user'])
            ->field('cash.*, repair.real_name, repair.mobile, user.nickName, user.avatarUrl')
            ->join('user', 'user.user_id = cash.user_id')
            ->join('release_supply_user repair', 'repair.user_id = cash.user_id')
            ->order(['cash.create_time' => 'desc']);
        // 查询条件
        if ($user_id > 0) {
            $model = $model->where('cash.user_id', '=', $user_id);
        }
        if (!empty($search)) {
            $model = $model->where('repair.real_name|repair.mobile', 'like', '%' . $search . '%');
        }
        if ($apply_status > 0) {
            $model = $model->where('cash.apply_status', '=', $apply_status);
        }
        if ($pay_type > 0) {
            $model = $model->where('cash.pay_type', '=', $pay_type);
        }
        // 获取列表数据
        $list = $model->select();
        // 导出excel文件
        (new Exportservice)->cashList($list);
 
    }
 
}