CouponRewardRule.php 8.8 KB

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