CouponRewardRule.php 7.1 KB

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