shopxo/app/service/IntegralService.php

410 lines
16 KiB
PHP
Raw Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace app\service;
2021-07-18 23:42:10 +08:00
use think\facade\Db;
use app\service\MessageService;
use app\service\UserService;
2018-12-28 18:58:37 +08:00
/**
* 积分服务层
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2016-12-01T21:51:08+0800
*/
class IntegralService
{
/**
* [UserIntegralLogAdd 用户积分日志添加]
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @datetime 2018-05-18T16:51:12+0800
* @param [int] $user_id [用户id]
* @param [int] $original_integral [原始积分]
2020-06-28 22:50:10 +08:00
* @param [int] $operation_integral[操作积分]
2018-12-28 18:58:37 +08:00
* @param [string] $msg [操作原因]
* @param [int] $type [操作类型0减少, 1增加)]
* @param [int] $operation_id [操作人员id]
* @return [boolean] [成功true, 失败false]
*/
2020-06-28 22:50:10 +08:00
public static function UserIntegralLogAdd($user_id, $original_integral, $operation_integral, $msg = '', $type = 0, $operation_id = 0)
2018-12-28 18:58:37 +08:00
{
$data = array(
2020-06-28 22:50:10 +08:00
'user_id' => intval($user_id),
'original_integral' => intval($original_integral),
'operation_integral' => intval($operation_integral),
'msg' => $msg,
'type' => intval($type),
'operation_id' => intval($operation_id),
'add_time' => time(),
2018-12-28 18:58:37 +08:00
);
2020-06-28 22:50:10 +08:00
$data['new_integral'] = ($data['type'] == 1) ? $data['original_integral']+$data['operation_integral'] : $data['original_integral']-$data['operation_integral'];
2020-07-31 18:55:47 +08:00
$log_id = Db::name('UserIntegralLog')->insertGetId($data);
if($log_id > 0)
2018-12-28 18:58:37 +08:00
{
2021-08-14 17:17:45 +08:00
$type_msg = MyConst('common_integral_log_type_list')[$type]['name'];
2020-06-28 22:50:10 +08:00
$detail = $msg.'积分'.$type_msg.$operation_integral;
2020-07-31 18:55:47 +08:00
MessageService::MessageAdd($user_id, '积分变动', $detail, '积分', $log_id);
2018-12-28 18:58:37 +08:00
return true;
}
return false;
}
2020-06-27 22:48:16 +08:00
/**
* 列表
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function IntegralLogList($params = [])
{
$where = empty($params['where']) ? [] : $params['where'];
$field = empty($params['field']) ? '*' : $params['field'];
$order_by = empty($params['order_by']) ? 'id desc' : $params['order_by'];
2020-06-27 22:48:16 +08:00
$m = isset($params['m']) ? intval($params['m']) : 0;
$n = isset($params['n']) ? intval($params['n']) : 10;
// 获取数据列表
2021-07-18 23:42:10 +08:00
$data = Db::name('UserIntegralLog')->where($where)->field($field)->limit($m, $n)->order($order_by)->select()->toArray();
return DataReturn(MyLang('common.handle_success'), 0, self::IntegralLogListHandle($data, $params));
2022-08-02 17:23:10 +08:00
}
/**
* 列表数据处理
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-08-01
* @desc description
* @param [array] $data [数据列表]
* @param [array] $params [输入参数]
*/
public static function IntegralLogListHandle($data, $params = [])
{
2020-06-27 22:48:16 +08:00
if(!empty($data))
{
2021-08-14 17:17:45 +08:00
$integral_log_type_list = MyConst('common_integral_log_type_list');
2020-06-27 22:48:16 +08:00
foreach($data as &$v)
{
// 用户信息
if(isset($v['user_id']))
{
if(isset($params['is_public']) && $params['is_public'] == 0)
{
$v['user'] = UserService::GetUserViewInfo($v['user_id']);
}
}
// 操作类型
$v['type_text'] = $integral_log_type_list[$v['type']]['name'];
// 时间
$v['add_time_time'] = date('Y-m-d H:i:s', $v['add_time']);
$v['add_time_date'] = date('Y-m-d', $v['add_time']);
}
}
2022-08-02 17:23:10 +08:00
return $data;
2020-06-27 22:48:16 +08:00
}
/**
* 总数
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $where [条件]
*/
public static function IntegralLogTotal($where = [])
{
return (int) Db::name('UserIntegralLog')->where($where)->count();
}
2018-12-28 18:58:37 +08:00
/**
* 前端积分列表条件
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-09-29
* @desc description
* @param [array] $params [输入参数]
*/
public static function UserIntegralLogListWhere($params = [])
2018-12-28 18:58:37 +08:00
{
2019-01-10 11:32:15 +08:00
// 条件初始化
2018-12-28 18:58:37 +08:00
$where = [];
2019-01-10 11:43:53 +08:00
// 用户id
if(!empty($params['user']))
2018-12-28 18:58:37 +08:00
{
2019-01-10 11:43:53 +08:00
$where[] = ['user_id', '=', $params['user']['id']];
2018-12-28 18:58:37 +08:00
}
if(!empty($params['keywords']))
{
$where[] = ['msg', 'like', '%'.$params['keywords'] . '%'];
}
// 是否更多条件
if(isset($params['is_more']) && $params['is_more'] == 1)
{
if(isset($params['type']) && $params['type'] > -1)
{
$where[] = ['type', '=', intval($params['type'])];
}
// 时间
if(!empty($params['time_start']))
{
$where[] = ['add_time', '>', strtotime($params['time_start'])];
}
if(!empty($params['time_end']))
{
$where[] = ['add_time', '<', strtotime($params['time_end'])];
}
}
return $where;
}
/**
2020-06-28 22:50:10 +08:00
* 订单商品积分赠送
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2018-11-14
* @desc description
* @param [array] $params [输入参数]
*/
public static function OrderGoodsIntegralGiving($params = [])
2018-12-28 18:58:37 +08:00
{
// 请求参数
$p = [
[
'checked_type' => 'empty',
'key_name' => 'order_id',
'error_msg' => '订单id有误',
]
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
// 订单
$order = Db::name('Order')->field('id,user_id,status')->find(intval($params['order_id']));
if(empty($order))
{
2020-07-13 21:33:55 +08:00
return DataReturn('订单不存在或已删除,终止操作', 0);
2018-12-28 18:58:37 +08:00
}
if(!in_array($order['status'], [4]))
{
2020-06-28 22:50:10 +08:00
return DataReturn('当前订单状态不允许操作,未完成', 0);
2018-12-28 18:58:37 +08:00
}
// 获取用户信息
2020-06-28 22:50:10 +08:00
$user = Db::name('User')->field('id')->find($order['user_id']);
2018-12-28 18:58:37 +08:00
if(empty($user))
{
2020-07-13 21:33:55 +08:00
return DataReturn('用户不存在或已删除,终止操作', 0);
2018-12-28 18:58:37 +08:00
}
// 获取订单商品
2022-03-07 12:30:10 +08:00
$order_detail = Db::name('OrderDetail')->where(['order_id'=>$params['order_id']])->field('id,order_id,goods_id,user_id,total_price')->select()->toArray();
2020-06-28 22:50:10 +08:00
if(!empty($order_detail))
2018-12-28 18:58:37 +08:00
{
2020-06-28 22:50:10 +08:00
// 获取赠送积分的商品
2022-03-07 12:30:10 +08:00
$where = [
['id', 'in', array_column($order_detail, 'goods_id')],
['give_integral', '>', 0],
];
$goods_give = Db::name('Goods')->where($where)->column('give_integral', 'id');
if(!empty($goods_give))
2018-12-28 18:58:37 +08:00
{
2022-03-07 12:30:10 +08:00
// 循环发放
foreach($order_detail as $dv)
2018-12-28 18:58:37 +08:00
{
2022-03-07 12:30:10 +08:00
if(array_key_exists($dv['goods_id'], $goods_give))
2018-12-28 18:58:37 +08:00
{
2022-03-07 12:30:10 +08:00
$give_rate = $goods_give[$dv['goods_id']];
if($give_rate > 0 && $give_rate <= 100)
2020-06-28 22:50:10 +08:00
{
2022-03-07 12:30:10 +08:00
// 实际赠送积分
$give_integral = intval(($give_rate/100)*$dv['total_price']);
if($give_integral >= 1)
2020-06-28 22:50:10 +08:00
{
2022-03-07 12:30:10 +08:00
// 是否已存在日志记录
$where = [
['order_id', '=', $dv['order_id']],
['order_detail_id', '=', $dv['id']],
['goods_id', '=', $dv['goods_id']],
];
$temp = Db::name('GoodsGiveIntegralLog')->where($where)->count();
if(empty($temp))
{
// 增加用户锁定积分
if(!Db::name('User')->where(['id'=>$user['id']])->inc('locking_integral', $give_integral)->update())
{
return DataReturn('用户积分赠送失败['.$params['order_id'].'-'.$dv['goods_id'].']', -10);
}
2018-12-28 18:58:37 +08:00
2022-03-07 12:30:10 +08:00
// 积分赠送日志添加
$log_data = [
'order_id' => $dv['order_id'],
'order_detail_id' => $dv['id'],
'goods_id' => $dv['goods_id'],
'user_id' => $dv['user_id'],
'status' => 0,
'rate' => $give_rate,
'integral' => $give_integral,
'add_time' => time(),
];
if(Db::name('GoodsGiveIntegralLog')->insertGetId($log_data) <= 0)
{
return DataReturn('用户积分赠送日志添加失败['.$params['order_id'].'-'.$dv['goods_id'].']', -11);
}
}
}
2020-06-28 22:50:10 +08:00
}
}
2018-12-28 18:58:37 +08:00
}
return DataReturn(MyLang('common.operate_success'), 0);
2018-12-28 18:58:37 +08:00
}
}
return DataReturn('没有需要操作的数据', 0);
}
2020-06-28 22:50:10 +08:00
/**
* 订单商品积分释放
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-06-28
* @desc description
* @param [array] $params [输入参数]
*/
public static function OrderGoodsIntegralRollback($params = [])
{
// 请求参数
$p = [
2020-07-13 21:33:55 +08:00
[
'checked_type' => 'empty',
'key_name' => 'order_id',
'error_msg' => '订单id有误',
],
2020-06-28 22:50:10 +08:00
[
'checked_type' => 'empty',
'key_name' => 'order_detail_id',
'error_msg' => '订单详情id有误',
]
];
$ret = ParamsChecked($params, $p);
if($ret !== true)
{
return DataReturn($ret, -1);
}
2020-07-13 21:33:55 +08:00
// 订单是否存在完成状态(订单赠送积分的条件是完成赠送)
$order_status_history = Db::name('OrderStatusHistory')->where(['order_id'=>intval($params['order_id'])])->column('new_status');
if(empty($order_status_history) || !in_array(4, $order_status_history))
{
return DataReturn('订单状态有误或未存在完成状态,终止操作', 0);
}
2020-06-28 22:50:10 +08:00
// 订单详情
$order_detail = Db::name('OrderDetail')->field('id,user_id,order_id,goods_id,price,total_price,refund_price,returned_quantity')->find(intval($params['order_detail_id']));
2020-06-28 22:50:10 +08:00
if(empty($order_detail))
{
2020-07-13 21:33:55 +08:00
return DataReturn('订单详情不存在或已删除,终止操作', 0);
2020-06-28 22:50:10 +08:00
}
// 获取用户信息
2022-03-07 12:30:10 +08:00
$user = Db::name('User')->where(['id'=>$order_detail['user_id']])->field('id')->find();
2020-06-28 22:50:10 +08:00
if(empty($user))
{
2020-07-13 21:33:55 +08:00
return DataReturn('用户不存在或已删除,终止操作', 0);
2020-06-28 22:50:10 +08:00
}
2022-03-07 12:30:10 +08:00
// 获取日志
$where = [
['order_id', '=', $order_detail['order_id']],
['order_detail_id', '=', $order_detail['id']],
['goods_id', '=', $order_detail['goods_id']],
['user_id', '=', $order_detail['user_id']],
['status', '=', 0],
];
$info = Db::name('GoodsGiveIntegralLog')->where($where)->find();
if(empty($info))
{
return DataReturn('无待发放日志,终止操作', 0);
}
// 存在退款金额则使用退款金额
// 未存在退款金额则判断是否存在退款数量
// 存在退款数量则使用退款数量*单价金额计算(防止订单退款金额为空仅存在退款数量)
$refund_integral = 0;
if($order_detail['refund_price'] > 0)
2020-06-28 22:50:10 +08:00
{
2022-03-07 12:30:10 +08:00
$refund_integral = intval(($info['rate']/100)*$order_detail['refund_price']);
} else {
if($order_detail['returned_quantity'] > 0)
{
2022-03-07 12:30:10 +08:00
$refund_integral = intval(($info['rate']/100)*($order_detail['price']*$order_detail['returned_quantity']));
}
2022-03-07 12:30:10 +08:00
}
if($refund_integral >= 1)
{
// 扣减用户锁定积分
if(!Db::name('User')->where(['id'=>$user['id']])->dec('locking_integral', $refund_integral)->update())
2020-06-28 22:50:10 +08:00
{
2022-03-07 12:30:10 +08:00
return DataReturn('用户锁定积分扣减失败['.$user['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -10);
}
// 扣减日志积分
if(!Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->dec('integral', $refund_integral)->update())
{
return DataReturn('日志积分扣减失败['.$info['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -11);
}
// 剩余0积分则关闭
if(Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->value('integral') <= 0)
{
if(!Db::name('GoodsGiveIntegralLog')->where(['id'=>$info['id']])->update(['status'=>2, 'upd_time'=>time()]))
2020-06-28 22:50:10 +08:00
{
2022-03-07 12:30:10 +08:00
return DataReturn('日志积分关闭失败['.$info['id'].'-'.$order_detail['order_id'].'-'.$order_detail['goods_id'].']', -12);
2020-06-28 22:50:10 +08:00
}
}
}
2022-03-07 12:30:10 +08:00
return DataReturn(MyLang('common.operate_success'), 0);
2020-06-28 22:50:10 +08:00
}
2020-07-01 22:46:52 +08:00
/**
* 用户积分
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2020-07-01
* @desc description
* @param [int] $user_id [用户 id]
*/
public static function UserIntegral($user_id)
{
2021-03-06 13:10:20 +08:00
return Db::name('User')->where(['id'=>$user_id])->field('integral,locking_integral')->find();
2020-07-01 22:46:52 +08:00
}
2018-12-28 18:58:37 +08:00
}
?>