Gift.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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\Manage;
  9. use app\allwin\controller\Common;
  10. use app\allwin\model\AllwinStore;
  11. use app\allwin\model\AllwinGift;
  12. use app\allwin\model\AllwinGiftData;
  13. use app\allwin\model\AllwinGiftStore;
  14. use think\facade\Request;
  15. use think\helper\Time;
  16. class Gift extends Common
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize();
  21. $this->assign('pathMaps', [['name'=>'支付有礼','url' => url('manage.gift/index')]]);
  22. }
  23. /**
  24. * 列表
  25. */
  26. public function index(){
  27. $condition = [];
  28. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  29. $view['lists'] = AllwinGift::where($condition)->order('id desc')->paginate(20);
  30. return view()->assign($view);
  31. }
  32. /**
  33. * 活动预览
  34. */
  35. public function review(int $id){
  36. $condition = [];
  37. $view['info'] = AllwinGift::where($this->mini_program)->where(['id' => $id])->where($condition)->find();
  38. if(!$view['info']){
  39. $this->error("404 NOT FOUND");
  40. }
  41. $view['pay_num'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->sum('pay_num');
  42. $view['pay_amount'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->sum('pay_amount');
  43. $view['gite_num'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->count();
  44. $view['coupon_num'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->where('coupon_id','>',0)->count();
  45. $view['take_num'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->where(['is_take' => 1])->count();
  46. $time = Request::param('time/d',0);
  47. $starttime = Request::param('starttime/s');
  48. $endtime = Request::param('endtime/s');
  49. if($time){
  50. switch ($time) {
  51. case 2:
  52. list($start, $end) = Time::yesterday();
  53. break;
  54. case 30:
  55. list($start, $end) = Time::month();
  56. break;
  57. case 60:
  58. list($start, $end) = Time::lastMonth();
  59. break;
  60. default:
  61. list($start, $end) = Time::today();
  62. break;
  63. }
  64. $condition[] = ['update_time','>=',$start];
  65. $condition[] = ['update_time','<=',$end];
  66. }else{
  67. if($starttime){
  68. $condition[] = ['update_time','>=',strtotime($starttime)];
  69. }
  70. if($endtime){
  71. $condition[] = ['update_time','<=',strtotime($endtime)];
  72. }
  73. }
  74. $types =Request::param('types/d',2);
  75. switch ($types) {
  76. case 1:
  77. $condition[] = ['coupon_id','=',0];
  78. break;
  79. case 2:
  80. $condition[] = ['coupon_id','>',0];
  81. break;
  82. case 3:
  83. $condition[] = ['coupon_id','>',0];
  84. $condition[] = ['is_take','=',0];
  85. break;
  86. case 4:
  87. $condition[] = ['coupon_id','>',0];
  88. $condition[] = ['is_take','=',1];
  89. break;
  90. }
  91. $view['list'] = AllwinGiftData::where(['gift_id' => $id])->where($condition)->order('id desc')->paginate(20);
  92. $view['types'] = $types;
  93. $view['time'] = $time;
  94. $view['starttime'] = $starttime;
  95. $view['endtime'] = $endtime;
  96. $view['id'] = $id;
  97. return view()->assign($view);
  98. }
  99. /**
  100. * 添加
  101. */
  102. public function add(){
  103. if(request()->isAjax()){
  104. $data = [
  105. 'member_miniapp_id' => $this->member_miniapp_id,
  106. 'types' => Request::param('types/d',0),
  107. 'pay_num' => Request::param('pay_num/d',0),
  108. 'pay_amount' => Request::param('pay_amount/f',0),
  109. 'coupon_id' => Request::param('coupon_id/d',0),
  110. 'title' => Request::param('title/s'),
  111. 'share_img' => Request::param('share_img/s'),
  112. 'share_title' => Request::param('share_title/s'),
  113. 'share_text' => Request::param('share_text/s'),
  114. 'note' => Request::param('note/s'),
  115. 'start_time' => Request::param('start_time/s'),
  116. 'end_time' => Request::param('end_time/s'),
  117. ];
  118. $validate = $this->validate($data,'Gift.add');
  119. if(true !== $validate){
  120. return json(['code'=>0,'msg'=>$validate]);
  121. }
  122. $result = AllwinGift::edit($data);
  123. if($result){
  124. return json(['code'=>200,'url'=>url('manage.gift/index'),'msg'=>'操作成功']);
  125. }else{
  126. return json(['code'=>0,'msg'=>'操作失败']);
  127. }
  128. }else{
  129. return view();
  130. }
  131. }
  132. //编辑
  133. public function edit(){
  134. if(request()->isAjax()){
  135. $data = [
  136. 'id' => Request::param('id/d'),
  137. 'member_miniapp_id' => $this->member_miniapp_id,
  138. 'types' => Request::param('types/d',0),
  139. 'pay_num' => Request::param('pay_num/d',0),
  140. 'pay_amount' => Request::param('pay_amount/f',0),
  141. 'coupon_id' => Request::param('coupon_id/d',0),
  142. 'title' => Request::param('title/s'),
  143. 'share_img' => Request::param('share_img/s'),
  144. 'share_title' => Request::param('share_title/s'),
  145. 'share_text' => Request::param('share_text/s'),
  146. 'note' => Request::param('note/s'),
  147. 'start_time' => Request::param('start_time/s'),
  148. 'end_time' => Request::param('end_time/s'),
  149. ];
  150. $validate = $this->validate($data,'Gift.edit');
  151. if(true !== $validate){
  152. return json(['code'=>0,'msg'=>$validate]);
  153. }
  154. $result = AllwinGift::edit($data);
  155. if($result){
  156. return json(['code'=>200,'url'=>url('manage.gift/index'),'msg'=>'操作成功']);
  157. }else{
  158. return json(['code'=>0,'msg'=>'操作失败']);
  159. }
  160. }else{
  161. $condition = [];
  162. $condition['id'] = Request::param('id');
  163. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  164. $view['info'] = AllwinGift::where($condition)->find();
  165. if(!$view['info']){
  166. $this->error("404 NOT FOUND");
  167. }
  168. return view()->assign($view);
  169. }
  170. }
  171. //删除
  172. public function delete(int $id){
  173. $condition = [];
  174. $result = AllwinGiftAllwinStore::where($this->mini_program)->where($condition)->where(['store_id' => $id])->find();
  175. if($result){
  176. return json(['code'=>0,'msg'=>'禁止删除,请先删除关联商家']);
  177. }else{
  178. AllwinGift::where($this->mini_program)->where($condition)->where(['id' => $id])->delete();
  179. return json(['code'=>200,'msg'=>'操作成功']);
  180. }
  181. }
  182. /**
  183. * 是否锁定和解锁活动
  184. * @param integer $id
  185. * @return json
  186. */
  187. public function isLock(int $id){
  188. $info = AllwinGift::where($this->mini_program)->where(['id' => $id])->find();
  189. if(!$info){
  190. return json(['code'=>0,'msg'=>'未找到活动']);
  191. }
  192. if($info->is_lock){
  193. $store_id = AllwinGiftAllwinStore::where($this->mini_program)->where(['gift_id' => $id,'is_lock' => 1])->column('store_id');
  194. $is_store = AllwinGiftAllwinStore::where($this->mini_program)->where(['is_lock' => 0,'store_id' => $store_id])->count();
  195. if($is_store){
  196. return json(['code'=>0,'msg'=>'禁止启用,请先删除其它活动中相同的好店']);
  197. }
  198. }
  199. $is_lock = $info->is_lock ? 0 : 1;
  200. $result = AllwinGift::where($this->mini_program)->where(['id' => $id])->update(['is_lock' => $is_lock]);
  201. if($result){
  202. AllwinGiftAllwinStore::where($this->mini_program)->where(['gift_id' => $id])->update(['is_lock' => $is_lock]);
  203. return json(['code'=>200,'msg'=>'操作成功']);
  204. }else{
  205. return json(['code'=>0,'msg'=>'操作失败']);
  206. }
  207. }
  208. /**
  209. * 好店列表
  210. */
  211. public function store(int $id){
  212. $condition = [];
  213. $condition['id'] = $id;
  214. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  215. $info = AllwinGift::where($condition)->find();
  216. if(!$info){
  217. $this->error("404 NOT FOUND");
  218. }
  219. $view['info'] = $info;
  220. $view['store'] = AllwinGiftAllwinStore::where(['gift_id' => $info->id])->order('id desc')->paginate(20,false,['query' => ['id' => $id]]);
  221. return view()->assign($view);
  222. }
  223. /**
  224. * 选择并添加活动好店
  225. */
  226. public function selectStore(int $id){
  227. if(request()->isAjax()){
  228. $ids = input('post.ids/s');
  229. if(empty($ids)){
  230. return json(['code'=>0,'msg'=>'请选择要关联的商家']);
  231. }
  232. $gift = AllwinGift::where($this->mini_program)->where(['id' => $id])->find();
  233. if(empty($gift)){
  234. return json(['code'=>0,'msg'=>'未找到活动']);
  235. }
  236. $ids = (array)ids($ids,true);
  237. $condition = [];
  238. $condition['is_lock'] = 0;
  239. $condition['store_id'] = $ids;
  240. $store_id = AllwinGiftAllwinStore::where($this->mini_program)->where($condition)->column('store_id');
  241. if(!empty($store_id)){
  242. return json(['code'=>0,'msg'=>'同一个好店禁止在多个活动中']);
  243. }
  244. $data = [];
  245. foreach ($ids as $key => $value) {
  246. $data[$key]['member_miniapp_id'] = $gift->member_miniapp_id;
  247. $data[$key]['gift_id'] = $gift->id;
  248. $data[$key]['store_id'] = $value;
  249. }
  250. AllwinGiftAllwinStore::insertAll($data);
  251. return json(['code'=>200,'msg'=>'操作成功','data' =>[]]);
  252. }else{
  253. $condition = [];
  254. $condition[] = ['is_lock','=',0];
  255. $store_id = AllwinGiftAllwinStore::where($this->mini_program)->where($condition)->column('store_id');
  256. $keyword = Request::param('keyword','');
  257. if(!empty($keyword)){
  258. $condition[] = ['name','like','%'.$keyword.'%'];
  259. }
  260. $view['lists'] = AllwinStore::where($this->mini_program)->where($condition)->whereNotIn('id',$store_id)->order('is_top desc,sort desc,id desc')->paginate(20);
  261. $view['keyword'] = $keyword;
  262. $view['id'] = $id;
  263. return view()->assign($view);
  264. }
  265. }
  266. //删除好店
  267. public function storeDelete(int $id){
  268. $result = AllwinGiftAllwinStore::where($this->mini_program)->where(['id' => $id])->delete();
  269. if($result){
  270. return json(['code'=>200,'msg'=>'操作成功']);
  271. }else{
  272. return json(['code'=>0,'msg'=>'删除失败']);
  273. }
  274. }
  275. }