shopxo/app/service/CrontabService.php

249 lines
8.7 KiB
PHP
Raw Normal View History

2019-08-18 18:46:49 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2019-08-18 18:46:49 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2019-08-18 18:46:49 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
2019-08-18 18:46:49 +08:00
use app\service\OrderService;
use app\service\BuyService;
use app\service\MessageService;
2020-06-28 22:50:10 +08:00
use app\service\IntegralService;
2019-08-18 18:46:49 +08:00
/**
* 定时任务服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2019-08-18T17:19:33+0800
*/
class CrontabService
{
/**
* 订单自动关闭
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-08-18T17:22:29+0800
* @desc 从订单创建时间开始计算周期
* @param [array] $params [输入参数]
*/
public static function OrderClose($params = [])
{
// 获取可关闭订单
2019-08-20 13:16:42 +08:00
$time = time()-(intval(MyC('common_order_close_limit_time', 30, true))*60);
2019-08-18 18:46:49 +08:00
$where = [
['add_time', '<', $time],
['status', '=', 1],
];
2021-07-18 23:42:10 +08:00
$order = Db::name('Order')->where($where)->field('id,status,user_id')->select()->toArray();
2019-08-18 18:46:49 +08:00
// 状态
$sucs = 0;
$fail = 0;
if(!empty($order))
{
// 订单更新数据
$upd_data = [
'status' => 6,
'close_time' => time(),
'upd_time' => time(),
];
foreach($order as $v)
{
// 开启事务
Db::startTrans();
2019-08-18 19:46:32 +08:00
if(Db::name('Order')->where(['id'=>$v['id'], 'status'=>1])->update($upd_data))
2019-08-18 18:46:49 +08:00
{
// 库存回滚
$ret = BuyService::OrderInventoryRollback(['order_id'=>$v['id'], 'order_data'=>$upd_data]);
if($ret['code'] == 0)
{
// 用户消息
2020-07-31 18:55:47 +08:00
MessageService::MessageAdd($v['user_id'], '订单关闭', '订单超时关闭', '订单', $v['id']);
2019-08-18 18:46:49 +08:00
// 订单状态日志
OrderService::OrderHistoryAdd($v['id'], $upd_data['status'], $v['status'], '超时关闭', 0, '系统');
// 提交事务
Db::commit();
$sucs++;
continue;
}
}
// 事务回滚
Db::rollback();
$fail++;
}
}
return DataReturn(MyLang('common.operate_success'), 0, ['sucs'=>$sucs, 'fail'=>$fail]);
2019-08-18 18:46:49 +08:00
}
/**
* 订单自动收货
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2019-08-18T17:24:05+0800
* @desc 从发货时间开始计算周期
* @param [array] $params [输入参数]
*/
public static function OrderSuccess($params = [])
{
// 获取可收货订单
2019-08-20 13:16:42 +08:00
$time = time()-(intval(MyC('common_order_success_limit_time', 21600, true))*60);
2019-08-18 18:46:49 +08:00
$where = [
['delivery_time', '<', $time],
['status', '=', 3],
];
2021-07-18 23:42:10 +08:00
$order = Db::name('Order')->where($where)->field('id,status,user_id')->select()->toArray();
2019-08-18 18:46:49 +08:00
// 状态
$sucs = 0;
$fail = 0;
if(!empty($order))
{
// 更新订单状态
$upd_data = [
'status' => 4,
'collect_time' => time(),
'upd_time' => time(),
];
foreach($order as $v)
{
// 开启事务
Db::startTrans();
2019-08-18 19:46:32 +08:00
if(Db::name('Order')->where(['id'=>$v['id'], 'status'=>3])->update($upd_data))
2019-08-18 18:46:49 +08:00
{
2020-06-28 22:50:10 +08:00
// 订单商品积分赠送
$ret = IntegralService::OrderGoodsIntegralGiving(['order_id'=>$v['id']]);
2019-08-18 18:46:49 +08:00
if($ret['code'] == 0)
{
2020-06-28 22:50:10 +08:00
// 订单商品销量增加
2021-11-30 14:57:22 +08:00
$ret = OrderService::GoodsSalesCountInc(['order_id'=>$v['id'], 'opt_type'=>'collect']);
2020-06-28 22:50:10 +08:00
if($ret['code'] == 0)
{
// 用户消息
2020-07-31 18:55:47 +08:00
MessageService::MessageAdd($v['user_id'], '订单收货', '订单自动收货成功', '订单', $v['id']);
2019-08-18 18:46:49 +08:00
2020-06-28 22:50:10 +08:00
// 订单状态日志
OrderService::OrderHistoryAdd($v['id'], $upd_data['status'], $v['status'], '自动收货', 0, '系统');
2019-08-18 18:46:49 +08:00
2020-06-28 22:50:10 +08:00
// 提交事务
Db::commit();
$sucs++;
continue;
}
2019-08-18 18:46:49 +08:00
}
}
// 事务回滚
Db::rollback();
$fail++;
}
}
return DataReturn(MyLang('common.operate_success'), 0, ['sucs'=>$sucs, 'fail'=>$fail]);
2019-08-18 18:46:49 +08:00
}
2020-07-28 23:28:06 +08:00
/**
* 支付日志订单关闭
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-28
* @desc description
* @param [array] $params [输入参数]
*/
public static function PayLogOrderClose($params = [])
{
// 时长
$time = time()-(intval(MyC('common_pay_log_order_close_limit_time', 30, true))*60);
// 更新关闭
$where = [
['add_time', '<', $time],
['status', '=', 0],
];
$data = [
'status' => 2,
'close_time' => time(),
];
$res = Db::name('PayLog')->where($where)->update($data);
return DataReturn(MyLang('common.operate_success'), 0, $res);
2020-07-28 23:28:06 +08:00
}
2022-03-07 12:30:10 +08:00
/**
* 商品积分赠送
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-28
* @desc description
* @param [array] $params [输入参数]
*/
public static function GoodsGiveIntegral($params = [])
{
// 获取可赠送的日志数据
$time = time()-(intval(MyC('common_goods_give_integral_limit_time', 21600, true))*60);
$where = [
['add_time', '<', $time],
['status', '=', 0],
];
$data = Db::name('GoodsGiveIntegralLog')->where($where)->field('id,status,user_id,integral')->select()->toArray();
// 状态
$sucs = 0;
$fail = 0;
if(!empty($data))
{
// 更新状态
$upd_data = [
'status' => 1,
'upd_time' => time(),
];
foreach($data as $v)
{
// 开启事务
Db::startTrans();
if(Db::name('GoodsGiveIntegralLog')->where(['id'=>$v['id'], 'status'=>0])->update($upd_data))
{
2022-03-27 23:32:17 +08:00
// 用户是否存在
$count = (int) Db::name('User')->where(['id'=>$v['user_id']])->count();
if($count > 0)
2022-03-07 12:30:10 +08:00
{
2022-03-27 23:32:17 +08:00
// 扣减用户锁定积分
if(!Db::name('User')->where(['id'=>$v['user_id']])->dec('locking_integral', $v['integral'])->update())
{
return DataReturn('用户锁定积分扣减失败['.$v['id'].'-'.$v['user_id'].']', -2);
}
2022-03-07 12:30:10 +08:00
2022-03-27 23:32:17 +08:00
// 增加用户有效积分
$user_integral = Db::name('User')->where(['id'=>$v['user_id']])->value('integral');
if(!Db::name('User')->where(['id'=>$v['user_id']])->inc('integral', $v['integral'])->update())
{
return DataReturn('用户有效积分增加失败['.$v['id'].'-'.$v['user_id'].']', -3);
}
2022-03-07 12:30:10 +08:00
2022-03-27 23:32:17 +08:00
// 积分日志
IntegralService::UserIntegralLogAdd($v['user_id'], $user_integral, $v['integral'], '订单商品赠送', 1);
}
2022-03-07 12:30:10 +08:00
// 提交事务
Db::commit();
$sucs++;
continue;
}
// 事务回滚
Db::rollback();
$fail++;
}
}
return DataReturn(MyLang('common.operate_success'), 0, ['sucs'=>$sucs, 'fail'=>$fail]);
2022-03-07 12:30:10 +08:00
}
2019-08-18 18:46:49 +08:00
}
?>