Coupon.php 12 KB

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