Coupon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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\smartbc\controller;
  9. use app\smartbc\model\SmartbcConfig;
  10. use app\smartbc\model\SmartbcCoupon;
  11. use app\smartbc\model\SmartbcCouponUser;
  12. use app\smartbc\model\SmartbcCard;
  13. use app\common\facade\Inform;
  14. use think\facade\Request;
  15. use think\helper\Time;
  16. use app\smartbc\model\SmartbcStore;
  17. class Coupon extends Common{
  18. public function initialize() {
  19. parent::initialize();
  20. $this->assign('pathMaps', [['name'=>'优惠券管理','url'=>url("coupon/index")]]);
  21. }
  22. /**
  23. * 列表
  24. */
  25. public function index(int $types = 0,int $page = 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. //统计
  65. $view['coupon_count'] = SmartbcCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 0,'is_lock' => 0])->count();
  66. $view['coupon_end_count'] = SmartbcCoupon::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  67. $view['coupon_user_count'] = SmartbcCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  68. $view['coupon_user_end_count'] = SmartbcCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  69. switch ($types) {
  70. case 1:
  71. $condition[] = ['is_lock','=',1];
  72. $condition[] = ['is_end','=',0];
  73. break;
  74. case 2:
  75. $condition[] = ['is_lock','=',0];
  76. $condition[] = ['is_end','=',0];
  77. break;
  78. case 4:
  79. $condition[] = ['is_end','=',1];
  80. break;
  81. default;
  82. $condition[] = ['is_end','=',0];
  83. break;
  84. }
  85. $view['lists'] = SmartbcCoupon::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]]);
  86. $view['types'] = $types;
  87. $view['page'] = $page;
  88. $view['keyword'] = $keyword;
  89. $view['store_id'] = $store_id;
  90. $view['store_name'] = $store_name;
  91. $view['time'] = $time;
  92. $view['starttime'] = $starttime;
  93. $view['endtime'] = $endtime;
  94. return view()->assign($view);
  95. }
  96. //优惠券预览和统计
  97. public function review(int $id,int $types = 0){
  98. $info = SmartbcCoupon::where($this->mini_program)->where(['id' => $id])->find();
  99. if(empty($info)){
  100. $this->error('内容不存在');
  101. }
  102. $condition = [];
  103. $condition['is_end'] = $types ?? 0;
  104. $condition['coupon_id'] = $id;
  105. $view['lists'] = SmartbcCouponUser::where($this->mini_program)->where($condition)->order('id asc')->paginate(10,false,['query'=>['id' => $id,'types' => $types]]);
  106. $view['types'] = $types;
  107. $view['coupon_count'] = SmartbcCouponUser::where($this->mini_program)->where(['coupon_id' => $id])->count();
  108. $view['coupon_end_count'] = SmartbcCouponUser::where($this->mini_program)->where(['coupon_id' => $id,'is_end' => 1])->count();
  109. $view['info'] = $info;
  110. $config = SmartbcConfig::getConfig($this->member_miniapp_id);
  111. $view['end_time'] = $config->end_time * 60 * 60;
  112. return view()->assign($view);
  113. }
  114. //编辑
  115. public function edit(){
  116. $id = Request::param('id/d');
  117. if(request()->isAjax()){
  118. $data = [
  119. 'id' => Request::param('id/d'),
  120. 'name' => Request::param('name/s'),
  121. 'price' => Request::param('price/f'),
  122. 'discount' => Request::param('discount/d',0),
  123. 'howmuch' => Request::param('howmuch/d',0),
  124. 'tips' => Request::param('tips/s'),
  125. 'types' => Request::param('types/d',0),
  126. ];
  127. $validate = $this->validate($data,'Coupon.edit');
  128. if(true !== $validate){
  129. return json(['code'=>0,'msg'=>$validate]);
  130. }
  131. if($data['types'] == 0){
  132. $data['discount'] = 0;
  133. }else{
  134. $data['price'] = 0;
  135. }
  136. if($id){
  137. $data['id'] = $id;
  138. $result = SmartbcCoupon::where($this->mini_program)->where(['id' => $data['id']])->update($data);
  139. }else{
  140. $store_id = Request::param('store_id/d',0);
  141. $store = SmartbcStore::where(['id' => $store_id])->find();
  142. if(empty($store)){
  143. return json(['code'=>0,'msg'=>'商家必须选择']);
  144. }
  145. $data['member_miniapp_id'] = $this->member_miniapp_id;
  146. $data['create_time'] = time();
  147. $data['update_time'] = time();
  148. $data['store_id'] = $store_id;
  149. $data['cate_id'] = $store->cate_id;
  150. $data['cate_sid'] = $store->cate_sid;
  151. $data['is_top'] = 0;
  152. $result = SmartbcCoupon::create($data);
  153. }
  154. if($result){
  155. return json(['code'=>200,'url'=>url('coupon/index',['page' => input('get.page/d')]),'msg'=>'操作成功']);
  156. }else{
  157. return json(['code'=>0,'msg'=>'操作失败']);
  158. }
  159. }else{
  160. $info = SmartbcCoupon::where($this->mini_program)->where(['id' => $id])->find();
  161. $view['info'] = $info;
  162. $view['page'] = input('get.page/d');
  163. return view()->assign($view);
  164. }
  165. }
  166. //删除
  167. public function delete(int $id){
  168. $result = SmartbcCoupon::where($this->mini_program)->where(['id' => $id])->delete();
  169. if($result){
  170. return json(['code'=>200,'msg'=>'操作成功']);
  171. }else{
  172. return json(['code'=>403,'msg'=>'删除失败']);
  173. }
  174. }
  175. /**
  176. * 排序
  177. */
  178. public function sort(){
  179. if(request()->isAjax()){
  180. $data = [
  181. 'sort' => input('post.sort/d'),
  182. 'id' => input('post.id/d'),
  183. ];
  184. $validate = $this->validate($data,'Coupon.sort');
  185. if(true !== $validate){
  186. return json(['code'=>0,'msg'=>$validate]);
  187. }
  188. $result = SmartbcCoupon::where($this->mini_program)->where(['id' => $data['id']])->update(['sort'=>$data['sort']]);
  189. if($result){
  190. return json(['code'=>200,'msg'=>'操作成功']);
  191. }else{
  192. return json(['code'=>0,'msg'=>'操作失败']);
  193. }
  194. }
  195. }
  196. /**
  197. * 置顶/取消
  198. * @param integer $id 用户ID
  199. */
  200. public function isTop(int $id){
  201. $rel = SmartbcCoupon::where(['id' => $id])->field('is_top')->find();
  202. $rel->is_top = $rel->is_top ? 0 : 1;
  203. $rel->save();
  204. return json(['code'=>200,'msg'=>'操作成功']);
  205. }
  206. /**
  207. * 审核和取消
  208. * @param integer $id 用户ID
  209. */
  210. public function isLock(int $id){
  211. $result = SmartbcCoupon::where(['id' => $id])->field('is_lock,store_id')->find();
  212. $result->is_lock = $result->is_lock ? 0 : 1;
  213. $rel = $result->save();
  214. if($rel){
  215. if($result->is_lock == 0){
  216. $store = SmartbcStore::where('id','=',$result->store_id)->find();
  217. if($store){
  218. Inform::sms($store->manage_uid,$this->member_miniapp_id,['title' =>'业务进展通知','type' => '优惠券申请','content' =>'您的优惠券申请已经通过审核','state' => '成功']);
  219. }
  220. }
  221. return json(['code'=>200,'msg'=>'操作成功']);
  222. }else{
  223. return json(['code'=>0,'msg'=>'操作失败']);
  224. }
  225. }
  226. /**
  227. * 用户已领优惠券管理
  228. *
  229. * @param integer $id 优惠券ID
  230. * @param integer $uid 用户ID
  231. * @return void
  232. */
  233. public function user(int $types = 0){
  234. $condition = [];
  235. $uid = Request::param('uid/d');
  236. if($uid){
  237. $condition[] = ['uid','=',$uid];
  238. }
  239. $store_id = Request::param('store_id/d');
  240. $store_name = Request::param('store_name/s');
  241. if($store_id){
  242. $condition[] = ['store_id','=',$store_id];
  243. }
  244. $time = Request::param('time/d',0);
  245. if($time){
  246. switch ($time) {
  247. case 2:
  248. list($start, $end) = Time::yesterday();
  249. break;
  250. case 30:
  251. list($start, $end) = Time::month();
  252. break;
  253. case 60:
  254. list($start, $end) = Time::lastMonth();
  255. break;
  256. default:
  257. list($start, $end) = Time::today();
  258. break;
  259. }
  260. $condition[] = ['create_time','>=',$start];
  261. $condition[] = ['create_time','<=',$end];
  262. }
  263. $starttime = Request::param('starttime/s');
  264. $endtime = Request::param('endtime/s');
  265. if($starttime){
  266. $condition[] = ['create_time','>=',strtotime($starttime)];
  267. }
  268. if($endtime){
  269. $condition[] = ['create_time','<=',strtotime($endtime)];
  270. }
  271. $view['coupon_count'] = SmartbcCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 0])->count();
  272. $view['coupon_end_count'] = SmartbcCouponUser::where($this->mini_program)->where($condition)->where(['is_end' => 1])->count();
  273. $view['lists'] = SmartbcCouponUser::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]]);
  274. $view['types'] = $types;
  275. $view['time'] = $time;
  276. $view['starttime'] = $starttime;
  277. $view['endtime'] = $endtime;
  278. $view['uid'] = $uid;
  279. $view['store_id'] = $store_id;
  280. $view['store_name'] = $store_name;
  281. $view['pathMaps'] = [['name'=>'已领优惠券','url'=>url("coupon/user")]];
  282. $config = SmartbcConfig::getConfig($this->member_miniapp_id);
  283. $view['end_time'] = $config->end_time * 60 * 60;
  284. return view()->assign($view);
  285. }
  286. /**
  287. * 查看用户优惠券信息
  288. * @param integer $id
  289. * @return void
  290. */
  291. public function userCoupon(int $id = 0){
  292. $view['info'] = SmartbcCouponUser::where($this->mini_program)->where(['id' => $id])->find();
  293. return view()->assign($view);
  294. }
  295. /**
  296. * 删除用户的优惠券
  297. * @return void
  298. */
  299. public function deleteUser(int $id){
  300. $result = SmartbcCouponUser::where($this->mini_program)->where(['id' => $id])->delete();
  301. if($result){
  302. return json(['code'=>200,'msg'=>'操作成功']);
  303. }else{
  304. return json(['code'=>403,'msg'=>'删除失败']);
  305. }
  306. }
  307. }