Files
pingan_new/app/Http/Controllers/SkyxuController.php
2020-11-27 10:56:37 +08:00

332 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use App\Models\Coupon;
use App\Models\TestLog;
use App\Models\User;
use Illuminate\Queue\Jobs\Job;
use XuanChen\Coupon\Action\pingan\Query;
use XuanChen\Coupon\Action\ysd\YsdQuery;
class SkyxuController
{
public $user;
public $jiemi;
public $query_coupon;
public $ticket;
public $type;
public function index()
{
$list = Job::get();
dd($list);
Coupon::whereBetween('id', ['70091', '70513'])
->orderBy('created_at', 'ASC')
->chunk(100, function ($logs) {
foreach ($logs as $log) {
if (preg_match('/^YSD/', $log->redemptionCode, $matches)) {
$type = Coupon::TYPE_YSD;
} else {
$type = Coupon::TYPE_PINGAN;
}
$log->type = $type;
$log->save();
}
});
dd();
$count = TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
->where('type', 'freezecoupon')
->where('back', '0')
->orderBy('created_at', 'ASC')
->count();
dump($count);
// $list = TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
// ->where('type', 'freezecoupon')
// ->where('back', '0')
// ->orderBy('created_at', 'ASC')
// ->get();
//
//// $data = [];
//// foreach ($list as $cou) {
//// $data[] = $cou->in_source['jiemi']['redemptionCode'];
//// }
//// dump($data);
if ($count > 1) {
try {
TestLog::whereBetween('created_at', ['2020-11-10 11:06:40', '2020-11-11 10:42:26'])
->where('type', 'freezecoupon')
->where('back', '0')
->orderBy('created_at', 'ASC')
->chunk(100, function ($logs) {
$su = $er = [];
foreach ($logs as $log) {
$this->jiemi = $log->in_source['jiemi'];
$this->user = User::where('outlet_id', $this->jiemi['outletId'])->first();
$this->getQuery();
if (is_string($this->queryData)) {
$er[$this->jiemi['redemptionCode']] = $this->queryData;
continue;
}
if ($this->jiemi['redemptionCode'] == '157922465040') {
dump($this->jiemi);
dump($this->ticket);
dump($this->queryData);
}
$this->checkCode();
if (is_string($this->ticket)) {
$er[$this->jiemi['redemptionCode']] = $this->ticket;
$er[] = $this->jiemi['redemptionCode'];
continue;
}
$status = isset($log->out_source['code']) ? 2 : 3;
$exists = Coupon::where('status', $status)
->where('redemptionCode', $this->jiemi['redemptionCode'])->exists();
if ($exists && $status != 3) {
$er[$this->jiemi['redemptionCode']] = [
$log->out_source,
];
continue;
}
$couponData = [
'user_id' => $this->user->parent->id,
'type' => Coupon::TYPE_YSD,
'outletId' => $this->user->outlet_id,
'orderid' => '',
'PaOutletId' => $this->queryData['PaOutletId'],
'redemptionCode' => $this->jiemi['redemptionCode'],
'thirdPartyGoodsId' => $this->queryData['thirdPartyGoodsId'],
'couponName' => $this->queryData['couponName'],
'price' => $this->ticket['price'],
'total' => $this->jiemi['total'],
'profit' => $this->ticket['profit'],
'status' => $status,
'remark' => isset($log->out_source['code']) ? '核销成功!' : $log->out_source[0],
'startTime' => $this->queryData['startTime'],
'endTime' => $this->queryData['endTime'],
'created_at' => $log->created_at->format('Y-m-d H:i:s'),
];
// dump($this->jiemi);
// dump($this->ticket);
// dump($couponData);
// dd($this->queryData);
$coupon = Coupon::create($couponData);
$coupon->profit();
$log->back = 1;
$log->save();
}
if (count($er) > 1) {
dump($er);
}
});
} catch (\Exception $e) {
dd($e->getMessage());
}
}
}
public function getQuery()
{
if (preg_match('/^YSD/', $this->jiemi['redemptionCode'], $matches)) {
$this->query_coupon = (new YsdQuery)->setOutletId($this->jiemi['outletId'])
->setCode($this->jiemi['redemptionCode'])
->start();
$this->type = 'ysd';
} else {
$this->query_coupon = (new Query)->setOutletId($this->jiemi['outletId'])
->setCode($this->jiemi['redemptionCode'])
->start();
$this->type = 'pingan';
}
if ($this->type == 'ysd') {
return $this->getYsdPro();
} else {
return $this->getPro();
}
}
public function getYsdPro()
{
$rule_code = $this->query_coupon->activity->rule->code;
$code = $this->user->parent->code->where('code', $rule_code)->first();
if (!$code) {
$this->queryData = "核销失败,您没有权限使用此卡券优惠活动。";
}
$ticket = explode('-', $rule_code);
if (!is_array($ticket) || count($ticket) != 3) {
$this->queryData = "核销失败,卡券规则格式不正确";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
// preg_match('/(\d{3}(\.\d+)?)/is', $full, $match);
preg_match('/\d+/', $full, $match);
if (!is_array($match)) {
$this->queryData = "核销失败,卡券规则格式不正确。";
}
if (!is_numeric($this->jiemi['total'])) {
$this->queryData = "核销失败,订单金额必须是数字";
}
if ($match[0] > $this->jiemi['total']) {
$this->queryData = '核销失败,订单金额不足。';
}
$this->queryData = [
'couponName' => $this->query_coupon->activity->title,
'thirdPartyGoodsId' => $rule_code,
'productId' => '',
'PaOutletId' => '',
'startTime' => $this->query_coupon->start_at->format('Y-m-d H:i:s'),
'endTime' => $this->query_coupon->end_at->format('Y-m-d H:i:s'),
];
return $this->ticket = [
'total' => $match[0],
'price' => $price,
'profit' => $code->profit,
];
}
public function getPro()
{
if (is_string($this->query_coupon)) {
return $this->query_coupon;
}
$profitOfferItemVersion = $this->query_coupon['profitOfferItemVersion'];
$productItemList = $this->query_coupon['productItemList'];
if (!is_array($productItemList) || !is_array($productItemList[0])) {
$this->queryData = '核销失败,平安券数据有误,可能是未配置网点。';
}
//循环查找
$first = '';
foreach ($productItemList as $key => $item) {
$productId = $item['productId'];
$thirdPartyGoodsId = $item['thirdPartyGoodsId'];
$outletList = $item['outletList'];
if (!is_array($outletList) || !is_array($outletList[0])) {
$this->queryData = '核销失败,网点信息有误!请检查平安券配置信息。';
break;
}
$outletList = collect($outletList);
//判断是新版还是旧版
if ($profitOfferItemVersion) {
//新版通过第三方查询
$first = $outletList->firstWhere('thirdOutletNo', $this->user->outlet_id);
if ($first) {
break;
}
} else {
//旧版通过平安网点查询
$first = $outletList->firstWhere('outletNo', $this->user->PaOutletId);
if ($first) {
break;
}
}
}
if (!$first) {
$this->queryData = '核销失败,未找到可用网点信息。';
}
if (!$thirdPartyGoodsId) {
$this->queryData = '核销失败,平安券编号规则有误。';
}
if (!$productId) {
$this->queryData = '核销失败未查询到平安券商品id。';
}
return $this->queryData = [
'couponName' => $this->query_coupon['couponName'],
'thirdPartyGoodsId' => $thirdPartyGoodsId,
'productId' => $productId,
'PaOutletId' => $first['outletNo'],
'startTime' => $this->query_coupon['startTime'],
'endTime' => $this->query_coupon['endTime'],
];
}
public function checkCode()
{
if ($this->type == 'ysd') {
return $this->ticket;
}
$code = $this->user->parent->code->where('code', $this->queryData['thirdPartyGoodsId'])->first();
if (!$code) {
$this->ticket = "核销失败,未找到此项平安券规则,请联系管理人员检查渠道配置。";
}
$ticket = explode('-', $this->queryData['thirdPartyGoodsId']);
if (!is_array($ticket) || count($ticket) != 3) {
$this->ticket = "核销失败,平安券规则格式不正确。";
}
$full = $ticket[1]; //full100
$price = $ticket[2];
preg_match('/\d+/', $full, $result);
if (empty($result) || !is_array($result)) {
$this->ticket = "核销失败,平安券规则格式不正确。";
}
if (!is_numeric($this->jiemi['total'])) {
$this->ticket = "核销失败,订单金额必须是数字";
}
if ($result[0] > $this->jiemi['total']) {
$this->ticket = '核销失败,订单金额不足,平安券不可使用。';
}
$this->ticket = [
'total' => $result[0],
'price' => $price,
'profit' => $code->profit,
];
return $this->ticket;
}
}