Index.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 优惠券管理
  7. */
  8. namespace app\ais\controller\coupon;
  9. use app\ais\controller\Common;
  10. use app\ais\model\AisConfig;
  11. use app\ais\model\AisCoupon;
  12. use app\ais\model\AisCouponUser;
  13. use app\ais\model\AisCard;
  14. use app\ais\model\AisStore;
  15. use think\facade\Request;
  16. use think\helper\Time;
  17. class Index extends Common{
  18. public function initialize() {
  19. parent::initialize();
  20. $this->assign('pathMaps', [['name'=>'优惠券管理','url'=>url("coupon.index/index")]]);
  21. }
  22. /**
  23. * 列表
  24. */
  25. public function index(int $types = 0){
  26. $condition = [];
  27. $time = Request::param('time/d',0);
  28. $starttime = Request::param('starttime/s');
  29. $endtime = Request::param('endtime/s');
  30. if($time){
  31. switch ($time) {
  32. case 2:
  33. list($start, $end) = Time::yesterday();
  34. break;
  35. case 30:
  36. list($start, $end) = Time::month();
  37. break;
  38. case 60:
  39. list($start, $end) = Time::lastMonth();
  40. break;
  41. default:
  42. list($start, $end) = Time::today();
  43. break;
  44. }
  45. $condition[] = ['create_time','>=',$start];
  46. $condition[] = ['create_time','<=',$end];
  47. }else{
  48. if($starttime){
  49. $condition[] = ['create_time','>=',strtotime($starttime)];
  50. }
  51. if($endtime){
  52. $condition[] = ['create_time','<=',strtotime($endtime)];
  53. }
  54. }
  55. $store_id = Request::param('store_id/d');
  56. $store_name = Request::param('store_name');
  57. if($store_id){
  58. $condition[] = ['store_id','=',$store_id];
  59. }
  60. $keyword = Request::param('keyword');
  61. if(!empty($keyword)){
  62. $condition[] = ['name','like','%'.$keyword.'%'];
  63. }
  64. switch ($types) {
  65. case 1:
  66. $condition[] = ['is_lock','=',1];
  67. $condition[] = ['is_end','=',0];
  68. break;
  69. case 2:
  70. $condition[] = ['is_end','=',1];
  71. break;
  72. default;
  73. $condition[] = ['is_end','=',0];
  74. break;
  75. }
  76. $view['lists'] = AisCoupon::where($this->mini_program)->where($condition)->order('sort desc,is_lock desc,update_time desc')->paginate(10,false,['query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name]]);
  77. $view['types'] = $types;
  78. $view['keyword'] = $keyword;
  79. $view['store_id'] = $store_id;
  80. $view['store_name'] = $store_name;
  81. $view['time'] = $time;
  82. $view['starttime'] = $starttime;
  83. $view['endtime'] = $endtime;
  84. return view()->assign($view);
  85. }
  86. //优惠券预览和统计
  87. public function review(int $id,int $types = 0){
  88. $info = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();
  89. if(empty($info)){
  90. $this->error('内容不存在');
  91. }
  92. $condition = [];
  93. $condition['is_end'] = $types ?? 0;
  94. $condition['coupon_id'] = $id;
  95. $view['lists'] = AisCouponUser::where($this->mini_program)->where($condition)->order('id asc')->paginate(10,false,['query'=>['id' => $id,'types' => $types]]);
  96. $view['types'] = $types;
  97. $view['coupon_count'] = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id])->count();
  98. $view['coupon_end_count'] = AisCouponUser::where($this->mini_program)->where(['coupon_id' => $id,'is_end' => 1])->count();
  99. $view['info'] = $info;
  100. $config = AisConfig::getConfig($this->member_miniapp_id);
  101. $view['end_time'] = $config->coupon_longtime * 60 * 60;
  102. return view()->assign($view);
  103. }
  104. //编辑
  105. public function edit($id = 0){
  106. if(request()->isAjax()){
  107. $param = [
  108. 'id' => $this->request->param('id/d',0),
  109. 'name' => $this->request->param('name/s'),
  110. 'price' => $this->request->param('price/d'),
  111. 'types' => $this->request->param('types/d'),
  112. 'discount' => $this->request->param('discount/d',0),
  113. 'howmuch' => $this->request->param('howmuch/d',0),
  114. 'amount' => $this->request->param('amount/f',0),
  115. 'tips' => $this->request->param('tips/s'),
  116. 'num' => $this->request->param('num/d',0),
  117. ];
  118. $validate = $this->validate($param,'Coupon.edit');
  119. if(true !== $validate){
  120. return enjson(0,$validate);
  121. }
  122. $data['name'] = $param['name'];
  123. $data['price'] = $param['price'];
  124. $data['tips'] = $param['tips'];
  125. $data['num'] = $param['num'];
  126. $data['howmuch'] = $param['howmuch'];
  127. $data['types'] = $param['types'];;
  128. $data['price'] = 0;
  129. $data['amount'] = 0;
  130. $data['discount'] = 0;
  131. switch ($param['types'] ) {
  132. case 1: //折扣券
  133. $data['discount'] = $param['discount'];
  134. break;
  135. case 2: //兑换券
  136. $data['amount'] = $param['amount'];
  137. break;
  138. default: //代金券
  139. $data['price'] = $param['price'];
  140. break;
  141. }
  142. if($param['id']){
  143. $result = AisCoupon::where($this->mini_program)->where(['id' => $param['id']])->update($data);
  144. }else{
  145. $store_id = $this->request->param('store_id/d',0);
  146. $store = AisStore::where(['id' => $store_id])->find();
  147. if(empty($store)){
  148. return json(['code'=>0,'msg'=>'商家必须选择']);
  149. }
  150. $data['member_miniapp_id'] = $this->member_miniapp_id;
  151. $data['create_time'] = time();
  152. $data['update_time'] = time();
  153. $data['store_id'] = $store_id;
  154. $data['cate_id'] = $store->cate_id;
  155. $data['cate_sid'] = $store->cate_sid;
  156. $data['citycode'] = $store->citycode;
  157. $data['is_top'] = 0;
  158. $result = AisCoupon::create($data);
  159. }
  160. if($result){
  161. return enjson(200);
  162. }
  163. return enjson(0);
  164. }else{
  165. $view['info'] = AisCoupon::where($this->mini_program)->where(['id' => $id])->find();
  166. return view()->assign($view);
  167. }
  168. }
  169. //删除
  170. public function delete(int $id){
  171. if(AisCoupon::where($this->mini_program)->where(['id' => $id,'types' => 3])->find()){
  172. return enjson(0,'本券禁止删除');
  173. }
  174. $result = AisCoupon::where($this->mini_program)->where(['id' => $id])->delete();
  175. if($result){
  176. return enjson(200);
  177. }
  178. return enjson(0);
  179. }
  180. /**
  181. * 排序
  182. */
  183. public function sort(){
  184. if(request()->isAjax()){
  185. $data = [
  186. 'sort' => $this->request->param('sort/d'),
  187. 'id' => $this->request->param('id/d'),
  188. ];
  189. $validate = $this->validate($data,'Category.sort');
  190. if(true !== $validate){
  191. return enjson(0,$validate);
  192. }
  193. $result = AisCoupon::where($this->mini_program)->where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
  194. if($result){
  195. return enjson(200);
  196. }
  197. return enjson(0);
  198. }
  199. }
  200. /**
  201. * 置顶/取消
  202. * @param integer $id 用户ID
  203. */
  204. public function isTop(int $id){
  205. $result = AisCoupon::where(['id' => $id])->where('types','<','3')->field('is_top')->find();
  206. if(empty($result)){
  207. return enjson(0,'本券禁止操作');
  208. }
  209. $result->is_top = $result->is_top ? 0 : 1;
  210. $rel = $result->save();
  211. if($rel){
  212. return enjson(200);
  213. }
  214. return enjson(0);
  215. }
  216. /**
  217. * 审核和取消
  218. * @param integer $id 用户ID
  219. */
  220. public function isLock(int $id){
  221. $result = AisCoupon::where(['id' => $id])->where('types','<','3')->field('is_lock,store_id')->find();
  222. if(empty($result)){
  223. return enjson(0,'本券禁止操作');
  224. }
  225. $result->is_lock = $result->is_lock ? 0 : 1;
  226. $rel = $result->save();
  227. if($rel){
  228. return enjson(200);
  229. }
  230. return enjson(0);
  231. }
  232. /**
  233. * 优惠券选项
  234. */
  235. public function select(){
  236. $view['input'] = $this->request->param('input/s');
  237. $view['store_id'] = $this->request->param('store_id/d',0);
  238. $view['keyword'] = $this->request->param('keyword');
  239. $view['types'] = $this->request->param('types/d',0);
  240. $view['is_lock'] = $this->request->param('is_lock/d',0);
  241. $condition = [];
  242. $condition[] = ['is_lock', '=',0];
  243. if($view['types']){
  244. $condition[] = ['types','=',$view['types']];
  245. }else{
  246. $condition[] = ['types','<=',1];
  247. }
  248. if($view['store_id']){
  249. $condition[] = ['store_id','=',$view['store_id']];
  250. }
  251. $view['lists'] = AisCoupon::where($this->mini_program)->withSearch(['name'],['name' => $view['keyword']])->where($condition)->order('is_top desc,sort desc,is_lock desc,update_time desc')->paginate(10,false,['query' => ['keyword' => $view['keyword'],'store_id' => $view['store_id'],'input' => $view['input']]]);
  252. return view()->assign($view);
  253. }
  254. }