Bargain.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\bargain\api\controller;
  11. use app\api\controller\BaseApi;
  12. use addon\bargain\model\Bargain as BargainModel;
  13. use app\model\order\OrderCommon;
  14. /**
  15. * 砍价
  16. */
  17. class Bargain extends BaseApi
  18. {
  19. /**
  20. * 获取我的砍价详情
  21. */
  22. public function info()
  23. {
  24. $token = $this->checkToken();
  25. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  26. if (empty($id)) {
  27. return $this->response($this->error('', 'REQUEST_ID'));
  28. }
  29. $condition = [
  30. [ 'launch_id', '=', $id ],
  31. [ 'site_id', '=', $this->site_id ],
  32. ];
  33. $bargain = new BargainModel();
  34. $data = $bargain->getBargainLaunchDetail($condition, 'bargain_id,bargain_type,buy_type,curr_num,curr_price,floor_price,end_time,goods_id,headimg,launch_id,member_id,nickname,order_id,price,site_id,sku_id,sku_image,sku_name,start_time,status');
  35. if ($data[ 'code' ] == 0) {
  36. if ($token[ 'code' ] == 0) {
  37. $bargain_goods_info = $bargain->getBargainGoodsDetail([ [ 'pbg.sku_id', '=', $data[ 'data' ][ 'sku_id' ] ], [ 'pb.status', '=', '1' ] ], 'pbg.bargain_stock,pb.status,g.goods_content')[ 'data' ];
  38. $data[ 'data' ][ 'bargain_stock' ] = $bargain_goods_info[ 'bargain_stock' ];
  39. $data[ 'data' ][ 'bargain_status' ] = $bargain_goods_info[ 'status' ];
  40. $data[ 'data' ][ 'goods_content' ] = $bargain_goods_info[ 'goods_content' ];
  41. if ($data[ 'data' ][ 'member_id' ] == $this->member_id) {
  42. $data[ 'data' ][ 'self' ] = 1;
  43. } else {
  44. $data[ 'data' ][ 'self' ] = 0;
  45. $record_info = $bargain->getBargainRecordInfo([ [ 'launch_id', '=', $id ], [ 'member_id', '=', $this->member_id ] ], 'id');
  46. $data[ 'data' ][ 'cut' ] = empty($record_info[ 'data' ]) ? 0 : 1;
  47. }
  48. } else {
  49. $data[ 'data' ][ 'self' ] = 0;
  50. $data[ 'data' ][ 'cut' ] = 0;
  51. }
  52. }
  53. return $this->response($data);
  54. }
  55. /**
  56. * 获取我的砍价分页列表
  57. */
  58. public function launchPage()
  59. {
  60. $token = $this->checkToken();
  61. if ($token[ 'code' ] < 0) return $this->response($token);
  62. $page = $this->params[ 'page' ] ?? 1;
  63. $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
  64. $status = $this->params[ 'status' ] ?? 'all';
  65. $condition = [
  66. [ 'pbl.site_id', '=', $this->site_id ],
  67. [ 'pbl.member_id', '=', $this->member_id ]
  68. ];
  69. if ($status != 'all') {
  70. $condition[] = [ 'pbl.status', '=', $status ];
  71. }
  72. $bargain = new BargainModel();
  73. $field = 'pbl.*, pb.status as bargain_status';
  74. $data = $bargain->getBargainLaunchPageList($condition, $field, 'pbl.launch_id desc', $page, $page_size, 'pbl', [
  75. [ 'promotion_bargain pb', 'pbl.bargain_id = pb.bargain_id', 'inner' ]
  76. ]);
  77. return $this->response($data);
  78. }
  79. /**
  80. * 发起砍价
  81. * @return false|string
  82. */
  83. public function launch()
  84. {
  85. $token = $this->checkToken();
  86. if ($token[ 'code' ] < 0) return $this->response($token);
  87. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  88. if (empty($id)) {
  89. return $this->response($this->error('', 'REQUEST_ID'));
  90. }
  91. $store_id = isset($this->params[ 'store_id' ]) ? $this->params[ 'store_id' ] : 0; // 默认门店
  92. $bargain = new BargainModel();
  93. $res = $bargain->launch($id, $this->member_id, $this->site_id, $store_id);
  94. return $this->response($res);
  95. }
  96. /**
  97. * 砍价
  98. */
  99. public function bargain()
  100. {
  101. $token = $this->checkToken();
  102. if ($token[ 'code' ] < 0) return $this->response($token);
  103. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  104. if (empty($id)) {
  105. return $this->response($this->error('', 'REQUEST_ID'));
  106. }
  107. $bargain = new BargainModel();
  108. $res = $bargain->bargain($id, $this->member_id, $this->site_id);
  109. return $this->response($res);
  110. }
  111. /**
  112. * 获取砍价记录
  113. * @return false|string
  114. */
  115. public function record()
  116. {
  117. $id = isset($this->params[ 'id' ]) ? $this->params[ 'id' ] : 0;
  118. if (empty($id)) {
  119. return $this->response($this->error('', 'REQUEST_ID'));
  120. }
  121. $page = $this->params[ 'page' ] ?? 1;
  122. $page_size = $this->params[ 'page_size' ] ?? PAGE_LIST_ROWS;
  123. $condition = [
  124. [ 'launch_id', '=', $id ]
  125. ];
  126. $bargain = new BargainModel();
  127. $data = $bargain->getBargainRecordPageList($condition, '*', 'id desc', $page, $page_size);
  128. return $this->response($data);
  129. }
  130. /**
  131. * 砍价详情
  132. * @return false|string|void
  133. */
  134. public function detail()
  135. {
  136. $bargain_id = isset($this->params[ 'bargain_id' ]) ? $this->params[ 'bargain_id' ] : 0;
  137. $launch_id = isset($this->params[ 'launch_id' ]) ? $this->params[ 'launch_id' ] : 0;
  138. if (empty($bargain_id)) {
  139. return $this->response($this->error('', 'REQUEST_ID'));
  140. }
  141. $token = $this->checkToken();
  142. $bargain = new BargainModel();
  143. $condition = [
  144. [ 'pb.bargain_id', '=', $bargain_id ],
  145. [ 'pbg.site_id', '=', $this->site_id ],
  146. // [ 'pbg.status', '=', 1 ],
  147. [ 'g.goods_state', '=', 1 ],
  148. [ 'g.is_delete', '=', 0 ]
  149. ];
  150. $goods_sku_detail = $bargain->getBargainGoodsDetail($condition, '')[ 'data' ];
  151. $bargain->bargainBrowseInc([ [ 'bargain_id', '=', $bargain_id ], [ 'site_id', '=', $this->site_id ] ]);
  152. $res[ 'goods_sku_detail' ] = $goods_sku_detail;
  153. if (empty($goods_sku_detail)) return $this->response($this->error($res));
  154. if (!empty($goods_sku_detail[ 'goods_spec_format' ])) {
  155. //判断商品规格项
  156. $goods_spec_format = $bargain->getGoodsSpecFormat($bargain_id, $this->site_id, $goods_sku_detail[ 'goods_spec_format' ]);
  157. $res[ 'goods_sku_detail' ][ 'goods_spec_format' ] = json_encode($goods_spec_format);
  158. }
  159. $launch_info = [];
  160. if ($launch_id) {
  161. $condition = [
  162. [ 'launch_id', '=', $launch_id ],
  163. [ 'site_id', '=', $this->site_id ],
  164. ];
  165. $launch_info = $bargain->getBargainLaunchDetail($condition, 'bargain_id,bargain_type,buy_type,curr_num,curr_price,floor_price,end_time,goods_id,headimg,launch_id,member_id,nickname,order_id,price,site_id,sku_id,sku_image,sku_name,start_time,status')[ 'data' ] ?? [];
  166. } else {
  167. if ($token[ 'code' ] == 0) {
  168. $launch_info = $bargain->getBargainLaunchDetail([
  169. [ 'bargain_id', '=', $goods_sku_detail[ 'bargain_id' ] ],
  170. [ 'sku_id', '=', $goods_sku_detail[ 'sku_id' ] ],
  171. [ 'member_id', '=', $this->member_id ],
  172. [ 'status', '=', 0 ]
  173. ], 'bargain_id,bargain_type,buy_type,curr_num,curr_price,floor_price,end_time,goods_id,headimg,launch_id,member_id,nickname,order_id,price,site_id,sku_id,sku_image,sku_name,start_time,status')[ 'data' ] ?? [];
  174. }
  175. }
  176. if ($launch_info) {
  177. $launch_info[ 'pay_status' ] = 0;
  178. if ($launch_info[ 'order_id' ]) {
  179. $order = new OrderCommon();
  180. $order_info = $order->getOrderInfo([ [ 'order_id', '=', $launch_info[ 'order_id' ] ], [ 'site_id', '=', $this->site_id ] ], 'order_status, pay_status')[ 'data' ] ?? [];
  181. $launch_info[ 'pay_status' ] = $order_info[ 'pay_status' ] ?? 0;
  182. $launch_info[ 'order_status' ] = $order_info[ 'order_status' ];
  183. // if($launch_info['order_status'] == OrderCommon::ORDER_CLOSE) $launch_info['order_id'] = 0;
  184. }
  185. }
  186. if (!empty($launch_info)) {
  187. if ($token[ 'code' ] == 0) {
  188. if ($launch_info[ 'member_id' ] == $this->member_id) {
  189. $launch_info[ 'self' ] = 1;
  190. $record_info = $bargain->getBargainRecordInfo([ [ 'launch_id', '=', $launch_info[ 'launch_id' ] ], [ 'member_id', '=', $this->member_id ] ], 'money');
  191. $launch_info[ 'my_bargain_money' ] = $record_info[ 'data' ][ 'money' ] ?? 0;
  192. } else {
  193. $launch_info[ 'self' ] = 0;
  194. $record_info = $bargain->getBargainRecordInfo([ [ 'launch_id', '=', $launch_info[ 'launch_id' ] ], [ 'member_id', '=', $this->member_id ] ], 'id');
  195. $launch_info[ 'cut' ] = empty($record_info[ 'data' ]) ? 0 : 1;
  196. }
  197. } else {
  198. $launch_info[ 'self' ] = 0;
  199. $launch_info[ 'cut' ] = 0;
  200. }
  201. }
  202. $res[ 'launch_info' ] = $launch_info;
  203. //已砍成功的人
  204. $condition = [
  205. [ 'site_id', '=', $this->site_id ],
  206. [ 'bargain_id', '=', $bargain_id ],
  207. [ 'status', '=', 1 ]
  208. ];
  209. $launch_list = $bargain->getBargainLaunchList($condition, 'status,curr_price,nickname,headimg,end_time', 'launch_id desc', '', '', '', 20);
  210. $res[ 'launch_list' ] = $launch_list;
  211. return $this->response($this->success($res));
  212. }
  213. public function browse()
  214. {
  215. $token = $this->checkToken();
  216. $bargain_id = isset($this->params[ 'bargain_id' ]) ? $this->params[ 'bargain_id' ] : 0;
  217. if (empty($bargain_id)) {
  218. return $this->response($this->error('', 'REQUEST_ID'));
  219. }
  220. $bargain = new BargainModel();
  221. $res = $bargain->bargainBrowseInc([ [ 'bargain_id', '=', $bargain_id ], [ 'site_id', '=', $this->site_id ] ]);
  222. return $this->response($res);
  223. }
  224. public function share()
  225. {
  226. $token = $this->checkToken();
  227. $bargain_id = isset($this->params[ 'bargain_id' ]) ? $this->params[ 'bargain_id' ] : 0;
  228. if (empty($bargain_id)) {
  229. return $this->response($this->error('', 'REQUEST_ID'));
  230. }
  231. $bargain = new BargainModel();
  232. $res = $bargain->bargainShareInc([ [ 'bargain_id', '=', $bargain_id ], [ 'site_id', '=', $this->site_id ] ]);
  233. return $this->response($res);
  234. }
  235. }