CouponList.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\store\controller;
  15. use app\store\service\ExtendService;
  16. use library\Controller;
  17. use library\tools\Data;
  18. use think\Db;
  19. /**
  20. * 优惠券列表
  21. * Class Config
  22. * @package app\store\controller
  23. */
  24. class CouponList extends Controller
  25. {
  26. /**
  27. * 绑定数据表
  28. * @var string
  29. */
  30. protected $table = 'StoreCouponList';
  31. /**
  32. * 优惠券管理
  33. * @auth true
  34. * @menu true
  35. * @throws \think\Exception
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. * @throws \think\exception\DbException
  39. * @throws \think\exception\PDOException
  40. */
  41. public function index()
  42. {
  43. $this->title = '优惠券列表';
  44. $coupon_id=$this->request->get('coupon_id');
  45. $this->byWhere(1)
  46. ->field('a.*,b.name,b.headimg')
  47. ->where('a.config_id',$coupon_id)
  48. ->order('a.id desc')
  49. ->page();
  50. }
  51. /**
  52. * 搜索条件
  53. * @return \library\helper\QueryHelper
  54. */
  55. protected function byWhere($type)
  56. {
  57. if ($type == 1) {
  58. $query = $this->_query($this->table);
  59. } elseif ($type == 2) {
  60. $query = Db::name($this->table);
  61. }
  62. $query = $query->alias('a')
  63. ->join('store_member b', 'a.user_id=b.id');
  64. if (isset($_GET['status']) && $_GET['status']) {
  65. $query->where('a.status', $_GET['status']);
  66. }
  67. if (isset($_GET['title']) && $_GET['title']) {
  68. $query->where('a.title', 'like','%'.$_GET['title'].'%');
  69. }
  70. if (isset($_GET['user_info']) && $_GET['user_info']) {
  71. $query->where('b.name|b.phone', 'like', '%' . $_GET['user_info'] . '%');
  72. }
  73. return $query;
  74. }
  75. /**
  76. * 数据列表处理
  77. * @param array $data
  78. * @throws \think\db\exception\DataNotFoundException
  79. * @throws \think\db\exception\ModelNotFoundException
  80. * @throws \think\exception\DbException
  81. */
  82. protected function _index_page_filter(&$data)
  83. {
  84. foreach ($data as $k=>&$v) {
  85. if($v['status'] == 0 && $v['end'] < date('Y-m-d')){
  86. Db::name('store_coupon_list')->where('id',$v['id'])->update(array('status'=>2));
  87. $v['status'] = 2;
  88. }
  89. $v['member'] = Db::table('store_member')->where('id',$v['user_id'])->find();
  90. }
  91. }
  92. /**
  93. * 添加优惠券
  94. * @auth true
  95. * @throws \think\Exception
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. * @throws \think\exception\PDOException
  100. */
  101. public function add()
  102. {
  103. $this->title = '添加优惠券';
  104. if($this->request->isPost()){
  105. if($this->request->post('user')==0){
  106. $user_ids=explode(',',$this->request->post('user_ids'));
  107. //指定用户发放
  108. }else{
  109. //全部用户发方
  110. $user_ids=Db::table('store_member')->where('status',1)->column('id');
  111. }
  112. if(empty($user_ids[0])){
  113. $this->error('没有要发放的用户');
  114. }
  115. $coupon=Db::table('store_coupon_config')->where('id',$this->request->post('coupon_id'))->where('status',1)->where('is_deleted',0)->find();
  116. if(empty($coupon)){
  117. $this->error('优惠券不存在或已禁用');
  118. }
  119. $error=[];
  120. foreach ($user_ids as $k=>$v){
  121. $data=[];
  122. $data['config_id']=$coupon['id'];
  123. $data['user_id']=$v;
  124. $data['title']=$coupon['title'];
  125. $data['low_amount']=$coupon['low_amount'];
  126. $data['amount']=$coupon['amount'];
  127. $data['start']=date('Y-m-d');
  128. $data['end']=date('Y-m-d', strtotime('+'.$coupon['low_day'].' days') );
  129. $data['status']=0;
  130. Db::table('store_coupon_list')->insert($data);
  131. }
  132. $msg='发放成功';
  133. if($error){
  134. $name=Db::table('store_member')->where('id','in',$error)->column('name');
  135. $name=implode(',',$name);
  136. $msg='发放成功'.$name.'等用户因规则原因,发放失败';
  137. }
  138. $this->success($msg);
  139. }else{
  140. $this->_form($this->table, 'form');
  141. }
  142. }
  143. /**
  144. * 编辑优惠券
  145. * @auth true
  146. * @throws \think\Exception
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. * @throws \think\exception\PDOException
  151. */
  152. public function edit()
  153. {
  154. $this->title = '编辑优惠券';
  155. $this->_form($this->table, 'form');
  156. }
  157. /**
  158. * 表单数据处理
  159. * @param array $data
  160. * @throws \think\Exception
  161. * @throws \think\db\exception\DataNotFoundException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. * @throws \think\exception\DbException
  164. * @throws \think\exception\PDOException
  165. */
  166. protected function _form_filter(&$data)
  167. {
  168. if($this->request->isPost()){
  169. }else{
  170. $coupon_id=$this->request->get('coupon_id');
  171. $this->coupon_id=$coupon_id;
  172. $this->member=Db::table('store_member')->field('id,phone,name')->where('status',1)->select();
  173. $this->member_count=count($this->member);
  174. }
  175. }
  176. /**
  177. * 禁用优惠券
  178. * @auth true
  179. * @throws \think\Exception
  180. * @throws \think\exception\PDOException
  181. */
  182. public function forbid()
  183. {
  184. $this->_save($this->table, ['status' => '0']);
  185. }
  186. /**
  187. * 启用优惠券
  188. * @auth true
  189. * @throws \think\Exception
  190. * @throws \think\exception\PDOException
  191. */
  192. public function resume()
  193. {
  194. $this->_save($this->table, ['status' => '1']);
  195. }
  196. /**
  197. * 删除优惠券
  198. * @auth true
  199. * @throws \think\Exception
  200. * @throws \think\exception\PDOException
  201. */
  202. public function remove()
  203. {
  204. $this->_delete($this->table);
  205. }
  206. }