CouponRewardRule.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. $removeCustom = str_ireplace("\r\n",',',$removeCustom);
  67. $removeCustom = str_ireplace("\n",',',$removeCustom);
  68. // 转数组处理
  69. $removeCustom = explode(',',$removeCustom);
  70. // 循环处理
  71. foreach ($removeCustom as $key=>$value) {
  72. // 转ID
  73. $customUid = $Custom->codeToId($value);
  74. // id有问题
  75. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  76. // 重组
  77. $removeCustom[$key] = $customUid;
  78. }
  79. // 去重
  80. $removeCustom = array_unique($removeCustom);
  81. }
  82. // 排除数据
  83. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  84. // 写入数据表
  85. $id = $Model->add($data);
  86. // 如果操作失败
  87. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  88. // 记录行为
  89. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  90. // 告知结果
  91. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  92. }
  93. // 获取列表
  94. $cityList = $City->getCityList();
  95. // 分配数据
  96. $this->assign('cityList',$cityList);
  97. $this->assign('crumbs','新增');
  98. // 加载模板
  99. return $this->fetch();
  100. }
  101. /**
  102. * 修改
  103. *
  104. * */
  105. public function edit(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City){
  106. // 接收参数
  107. $id = request('id',0);
  108. // 查询用户
  109. $oldData = $Model->where(['id'=>$id])->first();
  110. // 修改
  111. if(request()->isMethod('post')){
  112. // 验证参数
  113. $request->scene('edit')->validate();
  114. // 接收数据
  115. $couponCode = request('coupon_code','');
  116. $data['coupon_id'] = $Coupon->codeToId($couponCode);
  117. $data['name'] = request('name','');
  118. $data['std_num'] = request('std_num',0);
  119. $data['start_time'] = request('start_time','');
  120. $data['end_time'] = request('end_time','');
  121. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  122. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  123. $cityIds = request('city_ids',[]);
  124. $data['city_ids'] = implode(',',$cityIds);
  125. $removeCustom = request('remove_custom','');
  126. // 循环处理
  127. if( $removeCustom ) {
  128. // 兼容逗号问题
  129. $removeCustom = str_ireplace(',',',',$removeCustom);
  130. $removeCustom = str_ireplace("\r\n",',',$removeCustom);
  131. $removeCustom = str_ireplace("\n",',',$removeCustom);
  132. // 转数组处理
  133. $removeCustom = explode(',',$removeCustom);
  134. // 循环处理
  135. foreach ($removeCustom as $key=>$value) {
  136. // 转ID
  137. $customUid = $Custom->codeToId($value);
  138. // id有问题
  139. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  140. // 重组
  141. $removeCustom[$key] = $customUid;
  142. }
  143. // 去重
  144. $removeCustom = array_unique($removeCustom);
  145. }
  146. // 排除数据
  147. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  148. // 写入数据表
  149. $result = $Model->edit($id,$data);
  150. // 如果操作失败
  151. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  152. // 记录行为
  153. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  154. // 告知结果
  155. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  156. }
  157. // 错误告知
  158. if( !$oldData ) return $this->error('查无数据');
  159. // 优惠券编码
  160. $oldData['coupon_code'] = $Coupon->idToCode($oldData['coupon_id']);
  161. // 排除客户
  162. if( $oldData['remove_custom'] ) {
  163. // 列表
  164. $removeCustom = [];
  165. // 循环处理
  166. foreach (explode(',',$oldData['remove_custom']) as $key => $value) {
  167. // id转编码下发
  168. $removeCustom[] = $Custom->idToCode($value);
  169. }
  170. // 排除用户
  171. $oldData['remove_custom'] = implode("\n",$removeCustom);
  172. }
  173. // 获取城市ID
  174. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  175. // 获取列表
  176. $cityList = $City->getCityList();
  177. // 分配数据
  178. $this->assign('cityList',$cityList);
  179. $this->assign('oldData',$oldData);
  180. $this->assign('crumbs','修改');
  181. // 加载模板
  182. return $this->fetch();
  183. }
  184. /**
  185. * 修改状态
  186. *
  187. * */
  188. public function set_status(Request $request,Model $Model){
  189. // 验证参数
  190. $request->scene('set_status')->validate();
  191. // 设置状态
  192. $id = request('id',0);
  193. $status = request('status',0);
  194. // 查询用户
  195. $oldData = $Model->where(['id'=>$id])->first();
  196. // 如果用户不存在
  197. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  198. // 执行修改
  199. $result = $Model->edit($id,['status'=>$status]);
  200. // 提示新增失败
  201. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  202. // 记录行为
  203. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  204. // 告知结果
  205. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  206. }
  207. }