MemberMarketmanage.php 15 KB

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