MemberBargain.php 9.4 KB

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