Card.php 13 KB

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