Promo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. $session = session('userRule');
  128. if ($session){
  129. $data['company_id'] = $session['company_id'];
  130. }
  131. // 组合数据,写入订单表,子表
  132. DB::beginTransaction();
  133. try {
  134. // 写入
  135. $id = $Model->add($data);
  136. // 提示新增失败
  137. if( !$id ) {
  138. // 回滚
  139. DB::rollBack();
  140. // 提示
  141. return json_send(['code'=>'error','msg'=>'新增失败']);
  142. }
  143. // 如果是赠品
  144. if( $rebateId ) {
  145. // 写入数据
  146. $result = $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  147. // 提示新增失败
  148. if( !$result ) {
  149. // 回滚
  150. DB::rollBack();
  151. // 提示失败
  152. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  153. }
  154. }
  155. // 提交
  156. DB::commit();
  157. // 记录行为
  158. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,1,[],$data);
  159. // 告知结果
  160. return json_send(['code'=>'success','msg'=>'新增成功','action'=>'add']);
  161. } catch (\Throwable $th) {
  162. // 回滚
  163. DB::rollBack();
  164. // 提示失败
  165. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  166. }
  167. }
  168. // 获取列表
  169. $cityList = $City->getCityList();
  170. // 标签列表
  171. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  172. $tagList = [];
  173. // 循环数据
  174. foreach ($tagData as $value) {
  175. $tagList[$value['group']][] = $value['name'];
  176. }
  177. $grantList = $Model->getGrantTypeList();
  178. // 分配数据
  179. $this->assign('cityList',$cityList);
  180. $this->assign('grantList',$grantList);
  181. $this->assign('tagList',$tagList);
  182. $this->assign('crumbs','新增');
  183. // 加载模板
  184. return $this->fetch();
  185. }
  186. /**
  187. * 编辑
  188. *
  189. * */
  190. public function edit( Request $request, Model $Model,City $City,PromoRebate $PromoRebate,Product $Product,WeiBanTags $WeiBanTags,Business $Business){
  191. // 接收参数
  192. $id = request('id',0);
  193. // 查询数据
  194. $oldData = $Model->where(['id'=>$id])->first();
  195. // 修改
  196. if(request()->isMethod('post')){
  197. // 验证参数
  198. $request->scene('edit')->validate();
  199. // 组合数据
  200. $data['name'] = request('name',0);
  201. $data['rebate_type'] = request('rebate_type',0);
  202. $data['std_pay'] = request('std_pay',0);
  203. $data['rebate'] = request('rebate',0);
  204. $data['start_time'] = request('start_time','');
  205. $data['end_time'] = request('end_time','');
  206. $data['type_id'] = request('type_id',1);
  207. $cityIds = request('city_ids',[]);
  208. $tagScope = request('tag_scope',[]);
  209. $data['city_ids'] = implode(',',$cityIds);
  210. $data['tag_scope'] = implode(',',$tagScope);
  211. // 转换时间,默认现在现在生效
  212. $data['start_time'] = $data['start_time'] ? strtotime($data['start_time']) : time();
  213. // 转换时间,默认无结束时间
  214. $data['end_time'] = $data['end_time'] ? strtotime($data['end_time']) : 0;
  215. // 赠品ID
  216. $rebateId = 0;
  217. // 如果是赠品表的话。处理赠品逻辑
  218. if( $data['rebate_type'] == 3 ) {
  219. // 产品编码
  220. $rebateId = $Product->codeToId($data['rebate']);
  221. // 赠品ID不存在
  222. if( !$rebateId ) return json_send(['code'=>'error','msg'=>'赠品编码格式有误']);
  223. // 设置为0,避免数据异常
  224. $data['rebate'] = 0;
  225. }
  226. // 验证信息
  227. if( $data['rebate_type'] == 2 && $data['rebate'] > 9.99 ) return json_send(['code'=>'error','msg'=>'不能设置大于9.99折']);
  228. if( $data['start_time'] < 0 ) return json_send(['code'=>'error','msg'=>'请填写活动开始时间']);
  229. if( $data['end_time'] < time() ) return json_send(['code'=>'error','msg'=>'活动时间必须大于当前时间']);
  230. // 组合数据,写入订单表,子表
  231. DB::beginTransaction();
  232. try {
  233. // 写入
  234. $result = $Model->edit($id,$data);
  235. // 提示新增失败
  236. if( !$result ) {
  237. // 回滚
  238. DB::rollBack();
  239. // 提示
  240. return json_send(['code'=>'error','msg'=>'编辑失败']);
  241. }
  242. // 如果是赠品券
  243. if( $rebateId ) {
  244. // 查询旧的
  245. $oldRebateId = $PromoRebate->query()->where([['promo_id','=',$id],['product_id','=',$rebateId]])->value('id');
  246. // 有旧的修改,无则添加新的
  247. $result = $oldRebateId ? $PromoRebate->edit($oldRebateId,['product_id'=>$rebateId,'rebate_num'=>1]) : $PromoRebate->add(['promo_id'=>$id,'product_id'=>$rebateId,'rebate_num'=>1]);
  248. // 提示新增失败
  249. if( !$result ) {
  250. // 回滚
  251. DB::rollBack();
  252. // 提示失败
  253. return json_send(['code'=>'error','msg'=>'赠品结果写入失败']);
  254. }
  255. }
  256. // 提交
  257. DB::commit();
  258. // 记录行为
  259. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],$data);
  260. // 告知结果
  261. return json_send(['code'=>'success','msg'=>'修改成功','action'=>'edit']);
  262. } catch (\Throwable $th) {
  263. // 回滚
  264. DB::rollBack();
  265. // 提示失败
  266. return json_send(['code'=>'error','msg'=>'内部错误,请重试','data'=>['error'=>$th->getMessage()]]);
  267. }
  268. }
  269. // 如果是没有数据
  270. if( !$oldData ) return $this->error('查无数据');
  271. // 如果是赠品表的话。处理赠品逻辑
  272. if( $oldData['rebate_type'] == 3 ) {
  273. // 产品编码
  274. $rebateId = $PromoRebate->getProductByCouponId($oldData['id']);
  275. // 产品ID
  276. $rebateId = isset($rebateId[0]['product_id']) ? $Product->idToCode($rebateId[0]['product_id']) : '';
  277. // 设置为0,避免数据异常
  278. $oldData['rebate'] = $rebateId;
  279. }
  280. // 城市处理
  281. $oldData['city_ids'] = explode(',',$oldData['city_ids']);
  282. $oldData['tag_scope'] = explode(',',$oldData['tag_scope']);
  283. // 有效期,时间戳或者时间为0表示的时间段,否则表示领取后N天
  284. $oldData['exp_type'] = $oldData['exp_time'] > 1000 ? 2 : 1;
  285. // 获取列表
  286. $cityList = $City->getCityList();
  287. $grantList = $Model->getGrantTypeList();
  288. // 标签列表
  289. $tagData = $WeiBanTags->query()->groupBy('group')->groupBy('name')->get(['group','name'])->toArray();
  290. // 标签列表
  291. $tagList = [];
  292. // 循环数据
  293. foreach ($tagData as $value) {
  294. $tagList[$value['group']][] = $value['name'];
  295. }
  296. // 分配数据
  297. $this->assign('cityList',$cityList);
  298. $this->assign('grantList',$grantList);
  299. $this->assign('tagList',$tagList);
  300. $this->assign('oldData',$oldData);
  301. $this->assign('crumbs','修改');
  302. // 加载模板
  303. return $this->fetch();
  304. }
  305. /**
  306. * 状态
  307. *
  308. * */
  309. public function set_status( Request $request, Model $Model){
  310. // 验证参数
  311. $request->scene('set_status')->validate();
  312. // 接收参数
  313. $id = request('id',0);
  314. $status = request('status',0);
  315. // 查询数据
  316. $result = $Model->edit($id,['status'=>$status]);
  317. // 提示新增失败
  318. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  319. // 记录行为
  320. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  321. // 告知结果
  322. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  323. }
  324. }