123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671 |
- <?php
- namespace App\Http\Logic\Cron;
- use App\Exceptions\CommonException;
- use App\Http\Bean\Cron\logic\HandleOrderParamBean;
- use App\Http\Bean\Util\Jutuike\GetOrderListParamBean;
- use App\Http\Bean\Util\Meituan\OrderListParamBean;
- use App\Http\Bean\Util\Pdd\Ddk\OrderListIncrementGetParamBean;
- use App\Http\Enum\ErrorEnum;
- use App\Http\Enum\OrderStatusEnum;
- use App\Http\Enum\PlatformTypeEnum;
- use App\Http\Logic\BaseLogic;
- use App\Http\Logic\Order\OrderLogic;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\Jutuike\JutuikeUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Http\Utils\Meituan\MeituanLianmengUtil;
- use App\Http\Utils\Pdd\DuoDuoKeUtil;
- use App\Http\Utils\WechatAccountUtil;
- use App\Models\CategoryModel;
- use App\Models\UserCashModel;
- use App\Models\UserFinanceModel;
- use App\Models\UserModel;
- use App\Models\UserOrderCommissionModel;
- use App\Models\UserOrderModel;
- use App\Models\WechatAccountModel;
- use EasyWeChat\Factory;
- use Illuminate\Support\Facades\DB;
- class CronLogic extends BaseLogic
- {
-
- public static function meituanOrderslogic()
- {
- $params = request()->all();
- $arr = [
- "appkey"=>env("MEITUAN_LIANMENG_KEY"),
- "type"=>$params["order_type"],
- "startTime"=>time()-$params["minute"]*60,
- "endTime"=>time(),
- "page"=>1,
- "limit"=>30,
- "queryTimeType"=>2,
- ];
- $instance = new LoggerFactoryUtil(CronLogic::class);
- $instance->info("获取订单数据开始");
- $bean = new OrderListParamBean($arr);
- $res = MeituanLianmengUtil::orderList($bean);
- $instance->info("美团返回数据:".json_encode($res));
- $instance->info("订单数量:".$res["total"]);
- if($res){
-
- if($res["total"]){
- $instance->info("订单数据:".json_encode($res));
- BaseUtil::sendBaoJing("","获取到订单数量:".$res["total"],"定时获取美团订单");
-
- self::checkOrder($res["dataList"],$params["order_type"]);
-
- $pageNum = ceil($res["total"]/100)-1;
- if($pageNum){
- for ($i=1;$i<=$pageNum;$i++){
- $arr["page"]++;
- $bean = new OrderListParamBean($arr);
- $res = MeituanLianmengUtil::orderList($bean);
- if ($res) {
-
- if ($res["total"]) {
- self::checkOrder($res["dataList"],$params["order_type"]);
- }
- }
- }
- }
- }
- }
- }
-
- protected static function checkOrder($orders,$orderType)
- {
- foreach ($orders as $data) {
- switch ($data["status"]) {
- case 1:
- $orderStatus = OrderStatusEnum::ALREADY_PAY;
- break;
- case 8:
- $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
- break;
- case 9:
- $orderStatus = OrderStatusEnum::ALREADY_REFUND;
- break;
- }
- $params = [
- "spreadSonType" => 0,
- "orderNumber" => $data["orderid"],
- "payTime" => date("Y-m-d H:i:s", $data["paytime"]),
- "orderPrice" => $data["payprice"] * 100,
- "orderCommission" => $data["profit"] * 100,
- "orderTitle" => $data["smstitle"],
- "orderRefundPrice" => isset($data["refundprice"]) ? $data["refundprice"] * 100 : 0,
- "orderRefundTime" => isset($data["refundtime"]) ? date("Y-m-d H:i:s", $data["refundtime"]) : null,
- "orderRefundCommission" => isset($data["refundprofit"]) ? $data["refundprofit"] : 0,
- "orderStatus" => $orderStatus,
- "orderCouponPrice" => 0,
- "productImgUrl" => "https://pic.rmb.bdstatic.com/bjh/f049242a789a22fb1a412bb6418c52e2.jpeg",
- "spreadType"=>PlatformTypeEnum::PLATFORM_MEITUAN,
- "userSpreadId"=>$data["sid"],
- "platformSpreadId"=>$data["sid"],
- "createdAt"=>date("Y-m-d H:i:s", $data["paytime"])
- ];
- $bean = new HandleOrderParamBean($params);
- OrderLogic::handleOrderLogic($bean);
- }
- }
-
- public static function jutuikeOrdersLogic()
- {
- $params = request()->all();
- $startTime = date("Y-m-d H:i:s",time()-600);
- $endTime = date("Y-m-d H:i:s");
- if(isset($params["start_time"])){
- $startTime = $params["start_time"];
- }
- if (isset($params["end_time"])){
- $endTime= $params["end_time"];
- }
- $page = 1;
- $params = [
- "start_time"=>$startTime,
- "end_time"=>$endTime,
- "query_type"=>2,
- "page"=>$page,
- "pageSize"=>100
- ];
- $bean = new GetOrderListParamBean($params);
- $data = JutuikeUtil::getOrderList($bean);
- $instance = new LoggerFactoryUtil(CronLogic::class);
- $instance->info("上游返回数据:".json_encode($data));
- if($data["code"]!=1){
-
- return;
- }
-
- $orderLists = $data["data"]["data"];
- if(count($orderLists)){
- BaseUtil::sendBaoJing("","获取到订单数量:".count($orderLists),"定时获取聚推客订单");
- }
- foreach ($orderLists as $orderList){
- self::handleOrderLogic($orderList);
- }
- }
- public static function handleOrderLogic($orderList)
- {
-
- switch ($orderList["status"]){
- case 0:
- $orderStatus = OrderStatusEnum::PRE_PAY;
- break;
- case 1:
- $orderStatus = OrderStatusEnum::ALREADY_PAY;
- break;
- case 2:
- $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
- break;
- case 3:
- $orderStatus = OrderStatusEnum::ALREADY_FINISH;
- break;
- case 4:
- $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
- break;
- }
-
- $actImgUrl = CategoryModel::query()
- ->where("platform_type",PlatformTypeEnum::PLATFORM_JUTUIKE)
- ->where("activity_id",$orderList["act_id"])
- ->value("category_img_url");
- $params = [
- "spreadSonType" => 0,
- "orderNumber" => $orderList["order_sn"],
- "payTime" => $orderList["pay_time"],
- "orderPrice" => $orderList["order_price"] * 100,
- "orderCommission" => $orderList["jtk_share_fee"] * 100,
- "orderTitle" => $orderList["order_title"],
- "orderRefundPrice" => 0,
- "orderRefundTime" => null,
- "orderRefundCommission" => 0,
- "orderStatus" => $orderStatus,
- "orderCouponPrice" => 0,
- "productImgUrl" => $actImgUrl,
- "spreadType"=>PlatformTypeEnum::PLATFORM_JUTUIKE,
- "userSpreadId"=>$orderList["sid"],
- "platformSpreadId"=>$orderList["sid"],
- "createdAt"=>$orderList["create_time"],
- ];
- $bean = new HandleOrderParamBean($params);
- OrderLogic::handleOrderLogic($bean);
- }
-
- public static function pddOrdersLogic()
- {
- $params = [
- "startUpdateTime"=>time()-600,
- "endUpdateTime"=>time()
- ];
- $bean = new OrderListIncrementGetParamBean($params);
- $res = DuoDuoKeUtil::orderListIncrementGet($bean);
- $instance = new LoggerFactoryUtil(CronLogic::class);
- $instance->info("上游返回信息:".json_encode($res));
- if (isset($res["error_response"])){
- return;
- }
- if($res["order_list_get_response"]["total_count"]){
- BaseUtil::sendBaoJing("","获取到订单数量:".$res["order_list_get_response"]["total_count"],"定时获取拼多多订单");
- }
-
- $datas = $res["order_list_get_response"]["order_list"];
- foreach ($datas as $orderList){
- switch ($orderList["order_status"]){
- case 0:
-
- $orderStatus = OrderStatusEnum::ALREADY_PAY;
- break;
- case 1:
-
- $orderStatus = OrderStatusEnum::ALREADY_PAY;
- break;
- case 2:
-
- $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
- break;
- case 3:
-
- $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
- break;
- case 4:
-
- $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
- break;
- case 5:
-
- $orderStatus = OrderStatusEnum::ALREADY_RECEIVE;
- break;
- case 10:
-
- $orderStatus = OrderStatusEnum::ALREADY_CANCEL;
- break;
- }
- $params = [
- "spreadSonType" => 0,
- "orderNumber" => $orderList["order_sn"],
- "payTime" => date("Y-m-d H:i:s",$orderList["order_pay_time"]),
- "orderPrice" => $orderList["order_amount"],
- "orderCommission" => $orderList["promotion_amount"],
- "orderTitle" => $orderList["goods_name"],
- "orderRefundPrice" => 0,
- "orderRefundTime" => null,
- "orderRefundCommission" => 0,
- "orderStatus" => $orderStatus,
- "orderCouponPrice" => 0,
- "productImgUrl" => "https://pic.rmb.bdstatic.com/bjh/6d9fb4a81eb5fc933da8949854c4fab2.jpeg",
- "spreadType"=>PlatformTypeEnum::PLATFORM_PINGDUODUO,
- "userSpreadId"=>$orderList["p_id"],
- "platformSpreadId"=>$orderList["p_id"],
- "createdAt"=>date("Y-m-d H:i:s",$orderList["order_create_time"]),
- ];
- $bean = new HandleOrderParamBean($params);
- OrderLogic::handleOrderLogic($bean);
- }
- }
-
- public static function sendOrderTemplateLogic()
- {
- $instance = new LoggerFactoryUtil(CronLogic::class);
-
- $userFans = UserModel::query()
- ->where("invite_user_id","<>",0)
- ->where("user_notify_status",0)
- ->get();
- foreach ($userFans as $userFan){
-
- $inviteUser = UserModel::findById($userFan->invite_user_id);
- if($inviteUser){
- try{
-
- $app = WechatAccountUtil::getAppByID($userFan->wechat_account_id);
- $data = [
- 'touser' => $inviteUser->user_open_id,
- 'template_id' => env("WECHAT_FANS_TEMPLATE_URL"),
- 'url' => env("WECHAT_FANS_URL"),
- 'data' => [
- 'first' => "亲,您有新的粉丝",
- 'keyword1' => $userFan->user_nickname,
- 'keyword2' => $userFan->id+100000,
- "remark"=>"粉丝将提供分享佣金,粉丝越多比例越高哦,点击查看详情"
- ],
- ];
- $res = $app->template_message->send($data);
- $instance->info("粉丝通知微信返回结果:".json_encode($res));
- }catch (\Throwable $exception){
- $instance->info("粉丝通知结果异常:".$exception->getMessage());
- }
- UserModel::query()->where("id",$userFan->id)->update(
- [
- "user_notify_status"=>1
- ]
- );
- }
- }
-
- $orders = UserOrderCommissionModel::query()
- ->where("commission_status",0)
- ->where("order_commission",">",0)
- ->where("user_id","<>",0)
- ->where("order_notify_status",0)
- ->limit(10)
- ->get();
- foreach ($orders as $order){
-
- if($order->user_provider_id){
-
- $user = UserModel::findById($order->user_id);
- if($user){
- try{
-
- $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
- $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
- $privideUser = UserModel::findById($order->user_provider_id);
- $data = [
- 'touser' => $user->user_open_id,
- 'template_id' => env("WECHAT_FANS_ORDER_TEMPLATE_URL"),
- 'url' => env("WECHAT_FANS_ORDER_URL"),
- 'data' => [
- 'first' => "粉丝下单成功通知",
- 'keyword1' => $order->order_number,
- 'keyword2' => $tmpOrder->product_name,
- 'keyword3' => $order->created_at,
- 'keyword4' => round($tmpOrder->order_price/100,2)."元",
- 'keyword5' => $privideUser->user_nickname,
- "remark"=>"您的粉丝有新的订单,分享佣金已产生,3天后结算到您账户中,点击详情跳转粉丝订单"
- ],
- ];
- $res = $app->template_message->send($data);
- $instance->info("粉丝下单微信返回结果:".json_encode($res));
- }catch (\Throwable $exception){
- $instance->info("粉丝下单通知结果异常:".$exception->getMessage());
- }
- }
- }else{
- $user = UserModel::findById($order->user_id);
- if($user){
- try{
-
- $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
- $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
- $data = [
- 'touser' => $user->user_open_id,
- 'template_id' => env("WECHAT_ORDER_TEMPLATE_URL"),
- 'url' => env("WECHAT_ORDER_URL"),
- 'data' => [
- 'first' => "下单成功通知",
- 'keyword1' => $order->created_at,
- 'keyword2' => $tmpOrder->product_name,
- 'keyword3' => $order->order_number,
- "remark"=>"感谢您的使用,每月20号结算上个月的佣金,将会结算到您的账户中点击详情跳转我的订单"
- ],
- ];
- $res = $app->template_message->send($data);
- $instance->info("自购下单微信返回结果:".json_encode($res));
- }catch (\Throwable $exception){
- $instance->info("自购下单通知结果异常:".$exception->getMessage());
- }
- }
- }
- UserOrderCommissionModel::query()->where("id",$order->id)->update(
- [
- "order_notify_status"=>1
- ]
- );
- }
-
- $completeOrders = UserOrderCommissionModel::query()
- ->where("commission_status",1)
- ->where("order_commission",">",0)
- ->where("user_id","<>",0)
- ->where("order_complete_notify_status",0)
- ->limit(10)
- ->get();
- foreach ($completeOrders as $order){
- $user = UserModel::findById($order->user_id);
- if($user){
- try{
-
- $app = WechatAccountUtil::getAppByID($user->wechat_account_id);
- $tmpOrder = UserOrderModel::query()->where("order_number",$order->order_number)->first();
- $data = [
- 'touser' => $user->user_open_id,
- 'template_id' => env("WECHAT_COMPLETE_ORDER_TEMPLATE_URL"),
- 'url' => env("WECHAT_BALANCE_URL"),
- 'data' => [
- 'first' => "订单:[".$tmpOrder["order_number"]."]佣金已结算完成",
- 'keyword1' => round($order->order_commission/100,2)."元",
- 'keyword2' => round($tmpOrder->order_price/100,2)."元",
- 'keyword3' => $order->updated_at,
- "remark"=>"订单佣金已结算完成,感谢您的使用,点击详情跳转我的账户"
- ],
- ];
- $res = $app->template_message->send($data);
- $instance->info("订单结算微信返回结果:".json_encode($res));
- }catch (\Throwable $exception){
- $instance->info("订单结算通知结果异常:".$exception->getMessage());
- }
- }
- UserOrderCommissionModel::query()->where("id",$order->id)->update(
- [
- "order_complete_notify_status"=>1
- ]
- );
- }
- }
-
- public static function settlementOrderCommissionLogic()
- {
-
- $startTime = date("Y-m-d",strtotime("-1 month"))." 00:00:00";
- $endTime = date("Y-m-")."01 00:00:00";
-
- UserOrderCommissionModel::query()
- ->where("commission_status",0)
- ->where("order_commission",">",0)
- ->where("user_id","<>",0)
- ->where("order_complete_notify_status",0)
- ->where("created_at","<",$endTime)
- ->chunk(100,function ($orderCommissions){
- foreach ($orderCommissions as $orderCommission){
- try{
-
- DB::beginTransaction();
-
- $user = UserModel::query()->lock(true)->find($orderCommission["user_id"]);
- if($user){
- $userRes = UserModel::query()
- ->where("id",$orderCommission["user_id"])
- ->increment("user_balance",$orderCommission["order_commission"]);
- if(!$userRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_SYSTEM);
- }
-
- $logRes = UserFinanceModel::query()
- ->insert(
- [
- "user_id"=>$orderCommission["id"],
- "order_number"=>$orderCommission["order_number"],
- "user_before_balance"=>$user["user_balance"],
- "user_after_balance"=>$orderCommission["order_commission"]+$user["user_balance"],
- "finance_balance"=>$orderCommission["order_commission"],
- "finance_remark"=>"订单佣金结算",
- "finance_type"=>1,
- "finance_number"=>uniqid("fbt-"),
- "created_at"=>date("Y-m-d H:i:s"),
- "updated_at"=>date("Y-m-d H:i:s")
- ]
- );
- if(!$logRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_SYSTEM);
- }
-
- $commissionRes = UserOrderCommissionModel::query()
- ->where("id",$orderCommission["id"])
- ->update(
- [
- "commission_status"=>1,
- "updated_at"=>date("Y-m-d H:i:s")
- ]
- );
- if(!$commissionRes){
- DB::rollBack();
- throw new CommonException(ErrorEnum::ERROR_SYSTEM);
- }
- DB::commit();
- }
- }catch (\Throwable $exception){}
- }
- });
- }
-
- public static function transferFinanceLogic()
- {
- $account = WechatAccountModel::query()
- ->where("wechat_app_code","fanbuting")
- ->first();
- $config = [
- "app_id"=>$account["wechat_app_id"],
- "mch_id"=>$account["account_mch_id"],
- "key"=>$account["account_key"],
- "cert_path"=>$account["account_cert_path"],
- "key_path"=>$account["account_key_path"]
- ];
- $app = Factory::payment($config);
-
- $cashLogs = UserCashModel::query()
- ->where("check_status",2)
- ->where("cash_status",0)
- ->get();
- if(!$cashLogs){
- return ;
- }
-
- $logIds = [];
- foreach ($cashLogs as $cashLog){
- $logIds[] = $cashLog["id"];
- }
- UserCashModel::query()
- ->whereIn("id",$logIds)
- ->update(
- [
- "cash_status"=>1
- ]
- );
- foreach ($cashLogs as $cashLog){
-
- $user = UserModel::query()->find($cashLog["user_id"]);
- if($user){
- $res = $app->transfer->toBalance(
- [
- "partner_trade_no"=>$cashLog["cash_order_number"],
- "openid"=>$user["user_open_id"],
- "check_name"=>"NO_CHECK",
- "re_user_name"=>"",
- "amount"=>$cashLog["cash_money"],
- "desc"=>"用户提现"
- ]
- );
-
- if($res["return_code"]=="SUCCESS" && $res["result_code"]=="SUCCESS"){
- UserCashModel::query()
- ->where("id",$cashLog["id"])
- ->update(
- [
- "cash_status"=>2,
- "upstream_response"=>json_encode($res),
- "cash_receive_at"=>date("Y-m-d H:i:s")
- ]
- );
- $cash_status = 1;
- }else{
-
- UserCashModel::query()
- ->where("id",$cashLog["id"])
- ->update(
- [
- "cash_status"=>3,
- "upstream_response"=>json_encode($res)
- ]
- );
- $cash_status = 2;
- }
-
- UserFinanceModel::query()->where("id",$cashLog["user_finance_id"])
- ->update(
- [
- "cash_status"=>$cash_status,
- "updated_at"=>date("Y-m-d H:i:s")
- ]
- );
- }
- }
- }
-
- public static function callbackCashLogic()
- {
-
- $cashLogs = UserCashModel::query()
- ->where("callback_status",0)
- ->where(function ($query){
- $query->where("check_status",3)
- ->orWhere("cash_status",3);
- })
- ->get();
- if(!$cashLogs){
- return;
- }
- foreach ($cashLogs as $cashLog){
- DB::beginTransaction();
-
- $user = UserModel::query()->lock(true)->find($cashLog["user_id"]);
-
- $userRes = UserModel::query()
- ->where("id",$user["user_id"])
- ->increment("user_balance",$cashLog["cash_money"]);
- if(!$userRes){
- DB::rollBack();
- break;
- }
-
- $financeRes = UserFinanceModel::query()->insert(
- [
- "user_id"=>$cashLog["user_id"],
- "user_before_balance"=>$user["user_balance"],
- "user_after_balance"=>$user["user_balance"]+$cashLog["cash_money"],
- "finance_balance"=>$cashLog["cash_money"],
- "finance_remark"=>"提现失败退回,原提现记录ID为:".$cashLog["id"],
- "finance_type"=>1,
- "finance_number"=>BaseUtil::getOrderNumber(),
- "created_at"=>date("Y-m-d H:i:s"),
- "updated_at"=>date("Y-m-d H:i:s"),
- ]
- );
- if(!$financeRes){
- DB::rollBack();
- break;
- }
-
- $cashRes = UserCashModel::query()
- ->where("id",$cashLog["id"])
- ->update([
- "callback_status"=>1
- ]);
- if(!$cashRes){
- DB::rollBack();
- break;
- }
- DB::commit();
- }
- }
- }
|