Coupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Coupon as Request;
  3. use App\Models\Business;
  4. use App\Models\Coupon as Model;
  5. use App\Models\CouponRebate;
  6. use App\Models\CustomCoupon;
  7. use App\Models\Product;
  8. use Illuminate\Support\Facades\DB;
  9. use App\Models\City;
  10. /**
  11. * 优惠券管理
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class Coupon extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','营销管理');
  20. $this->assign('breadcrumb2','优惠券管理');
  21. }
  22. /**
  23. * 首页列表
  24. *
  25. * */
  26. public function index(Model $Model,CustomCoupon $CustomCoupon,City $City,CouponRebate $CouponRebate){
  27. // 接受参数
  28. $code = request('coupon_code','');
  29. $status = request('status');
  30. $startTime = request('start_time','');
  31. $endTime = request('end_time','');
  32. // 编码转ID
  33. $id = $code ? $Model->codeToId($code) : 0;
  34. // 查询条件
  35. $map = [];
  36. // 编码ID
  37. if( $id ) $map[] = ['id','=',$id];
  38. if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
  39. if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
  40. if( !is_null($status) ) $map[] = ['status','=',$status];
  41. $session = session('userRule');
  42. if ($session){
  43. $map[] = ['company_id','=',$session['company_id']];
  44. if ($session['business_id']){
  45. $map[] = ['business_id','=',$session['business_id']];
  46. }
  47. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  48. $shopIds = Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray();
  49. }
  50. }
  51. // 查询数据
  52. $list = $Model->query()->where($map);
  53. if (isset($shopIds)) $list->whereIn('business_id',$shopIds);
  54. $list = $list->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  55. // 计算各个优惠券的数量
  56. $customTotal = $CustomCoupon->whereIn('coupon_id',array_column($list->toArray()['data'],'id'))->groupBy('coupon_id')->select([DB::raw('count(*) as total'),'coupon_id'])->pluck('total','coupon_id')->toArray();
  57. // 循环处理数据
  58. foreach ($list as $key => $value) {
  59. // id转编号
  60. $value['coupon_code']= $Model->idToCode($value['id']);
  61. $value['business_name']= Business::query()->where('id',$value['business_id'])->value('name');
  62. // 发放类型
  63. $value['gant_name'] = $Model->getGrantType($value['grant_type'],'name');
  64. // 如果已经到了结束时间。活动结束
  65. if( $value['status'] == 0 && $value['end_time'] <= time() ) {
  66. // 状态设置
  67. $Model->edit($value['id'],['status'=>3]);
  68. // 状态设置
  69. $value['status'] = 3;
  70. }
  71. // 判断是不是可以参与
  72. if( $value['city_ids'] ) {
  73. // 解析数组
  74. $cityids = explode(',',$value['city_ids']);
  75. // 获取城市
  76. foreach ($cityids as $kk=>$vv) {
  77. // 获取值
  78. $vv = $City->getOne($vv,'name');
  79. // 获取城市名
  80. $cityids[$kk] = $vv;
  81. }
  82. // 城市列表
  83. $value['city_ids'] = implode('、',$cityids);
  84. }
  85. // 赠品
  86. if( $value['rebate_type'] == 3 ) {
  87. // 返回结果
  88. $value['rebate'] = (string) $CouponRebate->query()->join('product','coupon_rebate.product_id','=','product.id')->where([['coupon_id','=',$value['id']]])->value('product.name');
  89. }
  90. // id转编号
  91. $value['custom_total'] = isset($customTotal[$value['id']]) ? $customTotal[$value['id']] : 0;
  92. // 重组
  93. $list[$key] = $value;
  94. }
  95. // 分配数据
  96. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  97. $this->assign('list', $list);
  98. // 加载模板
  99. return $this->fetch();
  100. }
  101. /**
  102. * 添加
  103. *
  104. * */
  105. public function add( Request $request, Model $Model,City $City,CouponRebate $CouponRebate,Product $Product,Business $Business){
  106. if( request()->isMethod('post') ){
  107. // 验证参数
  108. $request->scene('add')->validate();
  109. // 组合数据
  110. $data['name'] = request('name',0);
  111. $data['rebate_type'] = request('rebate_type',0);
  112. $data['std_pay'] = request('std_pay',0);
  113. $data['rebate'] = request('rebate',0);
  114. $data['issue_total'] = request('issue_total',0);
  115. $data['start_time'] = request('start_time','');
  116. $data['end_time'] = request('end_time','');
  117. $data['type_id'] = request('type_id',1);
  118. $data['grant_type'] = request('grant_type',1);
  119. $data['status'] = request('status',2);
  120. $data['business_id'] = request('business_id',0);
  121. $cityIds = request('city_ids',[]);
  122. $data['city_ids'] = implode(',',$cityIds);
  123. // 转换时间,默认现在现在生效
  124. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  125. // 转换时间,默认无结束时间
  126. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  127. // 转换时间,默认无结束时间
  128. $data['exp_time'] = request('exp_time',0) ? strtotime(request('exp_time','')) : 0;
  129. // 判断有效时间类型。获取不同的过期时间
  130. $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['exp_time'];
  131. // 验证信息
  132. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  133. if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
  134. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
  135. if( $data['exp_time'] <= 0 ) return json_send(['code'=>'error','msg'=>'请填写领用后有效期']);
  136. // 赠品ID
  137. $rebateId = 0;
  138. // 如果是赠品表的话。处理赠品逻辑
  139. if( $data['rebate_type'] == 3 ) {
  140. // 产品编码
  141. $rebateId = $Product->codeToId($data['rebate']);
  142. // 赠品ID不存在
  143. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  144. // 设置为0,避免数据异常
  145. $data['rebate'] = 0;
  146. }
  147. // 组合数据,写入订单表,子表
  148. DB::beginTransaction();
  149. try {
  150. // 写入
  151. $id = $Model->add($data);
  152. // 提示新增失败
  153. if( !$id ) {
  154. // 回滚
  155. DB::rollBack();
  156. // 提示
  157. return json_send(['code'=>'error','msg'=>'新增失败']);
  158. }
  159. // 如果是赠品
  160. if( $rebateId ) {
  161. // 写入数据
  162. $result = $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  163. // 提示新增失败
  164. if( !$result ) {
  165. // 回滚
  166. DB::rollBack();
  167. // 提示失败
  168. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  169. }
  170. }
  171. // 提交
  172. DB::commit();
  173. // 记录行为
  174. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  175. // 告知结果
  176. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  177. } catch (\Throwable $th) {
  178. // 回滚
  179. DB::rollBack();
  180. // 提示失败
  181. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  182. }
  183. }
  184. // 获取列表
  185. $cityList = $City->getCityList();
  186. $grantList = $Model->getGrantTypeList();
  187. $businessList = $Business->getList();
  188. // 分配数据
  189. $this->assign('cityList',$cityList);
  190. $this->assign('grantList',$grantList);
  191. $this->assign('businessList',$businessList);
  192. $this->assign('crumbs','新增');
  193. // 加载模板
  194. return $this->fetch();
  195. }
  196. /**
  197. * 编辑
  198. *
  199. * */
  200. public function edit( Request $request, Model $Model,City $City,CouponRebate $CouponRebate,Product $Product,Business $Business){
  201. // 接收参数
  202. $id = request('id',0);
  203. // 查询数据
  204. $oldData = $Model->where(['id'=>$id])->first();
  205. // 修改
  206. if(request()->isMethod('post')){
  207. // 验证参数
  208. $request->scene('edit')->validate();
  209. // 组合数据
  210. $data['name'] = request('name',0);
  211. $data['rebate_type'] = request('rebate_type',0);
  212. $data['std_pay'] = request('std_pay',0);
  213. $data['rebate'] = request('rebate',0);
  214. $data['issue_total'] = request('issue_total',0);
  215. $data['start_time'] = request('start_time','');
  216. $data['end_time'] = request('end_time','');
  217. $data['type_id'] = request('type_id',1);
  218. $data['grant_type'] = request('grant_type',1);
  219. $data['business_id'] = request('business_id',1);
  220. $cityIds = request('city_ids',[]);
  221. $data['city_ids'] = implode(',',$cityIds);
  222. // 转换时间,默认现在现在生效
  223. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  224. // 转换时间,默认无结束时间
  225. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  226. // 转换时间,默认无结束时间
  227. $data['exp_time'] = request('exp_time',0) ? strtotime(request('exp_time','')) : 0;
  228. // 判断有效时间类型。获取不同的过期时间
  229. $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['exp_time'];
  230. // 赠品ID
  231. $rebateId = 0;
  232. // 如果是赠品表的话。处理赠品逻辑
  233. if( $data['rebate_type'] == 3 ) {
  234. // 产品编码
  235. $rebateId = $Product->codeToId($data['rebate']);
  236. // 赠品ID不存在
  237. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  238. // 设置为0,避免数据异常
  239. $data['rebate'] = 0;
  240. }
  241. // 验证信息
  242. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  243. if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
  244. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
  245. if( $data['exp_time'] <= 0 ) return json_send(['code'=>'error','msg'=>'请填写领用后有效期']);
  246. // 组合数据,写入订单表,子表
  247. DB::beginTransaction();
  248. try {
  249. // 写入
  250. $result = $Model->edit($id,$data);
  251. // 提示新增失败
  252. if( !$result ) {
  253. // 回滚
  254. DB::rollBack();
  255. // 提示
  256. return json_send(['code'=>'error','msg'=>'新增失败']);
  257. }
  258. // 如果是赠品券
  259. if( $rebateId ) {
  260. // 查询旧的
  261. $oldRebateId = $CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->value('id');
  262. // 有旧的修改,无则添加新的
  263. $result = $oldRebateId ? $CouponRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  264. // 提示新增失败
  265. if( !$result ) {
  266. // 回滚
  267. DB::rollBack();
  268. // 提示失败
  269. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  270. }
  271. }
  272. // 提交
  273. DB::commit();
  274. // 记录行为
  275. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  276. // 告知结果
  277. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  278. } catch (\Throwable $th) {
  279. // 回滚
  280. DB::rollBack();
  281. // 提示失败
  282. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  283. }
  284. }
  285. // 如果是没有数据
  286. if( !$oldData ) return $this->error('查无数据');
  287. // 如果是赠品表的话。处理赠品逻辑
  288. if( $oldData['rebate_type'] == 3 ) {
  289. // 产品编码
  290. $rebateId = $CouponRebate->getProductByCouponId($oldData['id']);
  291. // 产品ID
  292. $rebateId = isset($rebateId[0]['product_id']) ? $Product->idToCode($rebateId[0]['product_id']) : '';
  293. // 设置为0,避免数据异常
  294. $oldData['rebate'] = $rebateId;
  295. }
  296. // 城市处理
  297. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  298. // 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
  299. $oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
  300. // 获取列表
  301. $cityList = $City->getCityList();
  302. $grantList = $Model->getGrantTypeList();
  303. $businessList = $Business->getList();
  304. // 分配数据
  305. $this->assign('cityList',$cityList);
  306. $this->assign('grantList',$grantList);
  307. $this->assign('businessList',$businessList);
  308. $this->assign('oldData',$oldData);
  309. $this->assign('crumbs','修改');
  310. // 加载模板
  311. return $this->fetch();
  312. }
  313. /**
  314. * 状态
  315. *
  316. * */
  317. public function set_status( Request $request, Model $Model,CustomCoupon $CustomCoupon ){
  318. // 验证参数
  319. $request->scene('set_status')->validate();
  320. // 接收参数
  321. $id = request('id',0);
  322. $status = request('status',0);
  323. // 查询数据
  324. $result = $Model->edit($id,['status'=>$status]);
  325. // 提示新增失败
  326. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  327. // 查询数据
  328. $result = $CustomCoupon->setStatusByCouponId($id,$status);
  329. // 记录行为
  330. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  331. // 告知结果
  332. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  333. }
  334. }