123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- <?php
- namespace app\api\controller\auction;
- use app\admin\model\auction\AuctionDeliver;
- use app\admin\model\system\SystemConfig;
- use app\models\auction\Auction;
- use app\models\auction\AuctionBooking;
- use app\models\auction\AuctionFrozen;
- use app\models\auction\AuctionGu;
- use app\models\auction\AuctionOrder;
- use app\models\auction\AuctionProduct;
- use app\models\auction\AuctionTime;
- use app\models\user\User;
- use app\models\user\UserAddress;
- use app\models\user\UserBill;
- use app\Request;
- use crmeb\utils\Redis;
- use Monolog\Handler\Curl\Util;
- use think\facade\Cache;
- use crmeb\services\{
- CacheService,
- ExpressService,
- SystemConfigService
- };
- use crmeb\services\UtilService;
- use crmeb\repositories\OrderRepository;
- use think\facade\Db;
- class AuctionProductController
- {
- /**
- * 获取商品列表
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function auction_product(Request $request)
- {
- $data = UtilService::getMore([
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ['id'],
- ['name']
- ]);
- if (!$data['id']) return app('json')->fail('数据传入错误');
- return app('json')->successful(AuctionProduct::list($data, $request->uid()));
- }
- /**
- * 用户商品
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function user_product(Request $request)
- {
- $data = UtilService::getMore([
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ]);
- return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
- }
- /**
- * 购买商品
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function purchase(Request $request)
- {
- $data = UtilService::getMore([
- ['product_id'],
- ]);
- if (!$data['product_id']) return app('json')->fail('数据传入错误');
- $product = AuctionProduct::where('id', $data['product_id'])->find();
- if ($product['is_show'] == 0) return app('json')->fail('商品未挂售');
- $auction = Auction::where('id', $product['auction_id'])->find();
- $user = $request->user();
- $time = strtotime(date('Y-m-d', time()));// 今天
- $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天
- $radd_time = strtotime($auction['radd_time']); // 抢购时间
- if ($user['is_new'] == 1 or ($user['green_time'] >= $time and $user['green_time'] <= $today)){
- // 新人或者绿色通道提前三分钟入场
- if ($radd_time - 180 > time()){
- return app('json')->fail('未到抢购时间');
- }
- }else if($radd_time > time()){
- return app('json')->fail('当前未到抢购时间');
- }
- $count = AuctionOrder::where('frequency',$auction['frequency'])->where('uid', $request->uid())->where('auction_id', '=', $auction['id'])->count();// 查找今天订单数量
- $orderCount = AuctionOrder::where([['uid', '=', $request->uid()]])->whereBetweenTime('create_time', $time, $today)->count(); // 查找今天抢购的订单数量;
- $config = SystemConfigService::get('auction_number');
- $max = SystemConfigService::get('maximum');
- if ($orderCount >= (int)$max) return app('json')->fail('今日抢购数量已达到最大'); // 查看当前是否已到最大
- if ($count >= (int)$config) return app('json')->fail('单场购买数量已到达最大'); // 查看当前是否已到最大
- if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
- if ($product){
- AuctionOrder::beginTrans();
- //查询商品是否以卖出
- $order = AuctionOrder::where('product_id', $data['product_id'])->where('status', '>', 0)->where('frequency', $auction['frequency'])->find();
- if ($order){
- return app('json')->fail('商品已卖出');
- }
- $order_id = getNewOrderId();
- if ($count >= 1){
- if ($user['anticipate'] < 200) return app('json')->fail('广告值不足');
- $user['anticipate'] -= 200;
- User::where('uid', $user['uid'])->update(['anticipate' => $user['anticipate']]);
- UserBill::expend('冻结广告值',$user['uid'], 'anticipate', 'fz_anticipate', 200, $user['spread_uid'], $user['anticipate'], '购买订单'.$order_id.'冻结广告值');
- AuctionFrozen::create([
- 'order_id' => $order_id,
- 'uid' => $request->uid(),
- 'anticipate' => 200
- ]);
- }
- $redis = new \Redis();
- $redis->connect('127.0.0.1','6379'); // redis 缓存
- if (!$redis->lPop($data['product_id'])){
- return app('json')->fail('商品已卖出');
- }
- $res = AuctionOrder::create([
- 'uid' => $request->uid(),
- 'collection_id' => $product['uid'],// 商品拥有有人
- 'order_id' => $order_id,
- 'name' => $product['name'],
- 'product_id' => $product['id'],
- 'auction_id' => $auction['id'],
- 'image'=> $product['image'],
- 'price' => $product['hanging_price'],
- 'frequency' => $auction['frequency']
- ]);
- if ($res){
- AuctionOrder::commitTrans();
- return app('json')->successful('购买成功');
- }else{
- AuctionOrder::rollbackTrans();
- return app('json')->fail('购买失败');
- }
- }else{
- return app('json')->fail('购买商品不存在');
- }
- }
- /**
- * 获取用户竞拍订单
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function user_auction_order(Request $request)
- {
- $data = UtilService::getMore([
- [['type', 'd'], 0],
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ['order_id']
- ]);
- return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
- }
- /**
- * 上传打款凭证
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function up_image(Request $request)
- {
- $data = UtilService::getMore([
- ['image'],
- ['id']
- ]);
- if (!$data['image'] || !$data['id']) return app('json')->fail('请上传打款凭证');
- $order = AuctionOrder::where('id', $data['id'])->find();
- if (!$order) return app('json')->fail('订单不存在');
- $auction = Auction::where('id', $order['auction_id'])->find();
- if (strtotime($auction['radd_time']) > time() or strtotime($auction['rend_time']) < time()) return app('json')->fail('没到支付时间,支付时间为'.$auction['radd_time'].'-'.$auction['rend_time']);
- if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
- $order['upload_image'] = $data['image'];
- $order['status'] = 2;
- $order['voucher_time'] = time();
- if ($order->save()){
- return app('json')->successful('上传成功');
- }else{
- return app('json')->fail('上传失败');
- }
- }
- /**
- * 卖家显示订单
- * @param Request $request
- * @return void
- */
- public function seller(Request $request)
- {
- $data = UtilService::getMore([
- ['type', 0],
- [['page', 'd'], 0],
- [['limit', 'd'], 0],
- ['order_id']
- ]);
- return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
- }
- /**
- * 确定订单
- * @param Request $request
- * @return void
- */
- public function adopt(Request $request){
- $data = UtilService::postMore([
- ['order_id']
- ]);
- if (!$data['order_id']) return app('json')->fail('数据传入错误');
- $order = AuctionOrder::where('order_id', $data['order_id'])->find();
- if ($order['status'] < 1) return app('json')->fail('该订单已失效');
- if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
- if ($order['status'] == 3) return app('json')->fail('该订单已完成');
- $order['status'] = 3;
- try {
- Db::startTrans();
- $product = AuctionProduct::find($order['product_id']);
- if ($product['uid'] == $order['uid']) return app('json')->fail('商品已发放');
- if (!$product) return app('json')->fail('数据不存在');
- $uid = $product['uid']; // 所属人id
- $product['uid'] = $order['uid'];// 商品拥有人更新
- $product['add_time'] = time();
- $product['order'] = $data['order_id'];
- $product['is_show'] = 0;
- $res = $product->save();
- AuctionTime::where([['product_id', '=', $product['id']]])->delete();
- AuctionOrder::where('order_id', $data['order_id'])->update(['status' => 3]);
- AuctionOrder::return($order['id']); // 买家
- Db::commit();
- return app('json')->successful('完成');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('失败');
- }
- }
- /**
- * 产品详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function details(Request $request)
- {
- $data = UtilService::getMore([
- ['product_id']
- ]);
- if (!$data['product_id']) return app('json')->fail('数据传入错误');
- $details = AuctionProduct::where('id', $data['product_id'])->find();
- if (empty($details)) return app('json')->fail('商品不存在');
- $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
- $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : '';
- $details['user_nickname'] = User::where('uid', $details['uid'])->find()['nickname'];
- $auction = Auction::where('id', $details['auction_id'])->find();
- $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
- $details['times'] = strtotime($auction['radd_time']);
- $details = $details->toArray();
- return app('json')->successful($details);
- }
- /**商品以前属于人
- * @param Request $request
- * @return mixed
- */
- public function belong(Request $request)
- {
- $data = UtilService::getMore([
- ['product_id']
- ]);
- if (!$data['product_id']) return app('json')->fail('数据传入错误');
- $order = AuctionOrder::alias('a')
- ->field('u.nickname,u.avatar,a.create_time')
- ->leftJoin('user u', 'a.collection_id = u.uid')
- ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
- if (empty($order->toArray())) $order = AuctionProduct::alias('a')
- ->field('u.nickname,u.avatar,a.create_time')
- ->leftJoin('user u', 'a.uid = u.uid')
- ->where('a.id', $data['product_id'])->select();
- $order = empty($order)? [] : $order->toArray();
- return app('json')->successful($order);
- }
- /**
- * 挂售详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function gsxq(Request $request)
- {
- $data = UtilService::getMore([
- ['id']
- ]);
- $product = AuctionProduct::where('id', $data['id'])->find();
- if (!$product) return app('json')->fail('商品不存在');
- $data = AuctionProduct::bs($data['id']);
- return app('json')->successful($data);
- }
- /**
- * 挂售商品
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function hanging_sale(Request $request)
- {
- $data = UtilService::postMore([
- ['id'],
- ]);
- Db::startTrans();
- $product = AuctionProduct::where('id', $data['id'])->lock(true)->where('uid', $request->uid())->find();
- $user = User::where('uid', $request->uid())->find();
- if (!$product) return app('json')->fail('商品不存在');
- if ($product['frozen'] == 1) return app('json')->fail('商品已冻结,无法挂售');
- if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
- $guId = Auction::where('id', $product['auction_id'])->value('auction_gu_id');
- $auction_id = Auction::where('auction_gu_id', $guId)->column('id');
- $max = AuctionGu::where('id', $guId)->value('max_hanging');
- $money = AuctionProduct::where('is_show', 1)
- ->where('is_admin', 2)
- ->where('uid', $request->uid())
- ->where('auction_id', 'in', $auction_id)
- ->whereBetweenTime('update_time',strtotime('today'), strtotime('tomorrow'))
- ->sum('hanging_price');
- if (($money+$product['hanging_price']) > (float)$max) return app('json')->fail('今天挂售金额已超出上限');
- $datas = AuctionProduct::bs($data['id']);// 获取挂售详情
- if ($user['anticipate'] < $datas['anticipate']) return app('json')->fail('广告值不足');
- $product['price'] = $product['hanging_price'];
- $product['hanging_price'] = $datas['hanging_price']; // 商品变为挂售价格
- $product['is_show'] = 1; // 上架
- $product['is_admin'] = 2;
- $user['anticipate'] -= $datas['anticipate']; // 扣除广告值
- $user['integral'] += $datas['give'];
- try {
- // 新增挂售时间段
- AuctionTime::create([
- 'uid' => $request->uid(),
- 'product_id' => $product['id'],
- 'auction_id' => $product['auction_id'],
- 'add_time' => strtotime($datas['gs_time'])
- ]);
- $product->save();
- $user->save();
- AuctionOrder::where([['uid', '=', $request->uid()], ['product_id', '=', $product['id']], ['is_gs', '=', 0]])->update(['is_gs' => 1, 'gs_price' => $datas['hanging_price']]);// 修改订单挂售
- UserBill::expend('挂售扣除广告值', $user['uid'], 'anticipate', 'gs_anticipate', $datas['anticipate'], $user['spread_uid'], $user['anticipate'], '挂售商品'.$product['id'].'扣除广告值');
- UserBill::income('赠送趣豆', $user['uid'], 'integral', 'gs_integral', $datas['give'], $user['spread_uid'], $user['integral'], '挂售赠送趣豆');
- Db::commit();
- return app('json')->successful('挂售成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail($e->getMessage());
- }
- }
- /**
- * 取消挂售
- * @param Request $request
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function cancel(Request $request)
- {
- $data = UtilService::postMore([
- ['id']
- ]);
- $productModel = new AuctionProduct();
- $details = $productModel->find($data['id']);
- if (!$details) return app('json')->fail('商品不存在');
- if ($details['uid'] != $request->uid()) return app('json')->fail('当前商品不属于你');
- $order = AuctionOrder::where([['product_id', '=', $details['id']], ['status', '<', 3], ['status', '>', 0]])->find();
- if ($order) return app('json')->fail('商品已出售,无法取消');
- $auction = Auction::where('id', $details['auction_id'])->find();
- $data['time'] = explode(',', $auction['site']);
- $date = date('w', time()); // 今天星期几
- $bs = 0;
- if (in_array(1, $data['time'])){
- if ($date >= 1 and $date <= 2){
- $bs = 3 - $date;
- }elseif ($date >= 3 and $date <= 4){
- $bs = 5 - $date;
- }elseif ($date >= 5 or $date == 0){
- if ($date == 5) $bs = 3;
- if ($date == 6) $bs = 2;
- if ($date == 0) $bs = 1;
- }
- }else if (in_array(2, $data['time'])){
- if ($date >= 2 and $date <= 3){
- $bs = 4 - $date;
- }elseif ($date >= 4 and $date <= 5){
- $bs = 6 - $date;
- }elseif ($date >= 6 or $date == 0 or $date == 1){
- if ($date == 6) $bs = 3;
- if ($date == 0) $bs = 2;
- if ($date == 1) $bs = 1;
- }
- }
- $user = User::where('uid', $request->uid())->find();
- $anticipate = round($details['price'] * $details['deduct']/100, 2);
- $details['hanging_price'] = $details['price']; // 价格变=回未挂售前;
- $details['is_show'] = 0; // 价格变回未挂售前;
- $user['anticipate'] += $anticipate*$bs;// 退回挂售扣除的预约广告值
- $user['integral'] -= round(($details['price'] * $details['give']/100)*$bs, 2);// 挂售赠送趣豆
- try {
- Db::startTrans();
- $details->save();
- $user->save();
- AuctionOrder::where('order_id', $details['order'])->save(['is_gs' => 0]);
- UserBill::income('取消挂售退回广告值', $request->uid(), 'anticipate', 'gsth_anticipate', $anticipate*$bs, 0, $user['anticipate'], '取消挂售退回广告值');
- UserBill::expend('取消挂售扣除趣豆', $request->uid(), 'integral', 'gsth_integral', round(($details['price'] * $details['give']/100)*$bs, 2), 0, $user['integral'], '取消挂售扣除赠送趣豆');
- AuctionTime::where([['uid', '=', $request->uid()], ['product_id', '=', $data['id']]])->delete(); // 删除上架时间
- Db::commit();
- return app('json')->successful('取消挂售成功');
- } catch (\Exception $e) {
- Db::rollback();
- return app('json')->fail('取消挂售失败');
- }
- }
- /**
- * 订单数量
- * @param Request $request
- * @return mixed
- */
- public function untreated(Request $request){
- $data = [
- 'user' => [
- 'paid' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 1]])->count(), //待支付
- 'reviewed' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 2]])->count(), // 待审核
- 'hanging' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 3], ['is_gs', '=', 0]])->count(), // 待挂售
- ],
- 'seller' => [
- 'paid' => AuctionOrder::where([['collection_id', '=', $request->uid()], ['status', '=', 1]])->count(), //待支付
- 'reviewed' => AuctionOrder::where([['collection_id', '=', $request->uid()], ['status', '=', 2]])->count(), // 待审核
- 'hanging' => AuctionOrder::where([['uid', '=', $request->uid()], ['status', '=', 3], ['is_gs', '=', 1]])->count(), //挂售中
- ]
- ];
- return app('json')->successful($data);
- }
- /**
- * 提货
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function carry(Request $request)
- {
- list($addressId, $productId) = UtilService::postMore([
- ['addressId', ''], ['productId', '']
- ], $request, true);
- if (!$addressId) return app('json')->fail('请选择收货地址');
- if (!$productId) return app('json')->fail('请选择提货商品');
- $uid = $request->uid();
- $address = UserAddress::where('id', $addressId)->where('uid', $uid)->find();
- $product = AuctionProduct::where([['uid','=', $uid], ['id', '=', $productId]])->find();
- $order = AuctionOrder::where('uid', '<>', $uid)->where('status', '>', 0)->where('product_id', $productId)->where('create_time', '>', strtotime('today'))->find();
- if ($product['is_show'] == 1) return app('json')->fail('商品只能下架后提货');
- if ($order) return app('json')->fail('该商品出售中,无法提货');
- if (!$address) return app('json')->fail('收货地址不存在');
- if (!$product) return app('json')->fail('提货商品不存在');
- Db::startTrans();
- $res = AuctionDeliver::create([
- 'uid' => $uid,
- 'product_id' => $productId,
- 'name' => $product['name'],
- 'image' => $product['image'],
- 'price' => $product['hanging_price'],
- 'real_name' => $address['real_name'],
- 'user_phone' => $address['phone'],
- 'user_address' => $address['province'].' '.$address['city'].' '.$address['district'].' '.$address['detail'],
- ]);
- $res1 = $product->delete();
- if ($res && $res1){
- Db::commit();
- return app('json')->success('提货成功');
- }else{
- Db::rollback();
- return app('json')->fail('提货失败');
- }
- }
- /**
- * 提货订单
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function deliver_list(Request $request)
- {
- list($status, $page, $limit) = UtilService::postMore([
- ['status', 0], ['page', 1], ['limit', 10]
- ], $request, true);
- $list = AuctionDeliver::where('uid', $request->uid())
- ->where('status', $status)
- ->order('id DESC')
- ->page($page, $limit)
- ->select();
- $list = count($list) > 0? $list->toArray(): [];
- foreach ($list as &$item){
- if (!empty($item['goods_time'])) $item['goods_time'] = date('Y-m-d H:i:s', $item['goods_time']);
- if (!empty($item['collect_time'])) $item['collect_time'] = date('Y-m-d H:i:s', $item['collect_time']);
- }
- return app('json')->success($list);
- }
- /**
- * 提货订单详情
- * @param Request $request
- * @return mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function deliver(Request $request)
- {
- list($id) = UtilService::postMore([
- ['id', 0]
- ], $request, true);
- $details = AuctionDeliver::where('uid', $request->uid())->where('id', $id)->find();
- if (!$details) return app('json')->fail('订单不存在');
- if (!empty($details['goods_time'])) $details['goods_time'] = date('Y-m-d H:i:s', $details['goods_time']);
- if (!empty($details['collect_time'])) $details['collect_time'] = date('Y-m-d H:i:s', $details['collect_time']);
- return app('json')->success($details->toArray());
- }
- public function collect(Request $request)
- {
- list($id) = UtilService::postMore([
- ['id', 0]
- ], $request, true);
- $details = AuctionDeliver::where('uid', $request->uid())->where('id', $id)->find();
- if (!$details) return app('json')->fail('订单不存在');
- if ($details['status'] == 0) return app('json')->fail('未发货');
- if ($details['status'] == 2) return app('json')->fail('已收货');
- $res = AuctionDeliver::where('uid', $request->uid())->where('id', $id)->update(['status' => 2, 'collect_time' => time()]);
- if ($res) return app('json')->success('确认收货');
- return app('json')->fail('收货失败');
- }
- }
|