Files
2026-03-14 16:20:49 +08:00

261 lines
7.6 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\api\logic;
use app\common\logic\BaseLogic;
use app\common\model\order\OrderStore;
use app\common\model\order\OrderTeamaster;
use app\common\model\store\StoreUserAccountLog;
use app\common\model\teastore\TeaStore;
use app\common\model\teastore\TeaStoreRoom;
use app\common\model\teamaster\TeamasterUser;
use app\common\service\iot\IotService;
use app\common\service\iot\DoorService;
use app\common\model\user\User;
use app\common\model\user\UserMember;
use app\common\service\pay\WxRefundService;
use app\common\service\VoiceNotifyService;
use think\facade\Db;
class TaskLogic extends BaseLogic
{
public static function run()
{
self::order();
self::openlock();
self::openLamp();
self::closeLamp();
self::speaker();
self::openlock();
self::orderCancel();
}
/**
* @return void
* 订单相关操作
*/
public static function order()
{
//预约订单取消
$fiveMinutesAgo = date('Y-m-d H:i:s', time() - 60*5);
OrderStore::where('order_status', 0)
->where('dtime', '<', $fiveMinutesAgo)
->update(['order_status' => 4]);
//待使用->已使用
OrderStore::where('order_status', 1)
->where('start_time', '<', time())
->update([
'order_status' => 2
]);
//已使用->已完成
// 获取所有需要处理的订单ID
$orderArr = OrderStore::where("order_status = 2")
->where('end_time', '<', time())
->field('id,order_sn')->select()->toArray();
$orderIds =[];
foreach ($orderArr as &$ids){
$orderIds[] = $ids['id'];
}
// 批量更新订单状态
if (!empty($orderIds)) {
OrderStore::whereIn('id', $orderIds)
->update(['order_status' => 3]);
}
}
// 空开-打开
public static function openLamp(){
$orderList = OrderStore::where('order_status','1')->select();
foreach ($orderList as $v){
if (time() + 60 * 20 > $v['start_time']) {
$roomInfo = TeaStoreRoom::where(['id' => $v['room_id']])->find();
if ($roomInfo['is_open'] == 0) {
$ty = new IotService();
if ($roomInfo['device_id']) {
$ty->controlDevice($roomInfo['device_id'],1);// 灯光开关
TeaStoreRoom::where(['id' => $v['room_id']])->update(['is_open' => 1,'status'=>5]);
}
}
}
}
}
// 空开-关闭
public static function closeLamp()
{
$orderList = OrderStore::where(['order_status'=>'3',"is_release"=>0])->select();
foreach ($orderList as $v) {
$end_time = $v['end_time'];
if ((time() >= $end_time + 60 *2) && (time() < $end_time + 60 *3)) {
$roomInfo = TeaStoreRoom::where(['id' => $v['room_id']])->find();
if ($roomInfo['is_open'] == 1) {
$ty = new IotService();
if ($roomInfo['device_id']) {
$ty->controlDevice($roomInfo['device_id'],0);// 灯光开关
TeaStoreRoom::where(['id' => $v['room_id']])->update(['is_open' => 0,'status'=>1]);
}
}
}
}
}
//音响提醒
public static function speaker()
{
$room = TeaStoreRoom::select();
foreach ($room as $v) {
$orderInfo = OrderStore::where([
'room_id' => $v['id'],
'order_status'=>2,
])->order('id desc')->find();
if ($v['speaker_id']&&$orderInfo) {
// 结束前10分钟
if ($orderInfo['order_status'] == 2 && ($orderInfo['end_time'] - 60 * 10 < time())&& ($orderInfo['end_time'] - 60 * 9 > time())) {
$notifyService = new \app\common\service\iot\SpeakerService();
$re = $notifyService->sendSpeakerNotify($v['speaker_id']);
}
}
}
}
public static function openlock(){
$order = OrderStore::where(['order_status'=>1,'is_lockpwd'=>0])->find();
if(!$order){
return [];
}
$store_id = $order->store_id;
$room_id = $order->room_id;
$store = TeaStore::where('id',$store_id)->find();
$room = TeaStoreRoom::where('id',$room_id)->find();
if(!$order){
return [];
}
$istime = $order['start_time'] - 30 * 60;
$startTime = date('ymdHi', $istime);
$isend = strtotime("+1 day", $order['end_time']);
$endTime = date('ymdHi', $isend);
$service = new DoorService();
$keyboardPwdName = 'tt'.$order['id'];
$get_key = '';
$room_key = '';
if($order->is_lockpwd == 0){
if($store->lock_no<=0){
$get_key = '免密';
$order->is_lockpwd = 1;
$order->save();
}else{
$keyboardPwdstore = rand(100000,999999);
$result = $service->createTempPassword($store->lock_no,$keyboardPwdstore,$keyboardPwdName,$istime,$isend);
$get_key = $keyboardPwdstore.'#';
}
if($room->lock_no<=0){
$room_key = '免密';
}else{
$keyboardPwdroom = rand(100000,999999);
$service->createTempPassword($room->lock_no,$keyboardPwdroom,$keyboardPwdName,$istime,$isend);
$room_key= $keyboardPwdroom.'#';
}
}
OrderStore::where('id',$order->id)->update([
'room_key'=>$room_key,
'gate_key'=>$get_key,
'is_lockpwd'=>1,
'update_dtime'=>date('Y-m-d H:i:s')
]);
}
//茶艺师自动放弃订单
public static function orderCancel(){
$order = OrderTeamaster::where([
'order_status'=>28,
'del'=>0
])->find();
if(!$order){
throw new \Exception('订单错误');
}
if($order['pay_time']<time()-60*9){
$WxRefundService = new WxRefundService();
$result =$WxRefundService->Refund($order,10,40);
}
}
public static function steVip(){
$fiveMinutesAgo = date('Y-m-d H:i:s', time() - 60*5);
OrderStore::where('order_status', 0)
->where('dtime', '<', $fiveMinutesAgo)
->update(['order_status' => 4]);
}
/**
* 发送简单语音通知
*/
public static function sendSimple()
{
$order = OrderTeamaster::where([
'order_status'=>28,
'is_send'=>0
])->find();
if($order){
$team_user = TeamasterUser::where('id',$order->team_user_id)->find();
$voiceService = new VoiceNotifyService();
// 发送语音通知
$result = $voiceService->send(
$team_user['mobile'], // 被叫手机号
'405547', // 模板ID从文档示例中获取
[], // 模板参数
[
'displayNumber' => '02161704229', // 主显号码(可选)
'replayTimes' => 1, // 重播次数可选1,2,3
'callRec' => 0, // 是否录音可选0关闭1开启
]
);
$order->is_send = 1;
$order->save();
}
}
}