Promo.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Promo as Request;
  3. use App\Models\Business;
  4. use App\Models\Promo as Model;
  5. use App\Models\PromoRebate;
  6. use App\Models\Product;
  7. use Illuminate\Support\Facades\DB;
  8. use App\Models\City;
  9. use App\Models\WeiBan\Tags as WeiBanTags;
  10. /**
  11. * 产品促销活动
  12. *
  13. * @author 刘相欣
  14. *
  15. */
  16. class Promo extends Auth{
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','营销管理');
  20. $this->assign('breadcrumb2','产品促销活动');
  21. }
  22. /**
  23. * 首页列表
  24. *
  25. * */
  26. public function index(Model $Model,City $City,PromoRebate $PromoRebate){
  27. // 接受参数
  28. $code = request('promo_code','');
  29. $promoName = request('promo_name','');
  30. $status = request('status');
  31. $startTime = request('start_time','');
  32. $endTime = request('end_time','');
  33. // 编码转ID
  34. $id = $code ? $Model->codeToId($code) : 0;
  35. // 查询条件
  36. $map = [];
  37. // 编码ID
  38. if( $id ) $map[] = ['id','=',$id];
  39. if( $promoName ) $map[] = ['name','=',$promoName];
  40. if( $startTime ) $map[] = ['start_time','>=',strtotime($startTime)];
  41. if( $endTime ) $map[] = ['end_time','<=',strtotime($endTime)];
  42. if( !is_null($status) ) $map[] = ['status','=',$status];
  43. $session = session('userRule');
  44. if ($session){
  45. $map[] = ['company_id','=',$session['company_id']];
  46. if ($session['business_id']){
  47. $map[] = ['business_id','=',$session['business_id']];
  48. }
  49. if ($session['menu_type'] == 1 && $session['data_type'] == 2){
  50. $shopIds = Business::query()->where('leader_uid',$session['admin_uid'])->pluck('id')->toArray();
  51. }
  52. }
  53. // 查询数据
  54. $list = $Model->query()->where($map);
  55. if (isset($shopIds)) $list->whereIn('business_id',$shopIds);
  56. $list = $list->orderByDesc('id')->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  57. // 循环处理数据
  58. foreach ($list as $key => $value) {
  59. // id转编号
  60. $value['promo_code']= $Model->idToCode($value['id']);
  61. // 发放类型
  62. $value['gant_name'] = $Model->getGrantType($value['grant_type'],'name');
  63. // 如果已经到了结束时间。活动结束
  64. if( $value['status'] == 0 && $value['end_time'] <= time() ) {
  65. // 状态设置
  66. $Model->edit($value['id'],['status'=>3]);
  67. // 状态设置
  68. $value['status'] = 3;
  69. }
  70. // 判断是不是可以参与
  71. if( $value['city_ids'] ) {
  72. // 解析数组
  73. $cityids = explode(',',$value['city_ids']);
  74. // 获取城市
  75. foreach ($cityids as $kk=>$vv) {
  76. // 获取值
  77. $vv = $City->getOne($vv,'name');
  78. // 获取城市名
  79. $cityids[$kk] = $vv;
  80. }
  81. // 城市列表
  82. $value['city_ids'] = implode('、',$cityids);
  83. }
  84. // 赠品
  85. if( $value['rebate_type'] == 3 ) {
  86. // 返回结果
  87. $value['rebate'] = (string) $PromoRebate->query()->join('product','promo_rebate.product_id','=','product.id')->where([['promo_id','=',$value['id']]])->value('product.name');
  88. }
  89. $value['business_name'] = Business::query()->where([['id','=',$value['business_id']]])->value('name');
  90. // 重组
  91. $list[$key] = $value;
  92. }
  93. // 分配数据
  94. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  95. $this->assign('list', $list);
  96. // 加载模板
  97. return $this->fetch();
  98. }
  99. /**
  100. * 添加
  101. *
  102. * */
  103. public function add( Request $request, Model $Model,City $City,PromoRebate $PromoRebate,Product $Product,WeiBanTags $WeiBanTags,Business $Business){
  104. if( request()->isMethod('post') ){
  105. // 验证参数
  106. $request->scene('add')->validate();
  107. // 组合数据
  108. $data['name'] = request('name',0);
  109. $data['rebate_type'] = request('rebate_type',0);
  110. $data['std_pay'] = request('std_pay',0);
  111. $data['rebate'] = request('rebate',0);
  112. $data['start_time'] = request('start_time','');
  113. $data['end_time'] = request('end_time','');
  114. $data['status'] = request('status',2);
  115. $data['type_id'] = request('type_id',0);
  116. $cityIds = request('city_ids',[]);
  117. $tagScope = request('tag_scope',[]);
  118. $data['city_ids'] = implode(',',$cityIds);
  119. $data['tag_scope'] = implode(',',$tagScope);
  120. // 转换时间,默认现在现在生效
  121. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  122. // 转换时间,默认无结束时间
  123. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  124. // 验证信息
  125. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  126. if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
  127. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
  128. // 赠品ID
  129. $rebateId = 0;
  130. // 如果是赠品表的话。处理赠品逻辑
  131. if( $data['rebate_type'] == 3 ) {
  132. // 产品编码
  133. $rebateId = $Product->codeToId($data['rebate']);
  134. // 赠品ID不存在
  135. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  136. // 设置为0,避免数据异常
  137. $data['rebate'] = 0;
  138. }
  139. $data['business_id'] = request('business_id',0);
  140. $session = session('userRule');
  141. if ($session){
  142. $data['company_id'] = $session['company_id'];
  143. }
  144. // 组合数据,写入订单表,子表
  145. DB::beginTransaction();
  146. try {
  147. // 写入
  148. $id = $Model->add($data);
  149. // 提示新增失败
  150. if( !$id ) {
  151. // 回滚
  152. DB::rollBack();
  153. // 提示
  154. return json_send(['code'=>'error','msg'=>'新增失败']);
  155. }
  156. // 如果是赠品
  157. if( $rebateId ) {
  158. // 写入数据
  159. $result = $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  160. // 提示新增失败
  161. if( !$result ) {
  162. // 回滚
  163. DB::rollBack();
  164. // 提示失败
  165. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  166. }
  167. }
  168. // 提交
  169. DB::commit();
  170. // 记录行为
  171. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  172. // 告知结果
  173. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  174. } catch (\Throwable $th) {
  175. // 回滚
  176. DB::rollBack();
  177. // 提示失败
  178. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  179. }
  180. }
  181. // 获取列表
  182. $cityList = $City->getCityList();
  183. // 标签列表
  184. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  185. $tagList = [];
  186. // 循环数据
  187. foreach ($tagData as $value) {
  188. $tagList[$value['group']][] = $value['name'];
  189. }
  190. $grantList = $Model->getGrantTypeList();
  191. $businessList = $Business->getList();
  192. // 分配数据
  193. $this->assign('cityList',$cityList);
  194. $this->assign('grantList',$grantList);
  195. $this->assign('tagList',$tagList);
  196. $this->assign('businessList',$businessList);
  197. $this->assign('crumbs','新增');
  198. // 加载模板
  199. return $this->fetch();
  200. }
  201. /**
  202. * 编辑
  203. *
  204. * */
  205. public function edit( Request $request, Model $Model,City $City,PromoRebate $PromoRebate,Product $Product,WeiBanTags $WeiBanTags,Business $Business){
  206. // 接收参数
  207. $id = request('id',0);
  208. // 查询数据
  209. $oldData = $Model->where(['id'=>$id])->first();
  210. // 修改
  211. if(request()->isMethod('post')){
  212. // 验证参数
  213. $request->scene('edit')->validate();
  214. // 组合数据
  215. $data['name'] = request('name',0);
  216. $data['rebate_type'] = request('rebate_type',0);
  217. $data['std_pay'] = request('std_pay',0);
  218. $data['rebate'] = request('rebate',0);
  219. $data['start_time'] = request('start_time','');
  220. $data['end_time'] = request('end_time','');
  221. $data['type_id'] = request('type_id',1);
  222. $cityIds = request('city_ids',[]);
  223. $tagScope = request('tag_scope',[]);
  224. $data['business_id'] = request('business_id',0);
  225. $data['city_ids'] = implode(',',$cityIds);
  226. $data['tag_scope'] = implode(',',$tagScope);
  227. // 转换时间,默认现在现在生效
  228. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  229. // 转换时间,默认无结束时间
  230. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  231. // 赠品ID
  232. $rebateId = 0;
  233. // 如果是赠品表的话。处理赠品逻辑
  234. if( $data['rebate_type'] == 3 ) {
  235. // 产品编码
  236. $rebateId = $Product->codeToId($data['rebate']);
  237. // 赠品ID不存在
  238. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  239. // 设置为0,避免数据异常
  240. $data['rebate'] = 0;
  241. }
  242. // 验证信息
  243. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  244. if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
  245. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
  246. // 组合数据,写入订单表,子表
  247. DB::beginTransaction();
  248. try {
  249. // 写入
  250. $result = $Model->edit($id,$data);
  251. // 提示新增失败
  252. if( !$result ) {
  253. // 回滚
  254. DB::rollBack();
  255. // 提示
  256. return json_send(['code'=>'error','msg'=>'编辑失败']);
  257. }
  258. // 如果是赠品券
  259. if( $rebateId ) {
  260. // 查询旧的
  261. $oldRebateId = $PromoRebate->query()->where([['promo_id','=',$id],['product_id','=',$rebateId]])->value('id');
  262. // 有旧的修改,无则添加新的
  263. $result = $oldRebateId ? $PromoRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  264. // 提示新增失败
  265. if( !$result ) {
  266. // 回滚
  267. DB::rollBack();
  268. // 提示失败
  269. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  270. }
  271. }
  272. // 提交
  273. DB::commit();
  274. // 记录行为
  275. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  276. // 告知结果
  277. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  278. } catch (\Throwable $th) {
  279. // 回滚
  280. DB::rollBack();
  281. // 提示失败
  282. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  283. }
  284. }
  285. // 如果是没有数据
  286. if( !$oldData ) return $this->error('查无数据');
  287. // 如果是赠品表的话。处理赠品逻辑
  288. if( $oldData['rebate_type'] == 3 ) {
  289. // 产品编码
  290. $rebateId = $PromoRebate->getProductByCouponId($oldData['id']);
  291. // 产品ID
  292. $rebateId = isset($rebateId[0]['product_id']) ? $Product->idToCode($rebateId[0]['product_id']) : '';
  293. // 设置为0,避免数据异常
  294. $oldData['rebate'] = $rebateId;
  295. }
  296. // 城市处理
  297. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  298. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  299. // 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
  300. $oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
  301. // 获取列表
  302. $cityList = $City->getCityList();
  303. $grantList = $Model->getGrantTypeList();
  304. // 标签列表
  305. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  306. // 标签列表
  307. $tagList = [];
  308. // 循环数据
  309. foreach ($tagData as $value) {
  310. $tagList[$value['group']][] = $value['name'];
  311. }
  312. $businessList = $Business->getList();
  313. // 分配数据
  314. $this->assign('cityList',$cityList);
  315. $this->assign('grantList',$grantList);
  316. $this->assign('tagList',$tagList);
  317. $this->assign('oldData',$oldData);
  318. $this->assign('businessList',$businessList);
  319. $this->assign('crumbs','修改');
  320. // 加载模板
  321. return $this->fetch();
  322. }
  323. /**
  324. * 状态
  325. *
  326. * */
  327. public function set_status( Request $request, Model $Model){
  328. // 验证参数
  329. $request->scene('set_status')->validate();
  330. // 接收参数
  331. $id = request('id',0);
  332. $status = request('status',0);
  333. // 查询数据
  334. $result = $Model->edit($id,['status'=>$status]);
  335. // 提示新增失败
  336. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  337. // 记录行为
  338. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  339. // 告知结果
  340. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  341. }
  342. }