Card.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\Card as CardModel;
  10. use app\allwin\model\CardUser;
  11. use app\allwin\model\CardUserOrder;
  12. use app\allwin\model\AllwinStore;
  13. use app\allwin\model\Coupon;
  14. use think\facade\Request;
  15. use think\helper\Time;
  16. class Card extends Common{
  17. public function initialize(){
  18. parent::initialize();
  19. $this->assign('pathMaps', [['name'=>'商家储值','url'=>url("card/index")]]);
  20. }
  21. /**
  22. * 列表
  23. */
  24. public function index(int $types = 0){
  25. $condition = [];
  26. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  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. $view['card_num'] = CardModel::where($condition)->count();
  56. $view['card_amount'] = CardModel::where($condition)->sum('price');
  57. $view['carduser_num'] = CardUser::where($condition)->count();
  58. $view['carduser_amount'] = CardUserOrder::where($condition)->where(['state' => 1])->sum('amount');
  59. $keyword = Request::param('keyword');
  60. if(!empty($keyword)){
  61. $condition[] = ['name','like','%'.$keyword.'%'];
  62. }
  63. $condition[] = ['is_lock','=',$types ? 1 : 0];
  64. $view['lists'] = CardModel::where($condition)->order('update_time desc')->paginate(10);
  65. $view['types'] = $types;
  66. $view['time'] = $time;
  67. $view['starttime'] = $starttime;
  68. $view['endtime'] = $endtime;
  69. $view['keyword'] = $keyword;
  70. return view()->assign($view);
  71. }
  72. //添加
  73. public function add(){
  74. if(request()->isAjax()){
  75. $data = [
  76. 'store_id' => Request::param('store_id/d'),
  77. 'coupon_id' => Request::param('coupon_id/d'),
  78. 'name' => Request::param('name/s'),
  79. 'price' => Request::param('price/f'),
  80. 'coupon_num' => Request::param('coupon_num/d',2),
  81. 'tips' => Request::param('tips/s'),
  82. 'relation_l1' => Request::param('relation_l1/d'),
  83. 'relation_l2' => Request::param('relation_l2/d'),
  84. 'member_miniapp_id' => $this->member_miniapp_id,
  85. ];
  86. $data['locktime'] = empty($data['locktime']) ? 0 : strtotime($data['locktime']);
  87. $validate = $this->validate($data,'Card.apicreate');
  88. if(true !== $validate){
  89. return json(['code'=>0,'msg'=>$validate]);
  90. }
  91. $store = AllwinStore::where(['id' => $data['store_id']])->find();
  92. if(empty($store)){
  93. return enjson(0,'好店不存在');
  94. }
  95. $data['create_time'] = time();
  96. $data['update_time'] = time();
  97. $result = CardModel::create($data);
  98. if($result){
  99. return enjson(200,'成功',['url'=>url('allwin/card/index')]);
  100. }else{
  101. return enjson(0,'操作失败');
  102. }
  103. }else{
  104. return view();
  105. }
  106. }
  107. //编辑
  108. public function edit(){
  109. if(request()->isAjax()){
  110. $data = [
  111. 'member_miniapp_id' => $this->member_miniapp_id,
  112. 'id' => Request::param('id/d'),
  113. 'store_id' => Request::param('store_id/d'),
  114. 'coupon_id' => Request::param('coupon_id/d'),
  115. 'name' => Request::param('name/s'),
  116. 'price' => Request::param('price/f'),
  117. 'coupon_num' => Request::param('coupon_num/d',2),
  118. 'tips' => Request::param('tips/s'),
  119. 'relation_l1' => Request::param('relation_l1/d'),
  120. 'relation_l2' => Request::param('relation_l2/d'),
  121. ];
  122. $validate = $this->validate($data,'Card.apiedit');
  123. if(true !== $validate){
  124. return json(['code'=>0,'msg'=>$validate]);
  125. }
  126. $store = AllwinStore::where(['id' => $data['store_id']])->find();
  127. if(empty($store)){
  128. return enjson(0,'好店不存在');
  129. }
  130. $data['update_time'] = time();
  131. $result = CardModel::update($data);
  132. if($result){
  133. return enjson(200,'成功',['url'=>url('allwin/card/index')]);
  134. }else{
  135. return enjson(0,'操作失败');
  136. }
  137. }else{
  138. $id = $this->request->param('id/d');
  139. $view['info'] = CardModel::where($this->mini_program)->where(['id' => $id])->find();
  140. if(empty($view['info'])){
  141. $this->error('内容不存在');
  142. }
  143. return view()->assign($view);
  144. }
  145. }
  146. //删除
  147. public function delete(){
  148. $id = $this->request->param('id/d');
  149. $count = CardUser::where($this->mini_program)->where(['card_id' => $id])->count();
  150. if($count){
  151. return json(['code'=>403,'msg'=>'已有用户开通,禁止删除,建议锁定.']);
  152. }else{
  153. CardModel::where($this->mini_program)->where(['id' => $id])->delete();
  154. return json(['code'=>200,'msg'=>'操作成功']);
  155. }
  156. }
  157. /**
  158. * 置顶/取消
  159. * @param integer $id 用户ID
  160. */
  161. public function isLock(int $id){
  162. $result = CardModel::isLock($id, $this->member_miniapp_id);
  163. if(!$result){
  164. return json(['code'=>0,'message'=>'操作失败']);
  165. }else{
  166. return json(['code'=>200,'message'=>'操作成功']);
  167. }
  168. }
  169. /**
  170. * 已开通商家的会员卡用户
  171. * @param integer $id 赠品ID
  172. * @param integer $uid 用户ID
  173. * @return void
  174. */
  175. public function user(){
  176. $condition = [];
  177. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  178. $store_id = Request::param('store_id/d', 0);
  179. $time = Request::param('time/d', 0);
  180. $starttime = Request::param('starttime/s');
  181. $endtime = Request::param('endtime/s');
  182. if($time){
  183. switch ($time) {
  184. case 2:
  185. list($start, $end) = Time::yesterday();
  186. break;
  187. case 30:
  188. list($start, $end) = Time::month();
  189. break;
  190. case 60:
  191. list($start, $end) = Time::lastMonth();
  192. break;
  193. default:
  194. list($start, $end) = Time::today();
  195. break;
  196. }
  197. $condition[] = ['create_time','>=',$start];
  198. $condition[] = ['create_time','<=',$end];
  199. }else{
  200. if($starttime){
  201. $condition[] = ['create_time','>=',strtotime($starttime)];
  202. }
  203. if($endtime){
  204. $condition[] = ['create_time','<=',strtotime($endtime)];
  205. }
  206. }
  207. $view['pathMaps'] = [['name' => '储值用户', 'url' => url("card/user")]];
  208. if (!empty($store_id)) {
  209. $condition[] = ['store_id', '=', $store_id];
  210. };
  211. $view['carduser_num'] = CardUser::where($condition)->count();
  212. $view['carduser_amount'] = CardUserOrder::where($condition)->where(['state' => 1])->sum('amount');
  213. $view['lists'] = CardUser::where($this->mini_program)->order('id asc')->paginate(20);
  214. $view['time'] = $time;
  215. $view['starttime'] = $starttime;
  216. $view['endtime'] = $endtime;
  217. $view['store_id'] = $store_id;
  218. return $this->fetch()->assign($view);
  219. }
  220. /**
  221. * 删除某个用户的会员卡
  222. * @return void
  223. */
  224. public function deleteUser(int $id){
  225. $result = CardUser::where($this->mini_program)->where(['id' => $id])->delete();
  226. if($result){
  227. return json(['code'=>200,'msg'=>'操作成功']);
  228. }else{
  229. return json(['code'=>403,'msg'=>'删除失败']);
  230. }
  231. }
  232. /**
  233. * 选择好店
  234. */
  235. public function selectStore(){
  236. $condition = [];
  237. $condition[] = ['is_lock','=',0];
  238. $keyword = $this->request->param('keyword');
  239. if(!empty($keyword)){
  240. $condition[] = ['name','like','%'.$keyword.'%'];
  241. }
  242. $view['input'] = $this->request->param('input','store_id');
  243. $view['lists'] = AllwinStore::where($this->mini_program)->where($condition)->order('sort desc,id desc')->paginate(20,false,['query' => ['input' => $view['input'],'keyword'=>$keyword]]);
  244. $view['keyword'] = $keyword;
  245. return view()->assign($view);
  246. }
  247. /**
  248. * 选择专属赠品(用户领取赠品主送)
  249. */
  250. public function selectConpon(int $store_id){
  251. $condition = [];
  252. $keyword = $this->request->param('keyword');
  253. if(!empty($keyword)){
  254. $condition[] = ['name','like','%'.$keyword.'%'];
  255. }
  256. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  257. $condition[] = ['store_id','=',$store_id];
  258. $condition[] = ['is_platform','=',1];
  259. $condition[] = ['is_lock','=',0];
  260. $condition[] = ['is_check','=',0];
  261. $condition[] = ['is_shop','=',0];
  262. $condition[] = ['is_vip','=',0];
  263. $view['input'] = $this->request->param('input/s');
  264. $view['store_id']= $store_id;
  265. $view['lists'] = Coupon::where($condition)->order('sort desc,id desc')->paginate(20,false,['query' => ['input' => $view['input'],'store_id'=>$store_id,'keyword'=>$keyword]]);
  266. $view['keyword'] = $keyword;
  267. return view()->assign($view);
  268. }
  269. /**
  270. * 选择赠品
  271. */
  272. public function coupon(int $id){
  273. $condition = [];
  274. $vip = CardModel::where(['id' => $id])->where($condition)->field('coupon_ids')->find();
  275. if(empty($vip)){
  276. $this->error('没有找到对应会员类型');
  277. }
  278. $view['lists'] = Coupon::where($this->mini_program)->whereIn('id',$vip->coupon_ids)->order('sort desc,id desc')->paginate(20,false,['query' => ['id' => $id]]);
  279. $view['card_id'] = $id;
  280. return view()->assign($view);
  281. }
  282. /**
  283. * 弹出选择优惠券
  284. */
  285. public function winCoupon(int $card_id){
  286. if(request()->isAjax()){
  287. $ids = $this->request->param('ids');
  288. if(empty($ids)){
  289. return json(['code'=>0,'msg'=>'请选择要关联的赠品']);
  290. }
  291. $result = CardModel::editCoupon($card_id,(array)ids($ids,true));
  292. if($result){
  293. return json(['code'=>302,'msg'=>'关联赠品成功','data' =>[]]);
  294. }else{
  295. return json(['code'=>0,'msg'=>'关联赠品失败']);
  296. }
  297. }else{
  298. $coupon = CardModel::where($this->mini_program)->where(['id' => $card_id])->field('coupon_ids')->find();
  299. $condition = [];
  300. if(!empty($coupon->coupon_ids)){
  301. $coupon_ids = explode(',',$coupon->coupon_ids);
  302. $condition[] = ['id','notIn',$coupon_ids];
  303. }
  304. $keyword = $this->request->param('keyword');
  305. if(!empty($keyword)){
  306. $condition[] = ['name','like','%'.$keyword.'%'];
  307. }
  308. $condition[] = ['is_platform','=',0];
  309. $view['lists'] = Coupon::where($this->mini_program)->where($condition)->order('size desc,id desc')->paginate(20,false,['query' => ['card_id' => $card_id,'keyword'=>$keyword]]);
  310. $view['card_id'] = $card_id;
  311. $view['keyword'] = $keyword;
  312. return view()->assign($view);
  313. }
  314. }
  315. //删除
  316. public function delCoupon(){
  317. $card_id = $this->request->param('card_id/d');
  318. $coupon_id = $this->request->param('coupon_id/d');
  319. $info = CardModel::where(['id' => $card_id])->find();
  320. if($info){
  321. $coupon_ids = ids(array_values_unset($coupon_id,explode(',',$info->coupon_ids)));
  322. $result = CardModel::where($this->mini_program)->where(['id' => $card_id])->update(['coupon_ids' => $coupon_ids]);
  323. if($result){
  324. return json(['code' =>200,'msg'=>'操作成功']);
  325. }
  326. }
  327. return json(['code' => 0,'msg'=>'操作失败']);
  328. }
  329. }