Coupon.php 12 KB

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