AuctionProductController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. namespace app\api\controller\auction;
  3. use app\admin\model\system\SystemConfig;
  4. use app\common\model\Config;
  5. use app\models\auction\Auction;
  6. use app\models\auction\AuctionBooking;
  7. use app\models\auction\AuctionOrder;
  8. use app\models\auction\AuctionProduct;
  9. use app\models\auction\AuctionTime;
  10. use app\models\user\User;
  11. use app\models\user\UserBill;
  12. use app\Request;
  13. use Monolog\Handler\Curl\Util;
  14. use think\facade\Cache;
  15. use crmeb\services\{
  16. CacheService,
  17. ExpressService,
  18. SystemConfigService
  19. };
  20. use crmeb\services\UtilService;
  21. use crmeb\repositories\OrderRepository;
  22. use think\facade\Db;
  23. class AuctionProductController
  24. {
  25. /**
  26. * 获取商品列表
  27. * @param Request $request
  28. * @return mixed
  29. * @throws \think\db\exception\DataNotFoundException
  30. * @throws \think\db\exception\DbException
  31. * @throws \think\db\exception\ModelNotFoundException
  32. */
  33. public function auction_product(Request $request)
  34. {
  35. $data = UtilService::getMore([
  36. [['page', 'd'], 0],
  37. [['limit', 'd'], 0],
  38. ['id'],
  39. ['name']
  40. ]);
  41. if (!$data['id']) return app('json')->fail('数据传入错误');
  42. return app('json')->successful(AuctionProduct::list($data, $request->uid()));
  43. }
  44. /**
  45. * 用户商品
  46. * @param Request $request
  47. * @return mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function user_product(Request $request)
  53. {
  54. $data = UtilService::getMore([
  55. [['page', 'd'], 0],
  56. [['limit', 'd'], 0],
  57. ]);
  58. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  59. }
  60. /**
  61. * 购买商品
  62. * @param Request $request
  63. * @return mixed
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function purchase(Request $request)
  69. {
  70. $data = UtilService::getMore([
  71. ['product_id'],
  72. ]);
  73. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  74. $product = AuctionProduct::where('id', $data['product_id'])->find();
  75. $product_ids = AuctionProduct::where('auction_id', $product['auction_id'])->column('id');
  76. $auction = Auction::where('id', $product['auction_id'])->find();
  77. $count = AuctionOrder::where('frequency',$auction['frequency'])->where('product_id', 'in', $product_ids)->count();
  78. $config = SystemConfig::where('menu_name', 'auction_number')->find();
  79. if ($count >= (int) preg_replace('~(,(?=[^"]*"(?:[^"]*"[^"]*")*[^"]*$)|")~', '', $config['value'])) return app('json')->fail('单场购买数量已到达最大'); // 查看当前是否已到最大
  80. if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
  81. if ($product){
  82. AuctionOrder::beginTrans();
  83. // 查询商品是否以卖出
  84. $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();
  85. if ($order){
  86. return app('json')->fail('商品已卖出');
  87. }
  88. $res = AuctionOrder::create([
  89. 'uid' => $request->uid(),
  90. 'collection_id' => $product['uid'],// 商品拥有有人
  91. 'order_id' => getNewOrderId(),
  92. 'name' => $product['name'],
  93. 'product_id' => $product['id'],
  94. 'image'=> $product['image'],
  95. 'price' => $product['hanging_price'],
  96. 'frequency' => $auction['frequency']
  97. ]);
  98. if ($res){
  99. AuctionOrder::commitTrans();
  100. return app('json')->successful('购买成功');
  101. }else{
  102. AuctionOrder::rollbackTrans();
  103. return app('json')->fail('购买失败');
  104. }
  105. }else{
  106. return app('json')->fail('购买商品不存在');
  107. }
  108. }
  109. /**
  110. * 获取用户竞拍订单
  111. * @param Request $request
  112. * @return mixed
  113. * @throws \think\db\exception\DataNotFoundException
  114. * @throws \think\db\exception\DbException
  115. * @throws \think\db\exception\ModelNotFoundException
  116. */
  117. public function user_auction_order(Request $request)
  118. {
  119. $data = UtilService::getMore([
  120. [['type', 'd'], 0],
  121. [['page', 'd'], 0],
  122. [['limit', 'd'], 0],
  123. ['order_id']
  124. ]);
  125. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  126. }
  127. /**
  128. * 上传打款凭证
  129. * @param Request $request
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function up_image(Request $request)
  136. {
  137. $data = UtilService::getMore([
  138. ['image'],
  139. ['id']
  140. ]);
  141. if (!$data['image'] || !$data['id']) return app('json')->fail('请上传打款凭证');
  142. $order = AuctionOrder::where('id', $data['id'])->find();
  143. if (!$order) return app('json')->fail('订单不存在');
  144. if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
  145. $order['upload_image'] = $data['image'];
  146. $order['status'] = 2;
  147. if ($order->save()){
  148. return app('json')->successful('上传成功');
  149. }else{
  150. return app('json')->fail('上传失败');
  151. }
  152. }
  153. /**
  154. * 卖家显示订单
  155. * @param Request $request
  156. * @return void
  157. */
  158. public function seller(Request $request)
  159. {
  160. $data = UtilService::getMore([
  161. ['type', 0],
  162. [['page', 'd'], 0],
  163. [['limit', 'd'], 0],
  164. ['order_id']
  165. ]);
  166. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  167. }
  168. /**
  169. * 确定订单
  170. * @param Request $request
  171. * @return void
  172. */
  173. public function adopt(Request $request){
  174. $data = UtilService::postMore([
  175. ['order_id']
  176. ]);
  177. if (!$data['order_id']) return app('json')->fail('数据传入错误');
  178. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  179. if ($order['status'] < 1) return app('json')->fail('该订单已失效');
  180. if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
  181. if ($order['status'] == 3) return app('json')->fail('该订单已完成');
  182. $order['status'] = 3;
  183. AuctionOrder::beginTrans();
  184. $res = $order->save();
  185. if ($res){
  186. $product = AuctionProduct::find($order['product_id']);
  187. if (!$product) return app('json')->fail('数据不存在');
  188. $uid = $product['uid']; // 所属人id
  189. $product['uid'] = $order['uid'];// 商品拥有人更新
  190. $res = $product->save();
  191. if ($res){
  192. if ($uid > 0){
  193. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  194. }
  195. }
  196. AuctionOrder::return($order['id']); // 买家
  197. AuctionOrder::commitTrans();
  198. return app('json')->successful('完成');
  199. }else{
  200. AuctionOrder::rollbackTrans();
  201. return app('json')->fail('失败');
  202. }
  203. }
  204. /**
  205. * 产品详情
  206. * @param Request $request
  207. * @return mixed
  208. * @throws \think\db\exception\DataNotFoundException
  209. * @throws \think\db\exception\DbException
  210. * @throws \think\db\exception\ModelNotFoundException
  211. */
  212. public function details(Request $request)
  213. {
  214. $data = UtilService::getMore([
  215. ['product_id']
  216. ]);
  217. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  218. $details = AuctionProduct::where('id', $data['product_id'])->find();
  219. if (empty($details)) return app('json')->fail('商品不存在');
  220. $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
  221. $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : '';
  222. $details['user_nickname'] = User::where('uid', $details['auction_id'])->find()['nickname'];
  223. $auction = Auction::where('id', $details['auction_id'])->find();
  224. $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
  225. $details = $details->toArray();
  226. return app('json')->successful($details);
  227. }
  228. /**商品以前属于人
  229. * @param Request $request
  230. * @return mixed
  231. */
  232. public function belong(Request $request)
  233. {
  234. $data = UtilService::getMore([
  235. ['product_id']
  236. ]);
  237. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  238. $order = AuctionOrder::alias('a')
  239. ->field('u.nickname,u.avatar,a.create_time')
  240. ->leftJoin('user u', 'a.collection_id = u.uid')
  241. ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
  242. if (empty($order->toArray())) $order = AuctionProduct::alias('a')
  243. ->field('u.nickname,u.avatar,a.create_time')
  244. ->leftJoin('user u', 'a.uid = u.uid')
  245. ->where('a.id', $data['product_id'])->select();
  246. $order = empty($order)? [] : $order->toArray();
  247. return app('json')->successful($order);
  248. }
  249. /**
  250. * 挂售详情
  251. * @param Request $request
  252. * @return mixed
  253. * @throws \think\db\exception\DataNotFoundException
  254. * @throws \think\db\exception\DbException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. */
  257. public function gsxq(Request $request)
  258. {
  259. $data = UtilService::getMore([
  260. ['id']
  261. ]);
  262. $product = AuctionProduct::where('id', $data['id'])->find();
  263. $auction = Auction::where('id', $product['auction_id'])->find();
  264. if (!$product) return app('json')->fail('商品不存在');
  265. $data['time'] = explode(',', $auction['site']);
  266. $date = date('w', time()); // 今天星期几
  267. $data['gs_time'] = '';
  268. if (in_array(1, $data['time'])){
  269. if ($date >= 1 and $date <= 2){
  270. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Wednesday")); // 星期三
  271. }elseif ($date >= 3 and $date <= 4){
  272. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Friday")); // 星期五
  273. }elseif ($date >= 5 or $date == 0){
  274. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Monday")); // 下个星期一
  275. }
  276. }else if (in_array(2, $data['time'])){
  277. if ($date >= 2 and $date <= 3){
  278. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Thursday")); // 星期四
  279. }elseif ($date >= 4 and $date <= 5){
  280. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Saturday")); // 星期六
  281. }elseif ($date >= 6 or $date == 0 or $date == 1){
  282. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Tuesday")); // 下个星期二
  283. }
  284. }
  285. $hanging_price = number_format($product['hanging_price'] * $product['rise']/100, 2); // 溢价
  286. $anticipate = number_format($product['hanging_price'] * $product['deduct']/100, 2); // 扣除
  287. $data['hanging_price'] = $product['hanging_price'] + $hanging_price;
  288. $data['anticipate'] = (float)$anticipate;
  289. return app('json')->successful($data);
  290. }
  291. /**
  292. * 挂售商品
  293. * @param Request $request
  294. * @return mixed
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function hanging_sale(Request $request)
  300. {
  301. $data = UtilService::postMore([
  302. ['id'],
  303. ['gs_time']
  304. ]);
  305. $product = AuctionProduct::where('id', $data['id'])->find();
  306. $user = User::where('uid', $request->uid())->find();
  307. if (!$product) return app('json')->fail('商品不存在');
  308. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  309. $hanging_price = number_format($product['hanging_price'] * $product['rise']/100, 2); // 溢价
  310. $anticipate = number_format($product['hanging_price'] * $product['deduct']/100, 2); // 扣除
  311. if ($user['anticipate'] < $anticipate) return app('json')->fail('预约券不足');
  312. $product['price'] = $product['hanging_price'];
  313. $product['hanging_price'] += $hanging_price;
  314. $product['is_show'] = 1;
  315. $product['is_admin'] = 2;
  316. $user['anticipate'] -= $anticipate;
  317. try {
  318. Db::startTrans();
  319. AuctionTime::create([
  320. 'uid' => $request->uid(),
  321. 'product_id' => $product['id'],
  322. 'auction_id' => $product['auction_id'],
  323. 'add_time' => strtotime($data['gs_time'])
  324. ]);
  325. $product->save();
  326. $user->save();
  327. UserBill::expend('挂售扣除预约券', $user['uid'], 'anticipate', 'reduce_anticipate', $anticipate, $user['spread_uid'], $user['anticipate'], '挂售商品扣除预约券');
  328. Db::commit();
  329. return app('json')->successful('挂售成功');
  330. } catch (\Exception $e) {
  331. Db::rollback();
  332. return app('json')->fail('挂售失败');
  333. }
  334. }
  335. }