AuctionProductController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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\user\User;
  10. use app\models\user\UserBill;
  11. use app\Request;
  12. use Monolog\Handler\Curl\Util;
  13. use think\facade\Cache;
  14. use crmeb\services\{
  15. CacheService,
  16. ExpressService,
  17. SystemConfigService,
  18. };
  19. use crmeb\services\UtilService;
  20. use crmeb\repositories\OrderRepository;
  21. class AuctionProductController
  22. {
  23. /**
  24. * 获取商品列表
  25. * @param Request $request
  26. * @return mixed
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function auction_product(Request $request)
  32. {
  33. $data = UtilService::getMore([
  34. [['page', 'd'], 0],
  35. [['limit', 'd'], 0],
  36. ['id'],
  37. ['name']
  38. ]);
  39. if (!$data['id']) return app('json')->fail('数据传入错误');
  40. return app('json')->successful(AuctionProduct::list($data, $request->uid()));
  41. }
  42. /**
  43. * 用户商品
  44. * @param Request $request
  45. * @return mixed
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function user_product(Request $request)
  51. {
  52. $data = UtilService::getMore([
  53. [['page', 'd'], 0],
  54. [['limit', 'd'], 0],
  55. ]);
  56. return app('json')->successful(AuctionProduct::user_product( $data,$request->uid()));
  57. }
  58. /**
  59. * 购买商品
  60. * @param Request $request
  61. * @return mixed
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public function purchase(Request $request)
  67. {
  68. $data = UtilService::getMore([
  69. ['product_id'],
  70. ]);
  71. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  72. $product = AuctionProduct::where('id', $data['product_id'])->find();
  73. $product_ids = AuctionProduct::where('auction_id', $product['auction_id'])->column('id');
  74. $count = AuctionOrder::whereBetweenTime('create_time', day(),today())->where('product_id', 'in', $product_ids)->count();
  75. // $config = SystemConfig::where('menu_name', 'auction_number')->find();
  76. // if ($count >= $config['value']) return app('json')->fail('单场购买数量已到达最大');
  77. if ($product['uid'] == $request->uid()) return app('json')->fail('无法购买自己商品');
  78. if ($product){
  79. AuctionOrder::beginTrans();
  80. // 查询商品是否以卖出
  81. $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();
  82. if ($order){
  83. return app('json')->fail('商品已卖出');
  84. }
  85. $res = AuctionOrder::create([
  86. 'uid' => $request->uid(),
  87. 'collection_id' => $product['uid'],// 商品拥有有人
  88. 'order_id' => getNewOrderId(),
  89. 'name' => $product['name'],
  90. 'product_id' => $product['id'],
  91. 'image'=> $product['image'],
  92. 'price' => $product['hanging_price'],
  93. ]);
  94. if ($res){
  95. AuctionOrder::commitTrans();
  96. return app('json')->successful('购买成功');
  97. }else{
  98. AuctionOrder::rollbackTrans();
  99. return app('json')->fail('购买失败');
  100. }
  101. }else{
  102. return app('json')->fail('购买商品不存在');
  103. }
  104. }
  105. /**
  106. * 获取用户竞拍订单
  107. * @param Request $request
  108. * @return mixed
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function user_auction_order(Request $request)
  114. {
  115. $data = UtilService::getMore([
  116. [['type', 'd'], 0],
  117. [['page', 'd'], 0],
  118. [['limit', 'd'], 0],
  119. ['order_id']
  120. ]);
  121. return app('json')->successful(AuctionOrder::userOrder($data,$request->uid()));
  122. }
  123. /**
  124. * 上传打款凭证
  125. * @param Request $request
  126. * @return mixed
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. public function up_image(Request $request)
  132. {
  133. $data = UtilService::getMore([
  134. ['image'],
  135. ['id']
  136. ]);
  137. if (!$data['image'] || !$data['id']) return app('json')->fail('数据传入错误');
  138. $order = AuctionOrder::where('id', $data['id'])->find();
  139. if (!$order) return app('json')->fail('订单不存在');
  140. if ($order['status'] != 1) return app('json')->fail('当前订单状态无法上传凭证');
  141. $order['upload_image'] = $data['image'];
  142. $order['status'] = 2;
  143. if ($order->save()){
  144. return app('json')->successful('上传成功');
  145. }else{
  146. return app('json')->fail('上传失败');
  147. }
  148. }
  149. /**
  150. * 卖家显示订单
  151. * @param Request $request
  152. * @return void
  153. */
  154. public function seller(Request $request)
  155. {
  156. $data = UtilService::getMore([
  157. ['type', 0],
  158. [['page', 'd'], 0],
  159. [['limit', 'd'], 0],
  160. ['order_id']
  161. ]);
  162. return app('json')->successful(AuctionOrder::seller_list($data,$request->uid()));
  163. }
  164. /**
  165. * 确定订单
  166. * @param Request $request
  167. * @return void
  168. */
  169. public function adopt(Request $request){
  170. $data = UtilService::postMore([
  171. ['order_id']
  172. ]);
  173. if (!$data['order_id']) return app('json')->fail('数据传入错误');
  174. $order = AuctionOrder::where('order_id', $data['order_id'])->find();
  175. if ($order['status'] < 1) return app('json')->fail('该订单已失效');
  176. if ($order['status'] == 1) return app('json')->fail('未上传打款凭证');
  177. if ($order['status'] == 3) return app('json')->fail('该订单已完成');
  178. $order['status'] = 3;
  179. AuctionOrder::beginTrans();
  180. $res = $order->save();
  181. if ($res){
  182. $product = AuctionProduct::find($order['product_id']);
  183. if (!$product) return app('json')->fail('数据不存在');
  184. $uid = $product['uid']; // 所属人id
  185. $product['uid'] = $order['uid'];// 商品拥有人更新
  186. $res = $product->save();
  187. if ($res){
  188. if ($uid > 0){
  189. AuctionOrder::earn($uid,$order['price'] ,$product); // 卖家
  190. }
  191. }
  192. AuctionOrder::return($order['id']); // 买家
  193. AuctionOrder::commitTrans();
  194. return app('json')->successful('完成');
  195. }else{
  196. AuctionOrder::rollbackTrans();
  197. return app('json')->fail('失败');
  198. }
  199. }
  200. /**
  201. * 产品详情
  202. * @param Request $request
  203. * @return mixed
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function details(Request $request)
  209. {
  210. $data = UtilService::getMore([
  211. ['product_id']
  212. ]);
  213. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  214. $details = AuctionProduct::where('id', $data['product_id'])->find();
  215. if (empty($details)) return app('json')->fail('商品不存在');
  216. $details['slider_image'] = is_string($details['slider_image']) ? json_decode($details['slider_image'], true) : [];
  217. $details['description'] = !empty($details['description']) ? html_entity_decode($details['description'], ENT_COMPAT) : [];
  218. $details['user_nickname'] = User::where('uid', $details['auction_id'])->find()['nickname'];
  219. $auction = Auction::where('id', $details['auction_id'])->find();
  220. $details['time'] = $auction['radd_time'].'-'.$auction['rend_time'];
  221. $details = $details->toArray();
  222. return app('json')->successful($details);
  223. }
  224. /**商品以前属于人
  225. * @param Request $request
  226. * @return mixed
  227. */
  228. public function belong(Request $request)
  229. {
  230. $data = UtilService::getMore([
  231. ['product_id']
  232. ]);
  233. if (!$data['product_id']) return app('json')->fail('数据传入错误');
  234. $order = AuctionOrder::alias('a')
  235. ->field('u.nickname,u.avatar,a.create_time')
  236. ->leftJoin('user u', 'a.collection_id = u.uid')
  237. ->where('a.product_id', $data['product_id'])->where('a.status', 3)->select();
  238. $order = empty($order)? [] : $order->toArray();
  239. return app('json')->successful($order);
  240. }
  241. }