CouponRewardRule.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $session = session('userRule');
  28. if ($session){
  29. $map[] = ['company_id','=',$session['company_id']];
  30. if ($session['business_id']){
  31. $map[] = ['business_id','=',$session['business_id']];
  32. }
  33. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  34. $shopIds = Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray();
  35. }
  36. }
  37. // 查询数据
  38. $list = $Model->query()->where($map);
  39. if (isset($shopIds)) $list->whereIn('business_id',$shopIds);
  40. $list = $list->orderByDesc('id')->paginate(config('page_num',10));
  41. // 循环处理数据
  42. foreach ($list as $key => $value) {
  43. // 优惠券名称
  44. $value['coupon_name'] = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('name');
  45. $value['business_name'] = Business::query()->where([['id','=',$value['business_id']]])->value('name');
  46. // 重组
  47. $list[$key] = $value;
  48. }
  49. // 分配数据
  50. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  51. $this->assign('list',$list);
  52. // 加载模板
  53. return $this->fetch();
  54. }
  55. /**
  56. * 添加
  57. *
  58. * */
  59. public function add(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City,Business $Business){
  60. if( request()->isMethod('post') ){
  61. // 验证参数
  62. $request->scene('add')->validate();
  63. // 接收数据
  64. $couponCode = request('coupon_code','');
  65. $data['coupon_id'] = $Coupon->codeToId($couponCode);
  66. $data['name'] = request('name','');
  67. $data['std_num'] = request('std_num',0);
  68. $data['start_time'] = request('start_time','');
  69. $data['end_time'] = request('end_time','');
  70. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  71. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  72. $data['status'] = 1;
  73. $data['business_id'] = request('business_id',0);
  74. $session = session('userRule');
  75. if ($session){
  76. $data['company_id'] = $session['company_id'];
  77. }
  78. $cityIds = request('city_ids',[]);
  79. $data['city_ids'] = implode(',',$cityIds);
  80. $removeCustom = request('remove_custom','');
  81. //店铺优惠卷是否存在
  82. $res = $Coupon::query()->where(['id'=>$data['coupon_id'],'business_id'=>$data['business_id']])->first();
  83. if (!$res) return json_send(['code'=>'error','msg'=>'新增失败,店铺优惠卷编码错误']);
  84. // 循环处理
  85. if( $removeCustom ) {
  86. // 兼容逗号问题
  87. $removeCustom = str_ireplace(',',',',$removeCustom);
  88. $removeCustom = str_ireplace("\r\n",',',$removeCustom);
  89. $removeCustom = str_ireplace("\n",',',$removeCustom);
  90. // 转数组处理
  91. $removeCustom = explode(',',$removeCustom);
  92. // 循环处理
  93. foreach ($removeCustom as $key=>$value) {
  94. // 转ID
  95. $customUid = $Custom->codeToId($value);
  96. // id有问题
  97. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  98. // 重组
  99. $removeCustom[$key] = $customUid;
  100. }
  101. // 去重
  102. $removeCustom = array_unique($removeCustom);
  103. }
  104. // 排除数据
  105. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  106. // 写入数据表
  107. $id = $Model->add($data);
  108. // 如果操作失败
  109. if( !$id ) return json_send(['code'=>'error','msg'=>'新增失败']);
  110. // 记录行为
  111. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  112. // 告知结果
  113. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  114. }
  115. // 获取列表
  116. $cityList = $City->getCityList();
  117. $businessList = $Business->getList();
  118. // 分配数据
  119. $this->assign('cityList',$cityList);
  120. $this->assign('businessList',$businessList);
  121. $this->assign('crumbs','新增');
  122. // 加载模板
  123. return $this->fetch();
  124. }
  125. /**
  126. * 修改
  127. *
  128. * */
  129. public function edit(Request $request,Model $Model,Coupon $Coupon,Custom $Custom,City $City,Business $Business){
  130. // 接收参数
  131. $id = request('id',0);
  132. // 查询用户
  133. $oldData = $Model->where(['id'=>$id])->first();
  134. // 修改
  135. if(request()->isMethod('post')){
  136. // 验证参数
  137. $request->scene('edit')->validate();
  138. // 接收数据
  139. $couponCode = request('coupon_code','');
  140. $data['coupon_id'] = $Coupon->codeToId($couponCode);
  141. $data['name'] = request('name','');
  142. $data['std_num'] = request('std_num',0);
  143. $data['start_time'] = request('start_time','');
  144. $data['end_time'] = request('end_time','');
  145. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : 0;
  146. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  147. $data['business_id'] = request('business_id',0);
  148. $cityIds = request('city_ids',[]);
  149. $data['city_ids'] = implode(',',$cityIds);
  150. $removeCustom = request('remove_custom','');
  151. // 循环处理
  152. if( $removeCustom ) {
  153. // 兼容逗号问题
  154. $removeCustom = str_ireplace(',',',',$removeCustom);
  155. $removeCustom = str_ireplace("\r\n",',',$removeCustom);
  156. $removeCustom = str_ireplace("\n",',',$removeCustom);
  157. // 转数组处理
  158. $removeCustom = explode(',',$removeCustom);
  159. // 循环处理
  160. foreach ($removeCustom as $key=>$value) {
  161. // 转ID
  162. $customUid = $Custom->codeToId($value);
  163. // id有问题
  164. if( !$customUid ) return json_send(['code'=>'error','msg'=>'客户编码'.$value.'不存在']);
  165. // 重组
  166. $removeCustom[$key] = $customUid;
  167. }
  168. // 去重
  169. $removeCustom = array_unique($removeCustom);
  170. }
  171. // 排除数据
  172. $data['remove_custom'] = is_array($removeCustom) ? implode(',',$removeCustom) : $removeCustom;
  173. // 写入数据表
  174. $result = $Model->edit($id,$data);
  175. // 如果操作失败
  176. if( !$result ) return json_send(['code'=>'error','msg'=>'修改失败']);
  177. // 记录行为
  178. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,$data);
  179. // 告知结果
  180. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  181. }
  182. // 错误告知
  183. if( !$oldData ) return $this->error('查无数据');
  184. // 优惠券编码
  185. $oldData['coupon_code'] = $Coupon->idToCode($oldData['coupon_id']);
  186. // 排除客户
  187. if( $oldData['remove_custom'] ) {
  188. // 列表
  189. $removeCustom = [];
  190. // 循环处理
  191. foreach (explode(',',$oldData['remove_custom']) as $key => $value) {
  192. // id转编码下发
  193. $removeCustom[] = $Custom->idToCode($value);
  194. }
  195. // 排除用户
  196. $oldData['remove_custom'] = implode("\n",$removeCustom);
  197. }
  198. // 获取城市ID
  199. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  200. // 获取列表
  201. $cityList = $City->getCityList();
  202. $businessList = $Business->getList();
  203. // 分配数据
  204. $this->assign('cityList',$cityList);
  205. $this->assign('businessList',$businessList);
  206. $this->assign('oldData',$oldData);
  207. $this->assign('crumbs','修改');
  208. // 加载模板
  209. return $this->fetch();
  210. }
  211. /**
  212. * 修改状态
  213. *
  214. * */
  215. public function set_status(Request $request,Model $Model){
  216. // 验证参数
  217. $request->scene('set_status')->validate();
  218. // 设置状态
  219. $id = request('id',0);
  220. $status = request('status',0);
  221. // 查询用户
  222. $oldData = $Model->where(['id'=>$id])->first();
  223. // 如果用户不存在
  224. if( !$oldData ) return json_send(['code'=>'error','msg'=>'数据不存在']);
  225. // 执行修改
  226. $result = $Model->edit($id,['status'=>$status]);
  227. // 提示新增失败
  228. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  229. // 记录行为
  230. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,$oldData,['status'=>$status]);
  231. // 告知结果
  232. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  233. }
  234. }