Promo.php 12 KB

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