123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- <?php
- namespace app\api\controller\auction;
- use app\admin\model\system\SystemConfig;
- use app\common\model\Config;
- use app\models\auction\Auction;
- use app\models\auction\AuctionBooking;
- use app\models\auction\AuctionOrder;
- use app\models\auction\AuctionProduct;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\Request;
- use Monolog\Handler\Curl\Util;
- use think\facade\Cache;
- use crmeb\services\{
- CacheService,
- ExpressService,
- SystemConfigService,
- };
- use crmeb\services\UtilService;
- use crmeb\repositories\OrderRepository;
- 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();
- $product_ids = AuctionProduct::where('auction_id', $product['auction_id'])->column('id');
- $count = AuctionOrder::whereBetweenTime('create_time', day(),today())->where('product_id', 'in', $product_ids)->count();
- // $config = SystemConfig::where('menu_name', 'auction_number')->find();
- // if ($count >= $config['value']) 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)->whereBetweenTime('create_time', date('Y-m-d H:i:s', strtotime(date('Y-m-d'))), date('Y-m-d H:i:s', strtotime('+1 day')))->find();
- if ($order){
- return app('json')->fail('商品已卖出');
- }
- $res = AuctionOrder::create([
- 'uid' => $request->uid(),
- 'collection_id' => $product['uid'],// 商品拥有有人
- 'order_id' => getNewOrderId(),
- 'name' => $product['name'],
- 'product_id' => $product['id'],
- 'image'=> $product['image'],
- 'price' => $product['hanging_price'],
- ]);
- 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('订单不存在');
- if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
- $order['upload_image'] = $data['image'];
- $order['status'] = 2;
- 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;
- AuctionOrder::beginTrans();
- $res = $order->save();
- if ($res){
- $product = AuctionProduct::find($order['product_id']);
- if (!$product) return app('json')->fail('数据不存在');
- $uid = $product['uid']; // 所属人id
- $product['uid'] = $order['uid'];// 商品拥有人更新
- $res = $product->save();
- if ($res){
- if ($uid > 0){
- AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
- }
- }
- AuctionOrder::return($order['id']); // 买家
- AuctionOrder::commitTrans();
- return app('json')->successful('完成');
- }else{
- AuctionOrder::rollbackTrans();
- 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['auction_id'])->find()['nickname'];
- $auction = Auction::where('id', $details['auction_id'])->find();
- $details['time'] = $auction['radd_time'].'-'.$auction['rend_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();
- $order = empty($order)? [] : $order->toArray();
- return app('json')->successful($order);
- }
- }
|