manghe/app/api/controller/Warehouse.php
2025-05-03 15:05:08 +08:00

1143 lines
47 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace app\api\controller;
use app\common\model\Goods;
use app\common\model\GoodsList;
use app\common\model\Order;
use app\common\model\OrderList;
use app\common\model\OrderListRecovery;
use app\common\model\OrderListSend;
use app\common\model\Shang;
use app\common\model\User;
use think\facade\Db;
use think\Request;
use app\common\service\CommonService;
use app\common\helper\ConfigHelper;
class Warehouse extends Base
{
/**
* 赏袋列表
* @param $type 1赏品 2预售 3卡册 4保险柜
*/
public function warehouse_index()
{
$user = $this->getUser();
$user_id = $user['id'];
#自动变现货=========================
// OrderList::field("goodslist_type")
// ->where('user_id', '=', $user_id)
// ->where('status', '=', 0)
// ->where('goodslist_type', '=', 2)
// ->where('goodslist_sale_time', '<=', time())
// ->update(['goodslist_type' => 1]);
$linqi_list = OrderList::field("goodslist_type,goodslist_id,user_id,id")
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 2)
->select()->toArray();
foreach ($linqi_list as $k => $val) {
$sale_time = GoodsList::where(['id' => $val['goodslist_id']])->value('sale_time');
if (!empty($sale_time)) {
if ($sale_time <= time()) {
OrderList::where(['id' => $val['id'], 'status' => 0, 'user_id' => $val['user_id'], 'goodslist_type' => 2])->update(['goodslist_type' => 1]);
}
} else {
OrderList::where(['id' => $val['id'], 'status' => 0, 'user_id' => $val['user_id'], 'goodslist_type' => 2])->where('goodslist_sale_time', '<=', time())->update(['goodslist_type' => 1]);
}
}
#自动变现货=========================
$type = request()->param('type/d', 0);
$show_dadajuan = true;
// $type = 3;
if ($type == 1) {#赏品
$keyword = request()->param('keyword', '');
#搜索
$whe = [];
if ($keyword) {
$whe[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#总数量
$total = 0;
#找到所在盒子
$data = OrderList::field('goods_id')
->append(['goods_title'])
->withAttr('goods_title', function ($value, $data) {
if ($data['goods_id'] == 0) {
$title = '无限令奖品';
} elseif ($data['goods_id'] == -1) {
$title = '周榜奖品';
} elseif ($data['goods_id'] == -1) {
$title = '月榜奖品';
} else {
$title = Db::name('goods')->field('title')->where('id', '=', $data['goods_id'])->value('title');
}
return $title ? $title : '其他';
})
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', 'not in', [4])
->group("goods_id")
->order('goods_id asc')
->select()->toArray();
foreach ($data as &$data_value) {
$orderlist = OrderList::field("id,goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num")
->append(['shang_title'])
->where('goods_id', '=', $data_value['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', 'not in', [4])
->where($whe)
->order('shang_id asc')
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->withAttr('goodslist_money', function ($value, $data) {
return (float) $value * 100;
})
->group("prize_code")
->select()->toArray();
foreach ($orderlist as $k => &$v) {
$v['order_list_ids'] = OrderList::where('goods_id', '=', $v['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', 'not in', [4, 9])
->where('prize_code', '=', $v['prize_code'])
->order('shang_id asc')
->column('id');
}
$orderlist_total = array_sum(array_column($orderlist, 'prize_num'));
$total += $orderlist_total;
$data_value['orderlist'] = $orderlist;
$data_value['orderlist_total'] = $orderlist_total;
$data_value['orderlist_length'] = count($orderlist);
}
if (!$data) {
$show_dadajuan = false;
} else {
$show_dadajuan_limit = ConfigHelper::getAppSettingKey('show_dadajuan_limit');
if ($show_dadajuan_limit != null && $show_dadajuan_limit != "0") {
$show_dadajuan_limit = intval($show_dadajuan_limit);
$user_total_consumption = CommonService::getTotalConsumptionAmount($user_id);
if ($user_total_consumption < $show_dadajuan_limit) {
$show_dadajuan = false;
}
}
}
} elseif ($type == 2) {#预售
$keyword = request()->param('keyword', '');
#搜索
$whe = [];
if ($keyword) {
$whe[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#总数量
$total = 0;
#找到所在盒子
$data = OrderList::field('goods_id')
->append(['goods_title'])
->withAttr('goods_title', function ($value, $data) {
$title = Db::name('goods')->field('title')->where('id', '=', $data['goods_id'])->value('title');
return $title ? $title : '其他';
})
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 2)
->where('insurance_is', '=', 0)
->where('order_type', 'not in', [4])
->group("goods_id")
->order('goods_id asc')
->select()->toArray();
foreach ($data as &$data_value) {
$orderlist = OrderList::field("goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,goodslist_sale_time,count(id) as prize_num")
->append(['shang_title'])
->where('goods_id', '=', $data_value['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 2)
->where('insurance_is', '=', 0)
->where('order_type', 'not in', [4])
->where($whe)
->order('shang_id asc')
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->withAttr('goodslist_money', function ($value, $data) {
return (float) $value * 100;
})
->withAttr('goodslist_sale_time', function ($value, $data) {
return date('Y-m-d', $value);
})
->group("prize_code")
->select()->toArray();
$orderlist_total = array_sum(array_column($orderlist, 'prize_num'));
$total += $orderlist_total;
$data_value['orderlist'] = $orderlist;
$data_value['orderlist_total'] = $orderlist_total;
$data_value['orderlist_length'] = count($orderlist);
}
} elseif ($type == 3) {#卡册
$keyword = request()->param('keyword', '');
$category_id = request()->param('category_id/d', 0);
#搜索
$whe = [];
if ($keyword) {
$whe[] = ['o.goods_title', 'like', '%' . $keyword . '%'];
}
if ($category_id) {
$whe[] = ['g.category_id', $category_id];
}
// dd($whe);
#卡册列表
$obj = Order::field('o.id,o.goods_id,o.goods_title,o.goods_imgurl,g.category_id')->alias('o')
->join('goods g', 'g.id = o.goods_id')
->where('o.user_id', '=', $user_id)
->where('o.order_type', '=', 4);
if ($keyword) {
$obj->where('o.goods_title', 'like', '%' . $keyword . '%');
}
if ($category_id) {
$obj->where('g.category_id', $category_id);
}
$list = $obj->order('o.id desc')
->group('o.goods_id')
->paginate(30)->each(function ($itme) use ($user_id) {
$goods_info = Db::name('goods')->field('title,imgurl')->where('id', '=', $itme['goods_id'])->find();
if ($goods_info) {
$itme['goods_title'] = $goods_info['title'];
$itme['goods_imgurl'] = imageUrl($goods_info['imgurl']);
} else {
$itme['goods_imgurl'] = imageUrl($itme['goods_imgurl']);
}
#奖品总量
$all_count = GoodsList::field('id')
->where('goods_id', '=', $itme['goods_id'])
->count();
#获得数量
$buy_count = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('goods_id', '=', $itme['goods_id'])
->where('status', '=', 0)
->group('goodslist_id')
->count();
if ($buy_count > 0) {
$gailv = (float) bcdiv("$buy_count", "$all_count", 2);
$gailv = bcmul("$gailv", "100", 2);
$gailv = (float) $gailv;
} else {
$gailv = 0;
}
$itme['all_count'] = $all_count;
$itme['buy_count'] = $buy_count;
$itme['gailv'] = $gailv;
return $itme;
});
$data = $list->items();
$last_page = $list->lastPage();
} elseif ($type == 4) {#保险柜
$keyword = request()->param('keyword', '');
#搜索
$whe = [];
if ($keyword) {
$whe[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#总数量
$total = 0;
#找到所在盒子
$data = OrderList::field('goods_id')
->append(['goods_title'])
->withAttr('goods_title', function ($value, $data) {
if ($data['goods_id'] == 0) {
$title = '无限令奖品';
} elseif ($data['goods_id'] == -1) {
$title = '周榜奖品';
} elseif ($data['goods_id'] == -1) {
$title = '月榜奖品';
} else {
$title = Db::name('goods')->field('title')->where('id', '=', $data['goods_id'])->value('title');
}
return $title ? $title : '其他';
})
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 1)
->where('order_type', 'not in', [4])
->group("goods_id")
->order('goods_id asc')
->select()->toArray();
foreach ($data as &$data_value) {
$orderlist = OrderList::field("goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num")
->append(['shang_title'])
->where('goods_id', '=', $data_value['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 1)
->where('order_type', 'not in', [4])
->where($whe)
->order('shang_id asc')
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->withAttr('goodslist_money', function ($value, $data) {
return (float) $value * 100;
})
->group("prize_code")
->select()->toArray();
$orderlist_total = array_sum(array_column($orderlist, 'prize_num'));
$total += $orderlist_total;
$data_value['orderlist'] = $orderlist;
$data_value['orderlist_total'] = $orderlist_total;
$data_value['orderlist_length'] = count($orderlist);
}
} else if ($type == 5) { //无限赏
$keyword = request()->param('keyword', '');
#搜索
$whe = [];
if ($keyword) {
$whe[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#总数量
$total = 0;
#找到所在盒子
$data = OrderList::field('goods_id')
->append(['goods_title'])
->withAttr('goods_title', function ($value, $data) {
if ($data['goods_id'] == 0) {
$title = '无限令奖品';
} elseif ($data['goods_id'] == -1) {
$title = '周榜奖品';
} elseif ($data['goods_id'] == -1) {
$title = '月榜奖品';
} else {
$title = Db::name('goods')->field('title')->where('id', '=', $data['goods_id'])->value('title');
}
return $title ? $title : '其他';
})
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', '=', 2)
->group("goods_id")
->order('goods_id asc')
->select()->toArray();
foreach ($data as &$data_value) {
$orderlist = OrderList::field("id,goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num")
->append(['shang_title'])
->where('goods_id', '=', $data_value['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', '=', 2)
->where($whe)
->order('shang_id asc')
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->withAttr('goodslist_money', function ($value, $data) {
return (float) $value * 100;
})
->group("prize_code")
->select()->toArray();
foreach ($orderlist as $k => &$v) {
$v['order_list_ids'] = OrderList::where('goods_id', '=', $v['goods_id'])
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('goodslist_type', '=', 1)
->where('insurance_is', '=', 0)
->where('order_type', '=', 2)
->where('prize_code', '=', $v['prize_code'])
->order('shang_id asc')
->column('id');
}
$orderlist_total = array_sum(array_column($orderlist, 'prize_num'));
$total += $orderlist_total;
$data_value['orderlist'] = $orderlist;
$data_value['orderlist_total'] = $orderlist_total;
$data_value['orderlist_length'] = count($orderlist);
}
} else {
return $this->renderError("请求错误");
}
#运费设置
$config = getConfig("base");
$yufei = [
'free_post' => $config['free_post'],
'post_money' => $config['post_money'],
];
$new_data = [
'total' => isset($total) ? $total : 0,
'data' => $data,
'last_page' => isset($last_page) ? $last_page : 1,
'yufei' => $yufei,
'show_dadajuan' => $show_dadajuan,
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 卡册详情
*/
public function warehouse_carddetail_log()
{
$user = $this->getUser();
$user_id = $user['id'];
$keyword = request()->param('keyword', '');
$type = request()->param('type/d', 0);
$shang_id = request()->param('shang_id/d', 0);
$goods_id = request()->param('goods_id/d', 0);
#详情
$goods = Order::field('id,goods_id,goods_title,goods_imgurl')
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('order_type', '=', 4)
->order('id desc')
->find();
$goods_info = Db::name('goods')->field('title,card_banner')->where('id', '=', $goods['goods_id'])->find();
if ($goods_info) {
$goods['goods_title'] = $goods_info['title'];
$goods['goods_imgurl'] = imageUrl($goods_info['card_banner']);
} else {
$goods['goods_imgurl'] = imageUrl($goods['goods_imgurl']);
}
#全部数量
$all_count = GoodsList::field('id')->where('goods_id', '=', $goods_id)->count();
#获得数量
$buy_count = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('status', '=', 0)
->group('goodslist_id')
->count();
if ($buy_count > 0) {
$gailv = (float) bcdiv("$buy_count", "$all_count", 2);
$gailv = bcmul("$gailv", "100", 2);
$gailv = (float) $gailv;
} else {
$gailv = 0;
}
$goods['all_count'] = $all_count;
$goods['buy_count'] = $buy_count;
$goods['gailv'] = $gailv;
#奖品分类
$category = Shang::field('id,title')
->where('goods_id', '=', $goods_id)
->order('sort desc,id asc')
->select()->toArray();
$where = [];
$where_order = [];
if ($shang_id) {
$where[] = ['shang_id', '=', $shang_id];
$where_order[] = ['shang_id', '=', $shang_id];
}
if ($type == 0) {#全部
#搜索
if ($keyword) {
$where[] = ['title', 'like', '%' . $keyword . '%'];
}
#奖品
$data = GoodsList::field('id,title as goodslist_title,imgurl as goodslist_imgurl,money as goodslist_money,goods_id,shang_id,prize_code,card_no,shang_id')
->append(['shang_title'])
->where('goods_id', '=', $goods_id)
->where($where)
->order('sort desc,id asc')
->paginate(15);
$goodslist = $data->toArray()['data'];
$last_page = $data->lastPage();
foreach ($goodslist as $key => $value) {
unset($goodslist[$key]['id']);
$goodslist[$key]['goodslist_imgurl'] = imageUrl($value['goodslist_imgurl']);
#是否被抽中
$is_info = OrderList::field('goodslist_id,goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num,shang_id')
->append(['card_no', 'shang_title'])
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('status', '=', 0)
->where('goodslist_id', '=', $value['id'])
->where('insurance_is', '=', 0)
->where($where_order)
->group('prize_code')
->find();
if ($is_info) {
$goodslist[$key] = $is_info;
$goodslist[$key]['is'] = 1;
} else {
$goodslist[$key]['is'] = 0;
$goodslist[$key]['prize_num'] = 0;
}
}
} elseif ($type == 1) {#已拥有
#搜索
if ($keyword) {
$where[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#奖品
$data = OrderList::field('goodslist_id,goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num,shang_id')
->append(['card_no', 'shang_title'])
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->where($where)
->group('prize_code')
->paginate(15);
$goodslist = $data->toArray()['data'];
$last_page = $data->lastPage();
foreach ($goodslist as &$goodslist_value) {
$goodslist_value['is'] = 1;
}
} elseif ($type == 2) {#未拥有
#搜索
if ($keyword) {
$where[] = ['title', 'like', '%' . $keyword . '%'];
}
#全部奖品ID
$goodslist_id = GoodsList::field('id')->where('goods_id', '=', $goods_id)->column('id');
#中奖奖品id
$prize_id = OrderList::field('goodslist_id')
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->group('goodslist_id')
->column('goodslist_id');
$arr_id = array_diff($goodslist_id, $prize_id);
$where[] = ['id', 'in', $arr_id];
#奖品
$data = GoodsList::field('id,title as goodslist_title,imgurl as goodslist_imgurl,money as goodslist_money,goods_id,shang_id,prize_code,card_no,shang_id')
->append(['shang_title'])
->where('goods_id', '=', $goods_id)
->where($where)
->order('sort desc,id asc')
->paginate(15);
$goodslist = $data->toArray()['data'];
$last_page = $data->lastPage();
foreach ($goodslist as &$value) {
$value['goodslist_imgurl'] = imageUrl($value['goodslist_imgurl']);
unset($value['id']);
$value['is'] = 0;
$value['prize_num'] = 0;
}
} elseif ($type == 3) {#保险柜
#搜索
if ($keyword) {
$where[] = ['goodslist_title', 'like', '%' . $keyword . '%'];
}
#奖品
$data = OrderList::field('goodslist_id,goodslist_title,goodslist_imgurl,goodslist_money,goods_id,shang_id,prize_code,count(id) as prize_num,shang_id')
->append(['card_no', 'shang_title'])
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->where('user_id', '=', $user_id)
->where('goods_id', '=', $goods_id)
->where('num', '=', 0)
->where('status', '=', 0)
->where('insurance_is', '=', 1)
->where($where)
->group('prize_code')
->paginate(15);
$goodslist = $data->toArray()['data'];
$last_page = $data->lastPage();
foreach ($goodslist as &$goodslist_value) {
$goodslist_value['is'] = 1;
}
} else {
return $this->renderError("请求错误");
}
#运费设置
$config = getConfig("base");
$yufei = [
'free_post' => $config['card_free_post'],
'post_money' => $config['card_post_money'],
];
$new_data = [
'total' => array_sum(array_column($goodslist, 'prize_num')),
'goods' => $goods,
'yufei' => $yufei,
'category' => $category,
'goodslist' => $goodslist,
'last_page' => $last_page,
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 兑换
*/
public function warehouse_recovery()
{
$user = $this->getUser();
$user_id = $user['id'];
$recovery_info = request()->param('recovery_info', '');
if (empty($recovery_info)) {
return $this->renderError("请选择兑换的赏品");
}
$show_dadajuan_limit = ConfigHelper::getAppSettingKey('show_dadajuan_limit');
if ($show_dadajuan_limit != null && $show_dadajuan_limit != "0") {
$show_dadajuan_limit = intval($show_dadajuan_limit);
$user_total_consumption = CommonService::getTotalConsumptionAmount($user_id);
if ($user_total_consumption < $show_dadajuan_limit) {
return $this->renderError("消费满" . $show_dadajuan_limit . "才能兑换达达卷");
}
}
$cabinet_exchange_limit = \app\common\helper\ConfigHelper::getAppSettingKey("cabinet_exchange_limit");
if ($cabinet_exchange_limit > 0) {
$today_start = strtotime(date('Y-m-d 00:00:00', time()));
$today_end = strtotime(date('Y-m-d 23:59:59', time()));
$today_count = OrderListRecovery::where('user_id', '=', $user_id)
->where('addtime', '>=', $today_start)
->where('addtime', '<=', $today_end)
->count();
if ($today_count >= $cabinet_exchange_limit) {
return $this->renderError("今日兑换次数已达上限");
}
}
#装换结构
$recovery_info = json_decode($recovery_info, true);
$order_list_id = [];
foreach ($recovery_info as $value) {
$order_list_ids = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->where('prize_code', '=', $value['prize_code'])
->limit($value['number'])
->column("id");
$order_list_id = array_merge($order_list_id, $order_list_ids);
}
if (!$order_list_id) {
return $this->renderError("请刷新重新选择奖品");
}
#兑换金额
$total_money = OrderList::field('goodslist_money')
->where('id', 'in', $order_list_id)
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->sum("goodslist_money");
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_warehouse_recovery" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
#兑换单号
$recovery_num = create_order_no('HS_', 'order_list_recovery', 'recovery_num');
Db::startTrans();
$res = [];
#记录日志
$res[] = OrderListRecovery::insert([
'user_id' => $user_id,
'recovery_num' => $recovery_num,
'money' => $total_money,
'count' => count($order_list_id),
'addtime' => time(),
]);
#改变状态
$res[] = OrderList::field('recovery_num,status,choice_time')
->where('id', 'in', $order_list_id)
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->update([
'recovery_num' => $recovery_num,
'status' => 1,
'choice_time' => time(),
]);
#兑换金额
$total_money *= 100;
if ($total_money > 0) {
// $res[] = User::changeMoney($user_id, $total_money, 4, '兑换获得');
// User::changeIntegral($user_id, $total_money, 4, '兑换获得');
User::changeMoney2($user_id, $total_money, 4, '兑换获得');
}
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("打包成功");
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("打包失败");
}
}
/**
* 发货
*/
public function warehouse_send(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
$type = request()->param('type/d', 0);
#背包 抽卡机
if ($type != 1 && $type != 2) {
return $this->renderError("请求参数错误");
}
// $isTest = \app\common\helper\ConfigHelper::getSystemTestKey("disable_wechat_pay");
// if ($isTest == "1") {
// return $this->renderError("发货未开放");
// }
$recovery_info = request()->param('recovery_info', '');
if (empty($recovery_info)) {
return $this->renderError("请选择兑换的赏品");
}
$name = request()->param("name", '');
$mobile = request()->param("mobile", '');
$address = request()->param("address", '');
$message = request()->param("message", '');
if (empty($name) || empty($mobile) || empty($address)) {
return $this->renderError("缺少收货信息");
}
#装换结构
$recovery_info = json_decode($recovery_info, true);
$order_list_id = [];
foreach ($recovery_info as $value) {
$lim = $value['number'];
if (is_string($lim)) {
$lim = intval($lim);
}
$order_list_ids = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->where('goodslist_type', '=', 1)
->where('prize_code', '=', trim($value['prize_code']))
->limit($lim)
->column("id");
$order_list_id = array_merge($order_list_id, $order_list_ids);
}
if (!$order_list_id) {
return $this->renderError("请刷新重新选择奖品");
}
#发货数量
$count = count($order_list_id);
#运费设置
$config = getConfig("base");
if ($type == 1) {#背包
$free_post = (int) $config['free_post'];#发货数量
$free_post = $free_post <= 0 ? 0 : $free_post;
$post_money = (int) $config['post_money'];#发货运费
$post_money = $post_money <= 0 ? 0 : $post_money;
} elseif ($type == 2) {#抽卡机
$free_post = (int) $config['card_free_post'];#发货数量
$free_post = $free_post <= 0 ? 0 : $free_post;
$post_money = (int) $config['card_post_money'];#发货运费
$post_money = $post_money <= 0 ? 0 : $post_money;
}
#发货订单
// $send_num = create_order_no('FH_', 'order_list_send', 'send_num');
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_warehouse_send" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
Db::startTrans();
$title = '背包发货' . $count . '件';
$attach = 'order_list_send';
$fa_money = $post_money;
if ($free_post <= $count || $post_money == 0) {
$fa_money = 0;
}
$payRes = \app\common\server\platform\PlatformFactory::createPay($user, $fa_money, $title, $attach, "FH_");
if ($payRes['status'] !== 1) {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("支付失败,请刷新重试");
}
$send_num = $payRes['data']['order_no'];
#改变赏品信息
$res[] = OrderList::field('id,send_num')
->where('id', 'in', $order_list_id)
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->where('goodslist_type', '=', 1)
->update([
'send_num' => $send_num,
]);
#生成订单
$res[] = $end_order_id = OrderListSend::insertGetId([
'user_id' => $user_id,
'send_num' => $send_num,
'freight' => $post_money,
'status' => 0,#0待支付 1待发货 2待收货 3已完成
'count' => $count,
'name' => $name,
'mobile' => $mobile,
'address' => $address,
'message' => $message,
'addtime' => time(),
]);
#为满足发货条件
//
// dd($free_post,$count);
if ($free_post > $count && $post_money > 0) {
// $payRes = (new Pay())->wxCreateOrder($send_num, $post_money, $user['id'], $body, $attach);
if ($payRes['status'] == 1) {
#结果集
$new_data = [
'status' => 1,
'order_no' => $send_num,
'res' => $payRes['data']['res'],
];
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("下单失败");
}
} else {
OrderListSend::field('freight')->where('id', '=', $end_order_id)->update(['freight' => 0]);
#背包发货处理
$res[] = (new Notify($this->app))->reward_order_handle($user['id'], $end_order_id);
#结果集
$new_data = [
'status' => 0,
'order_no' => $send_num,
];
}
if (resCheck($res)) {
Db::commit();
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("发货成功", $new_data);
} else {
Db::rollback();
#删除redis
$redis->del($redis_key);
return $this->renderError("发货失败");
}
}
/**
* 移入保险柜
*/
public function warehouse_movein()
{
$user = $this->getUser();
$user_id = $user['id'];
$recovery_info = request()->param('recovery_info', '');
if (empty($recovery_info)) {
return $this->renderError("请选择兑换的赏品");
}
#装换结构
$recovery_info = json_decode($recovery_info, true);
$order_list_id = [];
foreach ($recovery_info as $value) {
$order_list_ids = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->where('prize_code', '=', $value['prize_code'])
->limit($value['number'])
->column("id");
$order_list_id = array_merge($order_list_id, $order_list_ids);
}
if (!$order_list_id) {
return $this->renderError("请刷新重新选择奖品");
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_warehouse_movein" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
$res = OrderList::field('insurance_is')
->where('id', 'in', $order_list_id)
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 0)
->update([
'insurance_is' => 1,
]);
if ($res) {
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("移入成功");
} else {
#删除redis
$redis->del($redis_key);
return $this->renderError("移入失败");
}
}
/**
* 移出保险柜
*/
public function warehouse_remove()
{
$user = $this->getUser();
$user_id = $user['id'];
$recovery_info = request()->param('recovery_info', '');
if (empty($recovery_info)) {
return $this->renderError("请选择兑换的赏品");
}
#装换结构
$recovery_info = json_decode($recovery_info, true);
$order_list_id = [];
foreach ($recovery_info as $value) {
$order_list_ids = OrderList::field('id')
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 1)
->where('prize_code', '=', $value['prize_code'])
->limit($value['number'])
->column("id");
$order_list_id = array_merge($order_list_id, $order_list_ids);
}
if (!$order_list_id) {
return $this->renderError("请刷新重新选择奖品");
}
$redis = (new \app\common\server\RedisHelper())->getRedis();
$redis_key = "kpw_warehouse_remove" . '_' . $user['id'];
$redis_key_info = $redis->get($redis_key);
if ($redis_key_info) {
return $this->renderError("当前操作太快了,请等待");
} else {
$redis->set($redis_key, 1, 10);
}
$res = OrderList::field('insurance_is')
->where('id', 'in', $order_list_id)
->where('user_id', '=', $user_id)
->where('status', '=', 0)
->where('insurance_is', '=', 1)
->update([
'insurance_is' => 0,
]);
if ($res) {
#删除redis
$redis->del($redis_key);
return $this->renderSuccess("移出成功");
} else {
#删除redis
$redis->del($redis_key);
return $this->renderError("移出失败");
}
}
/**
* 打包记录
*/
public function warehouse_recovery_record()
{
$user = $this->getUser();
$user_id = $user['id'];
$data = OrderListRecovery::field('recovery_num,money,count,addtime')
->append(['order_list'])
->withAttr('addtime', function ($value, $data) {
return date('Y-m-d H:i:s', $data['addtime']);
})
->where('user_id', '=', $user_id)
->order("id desc")
->paginate(10);
$new_data = [
'data' => $data->items(),
'last_pahe' => $data->lastPage(),
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 发货记录
*/
public function warehouse_send_record(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
$status = \request()->param('status/d', 1);
if ($status != 1 && $status != 2 && $status != 3) {
return $this->renderError("非法请求");
}
#自动收货=====================================
$guoqi_time = time() - (7 * 86400);
OrderListSend::field('status,shou_time')
->where('status', '=', 2)
->where('send_time', '<=', $guoqi_time)
->update(['status' => 3, 'shou_time' => time()]);
#自动收货=====================================
$data = OrderListSend::field('id,send_num,status,count,addtime')
->append(['order_list', 'status_name'])
->withAttr('addtime', function ($value, $data) {
return date('Y-m-d H:i:s', $data['addtime']);
})
->where('user_id', '=', $user_id)
->where('status', '=', $status)
->order("id desc")
->paginate(10);
$new_data = [
'data' => $data->items(),
'last_pahe' => $data->lastPage(),
];
return $this->renderSuccess("请求成功", $new_data);
}
/**
* 记录详情
*/
public function warehouse_send_record_detail(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
$id = request()->param('id/d', 0);
if (empty($id)) {
return $this->renderError("非法请求");
}
#发货订单详情
$info = OrderListSend::field('id,status,send_num,name,mobile,address,addtime,pay_time,send_time,shou_time')
->where('id', '=', $id)
->where('user_id', '=', $user_id)
->find();
if (!$info) {
return $this->renderError("请求参数错误");
}
if ($info['status'] != 1 && $info['status'] != 2 && $info['status'] != 3) {
return $this->renderError("请求参数错误1");
}
$info['addtime'] = date('Y-m-d H:i:s', $info['addtime']);
$info['pay_time'] = date('Y-m-d H:i:s', $info['pay_time']);
$info['send_time'] = $info['send_time'] ? date('Y-m-d H:i:s', $info['send_time']) : '待发货';
$info['shou_time'] = $info['shou_time'] ? date('Y-m-d H:i:s', $info['shou_time']) : '待收货';
#奖品
$data = OrderList::field('goodslist_title,goodslist_imgurl,shang_id,goodslist_money,1 as prize_num,fh_status')
->append(['shang_title'])
->where(['send_num' => $info['send_num']])
->withAttr('goodslist_imgurl', function ($value, $data) {
return imageUrl($value);
})
->order('goodslist_money desc')
// ->group("prize_code")
->select();
$info['goods'] = $data;
return $this->renderSuccess("请求成功", $info);
}
/**
* 确认收货
*/
public function warehouse_send_confirm(Request $request)
{
$user = $this->getUser();
$user_id = $user['id'];
$id = request()->param('id/d', 0);
if (empty($id)) {
return $this->renderError("非法请求");
}
#发货订单详情
$info = OrderListSend::field('id,status')
->where('id', '=', $id)
->where('user_id', '=', $user_id)
->find();
if (!$info) {
return $this->renderError("请求参数错误");
}
if ($info['status'] == 3) {
return $this->renderError("请勿重复操作");
}
if ($info['status'] != 2) {
return $this->renderError("该订单暂不能确认收货");
}
$res = OrderListSend::where('id', '=', $info['id'])->update(['status' => 3, 'shou_time' => time()]);
if ($res) {
return $this->renderSuccess("确认收货成功");
} else {
return $this->renderError("网络故障,请稍后重试");
}
}
/**
* 查看物流
*/
public function warehouse_order_logistics()
{
$status_name = ['快递收件(揽件)', '在途中', '正在派件', '已签收', '派送失败', '疑难件', '退件签收', '其他'];
$user = $this->getUser();
$user_id = $user['id'];
$id = request()->param('id/d', 0);
if (empty($id)) {
return $this->renderError("非法请求");
}
#发货订单详情
$info = OrderListSend::field('courier_number,courier_name,courier_code,delivery_list,delivery_status,delivery_time,send_num,count')
->where('id', '=', $id)
->where('user_id', '=', $user_id)
->find();
if (!$info) {
return $this->renderError("请求参数错误");
}
#奖品图片
$goodslist_imgurl = OrderList::field('goodslist_imgurl')->where('send_num', '=', $info['send_num'])->value('goodslist_imgurl');
$info['goodslist_imgurl'] = imageUrl($goodslist_imgurl);
#物流轨迹
$delivery_status = isset($status_name[$info['delivery_status']]) ? $status_name[$info['delivery_status']] : '暂无物流轨迹';
$delivery_list = $info['delivery_list'] ? json_decode($info['delivery_list'], true) : '';
#查找物流
if ($info['courier_number'] && (time() > ($info['delivery_time'] + 600))) {
#顺丰快递
if ($info['delivery_code'] == 'SFEXPRESS') {
$m = substr($info['mobile'], -4);
$info['courier_number'] = $info['courier_number'] . ':' . $m;
}
$result = expressWuliu($info['courier_number'], $info['courier_code']);
if ($result['code'] == 1) {
#投递状态 0快递收件(揽件)1.在途中 2.正在派件 3.已签收 4.派送失败 5.疑难件 6.退件签收
$delivery_status = $result['msg']['deliverystatus'];
$delivery_status = isset($status_name[$delivery_status]) ? $status_name[$delivery_status] : '暂无物流轨迹';
$delivery_list = $result['msg']['list'];
OrderListSend::where(['id' => $id])->update([
'delivery_list' => $delivery_list ? json_encode($delivery_list) : '',
'delivery_time' => time(),
]);
}
}
$info['delivery_status'] = $delivery_status;
$info['delivery_list'] = $delivery_list;
return $this->renderSuccess("请求成功", $info);
}
}