MemberMarketmanage.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 MemberMarketmanage extends MobileMember
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/membermarketmanage.lang.php');
  18. }
  19. /**
  20. * @api {POST} api/MemberMarketmanage/get_log 活动记录
  21. * @apiVersion 1.0.0
  22. * @apiGroup MemberMarketmanage
  23. *
  24. * @apiHeader {String} X-DS-KEY 用户授权token
  25. *
  26. * @apiSuccess {String} code 返回码,10000为成功
  27. * @apiSuccess {String} message 返回消息
  28. * @apiSuccess {Object} result 返回数据
  29. */
  30. public function get_log()
  31. {
  32. $marketmanage_model = model('marketmanage');
  33. $marketmanagelog_list = $marketmanage_model->getMarketmanageLogList(array('member_id' => $this->member_info['member_id']), $this->pagesize);
  34. $result = array_merge(array('log_list' => $marketmanagelog_list), mobile_page($marketmanage_model->page_info));
  35. ds_json_encode(10000, '', $result);
  36. }
  37. /**
  38. * @api {POST} api/MemberMarketmanage/add_log 参加活动
  39. * @apiVersion 1.0.0
  40. * @apiGroup MemberMarketmanage
  41. *
  42. * @apiHeader {String} X-DS-KEY 用户授权token
  43. *
  44. * @apiParam {Int} marketmanage_id 活动ID
  45. *
  46. * @apiSuccess {String} code 返回码,10000为成功
  47. * @apiSuccess {String} message 返回消息
  48. * @apiSuccess {Object} result 返回数据
  49. */
  50. public function add_log()
  51. {
  52. $can_draw = true;
  53. $count_left = 0;
  54. $marketmanage_id = intval(input('param.marketmanage_id'));
  55. if ($marketmanage_id < 0) {
  56. ds_json_encode(10001, lang('param_error'));
  57. }
  58. $marketmanage_model = model('marketmanage');
  59. $predeposit_model = model('predeposit');
  60. try {
  61. Db::startTrans();
  62. $condition = array();
  63. $condition[] = array('marketmanage_id', '=', $marketmanage_id);
  64. $marketmanage = $marketmanage_model->getOneMarketmanage($condition, true);
  65. if (!$marketmanage) {
  66. throw new \think\Exception(lang('market_activity_not_exist'), 10006);
  67. }
  68. if ($marketmanage['marketmanage_begintime'] > TIMESTAMP) {
  69. throw new \think\Exception(lang('market_activity_not_begin'), 10006);
  70. }
  71. if ($marketmanage['marketmanage_endtime'] < TIMESTAMP) {
  72. throw new \think\Exception(lang('market_activity_not_end'), 10006);
  73. }
  74. //是否有足够的积分
  75. if ($this->member_info['member_points'] < $marketmanage['marketmanage_point']) {
  76. throw new \think\Exception(lang('point_not_enough'), 10006);
  77. }
  78. if ($marketmanage['marketmanage_jointype'] != 2) { //有参与次数限制
  79. //判断当前用户是否参与过
  80. $condition = array();
  81. $condition[] = array('marketmanage_id', '=', $marketmanage_id);
  82. $condition[] = array('member_id', '=', $this->member_info['member_id']);
  83. $marketmanage_joincount = $marketmanage['marketmanage_joincount'];
  84. switch ($marketmanage['marketmanage_jointype']) {
  85. case 0:
  86. break;
  87. case 1:
  88. $condition[] = array('marketmanagelog_time', 'between', array(strtotime(date('Y-m-d 0:0:0')), TIMESTAMP));
  89. break;
  90. default:
  91. throw new \think\Exception(lang('marketmanage_jointype_set_error'), 10006);
  92. }
  93. $marketmanagelog = $marketmanage_model->getMarketmanageLogList($condition);
  94. if (count($marketmanagelog) >= $marketmanage_joincount) {
  95. throw new \think\Exception(sprintf(lang('marketmanage_joincount_error'), count($marketmanagelog)), 10006);
  96. }
  97. $count_left = $marketmanage_joincount - count($marketmanagelog) - 1;
  98. if ($count_left == 0) {
  99. $can_draw = false;
  100. }
  101. }
  102. $marketmanage_type_list = $marketmanage_model->marketmanage_type_list();
  103. if (!isset($marketmanage_type_list[$marketmanage['marketmanage_type']])) {
  104. throw new \think\Exception(lang('marketmanage_type_error'), 10006);
  105. }
  106. if ($marketmanage['marketmanage_point'] > 0) {
  107. //扣除会员积分
  108. $insert_arr = array();
  109. $insert_arr['pl_memberid'] = $this->member_info['member_id'];
  110. $insert_arr['pl_membername'] = $this->member_info['member_name'];
  111. $insert_arr['pl_points'] = -$marketmanage['marketmanage_point'];
  112. $insert_arr['pl_desc'] = sprintf(lang('marketmanage_sub_pl_desc'), $marketmanage_type_list[$marketmanage['marketmanage_type']]);
  113. $flag = model('points')->savePointslog('marketmanage', $insert_arr, true);
  114. if (!$flag) {
  115. throw new \think\Exception(lang('point_sub_fail'), 10006);
  116. }
  117. }
  118. //查看是否还有奖品
  119. $condition = 'marketmanage_id=' . $marketmanage_id . ' AND marketmanageaward_count>marketmanageaward_send AND marketmanageaward_probability>0';
  120. $marketmanageaward = $marketmanage_model->getMarketmanageAwardList($condition, true);
  121. if (empty($marketmanageaward)) {
  122. $result = array('draw_result' => false); //未中奖
  123. } else {
  124. $pro = array();
  125. $sum = 0;
  126. $marketmanageaward_list = array();
  127. foreach ($marketmanageaward as $val) {
  128. $sum += $val['marketmanageaward_probability'];
  129. $pro[] = array('marketmanageaward_probability' => $val['marketmanageaward_probability'], 'marketmanageaward_id' => $val['marketmanageaward_id']);
  130. $marketmanageaward_list[$val['marketmanageaward_id']] = $val;
  131. }
  132. $total_percent = count($pro) * 100;
  133. $pro[] = array('marketmanageaward_probability' => ($total_percent - $sum), 'marketmanageaward_id' => 0); //未中奖概率
  134. $pro = array_reverse($pro); //从未中奖到一等奖排序
  135. $sum = $total_percent;
  136. foreach ($pro as $v) {
  137. $r = mt_rand(1, $sum);
  138. if ($r <= $v['marketmanageaward_probability']) {
  139. if ($v['marketmanageaward_id'] == 0) {
  140. $result = array('draw_result' => false); //未中奖
  141. } else {
  142. $result = array('draw_result' => true, 'draw_info' => $marketmanageaward_list[$v['marketmanageaward_id']]); //已中奖
  143. }
  144. break;
  145. } else {
  146. $sum = max(0, $sum - $v['marketmanageaward_probability']);
  147. }
  148. }
  149. if ($result['draw_result']) { //中奖后续操作
  150. switch ($result['draw_info']['marketmanageaward_type']) {
  151. case 1: //奖励积分
  152. $result['draw_info']['marketmanageaward_text'] = $result['draw_info']['marketmanageaward_point'] . lang('ds_point');
  153. //扣除会员积分
  154. $insert_arr = array();
  155. $insert_arr['pl_memberid'] = $this->member_info['member_id'];
  156. $insert_arr['pl_membername'] = $this->member_info['member_name'];
  157. $insert_arr['pl_points'] = $result['draw_info']['marketmanageaward_point'];
  158. $insert_arr['pl_desc'] = sprintf(lang('marketmanage_add_pl_desc'), $marketmanage_type_list[$marketmanage['marketmanage_type']]);
  159. $flag = model('points')->savePointslog('marketmanage', $insert_arr, true);
  160. if (!$flag) {
  161. throw new \think\Exception(lang('point_add_fail'), 10006);
  162. }
  163. break;
  164. case 2: //奖励红包
  165. $bonus_model = model('bonus');
  166. $condition = array('bonus_id' => $result['draw_info']['bonus_id']);
  167. $bonus = $bonus_model->getOneBonus($condition); //获取当前红包的领取金额
  168. if (!$bonus) {
  169. throw new \think\Exception(lang('bonus_set_error'), 10006);
  170. }
  171. if ($bonus['bonus_type'] != 3) {
  172. throw new \think\Exception(lang('bonus_set_error'), 10006);
  173. }
  174. //获取未领取单个红包
  175. $condition = array();
  176. $condition[] = array('bonus_id', '=', $result['draw_info']['bonus_id']);
  177. $condition[] = array('member_id', '=', 0);
  178. $bonusreceive = $bonus_model->getOneBonusreceive($condition);
  179. if (empty($bonusreceive)) {
  180. throw new \think\Exception(lang('bonus_send_over'), 10006);
  181. }
  182. $result['draw_info']['marketmanageaward_text'] = $bonusreceive['bonusreceive_price'] . lang('bonus_fixedprice_notice');
  183. $res = $bonus_model->receiveBonus($this->member_info, $bonus, $bonusreceive, $marketmanage_type_list[$marketmanage['marketmanage_type']] . lang('bonus'));
  184. if (!$res['code']) {
  185. throw new \think\Exception($res['msg'], 10006);
  186. }
  187. break;
  188. case 3: //奖励优惠券
  189. $voucher_model = model('voucher');
  190. //验证是否可以兑换代金券
  191. $data = $voucher_model->getCanChangeTemplateInfo($result['draw_info']['vouchertemplate_id'], $this->member_info['member_id'], 0, true);
  192. if ($data['state'] == false) {
  193. throw new \think\Exception($data['msg'], 10006);
  194. }
  195. if ($data['info']['vouchertemplate_store_id'] == $this->member_info['store_id']) { //如果是自己店铺的代金券
  196. $result = array('draw_result' => false); //未中奖
  197. break;
  198. }
  199. $result['draw_info']['marketmanageaward_text'] = $data['info']['vouchertemplate_price'] . lang('vouchertemplate_notice');
  200. //添加代金券信息
  201. $data = $voucher_model->exchangeVoucher($data['info'], $this->member_info['member_id'], $this->member_info['member_name'], true);
  202. if ($data['state'] != true) {
  203. throw new \think\Exception($data['msg'], 10006);
  204. }
  205. break;
  206. default:
  207. throw new \think\Exception(lang('marketmanageaward_type_error'), 10006);
  208. }
  209. if ($result['draw_result']) {
  210. //增加中奖数量
  211. $condition = array();
  212. $condition[] = array('marketmanageaward_id', '=', $result['draw_info']['marketmanageaward_id']);
  213. $flag = $marketmanage_model->editMarketmanageAward($condition, array('marketmanageaward_send' => $result['draw_info']['marketmanageaward_send'] + 1));
  214. if (!$flag) {
  215. // echo $marketmanage_model->getLastSql();
  216. throw new \think\Exception(lang('marketmanageaward_send_update_fail'), 10006);
  217. }
  218. //更新中奖总数
  219. $condition = array();
  220. $condition[] = array('marketmanage_id', '=', $marketmanage_id);
  221. $flag = $marketmanage_model->editMarketmanage($condition, array('marketmanage_totalwin' => $marketmanage['marketmanage_totalwin'] + 1));
  222. if (!$flag) {
  223. throw new \think\Exception(lang('marketmanageaward_send_update_fail'), 10006);
  224. }
  225. }
  226. }
  227. //增加参与记录
  228. $flag = $marketmanage_model->addMarketmanageLog(array(
  229. 'member_id' => $this->member_info['member_id'],
  230. 'member_name' => $this->member_info['member_name'],
  231. 'marketmanage_id' => $marketmanage_id,
  232. 'marketmanageaward_id' => $result['draw_result'] ? $result['draw_info']['marketmanageaward_id'] : 0,
  233. 'marketmanagelog_win' => $result['draw_result'] ? 1 : 0,
  234. 'marketmanagelog_time' => TIMESTAMP,
  235. 'marketmanagelog_remark' => lang('join') . $marketmanage['marketmanage_name'] . ($result['draw_result'] ? (lang('draw') . $result['draw_info']['marketmanageaward_level'] . lang('prize')) : lang('not_draw')),
  236. ));
  237. if (!$flag) {
  238. throw new \think\Exception(lang('marketmanagelog_add_fail'), 10006);
  239. }
  240. //更新参与总数
  241. $condition = array();
  242. $condition[] = array('marketmanage_id', '=', $marketmanage_id);
  243. $flag = $marketmanage_model->editMarketmanage($condition, array('marketmanage_totalcount' => $marketmanage['marketmanage_totalcount'] + 1));
  244. if (!$flag) {
  245. throw new \think\Exception(lang('marketmanage_totalcount_update_fail'), 10006);
  246. }
  247. Db::commit();
  248. }
  249. } catch (\Exception $e) {
  250. Db::rollback();
  251. ds_json_encode(10001, $e->getMessage());
  252. }
  253. $result['can_draw'] = $can_draw;
  254. $result['count_left'] = $count_left;
  255. ds_json_encode(10000, '', $result);
  256. }
  257. }