You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

353 lines
18 KiB

<?php
namespace app\service\task;
use app\model\AccountRatioDetail;
use app\model\AccountRatioSetting;
use app\model\Goods;
use app\model\GoodsDetail;
use app\model\OperationLog;
use app\model\Order;
use app\model\OrderGoodsDetail;
use app\model\OrderGoodsSource;
use app\model\OrderUser;
use app\model\Ticket;
use app\service\BaseService;
use app\service\pay\PayService;
use app\service\pay\SeparateAccountService;
use fast\Http;
class OrderService extends BaseService
{
public function __construct()
{
parent::__construct();
}
public function payTimeout(){
$date = date('Y-m-d H:i:s', strtotime("-30 minute"));
$order = Order::where('status', 1)->whereTime('createtime', '<=', $date)->select()->toArray();
// $order = Order::where('batchcode', '1650098940946850')->select()->toArray();
if($order){
foreach($order as $val){
$order_detail = OrderGoodsDetail::where('batchcode', $val['batchcode'])->where('is_deleted', 0)->field("goods_islicode")->select()->toArray();
foreach($order_detail as $value){
$goods_detail_id = Goods::where('goods_islicode', $value['goods_islicode'])->value('goods_detail_id');
if(!$goods_detail_id){
continue;
}
$goods_detail = GoodsDetail::where('id', $goods_detail_id)->where('is_deleted', 0)->find();
if(!$goods_detail){
continue;
}
if($goods_detail->goods_entrust == 1 || $goods_detail->contractual_period == 2){
$goods_detail->stock = 1;
$goods_detail->save();
Goods::where('goods_islicode', $value['goods_islicode'])->update(['goods_status' => 1]);
$username = OrderUser::where('batchcode', $val['batchcode'])->where('islicode', $val['buy_islicode'])->value('name');
$operation_log = [
"type" => "goods",
"log_id" => Goods::where('goods_islicode', $value['goods_islicode'])->value('id'),
"message" => date('Y-m-d H:i:s')." {$username}未付款成功,重新发布该标的!",
];
(new OperationLog())->insert($operation_log);
}
}
Order::where('id', $val['id'])->update(['status' => 5, 'shuttime' => date('Y-m-d H:i:s')]);
}
}
}
public function SourceTimeout(){
$order = Order::where('status', '2')->whereTime('paymenttime', '<=', date('Y-m-d H:i:s', strtotime("-7 day")))->select()->toArray();
foreach($order as $v){
$buy_username = OrderUser::where('id', $v['user_id'])->value('name');
if($v['total_money'] <= 0){
$update = [
'status' => 4,
'receivetime' => date('Y-m-d H:i:s'),
'closetime' => date('Y-m-d H:i:s'),
'completetime' => date('Y-m-d H:i:s'),
];
}else{
$update = [
'status' => 3,
'receivetime' => date('Y-m-d H:i:s')
];
}
if(Order::where('batchcode', $v['batchcode'])->where('status', 2)->update($update)){
$this->orderComplete($v['batchcode']);
$goods_islicode = OrderGoodsDetail::where('batchcode', $v['batchcode'])->where('is_deleted', 0)->field('goods_islicode')->select()->toArray();
foreach($goods_islicode as $val){
$goods_id = Goods::where('goods_islicode', $val['goods_islicode'])->where('is_deleted', 0)->value('id');
if(empty($goods_id)){
continue;
}
$goods_entrust = OrderGoodsDetail::where('batchcode', $v['batchcode'])->where('is_deleted', 0)->where('goods_islicode', $val['goods_islicode'])->value('goods_entrust');
$operation_log = [
"type" => "goods",
"log_id" => $goods_id,
"message" => date('Y-m-d H:i:s')." ". ($goods_entrust == 1 ? "转让" : "授权") ."给用户{$buy_username}",
];
(new OperationLog())->insert($operation_log);
}
}
}
}
public function orderSourceDownload(){
$this->SourceTimeout();
//todo 请求获取资源下载状态 接口3.3
$order = Order::where('status', '2')->select()->toArray();
// $order = Order::where('batchcode', '1651046992561468')->select()->toArray();
$path = env("app.real_url") . "/dist/catalogs/v1/query/status";
$sign = parent::createSign("distribute");
$headers = array(
CURLOPT_HTTPHEADER => array(
"dist_token:{$sign}",
"Content-Type: application/json"
)
);
foreach($order as $value){
$buy_username = OrderUser::where('id', $value['user_id'])->value('name');
// $source = OrderGoodsSource::where('batchcode', $value['batchcode'])->select()->toArray();
$order_detail = OrderGoodsDetail::where('batchcode', $value['batchcode'])->where('is_deleted', 0)->select()->toArray();
$download = 1;
foreach($order_detail as $detail){
$source_url_arr = json_decode($detail['source_download'], true);
for($i=0;$i < count($source_url_arr);$i++){
if($source_url_arr[$i]['status'] < 4){
$download = 0;
continue;
}
if($source_url_arr[$i]['status'] == 7 || $source_url_arr[$i]['status'] == 5){
continue;
}
$url = "/{$value['batchcode']}/{$source_url_arr[$i]['islicode']}/{$value['buy_islicode']}";
// var_dump($path.$url);die;
$result = Http::get($path.$url, [], $headers);
$this->orderlog->info("{$value['batchcode']}获取资源下载状态", $result);
// var_dump($result);die;
if($result['code'] != 200){
// throw new \think\Exception($result['msg'], 400);
$download = 0;
$source_url_arr[$i]['status'] = 6;
continue;
}
$result = json_decode($result['data'], true);
if($result['resultCode'] != "00000000"){
// throw new \think\Exception($result['resultMsg'], 400);
$download = 0;
$source_url_arr[$i]['status'] = 6;
continue;
}
if(!isset($result['data']['sourceStatus'])){
$download = 0;
continue;
}
if($result['data']['sourceStatus'] == 0) {
$source_url_arr[$i]['status'] = 7;
}elseif($result['data']['sourceStatus'] == 2){
$source_url_arr[$i]['status'] = 8;
$download = 0;
}elseif($result['data']['sourceStatus'] == 1){
$download = 0;
}else{
$source_url_arr[$i]['status'] = 9;
$download = 0;
}
}
$source_url_arr = json_encode($source_url_arr);
OrderGoodsDetail::where('id', $detail['id'])->where('is_deleted', 0)->update(['source_download' => $source_url_arr]);
}
if($download == 1){
if($value['total_money'] <= 0){
$update = [
'status' => 4,
'receivetime' => date('Y-m-d H:i:s'),
'closetime' => date('Y-m-d H:i:s'),
'completetime' => date('Y-m-d H:i:s'),
];
}else{
$update = [
'status' => 3,
'receivetime' => date('Y-m-d H:i:s')
];
}
if(Order::where('batchcode', $value['batchcode'])->where('status', 2)->update($update)){
$this->orderComplete($value['batchcode']);
$goods_islicode = OrderGoodsDetail::where('batchcode', $value['batchcode'])->where('is_deleted', 0)->field('goods_islicode')->select()->toArray();
foreach($goods_islicode as $val){
$goods_id = Goods::where('goods_islicode', $val['goods_islicode'])->where('is_deleted', 0)->value('id');
if(empty($goods_id)){
continue;
}
$goods_entrust = OrderGoodsDetail::where('batchcode', $value['batchcode'])->where('is_deleted', 0)->where('goods_islicode', $val['goods_islicode'])->value('goods_entrust');
$operation_log = [
"type" => "goods",
"log_id" => $goods_id,
"message" => date('Y-m-d H:i:s')." ". ($goods_entrust == 1 ? "转让" : "授权") ."给用户{$buy_username}",
];
(new OperationLog())->insert($operation_log);
}
}
}
}
}
public function orderComplete($batchcode){
//todo 请求交易所撤销委托 接口1.4
$url = env("app.entrust_url") . "/consign/exchange/v1/exchangeRevokeEntrust";
$sign = parent::createSign("entrust");
$headers = array(
CURLOPT_HTTPHEADER => array(
"entrust_token:{$sign}"
)
);
$order_goods = OrderGoodsDetail::where('batchcode', $batchcode)->where('is_deleted', 0)->where('goods_entrust', 1)->select()->toArray();
foreach($order_goods as $val){
Goods::where('goods_islicode', $val['goods_islicode'])->where('goods_status', 5)->update(['goods_status' => 3, 'contractualtime_end_time' => date('Y-m-d H:i:s')]);
$post = [
"isliCode" => $val['islicode'],
"status" => 2,
"revokeReason" => "已转让"
];
Http::get($url, $post, $headers);
}
}
public function orderClose(){
$order = Order::where('status', '3')->select()->toArray();
// $order = Order::where('batchcode', '1652688520860660')->select()->toArray();
$ratio_setting = AccountRatioSetting::where('is_deleted', 0)->where('status', 1)->find();
$ratio_detail = AccountRatioDetail::where('setting_id', $ratio_setting->id)->where('role_type', 1)->find();
foreach($order as $item){
$account = [];
$total_money = 0;
$order_detail = OrderGoodsDetail::where('batchcode', $item['batchcode'])->where('is_deleted', 0)->where('is_close', 0)->where('close_status', '<>', 5)->select()->toArray();
foreach($order_detail as $value){
if(in_array($value['entrust_islicode'], $account)){
$user_money = $account[$value['entrust_islicode']]['txnamount'];
if($ratio_detail->calculate == 1){
if($ratio_detail->ratio <= 0){
$money = round($value['goods_price'], 2) * 100;
}else{
$money = round($value['goods_price'] * ($ratio_detail->ratio / 100), 2) * 100;
}
}else{
$money = round(($value['goods_price'] - $ratio_detail->amount), 2) * 100;
}
if($money <= 0){
OrderGoodsDetail::where('id', $value['id'])->update(['close_status' => 5, 'is_close' => 1]);
continue;
}
$total_money += $money;
$user_money = $user_money + $money;
$account[ $value['entrust_islicode'] ]['txnamount'] = $user_money;
}else{
if($ratio_detail->calculate == 1){
if($ratio_detail->ratio <= 0){
$money = round($value['goods_price'], 2) * 100;
}else{
$money = round($value['goods_price'] * ($ratio_detail->ratio / 100), 2) * 100;
}
}else{
$money = round(($value['goods_price'] - $ratio_detail->amount), 2) * 100;
}
if($money <= 0){
OrderGoodsDetail::where('id', $value['id'])->update(['close_status' => 5, 'is_close' => 1]);
continue;
}
$entrust_userinfo = OrderUser::where('id', $value['user_id'])->find();
$total_money += $money;
$order_id = md5(time() . $value['id'] . uniqid());
OrderGoodsDetail::where('batchcode', $value['batchcode'])->where('is_deleted', 0)->where('entrust_islicode', $value['entrust_islicode'])->update(['close_id' => $order_id]);
$result = (new SeparateAccountService())->getBankCode($entrust_userinfo['bankName'], $entrust_userinfo['publicAccount']);
$bankcode = "";
if($result['ret'] == 0){
if(count($result['data']['rows']) > 0){
$bankcode = $result['data']['rows']['0']['bankcode'];
}
}
$account[ $value['entrust_islicode'] ] = [
"ordernumber" => $order_id,
"accountname" => $entrust_userinfo['bankAccountName'],
"bankaccount" => $entrust_userinfo['publicAccount'],
"accounttype" => 0,
"bankcode" => $bankcode,
"bank" => $entrust_userinfo['bankName'],
"txnamount" => $money,
];
}
// $entrust_userinfo = OrderUser::where('id', $value['user_id'])->find();
// if($ratio_detail->calculate == 1){
// $money = (($value['money'] - $value['service_charge']) * ($ratio_detail->ratio / 100)) * 100;
// }else{
// $money = ($value['money'] - $value['service_charge'] - $ratio_detail->amount) * 100;
// }
// if($entrust_userinfo['userType'] == "个人"){
// $accounttype = 1;
// }else{
// $accounttype = 0;
// }
// $total_money = $money;
// $account[] = [
// "ordernumber" => $item['batchcode'],
// "accountname" => $entrust_userinfo['bankAccountName'],
// "bankaccount" => $entrust_userinfo['publicAccount'],
// "accounttype" => $accounttype,
// "bank" => $entrust_userinfo['bankName'],
// "txnamount" => $money,
// ];
}
if((!$order_detail || $total_money <= 0) && !OrderGoodsDetail::where('batchcode', $item['batchcode'])->where('is_deleted', 0)->where('close_status', '<', 5)->value('id')){
Order::where('batchcode', $item['batchcode'])->update(['closetime' => date('Y-m-d H:i:s'), "completetime" => date('Y-m-d H:i:s'), "status" => 4]);
continue;
}
if(!$order_detail || $total_money <= 0){
continue;
}
$batchnumber = md5(uniqid() . time() . rand(1000, 9999));
$account = array_values($account);
$this->orderlog->info("{$item['batchcode']}发起代付", $account);
$result = (new SeparateAccountService())->StartSplitting($account, $total_money, $batchnumber);
$this->orderlog->info("{$item['batchcode']}发起代付结果", $result);
if($result['ret'] == 0){
OrderGoodsDetail::where('batchcode', $item['batchcode'])->where('is_deleted', 0)->update(['close_status' => 2, 'is_close' => 1, 'callback_msg' => $result['message']]);
}else{
OrderGoodsDetail::where('batchcode', $item['batchcode'])->where('is_deleted', 0)->update(['close_status' => 4, 'is_close' => 1, 'callback_msg' => $result['message']]);
}
}
}
public function orderCreateBill(){
$pdf_path = (new PayService())->initDir();
Ticket::where('ticketurl', '=', '')->where('pdfUrl', '<>', '')->field('id,ticketurl,pdfUrl,ticketid')->select()
->each(function ($item) use($pdf_path){
if(empty($item['ticketurl']) && !empty($item['pdfUrl'])){
$status = get_headers($item['pdfUrl']);
$ticketurl = "";
if(strpos($status[0], "200")) {
$pdf_content = file_get_contents($item['pdfUrl']);
file_put_contents('./' . $pdf_path . "{$item['ticketid']}.pdf", $pdf_content);
$ticketurl = $pdf_path . "{$item['ticketid']}.pdf";
}
if($ticketurl){
Ticket::where('id', $item['id'])->update(['ticketurl' => $ticketurl]);
$item['pdfUrl'] = env('app.host') . $ticketurl;
}
}
});
}
}