631 lines
28 KiB
PHP
Executable File
631 lines
28 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\api\logic\OrderInvoiceLogic;
|
||
use app\api\logic\OrderLogic;
|
||
use app\api\validate\OrderValidate;
|
||
use app\common\basics\Api;
|
||
use app\common\server\JsonServer;
|
||
use think\facade\Db;
|
||
|
||
use app\common\model\after_sale\AfterSale;
|
||
use app\common\model\order\Order as Morder;
|
||
|
||
use app\common\model\user\User;
|
||
use app\common\server\ConfigServer;
|
||
use app\common\model\AccountLog;
|
||
use app\common\model\shop\Shop;
|
||
use app\common\model\user\UserDistribution;
|
||
use app\shop\logic\order\OrderLogic as Express;
|
||
|
||
|
||
class Order extends Api
|
||
{
|
||
public $like_not_need_login = ['testPromotion'];
|
||
/**
|
||
* @notes 下单
|
||
* @return \think\response\Json
|
||
* @throws \think\Exception
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function submitOrder()
|
||
{
|
||
$post = $this->request->post();
|
||
$post['user_id'] = $this->user_id;
|
||
$post['client'] = $this->client;
|
||
(new OrderValidate())->goCheck('add', $post);
|
||
|
||
$order = OrderLogic::add($post);
|
||
if (false === $order) {
|
||
return JsonServer::error(OrderLogic::getError());
|
||
}
|
||
return JsonServer::success('下单成功!', $order);
|
||
}
|
||
|
||
public function testPromotion(){ ///无相测试使用
|
||
$time = time();
|
||
$afterSaleTime = ConfigServer::get('distribution', 'settlement_days', 7);
|
||
$afterSaleTime = intval($afterSaleTime * 24 * 60 * 60);
|
||
|
||
//2.查询所有未分销结算的订单,推荐人提成计算 //无相增加
|
||
$promotionOrders_rz = Morder::alias('O') //入驻店铺
|
||
->field(['O.id','O.order_sn','O.total_amount','O.user_id','U.first_leader','S.type as stype','U.second_leader','U.third_leader','S.id as sid','U.id as uid','S.trade_promotion_fee','S.promotion_buy_fee','S.team_first_fee','S.team_second_fee','S.team_third_fee'])
|
||
->join('shop S','O.shop_id = S.id')
|
||
->join('shop_admin SA','O.shop_id = SA.shop_id')
|
||
->join('shop_apply SAP','SAP.account = SA.account')
|
||
->join('procurement U','SAP.user_id = U.id')
|
||
->whereRaw("O.confirm_take_time+$afterSaleTime < $time")
|
||
->where([['O.confirm_take_time', '>', 0],['O.promotion_settle',"=",0],['S.type','=',2]]) //已确认收货且没有结算的订单,商家为入驻商家
|
||
->limit(100)
|
||
->select()->toArray();
|
||
|
||
$promotionOrders_zy = Morder::alias('O') //自营店铺
|
||
->field(['O.id','O.order_sn','O.total_amount','U.first_leader','U.second_leader','U.third_leader','S.id as sid','U.id as uid','S.trade_promotion_fee','S.promotion_buy_fee','S.team_first_fee','S.team_second_fee','S.team_third_fee','S.type as stype'])
|
||
->join('shop S','O.shop_id = S.id')
|
||
->join('procurement U','O.user_id = U.id')
|
||
->whereRaw("O.confirm_take_time+$afterSaleTime < $time")
|
||
->where([['O.confirm_take_time', '>', 0],['O.promotion_settle',"=",0],['S.type','=',1]]) //已确认收货且没有结算的订单,商家为入驻商家
|
||
->limit(100)
|
||
->select()->toArray();
|
||
|
||
$promotionOrders = array_merge($promotionOrders_rz,$promotionOrders_zy);
|
||
//dump($promotionOrders);die();
|
||
foreach ($promotionOrders as &$order) {
|
||
|
||
// 增加用户佣金,推广者如果是个人,就直接返推广点,否者按推广层级进行返点
|
||
if($order["stype"]==1){ //如果是自营店铺,直接给购买人提成
|
||
$buyer = User::where('id', $order['uid'])->find(); //购买者
|
||
$wallet = Shop::where('id', $order['sid'])->value('wallet'); //商家减去佣金
|
||
$earnings_buy = User::where('id', $buyer['first_leader'])->value('earnings');
|
||
if($earnings_buy){ //如果购买者有推荐人,就发推荐奖励
|
||
$money_buy = $order['total_amount'] * $order['promotion_buy_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings_buy + $money_buy,
|
||
'update_time' => $time,
|
||
'id' => $buyer['first_leader']
|
||
]);
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money_buy,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
UserDistribution::where("user_id",$buyer['id'])->update([
|
||
'order_num' => Db::raw('order_num+1'),
|
||
'distribution_money' => $money_buy,
|
||
'order_amount' => Db::raw("order_amount+".$order['total_amount']),
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $buyer['first_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money_buy,
|
||
'left_amount' => $earnings_buy+$money_buy,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money_buy,
|
||
'left_amount' => $wallet - $money_buy,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
|
||
}
|
||
|
||
}else{
|
||
|
||
// 商家钱包金额
|
||
$wallet = Shop::where('id', $order['sid'])->value('wallet'); //商家减去佣金
|
||
//先执行推荐购买返点
|
||
$buyer = User::where('id', $order['user_id'])->find(); //购买者
|
||
$earnings_buy = User::where('id', $buyer['first_leader'])->value('earnings');
|
||
if($earnings_buy){ //如果购买者有推荐人,就发推荐奖励
|
||
$money_buy = $order['total_amount'] * $order['promotion_buy_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings_buy + $money_buy,
|
||
'update_time' => $time,
|
||
'id' => $buyer['first_leader']
|
||
]);
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money_buy,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $buyer['first_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money_buy,
|
||
'left_amount' => $earnings_buy+$money_buy,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money_buy,
|
||
'left_amount' => $wallet - $money_buy,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
|
||
}
|
||
|
||
//获取店铺
|
||
|
||
$promoter = User::where('id', $order['first_leader'])->find(); //查询推广者
|
||
if($promoter){
|
||
|
||
if(!$promoter["is_team"]){ //不是团队就直接分账
|
||
$earnings = User::where('id', $order['first_leader'])->value('earnings'); //给推广者增加佣金
|
||
$money = $order['total_amount'] * $order['trade_promotion_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings + $money,
|
||
'update_time' => $time,
|
||
'id' => $order['first_leader']
|
||
]);
|
||
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['first_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $earnings+$money,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $wallet - $money,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
}else{ //如果是团队需要按三级分成
|
||
//一级
|
||
$earnings = User::where('id', $order['first_leader'])->value('earnings'); //给推广者增加佣金
|
||
$money = $order['total_amount'] * $order['team_first_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings + $money,
|
||
'update_time' => $time,
|
||
'id' => $order['first_leader']
|
||
]);
|
||
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['first_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $earnings+$money,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $wallet - $money,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
|
||
//二级
|
||
$promoter_2 = User::where('id', $order['second_leader'])->find(); //给推广者增加佣金
|
||
if($promoter_2){
|
||
$earnings = User::where('id', $order['second_leader'])->value('earnings'); //给推广者增加佣金
|
||
$money = $order['total_amount'] * $order['team_second_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings + $money,
|
||
'update_time' => $time,
|
||
'id' => $order['second_leader']
|
||
]);
|
||
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['second_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $earnings+$money,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $wallet - $money,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
}
|
||
|
||
|
||
//三级
|
||
$promoter_3 = User::where('id', $order['third_leader'])->find(); //给推广者增加佣金
|
||
if($promoter_3){
|
||
$earnings = User::where('id', $order['third_leader'])->value('earnings'); //给推广者增加佣金
|
||
$money = $order['total_amount'] * $order['team_third_fee'] / 100;
|
||
User::update([
|
||
'earnings' => $earnings + $money,
|
||
'update_time' => $time,
|
||
'id' => $order['third_leader']
|
||
]);
|
||
|
||
|
||
Shop::update([
|
||
'wallet' => $wallet - $money,
|
||
'update_time' => $time,
|
||
'id' => $order['sid']
|
||
]);
|
||
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['third_leader'],
|
||
'source_type' => AccountLog::distribution_inc_earnings,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $earnings+$money,
|
||
'change_type' => 1,
|
||
'remark' => '推广佣金增加'
|
||
]);
|
||
// 记录流水
|
||
AccountLog::create([
|
||
'log_sn' => createSn('account_log', 'log_sn', '', 4),
|
||
'user_id' => $order['uid'],
|
||
'source_type' => AccountLog::shop_reduce_wallet,
|
||
'source_id' => $order['id'],
|
||
'source_sn' => $order['order_sn'],
|
||
'change_amount' => $money,
|
||
'left_amount' => $wallet - $money,
|
||
'change_type' => 1,
|
||
'remark' => '商铺钱包余额减少'
|
||
]);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
|
||
} ///入驻店铺结束
|
||
|
||
|
||
|
||
|
||
//更新订单推广状态
|
||
Morder::update([
|
||
'promotion_settle' => 1,
|
||
'update_time' => $time,
|
||
'id' => $order['id']
|
||
]);
|
||
|
||
}
|
||
|
||
//var_dump($promotionOrders);die();
|
||
return JsonServer::success('获取成功', $promotionOrders);
|
||
}
|
||
|
||
/**
|
||
* @notes 结算页数据
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function settlement()
|
||
{
|
||
$post = $this->request->post();
|
||
$post['user_id'] = $this->user_id;
|
||
$post['client'] = $this->client;
|
||
$result = OrderLogic::settlement($post);
|
||
if($result === false) {
|
||
return JsonServer::error(OrderLogic::getError(), [], 301);
|
||
}
|
||
return JsonServer::success('获取成功', $result);
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 订单列表
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function lists()
|
||
{
|
||
$type = $this->request->get('type', 'all');
|
||
$order_list = OrderLogic::getOrderList($this->user_id, $type, $this->page_no, $this->page_size);
|
||
return JsonServer::success('获取成功', $order_list);
|
||
}
|
||
|
||
/**
|
||
* @notes 获取订单详情
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function getOrderDetail()
|
||
{
|
||
$get = $this->request->get();
|
||
(new OrderValidate())->goCheck('detail', $get);
|
||
$detail = OrderLogic::getOrderDetail($get['id']);
|
||
return JsonServer::success('获取成功', $detail);
|
||
}
|
||
// public function getOrder1()
|
||
// {
|
||
|
||
// $detail = OrderLogic::billions();
|
||
|
||
// }
|
||
/**
|
||
* @notes 取消订单
|
||
* @return array|\think\response\Json
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @throws \think\exception\DbException
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function cancel()
|
||
{
|
||
$order_id = $this->request->post('id');
|
||
if (empty($order_id)) {
|
||
return JsonServer::error('参数错误');
|
||
}
|
||
return OrderLogic::cancel($order_id, $this->user_id);
|
||
}
|
||
|
||
/**
|
||
* @notes 确认收货
|
||
* @return array|\think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:11 下午
|
||
*/
|
||
public function confirm()
|
||
{
|
||
$order_id = $this->request->post('id');
|
||
if (empty($order_id)) {
|
||
return JsonServer::error('参数错误');
|
||
}
|
||
$result = OrderLogic::billions($order_id);
|
||
if($result === -2){
|
||
return JsonServer::error('账户被禁用');
|
||
}
|
||
if($result === -3){
|
||
return JsonServer::error('更新余额状态失败');
|
||
}
|
||
if($result === -4){
|
||
return JsonServer::error('更新余额失败');
|
||
}
|
||
if($result === -5){
|
||
return JsonServer::error('插入记录失败');
|
||
}
|
||
return OrderLogic::confirm($order_id, $this->user_id);
|
||
}
|
||
|
||
/**
|
||
* @notes 删除订单
|
||
* @return array|\think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:12 下午
|
||
*/
|
||
public function del()
|
||
{
|
||
$order_id = $this->request->post('id');
|
||
if (empty($order_id)) {
|
||
return JsonServer::error('参数错误');
|
||
}
|
||
return OrderLogic::del($order_id, $this->user_id);
|
||
}
|
||
|
||
/**
|
||
* @notes 订单支付结果页面数据
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:12 下午
|
||
*/
|
||
public function pay_result()
|
||
{
|
||
$id = $this->request->get('id');
|
||
$from = $this->request->get('from');//标识:trade:父订单,order:子订单
|
||
$result = OrderLogic::pay_result($id,$from);
|
||
if ($result !== false) {
|
||
return JsonServer::success('', $result);
|
||
} else {
|
||
return JsonServer::error('参数错误');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 获取支付方式
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/7/13 6:12 下午
|
||
*/
|
||
public function getPayWay()
|
||
{
|
||
$params = $this->request->get();
|
||
if(!isset($params['from']) || !isset($params['order_id'])) {
|
||
return JsonServer::error('参数缺失');
|
||
}
|
||
$pay_way = OrderLogic::getPayWay($this->user_id, $this->client, $params);
|
||
return JsonServer::success('', $pay_way);
|
||
}
|
||
|
||
/**
|
||
* @notes 物流查询
|
||
* @return \think\response\Json
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
* @throws \think\exception\DbException
|
||
* @author suny
|
||
* @date 2021/7/13 6:12 下午
|
||
*/
|
||
public function orderTraces()
|
||
{
|
||
$order_id = $this->request->get('id');
|
||
$tips = '参数错误';
|
||
if ($order_id) {
|
||
$traces = OrderLogic::orderTraces($order_id, $this->user_id);
|
||
if ($traces) {
|
||
return JsonServer::success('获取成功', $traces);
|
||
}
|
||
$tips = '暂无物流信息';
|
||
}
|
||
return JsonServer::error($tips);
|
||
}
|
||
|
||
/**
|
||
* @notes PC获取支付状态
|
||
* @return \think\response\Json
|
||
* @author suny
|
||
* @date 2021/10/29 3:52 下午
|
||
*/
|
||
public function getPayStatus()
|
||
{
|
||
$id = $this->request->get('id');
|
||
$from = $this->request->get('from');//标识:trade:父订单,order:子订单
|
||
$result = OrderLogic::getPayStatus($id,$from);
|
||
if ($result !== false) {
|
||
return JsonServer::success('', $result);
|
||
} else {
|
||
return JsonServer::error('参数错误');
|
||
}
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* @notes 发票详情
|
||
* @return \think\response\Json
|
||
* @author 段誉
|
||
* @date 2022/4/12 9:25
|
||
*/
|
||
public function invoice()
|
||
{
|
||
$id = $this->request->get('id');
|
||
$result = OrderInvoiceLogic::getInvoiceDetailByOrderId($id);
|
||
return JsonServer::success('获取成功', $result);
|
||
}
|
||
|
||
|
||
/*提交集采订单*/
|
||
public function submitJicai(){
|
||
$post = $this->request->post();
|
||
$post["user_id"] = $this->user_id;
|
||
$post["status"] = 0;
|
||
$post["create_time"] = time();
|
||
$post["get_time"] = strtotime($post["get_time"]);
|
||
$res = Db::name("jicai")->insert($post);
|
||
if($res){
|
||
return JsonServer::success('提交成功');
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 获取物流地址
|
||
*/
|
||
public function express()
|
||
{
|
||
$order_id = $this->request->get('order_id');
|
||
$result = Express::getShipping($order_id);
|
||
return JsonServer::success('获取成功', []);
|
||
try {
|
||
$result = json_decode($result, true);
|
||
if (isset($result['returnCode']) && $result['returnCode'] == 500) {
|
||
return JsonServer::success('获取成功', []);
|
||
}
|
||
} catch (\Exception $e ) {
|
||
}
|
||
//return JsonServer::success('获取成功', []);
|
||
if ($result == '暂无物流信息') {
|
||
return JsonServer::success('获取成功');
|
||
} else if(isset($result['returnCode']) && $result['returnCode'] == 200) {
|
||
return JsonServer::success('获取成功', $result);
|
||
} else if(isset($result['state'])) {
|
||
return JsonServer::success('获取成功', $result);
|
||
} else if (isset($result['returnCode']) && $result['returnCode'] != 200) {
|
||
return JsonServer::success('获取成功', []);
|
||
}
|
||
|
||
|
||
|
||
|
||
//dump(123);die;
|
||
// if ($result['returnCode'] == 200) {
|
||
|
||
// }
|
||
}
|
||
|
||
}
|