102 lines
3.7 KiB
PHP
102 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace App\Jobs;
|
||
|
||
use App\Facades\CommonUtilsService;
|
||
use App\Http\Response\ResponseJson;
|
||
use App\Models\Live\Viewer;
|
||
use App\Models\Server\MerchantUser;
|
||
use App\Models\Server\TouristOrder;
|
||
use App\Models\User;
|
||
use App\Models\Wechat;
|
||
use Illuminate\Bus\Queueable;
|
||
use Illuminate\Queue\SerializesModels;
|
||
use Illuminate\Queue\InteractsWithQueue;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Bus\Dispatchable;
|
||
|
||
class SyncOrderRemark implements ShouldQueue
|
||
{
|
||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||
use ResponseJson;
|
||
|
||
protected $activity_id, $user;
|
||
|
||
/**
|
||
* Create a new job instance.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function __construct($activity_id, $user)
|
||
{
|
||
$this->activity_id = $activity_id;
|
||
$this->user = $user;
|
||
}
|
||
|
||
/**
|
||
* Execute the job.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function handle()
|
||
{
|
||
try {
|
||
$user = User::find($this->user->id);
|
||
$activity_id = $this->activity_id;
|
||
$merchant_user = $this->matchSaasUser($user);
|
||
if (empty($merchant_user)) throw new \Exception("用户认证同步订单编号失败,未查询到saas用户,用户id:".$user->id);
|
||
$tourist_order = TouristOrder::where('type', 'community')->where('type_id', $activity_id)->where('pay_status', '!=', 0)->where('account_id', $merchant_user->id)->first();
|
||
if (empty($tourist_order)) throw new \Exception("用户认证同步订单编号失败,未查询到saas用户订单信息,用户id:".$user->id);
|
||
if (empty($tourist_order->remark)) {
|
||
$sex = CommonUtilsService::getSexByCard($user->card_num);
|
||
if (empty($sex)) throw new \Exception("用户认证同步订单编号失败,未查到性别信息, 用户id:".$user->id.', 身份证号card_num:'.$user->card_num);
|
||
$remark = $this->numMember($tourist_order, $sex);
|
||
TouristOrder::where('id', $tourist_order->id)->update(['remark'=>$remark]);
|
||
}
|
||
}catch (\Exception $e) {
|
||
$this->getError($e);
|
||
}
|
||
|
||
}
|
||
public function numMember($tourist_order, $sex){
|
||
|
||
|
||
if($sex && $sex == 1){
|
||
//当前男嘉宾数量
|
||
$male_count = TouristOrder::where('type','community')->where('type_id',$tourist_order->type_id)->where('remark','like',"%男%")->count();
|
||
//当前男嘉宾数量加1
|
||
$male_count = $male_count + 1;
|
||
$remark = $male_count.'男';
|
||
}elseif($sex && $sex == 2){
|
||
//当前女嘉宾数量
|
||
$female_count = TouristOrder::where('type','community')->where('type_id',$tourist_order->type_id)->where('remark','like',"%女%")->count();
|
||
//当前女嘉宾数量加1
|
||
$female_count = $female_count + 1;
|
||
$remark = $female_count.'女';
|
||
}else{
|
||
$remark = null;
|
||
}
|
||
return $remark;
|
||
}
|
||
//根据mp用户查到 saas用户
|
||
public function matchSaasUser($user){
|
||
$m_user = MerchantUser::where('user_id',$user->id)->first();
|
||
if($m_user) return $m_user;
|
||
if ($user->wechat) {
|
||
if ($user->wechat->unionid) {
|
||
$m_user = MerchantUser::where("unionid", $user->wechat->unionid)->first();
|
||
if ($m_user) return $m_user;
|
||
}
|
||
if ($user->wechat->official_openid) {
|
||
$m_user = MerchantUser::where("open_id", $user->wechat->official_openid)->first();
|
||
if ($m_user) return $m_user;
|
||
}
|
||
}
|
||
if ($user->viewer) {
|
||
$m_user = MerchantUser::where("open_id", $user->viewer->official)->first();
|
||
if ($m_user) return $m_user;
|
||
}
|
||
return null;
|
||
}
|
||
}
|