MemberBargain.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Db;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 用户砍价控制器
  11. */
  12. class MemberBargain extends MobileMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberbargain.lang.php');
  18. }
  19. /**
  20. * @api {POST} api/MemberBargain/add 发起砍价
  21. * @apiVersion 1.0.0
  22. * @apiGroup MemberBargain
  23. *
  24. * @apiHeader {String} X-DS-KEY 用户授权token
  25. *
  26. * @apiParam {Int} bargain_id 砍价id
  27. *
  28. * @apiSuccess {String} code 返回码,10000为成功
  29. * @apiSuccess {String} message 返回消息
  30. * @apiSuccess {Object} result 返回数据
  31. * @apiSuccess {Int} result.bargainorder_id 用户砍价id
  32. */
  33. public function add()
  34. {
  35. $bargain_id = input('param.bargain_id');
  36. if (!$bargain_id) {
  37. ds_json_encode(10001, lang('param_error'));
  38. }
  39. $pbargain_model = model('pbargain');
  40. $bargain_info = $pbargain_model->getOnlineBargainInfoByID($bargain_id);
  41. if (!$bargain_info) {
  42. ds_json_encode(10001, lang('bargain_not_exist'));
  43. }
  44. if ($bargain_info['member_id'] == $this->member_info['member_id']) {
  45. ds_json_encode(10001, lang('bargain_add_self'));
  46. }
  47. //是否已添加过正在进行的砍价活动
  48. $pbargainorder_model = model('pbargainorder');
  49. if ($pbargainorder_model->getOnePbargainorder(array(array('bargainorder_initiator_id', '=', $this->member_info['member_id']), array('bargain_id', '=', $bargain_info['bargain_id']), array('bargainorder_state', 'in', array(1, 2))))) {
  50. ds_json_encode(10001, lang('bargain_already_add'));
  51. }
  52. $bargainorder_id = $pbargainorder_model->addPbargainorder(array(
  53. 'bargainorder_initiator_id' => $this->member_info['member_id'],
  54. 'bargainorder_initiator_name' => $this->member_info['member_name'],
  55. 'store_id' => $bargain_info['store_id'],
  56. 'bargain_id' => $bargain_info['bargain_id'],
  57. 'bargain_name' => $bargain_info['bargain_name'],
  58. 'bargain_total' => $bargain_info['bargain_total'],
  59. 'bargain_goods_id' => $bargain_info['bargain_goods_id'],
  60. 'bargain_goods_name' => $bargain_info['bargain_goods_name'],
  61. 'bargain_goods_price' => $bargain_info['bargain_goods_price'],
  62. 'bargain_goods_image' => $bargain_info['bargain_goods_image'],
  63. 'bargainorder_current_price' => $bargain_info['bargain_goods_price'],
  64. 'bargainorder_begintime' => TIMESTAMP,
  65. 'bargainorder_endtime' => TIMESTAMP + $bargain_info['bargain_time'] * 3600,
  66. 'bargainorder_state' => 1,
  67. ));
  68. if (!$bargainorder_id) {
  69. ds_json_encode(10001, lang('ds_common_op_fail'));
  70. }
  71. ds_json_encode(10000, '', array('bargainorder_id' => $bargainorder_id));
  72. }
  73. /**
  74. * @api {POST} api/MemberBargain/add_log 用户砍价
  75. * @apiVersion 1.0.0
  76. * @apiGroup MemberBargain
  77. *
  78. * @apiHeader {String} X-DS-KEY 用户授权token
  79. *
  80. * @apiParam {Int} bargainorder_id 用户砍价id
  81. *
  82. * @apiSuccess {String} code 返回码,10000为成功
  83. * @apiSuccess {String} message 返回消息
  84. * @apiSuccess {Object} result 返回数据
  85. * @apiSuccess {Boolean} result.success 砍价是否成功
  86. * @apiSuccess {Float} result.price 砍价金额
  87. * @apiSuccess {Float} result.now_price 当前价格
  88. */
  89. public function add_log()
  90. {
  91. $bargainorder_id = input('param.bargainorder_id');
  92. if (!$bargainorder_id) {
  93. ds_json_encode(10001, lang('param_error'));
  94. }
  95. $pbargain_model = model('pbargain');
  96. $pbargainorder_model = model('pbargainorder');
  97. $pbargainlog_model = model('pbargainlog');
  98. Db::startTrans();
  99. $success = false; //砍价是否成功
  100. try {
  101. //是否正在进行的砍价活动
  102. $pbargainorder_info = $pbargainorder_model->getOnePbargainorder(array(array('bargainorder_id', '=', $bargainorder_id), array('bargainorder_state', '=', 1), array('bargainorder_endtime', '>', TIMESTAMP)), true);
  103. if (!$pbargainorder_info) {
  104. throw new \think\Exception(lang('bargain_not_exist'), 10006);
  105. }
  106. //不能帮自己砍
  107. if ($pbargainorder_info['bargainorder_initiator_id'] == $this->member_info['member_id']) {
  108. throw new \think\Exception(lang('bargain_self'), 10006);
  109. }
  110. //不能重复砍
  111. if ($pbargainlog_model->getOnePbargainlog(array('pbargainlog_member_id' => $this->member_info['member_id'], 'bargainorder_id' => $pbargainorder_info['bargainorder_id']))) {
  112. throw new \think\Exception(lang('bargain_repeat'), 10006);
  113. }
  114. $bargain_info = $pbargain_model->getOnlineBargainInfoByID($pbargainorder_info['bargain_id']);
  115. if (!$bargain_info) {
  116. throw new \think\Exception(lang('bargain_not_exist'), 10006);
  117. }
  118. //计算砍价金额
  119. $left_price = $pbargainorder_info['bargainorder_current_price'] - $bargain_info['bargain_floorprice'];
  120. $left_times = $pbargainorder_info['bargain_total'] - $pbargainorder_info['bargainorder_times'];
  121. if (($left_times - 1) > 0) {
  122. $max_price = ($left_price - ($left_times - 1) * 0.01) / ($left_times - 1);
  123. $price = round(mt_rand(1, intval($max_price * 100)) / 100, 2);
  124. $price = min(array($price, $bargain_info['bargain_max']));
  125. $now_price = $pbargainorder_info['bargainorder_current_price'] - $price;
  126. if (!$pbargainorder_model->editPbargainorder(array('bargainorder_id' => $pbargainorder_info['bargainorder_id']), array('bargainorder_times' => $pbargainorder_info['bargainorder_times'] + 1, 'bargainorder_current_price' => $now_price))) {
  127. throw new \think\Exception(lang('bargain_fail'), 10006);
  128. }
  129. } else {
  130. $price = $left_price;
  131. $now_price = $bargain_info['bargain_floorprice'];
  132. if (!$pbargainorder_model->editPbargainorder(array('bargainorder_id' => $pbargainorder_info['bargainorder_id']), array('bargainorder_state' => 2, 'bargainorder_times' => $pbargainorder_info['bargainorder_times'] + 1, 'bargainorder_current_price' => $now_price))) {
  133. throw new \think\Exception(lang('bargain_fail'), 10006);
  134. }
  135. }
  136. //新增砍价记录
  137. if (!$pbargainlog_model->addPbargainlog(array(
  138. 'pbargainlog_member_id' => $this->member_info['member_id'],
  139. 'pbargainlog_member_name' => $this->member_info['member_name'],
  140. 'pbargainlog_price' => $price,
  141. 'pbargainlog_time' => TIMESTAMP,
  142. 'bargain_id' => $pbargainorder_info['bargain_id'],
  143. 'bargainorder_id' => $pbargainorder_info['bargainorder_id'],
  144. ))) {
  145. throw new \think\Exception(lang('bargain_log_add_fail'), 10006);
  146. }
  147. Db::commit();
  148. } catch (\Exception $e) {
  149. Db::rollback();
  150. ds_json_encode(10001, $e->getMessage());
  151. }
  152. ds_json_encode(10000, '', array('success' => $success, 'price' => $price, 'now_price' => $now_price));
  153. }
  154. /**
  155. * @api {POST} api/MemberBargain/get_list 砍价活动列表
  156. * @apiVersion 1.0.0
  157. * @apiGroup MemberBargain
  158. *
  159. * @apiHeader {String} X-DS-KEY 用户授权token
  160. *
  161. * @apiParam {Int} page 页码
  162. * @apiParam {Int} per_page 每页数量
  163. *
  164. * @apiSuccess {String} code 返回码,10000为成功
  165. * @apiSuccess {String} message 返回消息
  166. * @apiSuccess {Object} result 返回数据
  167. * @apiSuccess {Object[]} result.bargainorder_list 砍价活动列表
  168. */
  169. public function get_list()
  170. {
  171. $pbargainorder_model = model('pbargainorder');
  172. $bargainorder_list = $pbargainorder_model->getPbargainorderList(array('bargainorder_initiator_id' => $this->member_info['member_id']), $this->pagesize);
  173. foreach ($bargainorder_list as $key => $val) {
  174. $bargainorder_list[$key]['bargain_goods_image_url'] = goods_cthumb($val['bargain_goods_image'], 480, $val['store_id']);
  175. }
  176. $result = array_merge(array('bargainorder_list' => $bargainorder_list), mobile_page($pbargainorder_model->page_info));
  177. ds_json_encode(10000, '', $result);
  178. }
  179. }