CouponRewardRule.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\CouponRewardRule as Request;
  3. use App\Models\Coupon;
  4. use App\Models\CouponRewardRule as Model;
  5. use App\Models\Custom;
  6. use App\Models\City;
  7. /**
  8. * 优惠券自动发放规则
  9. *
  10. * @author 刘相欣
  11. *
  12. */
  13. class CouponRewardRule extends Auth{
  14. protected function _initialize(){
  15. parent::_initialize();
  16. $this->assign('breadcrumb1','优惠券发放');
  17. $this->assign('breadcrumb2','规则配置');
  18. }
  19. /**
  20. * 列表页
  21. *
  22. * */
  23. public function index(Model $Model,Coupon $Coupon){
  24. // 查询条件
  25. $map = [];
  26. // 查询数据
  27. $list = $Model->query()->where($map)->orderByDesc('id')->paginate(config('page_num',10));
  28. // 循环处理数据
  29. foreach ($list as $key => $value) {
  30. // 优惠券名称
  31. $value['coupon_name'] = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('name');
  32. // 重组
  33. $list[$key] = $value;
  34. }
  35. // 分配数据
  36. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  37. $this->assign('list',$list);
  38. // 加载模板
  39. return $this->fetch();
  40. }
  41. /**
  42. * 添加
  43. *
  44. * */
  45. public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City){
  46. if( request()->isMethod('post') ){
  47. // 验证参数
  48. $request->scene('add')->validate();
  49. // 接收数据
  50. $couponCode = request('coupon_code','');
  51. $data['coupon_id'] = $Coupon->codeToId($couponCode);
  52. $data['name'] = request('name','');
  53. $data['std_num'] = request('std_num',0);
  54. $data['start_time'] = request('start_time','');
  55. $data['end_time'] = request('end_time','');
  56. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  57. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  58. $data['status'] = 1;
  59. $cityIds = request('city_ids',[]);
  60. $data['city_ids'] = implode(',',$cityIds);
  61. $removeCustom = request('remove_custom','');
  62. // 循环处理
  63. if( $removeCustom ) {
  64. // 兼容逗号问题
  65. $removeCustom = str_ireplace(',',',',$removeCustom);
  66. // 转数组处理
  67. $removeCustom = explode(',',$removeCustom);
  68. // 循环处理
  69. foreach ($removeCustom as $key=>$value) {
  70. // 转ID
  71. $customUid = $Custom->codeToId($value);
  72. // id有问题
  73. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  74. // 重组
  75. $removeCustom[$key] = $customUid;
  76. }
  77. // 去重
  78. $removeCustom = array_unique($removeCustom);
  79. }
  80. // 排除数据
  81. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  82. // 写入数据表
  83. $id = $Model->add($data);
  84. // 如果操作失败
  85. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  86. // 记录行为
  87. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  88. // 告知结果
  89. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  90. }
  91. // 获取列表
  92. $cityList = $City->getCityList();
  93. // 分配数据
  94. $this->assign('cityList',$cityList);
  95. $this->assign('crumbs','新增');
  96. // 加载模板
  97. return $this->fetch();
  98. }
  99. /**
  100. * 修改
  101. *
  102. * */
  103. public function edit(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City){
  104. // 接收参数
  105. $id = request('id',0);
  106. // 查询用户
  107. $oldData = $Model->where(['id'=>$id])->first();
  108. // 修改
  109. if(request()->isMethod('post')){
  110. // 验证参数
  111. $request->scene('edit')->validate();
  112. // 接收数据
  113. $couponCode = request('coupon_code','');
  114. $data['coupon_id'] = $Coupon->codeToId($couponCode);
  115. $data['name'] = request('name','');
  116. $data['std_num'] = request('std_num',0);
  117. $data['start_time'] = request('start_time','');
  118. $data['end_time'] = request('end_time','');
  119. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  120. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  121. $cityIds = request('city_ids',[]);
  122. $data['city_ids'] = implode(',',$cityIds);
  123. $removeCustom = request('remove_custom','');
  124. // 循环处理
  125. if( $removeCustom ) {
  126. // 兼容逗号问题
  127. $removeCustom = str_ireplace(',',',',$removeCustom);
  128. // 转数组处理
  129. $removeCustom = explode(',',$removeCustom);
  130. // 循环处理
  131. foreach ($removeCustom as $key=>$value) {
  132. // 转ID
  133. $customUid = $Custom->codeToId($value);
  134. // id有问题
  135. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  136. // 重组
  137. $removeCustom[$key] = $customUid;
  138. }
  139. // 去重
  140. $removeCustom = array_unique($removeCustom);
  141. }
  142. // 排除数据
  143. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  144. // 写入数据表
  145. $result = $Model->edit($id,$data);
  146. // 如果操作失败
  147. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  148. // 记录行为
  149. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  150. // 告知结果
  151. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  152. }
  153. // 错误告知
  154. if( !$oldData ) return $this->error('查无数据');
  155. // 优惠券编码
  156. $oldData['coupon_code'] = $Coupon->idToCode($oldData['coupon_id']);
  157. // 排除客户
  158. if( $oldData['remove_custom'] ) {
  159. // 列表
  160. $removeCustom = [];
  161. // 循环处理
  162. foreach (explode(',',$oldData['remove_custom']) as $key => $value) {
  163. // id转编码下发
  164. $removeCustom[] = $Custom->idToCode($value);
  165. }
  166. // 排除用户
  167. $oldData['remove_custom'] = implode(',',$removeCustom);
  168. }
  169. // 获取城市ID
  170. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  171. // 获取列表
  172. $cityList = $City->getCityList();
  173. // 分配数据
  174. $this->assign('cityList',$cityList);
  175. $this->assign('oldData',$oldData);
  176. $this->assign('crumbs','修改');
  177. // 加载模板
  178. return $this->fetch();
  179. }
  180. /**
  181. * 修改状态
  182. *
  183. * */
  184. public function set_status(Request $request,Model $Model){
  185. // 验证参数
  186. $request->scene('set_status')->validate();
  187. // 设置状态
  188. $id = request('id',0);
  189. $status = request('status',0);
  190. // 查询用户
  191. $oldData = $Model->where(['id'=>$id])->first();
  192. // 如果用户不存在
  193. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  194. // 执行修改
  195. $result = $Model->edit($id,['status'=>$status]);
  196. // 提示新增失败
  197. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  198. // 记录行为
  199. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  200. // 告知结果
  201. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  202. }
  203. }