Coupon.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Coupon as Request;
  3. use App\Models\AdminUser;
  4. use App\Models\Coupon as Model;
  5. use App\Models\CouponProduct;
  6. use App\Models\CouponRebate;
  7. use App\Models\Custom;
  8. use App\Models\CustomCoupon;
  9. use App\Models\Product;
  10. use App\Models\FilesManager;
  11. use Illuminate\Support\Facades\DB;
  12. /**
  13. * 优惠券管理
  14. *
  15. * @author 刘相欣
  16. *
  17. */
  18. class Coupon extends Auth{
  19. protected function _initialize(){
  20. parent::_initialize();
  21. $this->assign('breadcrumb1','营销管理');
  22. $this->assign('breadcrumb2','优惠券管理');
  23. }
  24. /**
  25. * 首页列表
  26. *
  27. * */
  28. public function index(Model $Model,AdminUser $AdminUser,CustomCoupon $CustomCoupon){
  29. // 接受参数
  30. $code = request('coupon_code','');
  31. $status = request('status');
  32. $startTime = request('start_time','');
  33. $endTime = request('end_time','');
  34. // 编码转ID
  35. $id = $code ? $Model->codeToId($code) : 0;
  36. // 查询条件
  37. $map = [];
  38. // 编码ID
  39. if( $id ) $map[] = ['id','=',$id];
  40. if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
  41. if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
  42. if( !is_null($status) ) $map[] = ['status','=',$status];
  43. // 查询数据
  44. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  45. // 计算各个优惠券的数量
  46. $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();
  47. // 循环处理数据
  48. foreach ($list as $key => $value) {
  49. // id转编号
  50. $value['coupon_code']= $Model->idToCode($value['id']);
  51. // id转编号
  52. $value['admin_name'] = $AdminUser->getOne($value['admin_uid'],'username');
  53. // 过期时间
  54. $value['exp_time'] = $Model->getExpTime($value['exp_time']);
  55. // 如果过期时间
  56. if( $value['status'] == 0 && ( $value['exp_time']>0 && $value['exp_time']< time() ) ) {
  57. // 设置过期状态
  58. $Model->setStatusByExpire();
  59. // 状态设置
  60. $value['status'] =3;
  61. }
  62. // id转编号
  63. $value['custom_total'] = isset($customTotal[$value['id']]) ? $customTotal[$value['id']] : 0;
  64. // 重组
  65. $list[$key] = $value;
  66. }
  67. // 分配数据
  68. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  69. $this->assign('list', $list);
  70. // 加载模板
  71. return $this->fetch();
  72. }
  73. /**
  74. * 添加
  75. *
  76. * */
  77. public function add( Request $request, Model $Model,Product $Product,Custom $Custom,FilesManager $FilesManager,CouponRebate $CouponRebate,CustomCoupon $CustomCoupon,CouponProduct $CouponProduct){
  78. if( request()->isMethod('post') ){
  79. // 验证参数
  80. $request->scene('add')->validate();
  81. // 组合数据
  82. $data['name'] = request('name',0);
  83. $data['rebate_type'] = request('rebate_type',0);
  84. $data['std_pay'] = request('std_pay',0);
  85. $data['rebate'] = request('rebate',0);
  86. $data['issue_total'] = request('issue_total',0);
  87. $data['start_time'] = request('start_time',date('Y-m-d'));
  88. $data['end_time'] = request('end_time','');
  89. $data['type_id'] = request('type_id',1);
  90. $data['is_appt'] = request('is_appt',1);
  91. // 根据是否上传了商品范围文件
  92. if( $data['type_id'] == 1 && !request()->hasFile('product_file') ) return json_send(['code'=>'error','msg'=>'请上传指定商品范围']);
  93. // 根据是否上传了客户范围文件
  94. if( $data['is_appt'] == 1 && !request()->hasFile('custom_file') ) return json_send(['code'=>'error','msg'=>'请上传指定客户范围']);
  95. // 转换时间,默认现在现在生效
  96. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  97. // 转换时间,默认无结束时间
  98. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time'].' 23:59:59') : 0;
  99. // 判断有效时间类型。获取不同的过期时间
  100. $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['end_time'];
  101. // 赠品ID
  102. $rebateId = 0;
  103. // 如果是赠品表的话。处理赠品逻辑
  104. if( $data['rebate_type'] == 3 ) {
  105. // 产品编码
  106. $rebateId = $Product->codeToId($data['rebate']);
  107. // 赠品ID不存在
  108. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  109. // 设置为0,避免数据异常
  110. $data['rebate'] = 0;
  111. }
  112. // 商品范围
  113. $products = [];
  114. // 如果是商品范围
  115. if( $data['type_id'] == 1 ) {
  116. // 读取文件获取指定的商品范围
  117. $products = $FilesManager->excelToCode(request()->file('product_file'),$Product);
  118. // 产品范围不存在
  119. if( !$products ) return json_send(['code'=>'error','msg'=>'请上传可用的商品范围']);
  120. // 去重
  121. $products = array_values(array_unique($products));
  122. // 商品数量
  123. if( count($products) > 1000 ) return json_send(['code'=>'error','msg'=>'可用的商品范围请勿超过1000']);
  124. }
  125. // 客户范围
  126. $customs = [];
  127. // 如果是客户范围
  128. if( $data['is_appt'] == 1 ) {
  129. // 读取文件获取指定的客户范围
  130. $customs = $FilesManager->excelToCode(request()->file('custom_file'),$Custom);
  131. // 客户范围不存在
  132. if( !$customs ) return json_send(['code'=>'error','msg'=>'请上传可用的客户范围']);
  133. // 去重
  134. $customs = array_values(array_unique($customs));
  135. // 客户数量
  136. if( count($customs) > 10000 ) return json_send(['code'=>'error','msg'=>'客户范围请勿超过10000']);
  137. // 如果超过数量,不允许发放
  138. if( $data['issue_total'] < count($customs) ) return json_send(['code'=>'error','msg'=>'客户数量请勿超过发行数量']);
  139. }
  140. // 组合数据,写入订单表,子表
  141. DB::beginTransaction();
  142. try {
  143. // 写入
  144. $id = $Model->add($data);
  145. // 提示新增失败
  146. if( !$id ) {
  147. // 回滚
  148. DB::rollBack();
  149. // 提示失败
  150. return json_send(['code'=>'error','msg'=>'新增失败']);
  151. }
  152. // 当前时间
  153. $time = time();
  154. // 如果有商品范围
  155. if( $products ) {
  156. // 循环商品
  157. foreach ($products as $key => $value) {
  158. // 时间处理
  159. $value = ['coupon_id'=>$id,'product_id'=>$value,'insert_time'=>$time,'update_time'=>$time];
  160. // 重组
  161. $products[$key] = $value;
  162. }
  163. // 写入数据
  164. $result = $CouponProduct->insert($products);
  165. // 提示新增失败
  166. if( !$result ) {
  167. // 回滚
  168. DB::rollBack();
  169. // 提示失败
  170. return json_send(['code'=>'error','msg'=>'商品范围写入失败']);
  171. }
  172. }
  173. // 如果是赠品
  174. if( $rebateId ) {
  175. // 写入数据
  176. $result = $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  177. // 提示新增失败
  178. if( !$result ) {
  179. // 回滚
  180. DB::rollBack();
  181. // 提示失败
  182. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  183. }
  184. }
  185. // 如果有客户范围
  186. if( $customs ) {
  187. // 分块,每1000一批写入
  188. foreach (array_chunk($customs,1000) as $chunk) {
  189. // 循环客户
  190. foreach ($chunk as $key => $value) {
  191. // 时间处理
  192. $value = ['coupon_id'=>$id,'custom_uid'=>$value,'exp_time'=>$Model->getExpTime($data['exp_time']),'insert_time'=>$time,'update_time'=>$time];
  193. // 重组
  194. $chunk[$key] = $value;
  195. }
  196. // 写入数据
  197. $result = $CustomCoupon->insert($chunk);
  198. // 提示新增失败
  199. if( !$result ) {
  200. // 回滚
  201. DB::rollBack();
  202. // 提示失败
  203. return json_send(['code'=>'error','msg'=>'客户范围写入失败']);
  204. }
  205. }
  206. }
  207. // 提交
  208. DB::commit();
  209. } catch (\Throwable $th) {
  210. // 回滚
  211. DB::rollBack();
  212. // 提示失败
  213. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  214. }
  215. // 记录行为
  216. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  217. // 告知结果
  218. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  219. }
  220. // 分配数据
  221. $this->assign('crumbs','新增');
  222. // 加载模板
  223. return $this->fetch();
  224. }
  225. /**
  226. * 编辑
  227. *
  228. * */
  229. public function edit( Request $request, Model $Model,Product $Product,Custom $Custom,FilesManager $FilesManager,CouponRebate $CouponRebate,CustomCoupon $CustomCoupon,CouponProduct $CouponProduct){
  230. // 接收参数
  231. $id = request('id',0);
  232. // 查询数据
  233. $oldData = $Model->where(['id'=>$id])->first();
  234. // 修改
  235. if(request()->isMethod('post')){
  236. // 验证参数
  237. $request->scene('edit')->validate();
  238. // 组合数据
  239. $data['name'] = request('name',0);
  240. $data['rebate_type'] = request('rebate_type',0);
  241. $data['std_pay'] = request('std_pay',0);
  242. $data['rebate'] = request('rebate',0);
  243. $data['issue_total'] = request('issue_total',0);
  244. $data['start_time'] = request('start_time',date('Y-m-d'));
  245. $data['end_time'] = request('end_time','');
  246. $data['type_id'] = request('type_id',1);
  247. $data['is_appt'] = request('is_appt',1);
  248. // 转换时间,默认现在现在生效
  249. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  250. // 转换时间,默认无结束时间
  251. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time'].' 23:59:59') : 0;
  252. // 判断有效时间类型。获取不同的过期时间
  253. $data['exp_time'] = request('exp_type',0) == 1 ? request('exp_days',0) : $data['end_time'];
  254. // 折扣不能超过10
  255. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  256. // 赠品ID
  257. $rebateId = 0;
  258. // 如果是赠品表的话。处理赠品逻辑
  259. if( $data['rebate_type'] == 3 ) {
  260. // 产品编码
  261. $rebateId = $Product->codeToId($data['rebate']);
  262. // 赠品ID不存在
  263. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  264. // 设置为0,避免数据异常
  265. $data['rebate'] = 0;
  266. }
  267. // 商品范围
  268. $products = [];
  269. // 如果是商品范围,并且上传了文件
  270. if( $data['type_id'] == 1 && request()->hasFile('product_file')) {
  271. // 读取文件获取指定的商品范围
  272. $products = $FilesManager->excelToCode(request()->file('product_file'),$Product);
  273. // 产品范围不存在
  274. if( !$products ) return json_send(['code'=>'error','msg'=>'请上传可用的商品范围']);
  275. // 去重
  276. $products = array_values(array_unique($products));
  277. // 商品数量
  278. if( count($products) > 1000 ) return json_send(['code'=>'error','msg'=>'可用的商品范围请勿超过1000']);
  279. }
  280. // 客户范围
  281. $customs = [];
  282. // 如果是客户范围,并且上传了文件
  283. if( $data['is_appt'] == 1 && request()->hasFile('custom_file') ) {
  284. // 客户范围不存在
  285. if( !$customs ) return json_send(['code'=>'error','msg'=>'请上传可用的客户范围']);
  286. // 去重
  287. $customs = array_values(array_unique($customs));
  288. // 客户数量
  289. if( count($customs) > 10000 ) return json_send(['code'=>'error','msg'=>'客户范围请勿超过10000']);
  290. // 如果超过数量,不允许发放
  291. if( $data['issue_total'] < count($customs) ) return json_send(['code'=>'error','msg'=>'客户数量请勿超过发行数量']);
  292. }
  293. // 组合数据,写入订单表,子表
  294. DB::beginTransaction();
  295. try {
  296. // 写入
  297. $result = $Model->edit($id,$data);
  298. // 提示新增失败
  299. if( !$result ) {
  300. // 回滚
  301. DB::rollBack();
  302. // 提示失败
  303. return json_send(['code'=>'error','msg'=>'新增失败']);
  304. }
  305. // 如果是赠品券
  306. if( $rebateId ) {
  307. // 查询旧的
  308. $oldRebateId = $CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->value('id');
  309. // 如果旧的与新的不一致
  310. if( $oldRebateId != $rebateId ) {
  311. // 删除旧的
  312. $CouponRebate->query()->where([['coupon_id','=',$id],['product_id','=',$rebateId]])->delete();
  313. // 有旧的修改,无则添加新的
  314. $result = $oldRebateId ? $CouponRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $CouponRebate->add(['coupon_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  315. // 提示新增失败
  316. if( !$result ) {
  317. // 回滚
  318. DB::rollBack();
  319. // 提示失败
  320. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  321. }
  322. }
  323. }
  324. // 当前时间
  325. $time = time();
  326. // 如果有商品范围
  327. if( $products ) {
  328. // 循环商品
  329. foreach ($products as $key => $value) {
  330. // 重复的产品移除
  331. $oldProduct = $CouponProduct->query()->where([['coupon_id','=',$id],['product_id','=',$value]])->value('product_id');
  332. // 存在产品,移除
  333. if( $oldProduct ) {
  334. unset($products[$key]);
  335. continue;
  336. }
  337. // 时间处理
  338. $value = ['coupon_id'=>$id,'product_id'=>$value,'insert_time'=>$time,'update_time'=>$time];
  339. // 重组
  340. $products[$key] = $value;
  341. }
  342. // 格式化
  343. $products = array_values($products);
  344. // 写入数据
  345. $result = $CouponProduct->insert($products);
  346. // 提示新增失败
  347. if( !$result ) {
  348. // 回滚
  349. DB::rollBack();
  350. // 提示失败
  351. return json_send(['code'=>'error','msg'=>'商品范围写入失败']);
  352. }
  353. }
  354. // 如果有客户范围
  355. if( $customs ) {
  356. // 分块,每1000一批写入
  357. foreach (array_chunk($customs,1000) as $chunk) {
  358. // 循环客户
  359. foreach ($chunk as $key => $value) {
  360. // 时间处理
  361. $value = ['coupon_id'=>$id,'custom_uid'=>$value,'exp_time'=>$Model->getExpTime($data['exp_time']),'insert_time'=>$time,'update_time'=>$time];
  362. // 重组
  363. $chunk[$key] = $value;
  364. }
  365. // 写入数据
  366. $result = $CustomCoupon->insert($chunk);
  367. // 提示新增失败
  368. if( !$result ) {
  369. // 回滚
  370. DB::rollBack();
  371. // 提示失败
  372. return json_send(['code'=>'error','msg'=>'客户范围写入失败']);
  373. }
  374. }
  375. }
  376. // 提交
  377. DB::commit();
  378. } catch (\Throwable $th) {
  379. // 回滚
  380. DB::rollBack();
  381. // 提示失败
  382. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  383. }
  384. // 记录行为
  385. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  386. // 告知结果
  387. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  388. }
  389. // 如果是没有数据
  390. if( !$oldData ) return $this->error('查无数据');
  391. // 如果是赠品表的话。处理赠品逻辑
  392. if( $oldData['rebate_type'] == 3 ) {
  393. // 产品编码
  394. $rebateId = $CouponRebate->getProductByCouponId($oldData['id']);
  395. // 产品ID
  396. $rebateId = isset($rebateId[0]['product_id']) ? $Product->idToCode($rebateId[0]['product_id']) : '';
  397. // 设置为0,避免数据异常
  398. $oldData['rebate'] = $rebateId;
  399. }
  400. // 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
  401. $oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
  402. // 分配数据
  403. $this->assign('oldData',$oldData);
  404. $this->assign('crumbs','修改');
  405. // 加载模板
  406. return $this->fetch();
  407. }
  408. /**
  409. * 状态
  410. *
  411. * */
  412. public function set_status( Request $request, Model $Model,CustomCoupon $CustomCoupon ){
  413. // 验证参数
  414. $request->scene('set_status')->validate();
  415. // 接收参数
  416. $id = request('id',0);
  417. $status = request('status',0);
  418. // 查询数据
  419. $result = $Model->edit($id,['status'=>$status]);
  420. // 提示新增失败
  421. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  422. // 查询数据
  423. $result = $CustomCoupon->setStatusByCouponId($id,$status);
  424. // 提示新增失败
  425. if( !$result ) return json_send(['code'=>'error','msg'=>'同步设置失败']);
  426. // 记录行为
  427. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  428. // 告知结果
  429. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  430. }
  431. }