Coupon.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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\allwin\controller;
  9. use app\allwin\model\AllwinShop;
  10. use app\allwin\model\Coupon as CouponModel;
  11. use app\allwin\model\CouponSubsidize;
  12. use app\allwin\model\CouponUser;
  13. use app\allwin\model\Card;
  14. use app\common\facade\Inform;
  15. use think\facade\Request;
  16. use think\helper\Time;
  17. use app\allwin\model\AllwinStore;
  18. class Coupon extends Common{
  19. public function initialize() {
  20. parent::initialize();
  21. $this->assign('pathMaps', [['name'=>'优惠券管理','url'=>url("coupon/index")]]);
  22. }
  23. /**
  24. * 列表
  25. */
  26. public function index(int $types = 0,int $page = 0){
  27. $condition = [];
  28. $time = Request::param('time/d',0);
  29. $starttime = Request::param('starttime/s');
  30. $endtime = Request::param('endtime/s');
  31. if($time){
  32. switch ($time) {
  33. case 2:
  34. list($start, $end) = Time::yesterday();
  35. break;
  36. case 30:
  37. list($start, $end) = Time::month();
  38. break;
  39. case 60:
  40. list($start, $end) = Time::lastMonth();
  41. break;
  42. default:
  43. list($start, $end) = Time::today();
  44. break;
  45. }
  46. $condition[] = ['create_time','>=',$start];
  47. $condition[] = ['create_time','<=',$end];
  48. }else{
  49. if($starttime){
  50. $condition[] = ['create_time','>=',strtotime($starttime)];
  51. }
  52. if($endtime){
  53. $condition[] = ['create_time','<=',strtotime($endtime)];
  54. }
  55. }
  56. $store_id = Request::param('store_id/d');
  57. $store_name = Request::param('store_name');
  58. if($store_id){
  59. $condition[] = ['store_id','=',$store_id];
  60. }
  61. $keyword = Request::param('keyword');
  62. if(!empty($keyword)){
  63. $condition[] = ['name','like','%'.$keyword.'%'];
  64. }
  65. //统计
  66. $view['coupon_count'] = CouponModel::where($this->mini_program)->where($condition)->where(['is_end' => 0,'is_lock' => 0])->count();
  67. $view['coupon_end_count'] = CouponModel::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  68. $view['coupon_user_count'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  69. $view['coupon_user_end_count'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  70. switch ($types) {
  71. case 1:
  72. $condition[] = ['is_lock','=',1];
  73. $condition[] = ['is_end','=',0];
  74. break;
  75. case 2:
  76. $condition[] = ['is_lock','=',0];
  77. $condition[] = ['is_end','=',0];
  78. break;
  79. case 3:
  80. $condition[] = ['is_platform','=',1];
  81. $condition[] = ['is_end','=',0];
  82. break;
  83. case 4:
  84. $condition[] = ['is_end','=',1];
  85. break;
  86. default;
  87. $condition[] = ['is_end','=',0];
  88. break;
  89. }
  90. $view['lists'] = CouponModel::where($this->mini_program)->where($condition)->order('is_platform desc,sort desc,is_lock desc,update_time desc')->paginate(10,false,['query' => ['types' => $types,'store_id' => $store_id,'store_name' => $store_name]]);
  91. $view['types'] = $types;
  92. $view['page'] = $page;
  93. $view['keyword'] = $keyword;
  94. $view['store_id'] = $store_id;
  95. $view['store_name'] = $store_name;
  96. $view['time'] = $time;
  97. $view['starttime'] = $starttime;
  98. $view['endtime'] = $endtime;
  99. return view()->assign($view);
  100. }
  101. /**
  102. * 优惠券选项
  103. */
  104. public function selectId(){
  105. $view['store_id'] = $this->request->param('store_id/d',0);
  106. $view['input'] = $this->request->param('input/s');
  107. $view['keyword'] = $this->request->param('keyword');
  108. $view['is_platform'] = $this->request->param('is_platform/d',0);
  109. $view['is_lock'] = $this->request->param('is_lock/d',0);
  110. $view['is_check'] = $this->request->param('is_check/d',0);
  111. $view['is_shop'] = $this->request->param('is_shop/d',0);
  112. $condition = [];
  113. $condition[] = ['is_lock', '=',$view['is_lock']];
  114. $condition[] = ['is_check','=',$view['is_check']];
  115. $condition[] = ['is_shop', '=',$view['is_lock'] ];
  116. $condition[] = ['is_platform','=',$view['is_platform'] ? 1 : 0];
  117. if($view['store_id']){
  118. $condition[] = ['store_id','=',$view['store_id']];
  119. }
  120. $view['lists'] = CouponModel::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']]]);
  121. return view()->assign($view);
  122. }
  123. //优惠券预览和统计
  124. public function review(int $id,int $types = 0){
  125. $info = CouponModel::where($this->mini_program)->where(['id' => $id])->find();
  126. if(empty($info)){
  127. $this->error('内容不存在');
  128. }
  129. $info->weekday = json_decode($info->weekday,true);
  130. $condition = [];
  131. $condition['is_end'] = $types ?? 0;
  132. $condition['coupon_id'] = $id;
  133. $view['lists'] = CouponUser::where($this->mini_program)->where($condition)->order('id asc')->paginate(10,false,['query'=>['id' => $id,'types' => $types]]);
  134. $view['types'] = $types;
  135. $view['coupon_count'] = CouponUser::where($this->mini_program)->where(['coupon_id' => $id])->count();
  136. $view['coupon_end_count'] = CouponUser::where($this->mini_program)->where(['coupon_id' => $id,'is_end' => 1])->count();
  137. $view['coupon_size'] = CouponUser::where($this->mini_program)->where(['coupon_id' => $id])->sum('size');
  138. $coupon_user_size = CouponUser::where($this->mini_program)->where(['coupon_id' => $id])->sum('user_size');
  139. $view['coupon_user_size'] = $view['coupon_size'] - $coupon_user_size;
  140. $view['info'] = $info;
  141. return view()->assign($view);
  142. }
  143. //编辑
  144. public function edit(){
  145. $id = Request::param('id/d');
  146. if(request()->isAjax()){
  147. $data = [
  148. 'id' => Request::param('id/d'),
  149. 'pay_price' => Request::param('pay_price/f'),
  150. 'shop_price' => Request::param('shop_price/f'),
  151. 'theme' => Request::param('theme/s'),
  152. 'name' => Request::param('name/s'),
  153. 'img' => Request::param('img/s'),
  154. 'size' => Request::param('size/f',0),
  155. 'price' => Request::param('price/f'),
  156. 'weekday' => Request::param('weekday/a',[]),
  157. 'discount' => Request::param('discount/d',0),
  158. 'longtime' => Request::param('longtime/d',0),
  159. 'daynum' => Request::param('daynum/d',0),
  160. 'num' => Request::param('num/d',0),
  161. 'ontypes' => Request::param('ontypes/d',0),
  162. 'howmuch' => Request::param('howmuch/d',0),
  163. 'starttime' => Request::param('starttime/s'),
  164. 'endtime' => Request::param('endtime/s'),
  165. 'tips' => Request::param('tips/s'),
  166. 'types' => Request::param('types/d',0),
  167. ];
  168. $validate = $this->validate($data,'Coupon.edit');
  169. if(true !== $validate){
  170. return json(['code'=>0,'msg'=>$validate]);
  171. }
  172. $data['weekday'] = json_encode(ids($data['weekday'],true));
  173. if($data['types'] == 0){
  174. $data['discount'] = 0;
  175. }else{
  176. $data['price'] = 0;
  177. }
  178. $data['starttime'] = strtotime($data['starttime']);
  179. $data['endtime'] = strtotime($data['endtime']);
  180. $data['is_check'] = 1;
  181. if($id){
  182. $data['id'] = $id;
  183. $result = CouponModel::where($this->mini_program)->where(['id' => $data['id']])->update($data);
  184. }else{
  185. $store_id = Request::param('store_id/d',0);
  186. $store = AllwinStore::where(['id' => $store_id])->find();
  187. if(empty($store)){
  188. return json(['code'=>0,'msg'=>'商家必须选择']);
  189. }
  190. $data['member_miniapp_id'] = $this->member_miniapp_id;
  191. $data['create_time'] = time();
  192. $data['num_of'] = $data['num'];
  193. $data['update_time'] = time();
  194. $data['store_id'] = $store_id;
  195. $data['cate_id'] = $store->cate_id;
  196. $data['cate_sid'] = $store->cate_sid;
  197. $data['is_top'] = 0;
  198. $result = CouponModel::create($data);
  199. }
  200. if($result){
  201. return json(['code'=>200,'url'=>url('coupon/index',['page' => input('get.page/d')]),'msg'=>'操作成功']);
  202. }else{
  203. return json(['code'=>0,'msg'=>'操作失败']);
  204. }
  205. }else{
  206. $info = CouponModel::where($this->mini_program)->where(['id' => $id])->find();
  207. if(!empty($info)){
  208. $info->weekday = json_decode($info->weekday,true);
  209. }
  210. $view['info'] = $info;
  211. $view['page'] = input('get.page/d');
  212. return view()->assign($view);
  213. }
  214. }
  215. //删除
  216. public function delete(int $id){
  217. $rel = CouponModel::where(['id' => $id])->field('is_platform')->find();
  218. if($rel->is_platform == 1){
  219. return enjson(0,'超级券,禁止删除');
  220. }
  221. $result = CouponModel::where($this->mini_program)->where(['id' => $id])->delete();
  222. if($result){
  223. CouponSubsidize::where($this->mini_program)->where(['coupon_id' => $id])->delete();
  224. return json(['code'=>200,'msg'=>'操作成功']);
  225. }else{
  226. return json(['code'=>403,'msg'=>'删除失败']);
  227. }
  228. }
  229. /**
  230. * 排序
  231. */
  232. public function sort(){
  233. if(request()->isAjax()){
  234. $data = [
  235. 'sort' => input('post.sort/d'),
  236. 'id' => input('post.id/d'),
  237. ];
  238. $validate = $this->validate($data,'Coupon.sort');
  239. if(true !== $validate){
  240. return json(['code'=>0,'msg'=>$validate]);
  241. }
  242. $result = CouponModel::where($this->mini_program)->where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
  243. if($result){
  244. return json(['code'=>200,'msg'=>'操作成功']);
  245. }else{
  246. return json(['code'=>0,'msg'=>'操作失败']);
  247. }
  248. }
  249. }
  250. /**
  251. * 置顶/取消
  252. * @param integer $id 用户ID
  253. */
  254. public function isTop(int $id){
  255. $rel = CouponModel::where(['id' => $id])->field('is_top,is_platform')->find();
  256. $rel->is_top = $rel->is_top ? 0 : 1;
  257. if($rel->is_platform == 1 && $rel->is_top == 1){
  258. return enjson(0,'超级券,禁止设置推荐');
  259. }
  260. $rel->save();
  261. return json(['code'=>200,'msg'=>'操作成功']);
  262. }
  263. /**
  264. * 审核和取消
  265. * @param integer $id 用户ID
  266. */
  267. public function isLock(int $id){
  268. $result = CouponModel::where(['id' => $id])->field('is_platform,is_lock,store_id')->find();
  269. $result->is_lock = $result->is_lock ? 0 : 1;
  270. if($result->is_platform == 1 && $result->is_lock == 1){
  271. return enjson(0,'超级券,禁止取消审核');
  272. }
  273. $rel = $result->save();
  274. if($rel){
  275. if($result->is_lock == 0){
  276. $store = AllwinStore::where('id','=',$result->store_id)->find();
  277. if($store){
  278. Inform::sms($store->manage_uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '优惠券申请','content' =>'您的优惠券申请已经通过审核','state' => '成功']);
  279. }
  280. }
  281. return json(['code'=>200,'msg'=>'操作成功']);
  282. }else{
  283. return json(['code'=>0,'msg'=>'操作失败']);
  284. }
  285. }
  286. /**
  287. * 设置默认
  288. * @param integer $id 用户ID
  289. */
  290. public function platform(int $id){
  291. $result = CouponModel::where(['id' => $id])->field('is_platform')->find();
  292. $result->is_platform = $result->is_platform ? 0 : 1;
  293. if($result->is_platform == 0){
  294. //简单平台券是否在其它地方使用
  295. $card = Card::where(['coupon_id' => $id])->count();
  296. if($card){
  297. return enjson(0,'在好店储值活动中有使用,禁止把优惠券设置为普通优惠券');
  298. }
  299. }else{
  300. $result->is_check = 0;
  301. $result->is_shop = 0;
  302. AllwinShop::where($this->mini_program)->where(['coupon_id' => $id])->update(['coupon_id' => 0]);
  303. }
  304. $result->save();
  305. return json(['code'=>200,'msg'=>'操作成功']);
  306. }
  307. /**
  308. * 允许消费买单
  309. * @param integer $id 用户ID
  310. */
  311. public function isCheck(int $id){
  312. $rel = CouponModel::where(['id' => $id])->field('is_check,is_platform')->find();
  313. $rel->is_check = $rel->is_check ? 0 : 1;
  314. if($rel->is_platform == 1 && $rel->is_check == 1){
  315. return enjson(0,'超级券,禁止设置消费买单');
  316. }
  317. $rel->save();
  318. return json(['code'=>200,'msg'=>'操作成功']);
  319. }
  320. /**
  321. * 允许优惠券铺子
  322. * @param integer $id 用户ID
  323. */
  324. public function isShop(int $id){
  325. $rel = CouponModel::where(['id' => $id])->field('is_shop,is_platform')->find();
  326. $rel->is_shop = $rel->is_shop ? 0 : 1;
  327. if($rel->is_platform == 1 && $rel->is_shop == 1){
  328. return enjson(0,'超级券,禁止设置允许单独领取');
  329. }
  330. $rel->save();
  331. return json(['code'=>200,'msg'=>'操作成功']);
  332. }
  333. /**
  334. * 过期或失效回复
  335. * @param integer $id 用户ID
  336. */
  337. public function isEnd(int $id){
  338. $rel = CouponModel::where($this->mini_program)->where(['id' => $id])->field('id,is_end,num,endtime')->find();
  339. if(!$rel){
  340. return json(['code'=>0,'msg'=>'未找到资源']);
  341. }else{
  342. if($rel->num <= 0 ){
  343. return enjson(0,"禁止恢复,剩余数量【{$rel->num}】");
  344. }
  345. if($rel->endtime <= Time::today()[0]){
  346. return enjson(0,"禁止恢复,结束日期【".date('Y-m-d',$rel->endtime)."】");
  347. }
  348. $rel->is_end = 0;
  349. $rel->save();
  350. return json(['code'=>200,'msg'=>'操作成功']);
  351. }
  352. }
  353. /**
  354. * 用户已领优惠券管理
  355. *
  356. * @param integer $id 优惠券ID
  357. * @param integer $uid 用户ID
  358. * @return void
  359. */
  360. public function user(int $types = 0){
  361. $condition = [];
  362. $uid = Request::param('uid/d');
  363. if($uid){
  364. $condition[] = ['uid','=',$uid];
  365. }
  366. $store_id = Request::param('store_id/d');
  367. $store_name = Request::param('store_name/s');
  368. if($store_id){
  369. $condition[] = ['store_id','=',$store_id];
  370. }
  371. $time = Request::param('time/d',0);
  372. if($time){
  373. switch ($time) {
  374. case 2:
  375. list($start, $end) = Time::yesterday();
  376. break;
  377. case 30:
  378. list($start, $end) = Time::month();
  379. break;
  380. case 60:
  381. list($start, $end) = Time::lastMonth();
  382. break;
  383. default:
  384. list($start, $end) = Time::today();
  385. break;
  386. }
  387. $condition[] = ['create_time','>=',$start];
  388. $condition[] = ['create_time','<=',$end];
  389. }
  390. $starttime = Request::param('starttime/s');
  391. $endtime = Request::param('endtime/s');
  392. if($starttime){
  393. $condition[] = ['create_time','>=',strtotime($starttime)];
  394. }
  395. if($endtime){
  396. $condition[] = ['create_time','<=',strtotime($endtime)];
  397. }
  398. $view['coupon_size'] = CouponUser::where($this->mini_program)->where($condition)->sum("size");
  399. $view['coupon_user_size'] = CouponUser::where($this->mini_program)->where($condition)->sum("user_size");
  400. $view['coupon_user'] = $view['coupon_size']-$view['coupon_user_size'];
  401. $view['coupon_count'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  402. $view['coupon_end_count'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  403. $view['lists'] = CouponUser::where($this->mini_program)->where($condition)->where(['is_end'=>$types ? 1 : 0])->order('id desc')->paginate(20,false,['query'=>['uid' => $uid,'types' => $types,'starttime' => $starttime,'endtime' => $endtime,'time'=>$time]]);
  404. $view['types'] = $types;
  405. $view['time'] = $time;
  406. $view['starttime'] = $starttime;
  407. $view['endtime'] = $endtime;
  408. $view['uid'] = $uid;
  409. $view['store_id'] = $store_id;
  410. $view['store_name'] = $store_name;
  411. $view['pathMaps'] = [['name'=>'已领优惠券','url'=>url("coupon/user")]];
  412. return view()->assign($view);
  413. }
  414. /**
  415. * 查看用户优惠券信息
  416. * @param integer $id
  417. * @return void
  418. */
  419. public function userCoupon(int $id = 0){
  420. $info = CouponUser::where($this->mini_program)->where(['id' => $id])->find();
  421. $info['weekday'] = weekdays(json_decode($info->weekday,true));
  422. $view['info'] = $info;
  423. return view()->assign($view);
  424. }
  425. /**
  426. * 删除用户的优惠券
  427. * @return void
  428. */
  429. public function deleteUser(int $id){
  430. $result = CouponUser::where($this->mini_program)->where(['id' => $id])->delete();
  431. if($result){
  432. return json(['code'=>200,'msg'=>'操作成功']);
  433. }else{
  434. return json(['code'=>403,'msg'=>'删除失败']);
  435. }
  436. }
  437. }