TaskController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\controllers;
  8. use app\constants\OptionSetting;
  9. use app\events\OrderEvent;
  10. use app\events\SupplierOrderEvent;
  11. use app\jobs\SharingReceiverHandleJob;
  12. use app\jobs\SyncSharerListJob;
  13. use app\jobs\YunstAutoCashJob;
  14. use app\models\AccountLog;
  15. use app\models\Cash;
  16. use app\models\Goods;
  17. use app\models\GoodsBook;
  18. use app\models\Level;
  19. use app\models\Lg;
  20. use app\models\Option;
  21. use app\models\Order;
  22. use app\models\OrderRefund;
  23. use app\models\SaasUser;
  24. use app\models\SaasUserPriceLog;
  25. use app\models\SharingReceiver;
  26. use app\models\Shop;
  27. use app\models\ShopShare;
  28. use app\models\Store;
  29. use app\models\StoreCash;
  30. use app\models\Supplier;
  31. use app\models\User;
  32. use app\utils\LgApi;
  33. use app\utils\OrderNo;
  34. use app\utils\yunst\src\OrderYunst;
  35. use yii\helpers\Json;
  36. use yii\web\Controller;
  37. use app\modules\admin\models\pospal\PospalForm;
  38. class TaskController extends Controller
  39. {
  40. public function actionTest() {
  41. $order = Order::findOne(8659);
  42. \app\utils\Share\BonusPool::generalUserConsume($order['user_id'], $order['store_id']);
  43. }
  44. /**
  45. * 订单,售后,发放积分,分销处理入口
  46. */
  47. public function actionExecute() {
  48. $h = date('H');
  49. $i = date('i');
  50. $s = date('s');
  51. $is = cache()->get('task_lock');
  52. if ($is) {
  53. return;
  54. }
  55. cache()->set('task_lock', 1, 50);
  56. // 商城列表
  57. $store_list = Store::find()->where(['is_delete' => 0])->select('id, name')->all();
  58. // 订单处理
  59. // $queue = queue_push(new \app\jobs\OrderEventJob([
  60. // 'in_action' => 'executeOrder',
  61. // ]));
  62. if(date('i') % 10 == 2){
  63. $queue = queue_push(new \app\jobs\orderEvent\StoreWxOrderShippingJob());
  64. }
  65. $supplier_list = Supplier::find()->where(['and',['is_delete' => 0], ['>', 'cloud_supplier_id', 0]])->all();
  66. foreach($supplier_list as $supplier) {
  67. try {
  68. // 定时任务
  69. $event = new SupplierOrderEvent();
  70. $event->supplier_id = $supplier->cloud_supplier_id;
  71. $event->onLoad();
  72. } catch(\Exception $e) {
  73. \Yii::error('--------auto-execute-error---- '.$supplier->id.' ----auto-execute-error--------' . $e->getMessage() . $e->getFile() . $e->getLine());
  74. }
  75. }
  76. // 同步视频号小店
  77. if(date('i') % 10 == 0){
  78. $SyncSharerStoreId = null;
  79. if (\Yii::$app->prod_is_duli()) {
  80. $SyncSharerStoreId = 1;
  81. }
  82. $queue = queue_push(new SyncSharerListJob(['type' => 1, 'store_id' => $SyncSharerStoreId]));
  83. $queue = queue_push(new SyncSharerListJob(['type' => 2, 'store_id' => $SyncSharerStoreId]));
  84. $queue = queue_push(new SyncSharerListJob(['type' => 3, 'store_id' => $SyncSharerStoreId]));
  85. $queue = queue_push(new SharingReceiverHandleJob());
  86. }
  87. // $queue = queue_push(new \app\jobs\OrderEventJob([
  88. // 'in_action' => 'executeLevel',
  89. // ]));
  90. $queue = queue_push(new \app\jobs\orderEvent\OtherThirdJob());
  91. // $queue = queue_push(new \app\jobs\OrderEventJob([
  92. // 'in_action' => 'executeAdapayShareToSelf',
  93. // ]));
  94. // $version = time();
  95. // cache()->set('executeBonusPool', $version);
  96. // $queue = queue_push(new \app\jobs\OrderEventJob([
  97. // 'in_action' => 'executeBonusPool',
  98. // 'version' => $version,
  99. // ]));
  100. if(date('Hi') === '0101'){
  101. $queue = queue_push(new \app\jobs\orderEvent\JuShuiTanTokenJob());
  102. /**
  103. * 全域分佣奖金池自动发放
  104. */
  105. $queue = queue_push(new \app\jobs\orderEvent\ReportPoolJob());
  106. }
  107. $queue = queue_push(new \app\jobs\orderEvent\Order2JstSupplierJob());
  108. if(date('Hi') === '0111'){
  109. $queue = queue_push(new \app\jobs\orderEvent\JuShuiTanTokenSupplierJob());
  110. }
  111. if(date('Hi') === '0121'){
  112. $queue = queue_push(new \app\jobs\orderEvent\MaiyatianTokenJob());
  113. }
  114. if(date('Hi') === '0201'){
  115. $res = \app\modules\admin\models\ActivityOrderRebateSelfForm::rebate(0, 1);
  116. }
  117. if(date('His') == '000000'){
  118. $queue = queue_push(new \app\jobs\orderEvent\WechatCashStatusJob());
  119. //定时任务 查询用户等级到期时间
  120. $queue = queue_push(new \app\jobs\orderEvent\UserLevelExpiresHandleJob());
  121. }
  122. $queue = queue_push(new \app\jobs\orderEvent\AutoSendTeamGradesPoolJob());
  123. if($h.$i.$s == '000100'){
  124. queue_push(new \app\jobs\ConsumptionGrowthValueJob());
  125. }
  126. //固定时间重置分拣顺序
  127. queue_push(new \app\jobs\CentralizeGoodsExtSortKeyJob());
  128. return $this->asJson([
  129. 'code' => 0,
  130. 'msg' => '执行完毕'
  131. ]);
  132. }
  133. /**
  134. * 从银豹同步订单到赤店
  135. */
  136. public function actionYinbaoSyncOrders() {
  137. // 商城列表
  138. $store_list = Store::find()->where(['is_delete' => 0])->all();
  139. foreach($store_list as $store) {
  140. try {
  141. // 定时任务
  142. (new PospalForm(['store_id' => $store->id]))->syncOrders();
  143. } catch(\Exception $e) {
  144. \Yii::error('--------auto-execute-error---- '.$store->id.' ----auto-execute-error--------' . $e->getMessage() . $e->getFile() . $e->getLine());
  145. }
  146. }
  147. return $this->asJson([
  148. 'code' => 0,
  149. 'msg' => '执行完毕'
  150. ]);
  151. }
  152. /**
  153. * 自动下架预约时间到期商品
  154. * 脚本执行时间:00:00
  155. */
  156. public function actionAutoOffGoods() {
  157. return $this->asJson([]);
  158. $goods_book = Goods::find()->alias('g')->leftJoin(['gb' => GoodsBook::tableName()], 'g.id=gb.goods_id')
  159. ->where(['g.is_delete' => 0, 'g.status' => 1])
  160. ->andWhere(['in', 'g.product_type', [1, 2]])->select('g.product_type, g.id, gb.date_book, gb.service_book')->asArray()->all();
  161. if (empty($goods_book)) {
  162. return $this->asJson([]);
  163. }
  164. $book_goods_ids = [];
  165. $today = strtotime(date('Y-m-d'));
  166. foreach ($goods_book as $book) {
  167. // 酒店预约商品
  168. if ($book['product_type'] == Goods::GOODS_TYPE_DATE) {
  169. $date_book = Json::decode($book['date_book']);
  170. $book_date_arr = array_column($date_book, 'date');
  171. if (empty($book_date_arr)) {
  172. $book_goods_ids[] = $book['id'];
  173. continue;
  174. }
  175. $count = 0;
  176. foreach ($book_date_arr as $value) {
  177. if (strtotime($value) >= $today) {
  178. $count ++;
  179. }
  180. }
  181. if ($count < 1) {
  182. $book_goods_ids[] = $book['id'];
  183. }
  184. }
  185. // 服务预约商品
  186. if ($book['product_type'] == Goods::GOODS_TYPE_TIME) {
  187. $service_book = Json::decode($book['service_book']);
  188. $service_date_arr = array_column($service_book['data'], 'date');
  189. if (empty($service_date_arr)) {
  190. $book_goods_ids[] = $book['id'];
  191. continue;
  192. }
  193. $count = 0;
  194. foreach ($service_date_arr as $value) {
  195. if (strtotime($value) >= $today) {
  196. $count ++;
  197. }
  198. }
  199. if ($count < 1) {
  200. $book_goods_ids[] = $book['id'];
  201. }
  202. }
  203. }
  204. // 下架商品
  205. if (!empty($book_goods_ids)) {
  206. Goods::updateAll(['status' => Goods::STATUS_DISABLE], ['in', 'id', $book_goods_ids]);
  207. \Yii::warning([date('Y-m-d') . ' 自动下架预约过期商品,ID为:', $book_goods_ids]);
  208. }
  209. }
  210. /**
  211. * 处理通联分账失败
  212. */
  213. public function actionYunstFail(){
  214. $store_list = Store::find()->where(['is_delete' => 0])->select('id, name')->all();
  215. foreach($store_list as $store) {
  216. try {
  217. /**
  218. * 查询待分账的记录
  219. */
  220. $receivers_share = SharingReceiver::find()->where(['store_id' => $store->id
  221. ])
  222. ->andWhere(['is_pay'=>SharingReceiver::PAY_FAIL])
  223. ->asArray()
  224. ->all();
  225. debug_log('开始分账');
  226. debug_log($receivers_share);
  227. foreach($receivers_share as $receivers_share1){
  228. sleep(1);
  229. //开始分账
  230. debug_log('开始分账1');
  231. debug_log($receivers_share1);
  232. $orderNoHead = substr($receivers_share1['order_no'], 0, 2);
  233. $where = [];
  234. $is_mall = 0;
  235. $is_scan = 0;
  236. $model = Order::find();
  237. switch ($orderNoHead) {
  238. case 'ML':
  239. $is_mall = 1;
  240. //商盟会员购物订单
  241. $model = Order::find();
  242. $where = [
  243. 'order_no' => $receivers_share1['order_no'],
  244. 'is_delete' => 0,
  245. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  246. ];
  247. break;
  248. case 'AL':
  249. //商盟会员购物订单
  250. $model = Order::find();
  251. $where = [
  252. 'order_no' => $receivers_share1['order_no'],
  253. 'is_delete' => 0,
  254. 'trade_status' => Order::ORDER_FLOW_CONFIRM
  255. ];
  256. break;
  257. case 'SC':
  258. //当面付订单
  259. $model = \app\plugins\scanCodePay\models\Order::find();
  260. $where = [
  261. 'order_no' => $receivers_share1['order_no'],
  262. 'is_delete' => 0,
  263. 'is_pay' => 1
  264. ];
  265. $is_scan = 1;
  266. break;
  267. }
  268. $order = $model->where($where)->one();
  269. if ($order){
  270. //这里重新方法调用orderYunst来实现分账
  271. $res = OrderYunst::share($order, $receivers_share1);
  272. //通联分账
  273. if ($res['code'] == 0) {
  274. debug_log('分账成功了');
  275. debug_log($res);
  276. $order_id = $res['res']['order_id'];
  277. SharingReceiver::updateAll(['updated_at' => time(), 'is_pay' => SharingReceiver::PAY_SUCCESS, 'order_id' => $res['res']['order_id']], ['id' => $receivers_share1['id']]);
  278. $this->saasUserPriceLog($order, [$receivers_share1]);
  279. $this->saasUserPriceLog($order, [$receivers_share1], true);
  280. //如果是分账成功自动提现
  281. // $this->autoCash($order_id);//不在分账后再自动分账,变为每天t+1十点半以后在处理提现
  282. }else{
  283. $faildDes = $res['msg'];
  284. $faildDes && $faildDes = addslashes('(失败:' . $faildDes . ')');
  285. //分到平台
  286. if($receivers_share1['type'] == 2){
  287. if ($is_scan) {
  288. $order_no = OrderNo::getOrderNo(OrderNo::ORDER_SCAN_CODE_PAY);
  289. $scan_order = \app\plugins\scanCodePay\models\Order::findOne(['id'=>$order->id]);
  290. } else {
  291. $order_no_head = OrderNo::ORDER_ALLIANCE;
  292. if ($is_mall) {
  293. $order_no_head = OrderNo::ORDER_MALL;
  294. }
  295. $order_no = OrderNo::getOrderNo($order_no_head);
  296. $scan_order = \app\models\Order::findOne(['id' => $order->id]);
  297. }
  298. $scan_order->order_no = $order_no;
  299. $scan_order->save();
  300. SharingReceiver::updateAll([
  301. 'updated_at' => time(),
  302. 'order_no' => $order_no,
  303. 'yunst_order_no'=> $res['res']['yunst_order_no'],
  304. 'is_pay' => SharingReceiver::PAY_FAIL,
  305. 'err_code' => !empty($res['res']['err_code']) ? $res['res']['err_code'] : $res['msg'],
  306. 'err_code_des' => !empty($res['res']['err_code_des']) ? $res['res']['err_code_des'] : $res['msg']], ['id' => $receivers_share1['id']]);
  307. $id = \Yii::$app->cache->get('yunst_receiver_fail_id' . $receivers_share1['id']);
  308. if ($id && !\Yii::$app->queue->isDone($id)) {
  309. \Yii::$app->queue->remove($id);
  310. }
  311. $id = \queue_push(new \app\jobs\YunstSharingReceiverHandleFailJob(['order_no' => $scan_order->order_no]), 86400);
  312. \Yii::$app->cache->set('yunst_receiver_fail_id' . $receivers_share1['id'], $id);
  313. }
  314. }
  315. }
  316. }
  317. } catch(\Exception $e) {
  318. // \Yii::error($e);
  319. debug_log('--------auto-yunst-error---- '.$store->id.' ----auto-yunst-error--------' . $e->getMessage() . $e->getFile() . $e->getLine());
  320. }
  321. }
  322. }
  323. /**
  324. * 自动提现
  325. * @return void
  326. */
  327. public function autoCash($order_id)
  328. {
  329. debug_log('进来了吧哈哈哈');
  330. $sharing_info = SharingReceiver::find()->where(['order_id' => $order_id, 'is_pay' => SharingReceiver::PAY_SUCCESS])->one();
  331. if ($sharing_info){
  332. $order_info = \app\plugins\scanCodePay\models\Order::find()->where(['id' => $sharing_info->order_id])->one();
  333. if ($order_info){
  334. // $store_price = $sharing_info->amount; //平台抽用金额
  335. // $mch_price = ($order_info->pay_price - $sharing_info->amount);//商户实际到账金额
  336. //因为手续费原因这里提现金额按照商户实际到账金额计算
  337. $fees = round(($order_info->pay_price * 0.003),2);
  338. if ($fees){
  339. $store_price = $sharing_info->amount - $fees; //平台抽用金额
  340. }else{
  341. $store_price = $sharing_info->amount ; //平台抽用金额
  342. }
  343. $mch_price = ($order_info->pay_price - $sharing_info->amount);//商户实际到账金额
  344. debug_log($store_price);
  345. debug_log($mch_price);
  346. //平台提现
  347. $cash = new StoreCash();
  348. $cash->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  349. $cash->is_delete = 0;
  350. $cash->status = 0;
  351. $cash->price = $store_price;
  352. $cash->created_at = time();
  353. $cash->store_id = -1;
  354. $cash->type =6;//通联提现
  355. $cash->name = '平台提现';
  356. $cash->mobile = '';
  357. $cash->pay_time = 0;
  358. $cash->service_charge = 0;
  359. $cash->cash_type = 0;
  360. if ($cash->save()) {
  361. //调用通联自动提现接口
  362. $orderYunst = new OrderYunst();
  363. // $res = $orderYunst->queryAcctInfo('66065100000219T');
  364. $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
  365. if ($oldConf) {
  366. $oldConf = json_decode($oldConf, true);
  367. if (!empty($oldConf)) {
  368. $customer_number = $oldConf['customer_number'];
  369. $res = $orderYunst->queryAcctInfo($customer_number);
  370. $amount = 0;
  371. if ($res['subCode'] == 'SUCCESS'){
  372. $amount = $res['data']['amount'];//真的
  373. }
  374. if ($store_price > $amount) {
  375. debug_log('平台提现金额大于通联账户余额');
  376. }
  377. $orderYunst = new OrderYunst();
  378. $data['bizOrderNo'] = $cash->order_no;
  379. $data['bizUserId'] = $customer_number;//客户号
  380. $data['amount'] = intval($cash->price * 100);
  381. $data['notifyUrl'] = \Yii::$app->request->hostInfo;
  382. $data['remark'] = "";
  383. $data['authWay'] = "3";
  384. $res_cash = $orderYunst->servicerWithdraw($data);
  385. if ($res_cash['subCode'] != 'SUCCESS'){
  386. $cash_info2 = StoreCash::findOne($cash->id);
  387. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  388. $cash_info2->order_no = $order_no;
  389. $cash_info2->save();
  390. }else{
  391. $cash_info = StoreCash::findOne($cash->id);
  392. $cash_info->status = 2;
  393. $cash_info->pay_time = time();
  394. $cash_info->save();
  395. }
  396. }
  397. }
  398. }else{
  399. debug_log('提现保存失败');
  400. }
  401. //商户提现
  402. $cash_mch = new StoreCash();
  403. $cash_mch->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  404. $cash_mch->is_delete = 0;
  405. $cash_mch->status = 0;
  406. $cash_mch->price = $mch_price;
  407. $cash_mch->created_at = time();
  408. $cash_mch->store_id = $sharing_info->store_id;
  409. $cash_mch->type =6;//通联提现
  410. $cash_mch->name = '商户提现';
  411. $cash_mch->mobile = '';
  412. $cash_mch->pay_time = 0;
  413. $cash_mch->service_charge = 0;
  414. $cash_mch->cash_type = 0;
  415. if ($cash_mch->save()) {
  416. //调用通联自动提现接口
  417. $store = \app\models\Store::findOne($sharing_info->store_id);
  418. $orderYunst = new OrderYunst();
  419. $res2 = $orderYunst->queryAcctInfo($store->store_number);
  420. debug_log($res2);
  421. $amount2 = 0;
  422. if ($res2['subCode'] == 'SUCCESS'){
  423. $amount2 = $res2['data']['amount'];//真的
  424. }
  425. if ($mch_price > $amount2) {
  426. debug_log('平台提现金额大于通联账户余额');
  427. }
  428. $orderYunst = new OrderYunst();
  429. $data2['bizOrderNo'] = $cash_mch->order_no;
  430. $store = Store::findOne(['id' => $cash_mch->store_id]);
  431. $data2['bizUserId'] = $store->store_number;
  432. $data2['amount'] = intval($cash_mch->price * 100);
  433. $data2['notifyUrl'] = \Yii::$app->request->hostInfo;
  434. $data2['remark'] = "";
  435. $data2['authWay'] = "3";
  436. debug_log($data2);
  437. $res_cash2 = $orderYunst->servicerWithdraw($data2);
  438. debug_log($res_cash2);
  439. if ($res_cash2['subCode'] != 'SUCCESS'){
  440. debug_log('商户提现失败');
  441. debug_log($res_cash2['subMsg']);
  442. $cash_info2 = StoreCash::findOne($cash_mch->id);
  443. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  444. $cash_info2->order_no = $order_no;
  445. $cash_info2->save();
  446. }else{
  447. $cash_info2 = StoreCash::findOne($cash_mch->id);
  448. $cash_info2->status = 2;
  449. $cash_info2->pay_time = time();
  450. $cash_info2->save();
  451. debug_log('商户提现成功');
  452. }
  453. }else{
  454. debug_log('提现保存失败2');
  455. }
  456. }else{
  457. debug_log('订单不存在,提现失败');
  458. }
  459. }else{
  460. debug_log('分账不存在,提现失败');
  461. }
  462. }
  463. private function saasUserPriceLog($order, $SharingReceivers = [], $expend = false, $log = '') {
  464. try {
  465. $log_type = SaasUserPriceLog::LOG_TYPE_INCOME;
  466. $remark = '';
  467. if($expend){
  468. $log_type = SaasUserPriceLog::LOG_TYPE_EXPEND;
  469. $remark = '-分账成功';
  470. }
  471. $log && $remark = $log;
  472. foreach ($SharingReceivers as $item) {
  473. \Yii::warning(['---- saasUserPriceLog Err11 ----', $item]);
  474. if(in_array($item['type'], [SharingReceiver::RECEIVE_MERCHANT_ID])){
  475. continue;
  476. }
  477. $desc = $item['remark'] . $remark;
  478. $saas_id = $item['saas_id'];
  479. if(!$saas_id){
  480. if(!$order->is_platform){
  481. $saas_id = $item['user_id'];
  482. }else{
  483. $userInfo = User::findOne($item['user_id']);
  484. if(!empty($userInfo)){
  485. $saas = SaasUser::findOne(['mobile' => $userInfo->binding]);
  486. $saas_id = $saas->id;
  487. }
  488. }
  489. }
  490. \Yii::warning(['---- saasUserPriceLog Err22 ----', $saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc]);
  491. SaasUserPriceLog::saveLog($saas_id, $item['amount'], $item['from'], $log_type, $order->order_no, $desc);
  492. }
  493. } catch (\Exception $e) {
  494. \Yii::warning('---- saasUserPriceLog Err ----' . __LINE__);
  495. }
  496. }
  497. /**
  498. * 自动提现
  499. * @return void
  500. */
  501. public function actionAutoCashNew()
  502. {
  503. $oldConf = Option::get(OptionSetting::SAAS_YUNST_SETTING, 0, 'saas')['value'];
  504. $queue_id = \queue_push(new YunstAutoCashJob(['retry' => 5]), 60);
  505. if ($oldConf){
  506. $oldConf = json_decode($oldConf, true);
  507. if (!empty($oldConf)) {
  508. $customer_number = $oldConf['customer_number'];
  509. //查询平台余额
  510. $orderYunst = new OrderYunst();
  511. $res = $orderYunst->queryAcctInfo($customer_number);
  512. debug_log($res);
  513. $store_amount = 0;
  514. if ($res['subCode'] == 'SUCCESS'){
  515. $store_amount = $res['data']['amount'];//真的
  516. }
  517. if ($store_amount) {
  518. $cash = new StoreCash();
  519. $cash->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  520. $cash->is_delete = 0;
  521. $cash->status = 0;
  522. $cash->price = $store_amount/100;
  523. $cash->created_at = time();
  524. $cash->store_id = -1;
  525. $cash->type =6;//通联提现
  526. $cash->name = '平台提现';
  527. $cash->mobile = '';
  528. $cash->pay_time = 0;
  529. $cash->service_charge = 0;
  530. $cash->cash_type = 0;
  531. if ($cash->save()) {
  532. //调用通联自动提现接口
  533. $orderYunst = new OrderYunst();
  534. $data['bizOrderNo'] = $cash->order_no;
  535. $data['bizUserId'] = $customer_number;//客户号
  536. $data['amount'] = ceil(bcmul($cash->price, 100));
  537. $data['notifyUrl'] = \Yii::$app->request->hostInfo;
  538. $data['remark'] = "";
  539. $data['authWay'] = "3";
  540. debug_log($data);
  541. $res_cash = $orderYunst->servicerWithdraw($data);
  542. debug_log($res_cash);
  543. if ($res_cash['subCode'] != 'SUCCESS'){
  544. debug_log('平台提现失败');
  545. debug_log($res_cash['subMsg']);
  546. $cash_info2 = StoreCash::findOne($cash->id);
  547. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  548. $cash_info2->order_no = $order_no;
  549. $cash_info2->save();
  550. }else{
  551. $cash_info = StoreCash::findOne($cash->id);
  552. $cash_info->status = 2;
  553. $cash_info->pay_time = time();
  554. $cash_info->save();
  555. debug_log('平台提现成功');
  556. }
  557. }else{
  558. debug_log('提现保存失败');
  559. }
  560. }else{
  561. debug_log('平台提现金额不足');
  562. }
  563. }
  564. }
  565. // 商城列表
  566. $store_list = Store::find()->where(['is_delete' => 0])->andWhere(['!=','cusid',''])->asArray()->all();
  567. foreach($store_list as $store) {
  568. try {
  569. $orderYunst = new OrderYunst();
  570. $res2 = $orderYunst->queryAcctInfo($store['store_number']);
  571. $mch_amount = 0;
  572. if ($res2['subCode'] == 'SUCCESS'){
  573. $mch_amount = $res2['data']['amount'];//真的
  574. }
  575. // print_r($mch_amount/100);die;
  576. if ($mch_amount) {
  577. $cash_mch = new StoreCash();
  578. $cash_mch->order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  579. $cash_mch->is_delete = 0;
  580. $cash_mch->status = 0;
  581. $cash_mch->price = $mch_amount/100;
  582. $cash_mch->created_at = time();
  583. $cash_mch->store_id = $store['id'];
  584. $cash_mch->type =6;//通联提现
  585. $cash_mch->name = '商户提现';
  586. $cash_mch->mobile = '';
  587. $cash_mch->pay_time = 0;
  588. $cash_mch->service_charge = 0;
  589. $cash_mch->cash_type = 0;
  590. if ($cash_mch->save()) {
  591. $orderYunst = new OrderYunst();
  592. $data2['bizOrderNo'] = $cash_mch->order_no;
  593. $data2['bizUserId'] = $store['store_number'];
  594. $data2['amount'] = ceil(bcmul($cash_mch->price, 100));
  595. $data2['notifyUrl'] = \Yii::$app->request->hostInfo;
  596. $data2['remark'] = "";
  597. $data2['authWay'] = "3";
  598. debug_log($data2);
  599. $res_cash2 = $orderYunst->servicerWithdraw($data2);
  600. debug_log($res_cash2);
  601. if ($res_cash2['subCode'] != 'SUCCESS'){
  602. debug_log('商户提现失败');
  603. debug_log($res_cash2['subMsg']);
  604. $cash_info2 = StoreCash::findOne($cash_mch->id);
  605. $order_no = \app\utils\OrderNo::getOrderNo(\app\utils\OrderNo::ORDER_CASH);
  606. $cash_info2->order_no = $order_no;
  607. $cash_info2->save();
  608. }else{
  609. $cash_info2 = StoreCash::findOne($cash_mch->id);
  610. $cash_info2->status = 2;
  611. $cash_info2->pay_time = time();
  612. $cash_info2->save();
  613. debug_log('商户提现成功');
  614. }
  615. }else{
  616. debug_log('提现保存失败2');
  617. }
  618. }else{
  619. debug_log('商户可用余额不足');
  620. }
  621. } catch(\Exception $e) {
  622. \Yii::error('--------auto-execute-cash-error---- '.$store->id.' ----auto-execute-cash-error--------' . $e->getMessage() . $e->getFile() . $e->getLine());
  623. }
  624. }
  625. if ($queue_id && !\Yii::$app->queue->isDone($queue_id)) {
  626. \Yii::$app->queue->remove($queue_id);
  627. }
  628. return $this->asJson([
  629. 'code' => 0,
  630. 'msg' => '执行完毕'
  631. ]);
  632. }
  633. }