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

138 lines
4.6 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class SanFangCodeService
{
//查询三方code
public function GetSanFangCode($code, $person_id, $qudao_type)
{
$person = DB::table('web_user_person')->where(['id' => $person_id, 'is_del' => 0])->first();
if (!$person) return ['status' => false, 'msg' => '体检人不存在'];
$return_data = [
'code_num' => '',
'price' => 0,
'combo_id' => 0,
'status' => 0,
];
if ($qudao_type == 1) {
$code = DB::table('transfer_code')->where(['sanfang_qudao' => 1, 'code' => $code, 'status' => 1, 'is_del' => 0])->first();
if (!$code) return ['status' => false, 'msg' => '此转赠码不可用'];
$return_data['code_num'] = $code->code;
$return_data['price'] = $code->price;
$return_data['combo_id'] = $code->combo_id;
$return_data['status'] = $code->status;
return ['status' => true, 'data' => $return_data];
}
if ($qudao_type == 3) {
$service = new ZhongKangService();
$res = $service->GetHexiaoCode($code, $person->name, $person->id_number);
if ($res['status'] == true && $res['data']['status'] == 1) {
$return_data['code_num'] = $res['data']['code_num'];
$return_data['price'] = $res['data']['price'] / 100;
$return_data['combo_id'] = $res['data']['combo_id'];
$return_data['status'] = $res['data']['status'];
//查询本地库是否有记录,进行相应的更新
$db_zk_code = DB::table('transfer_code')->where(['sanfang_qudao' => 3, 'code' => $code, 'is_del' => 0])->first();
if (!!$db_zk_code && $db_zk_code->status <> $res['data']['status']) {
return ['status' => false, 'msg' => '此转赠码状态不正确,暂不可用(中康)'];
}
if (!$db_zk_code) {
$data = [
'code' => $code,
'price' => $res['data']['price'] / 100,
'combo_id' => $res['data']['combo_id'],
'sanfang_qudao' => 3,
'status' => 1,
];
$u = DB::table('transfer_code')->insert($data);
}
return ['status' => true, 'data' => $return_data];
} else {
return ['status' => false, 'msg' => '此转赠码不可用(中康)'];
}
}
}
//核销三方code
public function HeXiaoSanFangCode($qudao_type, $code, $orderid)
{
$orderinfo = DB::table('orders')->where(['id' => $orderid])->first();
$sanfang_status = false;
$u = false;
if ($qudao_type == 1) {//H5转赠码
$sanfang_status=true;
}
if ($qudao_type == 2) { //有赞
}
if ($qudao_type == 3) { //中康
$service = new ZhongKangService();
$res = $service->HandleTransferCode(1, $code, $orderinfo->name, $orderinfo->id_number);
if ($res['status'] == true) {
$sanfang_status=true;
} else {
return ['status' => false,'msg' => $res['msg']];
}
}
if($sanfang_status) {
$data = [
'status' => 2,
'pay_order_id' => $orderid,
];
$u = DB::table('transfer_code')->where(['sanfang_qudao' => $qudao_type, 'code' => $code, 'status' => 1, 'is_del' => 0])->update($data);
$data2 = [
'qudao_type' => $qudao_type,
'code' => $code,
];
DB::table('orders')->where(['id' => $orderid])->update(['sanfang_code' => json_encode($data2, JSON_UNESCAPED_UNICODE)]);
if ($u) {
return ['status' => true, 'msg' => '操作成功'];
} else {
return ['status' => false, 'msg' => '核销失败'];
}
}else{
return ['status' => false,'msg' => '三方code核销失败'];
}
}
//撤销核销
public function CancelSanFangCode($qudao_type, $code, $orderid)
{
$orderinfo = DB::table('orders')->where(['id' => $orderid])->first();
$sanfang_status = false;
$u = false;
if ($qudao_type == 1) {//H5转赠码
$sanfang_status=true;
}
if ($qudao_type == 3) { //中康
$service = new ZhongKangService();
$res = $service->HandleTransferCode(2, $code, $orderinfo->name, $orderinfo->id_number);
if ($res['status'] == true) {
$sanfang_status=true;
} else {
return ['status' => false,'msg' => $res['msg']];
}
}
if($sanfang_status) {
$data = [
'status' => 1,
'pay_order_id' => null,
];
$u = DB::table('transfer_code')->where(['code' => $code, 'status' => 2, 'is_del' => 0])->update($data);
if ($u) {
return ['status' => true, 'msg' => '操作成功'];
} else {
return ['status' => false, 'msg' => '撤销失败'];
}
}else{
return ['status' => false,'msg' => '三方code撤销失败'];
}
}
}