VoucherModel.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 优惠券模块
  6. * @author fish
  7. *
  8. */
  9. class VoucherModel {
  10. public function send_user_voucher_byId($voucher_id,$user_id,$check_count =false, $uniacid = '')
  11. {
  12. if($user_id <= 0)
  13. {
  14. return 4;
  15. }
  16. $voucher_info = M('lionfish_comshop_coupon')->where( array('id' => $voucher_id) )->find();
  17. if($check_count) {
  18. if($voucher_info['total_count'] !=-1 && $voucher_info['total_count'] <= $voucher_info['send_count']){
  19. return 1;//被抢光了
  20. }else {
  21. $get_count = M('lionfish_comshop_coupon_list')->where( array('voucher_id' => $voucher_id , 'user_id' => $user_id ) )->count();
  22. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  23. return 2;//已领过
  24. }
  25. }
  26. }
  27. //判断是否是新人专享的优惠券
  28. if( $voucher_info['is_new_man'] == 1 )
  29. {
  30. //检测是否购买过
  31. $od_status = "1,2,4,6,7,8,9,10,11,12,14";
  32. $buy_count = M('lionfish_comshop_order')->where(" order_status_id in ({$od_status}) and member_id=".$user_id )->count();
  33. if( !empty($buy_count) && $buy_count >0 )
  34. {
  35. return 4;
  36. }
  37. }
  38. //开始生产优惠券
  39. $begin_time = $voucher_info['begin_time'];
  40. $end_time = $voucher_info['end_time'];
  41. if( $voucher_info['timelimit'] == 0)
  42. {
  43. $begin_time = time();
  44. $end_time = time() + 3600 * $voucher_info['get_over_hour'];
  45. }
  46. $voucher = array(
  47. 'voucher_id' => $voucher_id,
  48. 'voucher_title' => $voucher_info['voucher_title'],
  49. 'user_id' => $user_id,
  50. 'store_id' => 0,
  51. 'type' => $voucher_info['type'],
  52. 'credit' => $voucher_info['credit'],
  53. 'limit_money' => $voucher_info['limit_money'],
  54. 'is_limit_goods_buy' => $voucher_info['is_limit_goods_buy'],
  55. 'limit_goods_list' => $voucher_info['limit_goods_list'],
  56. 'goodscates' => $voucher_info['goodscates'],
  57. 'consume' => 'N',
  58. 'begin_time' => $begin_time,
  59. 'end_time' => $end_time,
  60. 'add_time'=>time(),
  61. );
  62. //user_id
  63. $id = M('lionfish_comshop_coupon_list')->add( $voucher );
  64. if($id){
  65. M()->execute("update ".C('DB_PREFIX')."lionfish_comshop_coupon set send_count=send_count+1 where id =".$voucher_id );
  66. }
  67. return 3;//领取成功
  68. }
  69. /**
  70. 优惠券活动页面领券
  71. **/
  72. public function send_user_voucher_byId_frombonus($voucher_id,$user_id,$check_count =false,$is_double = false)
  73. {
  74. $voucher_info = M('voucher')->where( array('id' => $voucher_id) )->find();
  75. if($check_count) {
  76. if($voucher_info['total_count'] <= $voucher_info['send_count']){
  77. return -1;//被抢光了
  78. }else {
  79. $get_count = M('voucher_list')->where( "voucher_id={$voucher_id} and user_id={$user_id} " )->count();
  80. if($voucher_info['person_limit_count'] > 0 && $voucher_info['person_limit_count'] <= $get_count) {
  81. return -2;//已领过
  82. }
  83. }
  84. }
  85. $voucher_list_one = M('voucher_list')->where( array('voucher_id' =>$voucher_id,'user_id' =>0 ) )->order('id desc')->find();
  86. if($voucher_list_one){
  87. $credit = $voucher_list_one['credit'];
  88. //get_over_hour
  89. if($is_double)
  90. {
  91. $credit = 2 * $voucher_list_one['credit'];
  92. }
  93. $end_time = $voucher_list_one['end_time'];
  94. if( $voucher_info['get_over_hour'] > 0)
  95. {
  96. $end_time = time() + intval(3600 * $voucher_info['get_over_hour']);
  97. }
  98. M('voucher')->where( array('id' => $voucher_id) )->setInc('send_count');
  99. M('voucher_list')->where( array('id' => $voucher_list_one['id']) )->save( array('user_id' => $user_id,'end_time' => $end_time ,'credit' => $credit) );
  100. }
  101. return $voucher_list_one['id'];//领取成功
  102. }
  103. /**
  104. * 获取用户可用给当前店铺商品支付的优惠券
  105. * @param unknown $user_id
  106. * @param unknown $store_id
  107. * @param unknown $total_money
  108. */
  109. public function get_user_canpay_voucher($user_id,$store_id,$total_money, $uniacid = '',$goods_ids = array())
  110. {
  111. //ims_
  112. $voucher_list = M()->query("select * from ".C('DB_PREFIX')."lionfish_comshop_coupon_list
  113. where (is_limit_goods_buy = 0 and (limit_money = 0 or (limit_money<=".$total_money.") ) ) and (store_id=".$store_id." or store_id=0) and user_id=".$user_id." and consume='N' and begin_time<".time().' and end_time >'.time() );
  114. //-----------
  115. if( empty($voucher_list) )
  116. {
  117. $voucher_list = array();
  118. }
  119. //判断是否有限制商品的券
  120. $voucher_list_goods = M('lionfish_comshop_coupon_list')->where( "is_limit_goods_buy = 1 and (store_id={$store_id} or store_id=0) and user_id={$user_id} and consume='N' and begin_time<".time().' and end_time >'.time() )->select();
  121. if( !empty($voucher_list_goods) )
  122. {
  123. foreach($voucher_list_goods as $gd_quan)
  124. {
  125. if( empty($gd_quan['limit_goods_list']) )
  126. {
  127. //(limit_money = 0 or (limit_money<=:total_money) )
  128. if($gd_quan['limit_money'] ==0 || $gd_quan['limit_money'] <= $total_money)
  129. {
  130. $voucher_list[] = $gd_quan;
  131. }
  132. }else{
  133. $voucher_goods_ids = explode(',', $gd_quan['limit_goods_list']);
  134. $voucher_goods_ids_total_money = 0;
  135. $is_in = false;
  136. foreach($goods_ids as $key_goods_id => $money_goods_id)
  137. {
  138. if( in_array($key_goods_id, $voucher_goods_ids ) )
  139. {
  140. $voucher_goods_ids_total_money += $money_goods_id;
  141. $is_in = true;
  142. }
  143. }
  144. if( $is_in && $voucher_goods_ids_total_money >= $gd_quan['limit_money'] )
  145. {
  146. $voucher_list[] = $gd_quan;
  147. }
  148. }
  149. }
  150. }
  151. //判断是否有限制商品分类的券
  152. $voucher_list_cate = M('lionfish_comshop_coupon_list')->where( "is_limit_goods_buy = 2 and (store_id={$store_id} or store_id=0) and user_id={$user_id} and consume='N' and begin_time<".time().' and end_time >'.time() )->select();
  153. if( !empty($voucher_list_cate) )
  154. {
  155. foreach($voucher_list_cate as $gd_quan)
  156. {
  157. if( empty($gd_quan['goodscates']) )
  158. {
  159. if($gd_quan['limit_money'] ==0 || $gd_quan['limit_money'] <= $total_money)
  160. {
  161. $voucher_list[] = $gd_quan;
  162. }
  163. }else{
  164. $voucher_goods_cate = $gd_quan['goodscates'];
  165. $voucher_goods_ids_total_money = 0;
  166. $is_in = false ;
  167. foreach($goods_ids as $key_goods_id => $money_goods_id)
  168. {
  169. $cate_gd_arr = M('lionfish_comshop_goods_to_category')->field('cate_id')->where( array('goods_id' => $key_goods_id) )->select();
  170. if( !empty($cate_gd_arr) )
  171. {
  172. foreach($cate_gd_arr as $cate_val)
  173. {
  174. if( $cate_val['cate_id'] == $voucher_goods_cate )
  175. {
  176. $is_in = true;
  177. $voucher_goods_ids_total_money += $money_goods_id;
  178. }
  179. }
  180. }
  181. }
  182. if($is_in && $voucher_goods_ids_total_money >= $gd_quan['limit_money'] )
  183. {
  184. $voucher_list[] = $gd_quan;
  185. }
  186. }
  187. }
  188. }
  189. //---------------
  190. if( !empty($voucher_list) )
  191. {
  192. foreach($voucher_list as $key => $val)
  193. {
  194. $val['begin_time'] = date('Y-m-d H:i:s', $val['begin_time']);
  195. $val['end_time'] = date('Y-m-d H:i:s', $val['end_time']);
  196. //---begin
  197. $coupon_info = M('lionfish_comshop_coupon')->where( array('id' =>$val['voucher_id'] ) )->find();
  198. if( $coupon_info['catid'] > 0 )
  199. {
  200. $cate_info = M('lionfish_comshop_coupon_category')->where( array('id' => $coupon_info['catid']) )->find();
  201. $val['cate_name'] = $cate_info['name'];
  202. }else{
  203. $val['cate_name'] = '';
  204. }
  205. //--end
  206. $voucher_list[$key] = $val;
  207. }
  208. }
  209. return $voucher_list;
  210. }
  211. }