AuctionProductController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. $product['add_time'] = time();
  191. $res = $product->save();
  192. if ($res){
  193. if ($uid > 0){
  194. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  195. }
  196. }
  197. AuctionOrder::return($order['id']); // 买家
  198. AuctionOrder::commitTrans();
  199. return app('json')->successful('完成');
  200. }else{
  201. AuctionOrder::rollbackTrans();
  202. return app('json')->fail('失败');
  203. }
  204. }
  205. /**
  206. * 产品详情
  207. * @param Request $request
  208. * @return mixed
  209. * @throws \think\db\exception\DataNotFoundException
  210. * @throws \think\db\exception\DbException
  211. * @throws \think\db\exception\ModelNotFoundException
  212. */
  213. public function details(Request $request)
  214. {
  215. $data = UtilService::getMore([
  216. ['product_id']
  217. ]);
  218. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  219. $details = AuctionProduct::where('id', $data['product_id'])->find();
  220. if (empty($details)) return app('json')->fail('商品不存在');
  221. $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
  222. $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : '';
  223. $details['user_nickname'] = User::where('uid', $details['auction_id'])->find()['nickname'];
  224. $auction = Auction::where('id', $details['auction_id'])->find();
  225. $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
  226. $details = $details->toArray();
  227. return app('json')->successful($details);
  228. }
  229. /**商品以前属于人
  230. * @param Request $request
  231. * @return mixed
  232. */
  233. public function belong(Request $request)
  234. {
  235. $data = UtilService::getMore([
  236. ['product_id']
  237. ]);
  238. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  239. $order = AuctionOrder::alias('a')
  240. ->field('u.nickname,u.avatar,a.create_time')
  241. ->leftJoin('user u', 'a.collection_id = u.uid')
  242. ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
  243. if (empty($order->toArray())) $order = AuctionProduct::alias('a')
  244. ->field('u.nickname,u.avatar,a.create_time')
  245. ->leftJoin('user u', 'a.uid = u.uid')
  246. ->where('a.id', $data['product_id'])->select();
  247. $order = empty($order)? [] : $order->toArray();
  248. return app('json')->successful($order);
  249. }
  250. /**
  251. * 挂售详情
  252. * @param Request $request
  253. * @return mixed
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function gsxq(Request $request)
  259. {
  260. $data = UtilService::getMore([
  261. ['id']
  262. ]);
  263. $product = AuctionProduct::where('id', $data['id'])->find();
  264. if (!$product) return app('json')->fail('商品不存在');
  265. $data = $this->bs($data['id']);
  266. return app('json')->successful($data);
  267. }
  268. /**
  269. * 挂售商品
  270. * @param Request $request
  271. * @return mixed
  272. * @throws \think\db\exception\DataNotFoundException
  273. * @throws \think\db\exception\DbException
  274. * @throws \think\db\exception\ModelNotFoundException
  275. */
  276. public function hanging_sale(Request $request)
  277. {
  278. $data = UtilService::postMore([
  279. ['id'],
  280. ]);
  281. $product = AuctionProduct::where('id', $data['id'])->find();
  282. $user = User::where('uid', $request->uid())->find();
  283. if (!$product) return app('json')->fail('商品不存在');
  284. if ($product['is_show'] == 1) return app('json')->fail('商品已挂售');
  285. $datas = $this->bs($data['id']);// 获取挂售详情
  286. if ($user['anticipate'] < $datas['anticipate']) return app('json')->fail('预约券不足');
  287. $product['price'] = $product['hanging_price'];
  288. $product['hanging_price'] = $datas['hanging_price']; // 商品变为挂售价格
  289. $product['is_show'] = 1; // 上架
  290. $product['is_admin'] = 2;
  291. $user['anticipate'] -= $datas['anticipate']; // 扣除预约券
  292. try {
  293. Db::startTrans();
  294. // 新增挂售时间段
  295. AuctionTime::create([
  296. 'uid' => $request->uid(),
  297. 'product_id' => $product['id'],
  298. 'auction_id' => $product['auction_id'],
  299. 'add_time' => strtotime($datas['gs_time'])
  300. ]);
  301. $product->save();
  302. $user->save();
  303. UserBill::expend('挂售扣除预约券', $user['uid'], 'anticipate', 'reduce_anticipate', $datas['anticipate'], $user['spread_uid'], $user['anticipate'], '挂售商品扣除预约券');
  304. Db::commit();
  305. return app('json')->successful('挂售成功');
  306. } catch (\Exception $e) {
  307. Db::rollback();
  308. return app('json')->fail('挂售失败');
  309. }
  310. }
  311. public function bs($id)
  312. {
  313. $product = AuctionProduct::where('id',$id)->find();
  314. $auction = Auction::where('id', $product['auction_id'])->find();
  315. if (!$product) return app('json')->fail('商品不存在');
  316. $data['time'] = explode(',', $auction['site']);
  317. $date = date('w', time()); // 今天星期几
  318. $data['gs_time'] = '';
  319. $bs = 0;
  320. if (in_array(1, $data['time'])){
  321. if ($date >= 1 and $date <= 2){
  322. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Wednesday")); // 星期三
  323. $bs = 2;
  324. }elseif ($date >= 3 and $date <= 4){
  325. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Friday")); // 星期五
  326. $bs = 2;
  327. }elseif ($date >= 5 or $date == 0){
  328. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Monday")); // 下个星期一
  329. $bs = 3;
  330. }
  331. }else if (in_array(2, $data['time'])){
  332. if ($date >= 2 and $date <= 3){
  333. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Thursday")); // 星期四
  334. $bs = 2;
  335. }elseif ($date >= 4 and $date <= 5){
  336. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Saturday")); // 星期六
  337. $bs = 2;
  338. }elseif ($date >= 6 or $date == 0 or $date == 1){
  339. $data['gs_time'] = date('Y-m-d H:i:s', strtotime("Tuesday")); // 下个星期二
  340. $bs = 3;
  341. }
  342. }
  343. $hanging_price = round($product['hanging_price'] * $product['rise']/100, 2); // 溢价
  344. $anticipate = round($product['hanging_price'] * $product['deduct']/100, 2); // 扣除
  345. $data['hanging_price'] = (int)$product['hanging_price'] + ($hanging_price * $bs);
  346. $data['anticipate'] = $anticipate;
  347. return $data;
  348. }
  349. }