Coupon.php 11 KB

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