Promotion.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\shop\controller;
  11. use addon\coupon\model\Coupon;
  12. use app\model\system\Addon;
  13. use app\model\system\AddonQuick;
  14. use app\model\system\Promotion as PrmotionModel;
  15. use app\model\member\MemberAccount;
  16. use app\model\order\Order;
  17. use app\model\system\Config as SystemConfig;
  18. use app\model\web\Config as ConfigModel;
  19. /**
  20. * 营销
  21. * Class Promotion
  22. * @package app\shop\controller
  23. */
  24. class Promotion extends BaseShop
  25. {
  26. protected $addon_arr = [];
  27. public function __construct()
  28. {
  29. //执行父类构造函数
  30. parent::__construct();
  31. if (!request()->isAjax()) $this->addon_arr = array_unique(array_column($this->menus, 'addon'));
  32. }
  33. /**
  34. * 营销概况
  35. */
  36. public function index()
  37. {
  38. $promotion_model = new PrmotionModel();
  39. $length = input('length', 0);
  40. $start_time = date('Y-m-01', strtotime($length . ' month'));
  41. $end_time = date('Y-m-d', strtotime("$start_time +1 month -1 day"));
  42. $start_time = strtotime($start_time . ' 00:00:00');
  43. $end_time = strtotime($end_time . ' 23:59:59');
  44. $summary = $promotion_model->getPromotionSummary($start_time, $end_time, $this->site_id)[ 'data' ];
  45. if (request()->isAjax()) {
  46. return success(0, '', [
  47. 'month' => date('Y/m', $start_time),
  48. 'days' => (int) date("t", $start_time),
  49. 'start_time' => $start_time,
  50. 'data' => $summary
  51. ]);
  52. }
  53. $order_num = ( new Order() )->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'promotion_type', '<>', '' ], [ 'pay_status', '=', 1 ] ], 'order_id')[ 'data' ];
  54. $this->assign('summary', $summary);
  55. $this->assign('month', date('Y/m', $start_time));
  56. $this->assign('days', date("t", $start_time));
  57. $this->assign('start_time', $start_time);
  58. $all_promotion = array_column($promotion_model->getSitePromotions($this->site_id), null, 'name');
  59. $all_promotion = array_filter(array_map(function($item) {
  60. if ($item[ 'show_type' ] == 'shop' || $item[ 'show_type' ] == 'member') return $item;
  61. }, $all_promotion));
  62. $addon_model = new Addon();
  63. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  64. $promotion_addon = $value[ 'promotion' ];
  65. foreach ($promotion_addon as $name) {
  66. if (isset($all_promotion[ $name ])) {
  67. array_unshift($all_promotion, $all_promotion[ $name ]);
  68. unset($all_promotion[ $name ]);
  69. }
  70. }
  71. $this->assign('all_promotion', $all_promotion);
  72. $this->assign('order_num', $order_num);
  73. return $this->fetch("promotion/index");
  74. }
  75. /**
  76. * 营销活动
  77. * @return mixed
  78. */
  79. public function market()
  80. {
  81. $promotion_model = new PrmotionModel();
  82. $promotions = $promotion_model->getSitePromotions($this->site_id);
  83. $promotions = $this->filterAddon($promotions);
  84. $this->assign("promotion", $promotions);
  85. $user_info = $this->user_info;
  86. $this->assign('user_info', $user_info);
  87. $addon_quick_model = new AddonQuick();
  88. //店铺促销
  89. $shop_addon = $addon_quick_model->getAddonQuickByAddonType($promotions, 'shop');
  90. $this->assign('shop_addon', $shop_addon);
  91. $member_addon = $addon_quick_model->getAddonQuickByAddonType($promotions, 'member');
  92. $this->assign('member_addon', $member_addon);
  93. $addon_model = new Addon();
  94. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  95. $promotion_addon = $value[ 'promotion' ];
  96. $this->assign('common_addon', $promotion_addon);
  97. return $this->fetch("promotion/market");
  98. }
  99. /**
  100. * 会员营销
  101. * @return mixed
  102. */
  103. public function member()
  104. {
  105. $promotion_model = new PrmotionModel();
  106. $promotions = $promotion_model->getSitePromotions($this->site_id);
  107. $promotions = $this->filterAddon($promotions);
  108. $addon_quick_model = new AddonQuick();
  109. $addon = $addon_quick_model->getAddonQuickByAddonType($promotions, 'member');
  110. $this->assign('tool_addon', $addon);
  111. $user_info = $this->user_info;
  112. $this->assign('user_info', $user_info);
  113. $this->assign("promotion", $promotions);
  114. return $this->fetch("promotion/member");
  115. }
  116. /**
  117. * 营销工具
  118. * @return mixed
  119. */
  120. public function tool()
  121. {
  122. $promotion_model = new PrmotionModel();
  123. $promotions = $promotion_model->getPromotions();
  124. $promotions[ 'shop' ] = $this->filterAddon($promotions[ 'shop' ]);
  125. $this->assign("promotion", $promotions[ 'shop' ]);
  126. $addon_quick_model = new AddonQuick();
  127. $addon = $addon_quick_model->getAddonQuickByAddonType($promotions[ 'shop' ], 'tool');
  128. $this->assign('tool_addon', $addon);
  129. $user_info = $this->user_info;
  130. $this->assign('user_info', $user_info);
  131. $addon_model = new Addon();
  132. $value = $addon_model->getAddonQuickMenuConfig($this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  133. $tool_addon = $value[ 'tool' ];
  134. $this->assign('common_addon', $tool_addon);
  135. return $this->fetch("promotion/tool");
  136. }
  137. /**
  138. * @param $data
  139. */
  140. protected function filterAddon($data)
  141. {
  142. $res = [];
  143. foreach ($data as $key => $val) {
  144. if (in_array($val[ 'name' ], $this->addon_arr)) {
  145. $res[] = $val;
  146. }
  147. }
  148. return $res;
  149. }
  150. public function summary()
  151. {
  152. if (request()->isAjax()) {
  153. $coupon_model = new Coupon();
  154. $order_model = new Order();
  155. $account_model = new MemberAccount();
  156. $promotion = event('ShowPromotion', [ 'count' => 1, 'site_id' => $this->site_id ]);
  157. $promotion = array_map(function($item) {
  158. if (isset($item[ 'shop' ]) && !empty($item[ 'shop' ]) && isset($item[ 'shop' ][ 0 ][ 'summary' ]) && !empty($item[ 'shop' ][ 0 ][ 'summary' ])) return $item[ 'shop' ][ 0 ][ 'summary' ][ 'count' ];
  159. }, $promotion);
  160. $data = [
  161. 'promotion_num' => array_sum($promotion),
  162. 'coupon_total_count' => $coupon_model->getMemberCouponCount([ [ 'site_id', '=', $this->site_id ] ])[ 'data' ],
  163. 'coupon_used_count' => $coupon_model->getMemberCouponCount([ [ 'site_id', '=', $this->site_id ], [ 'state', '=', 2 ] ])[ 'data' ],
  164. 'buyer_num' => $order_model->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'promotion_type', '<>', '' ] ], 'order_id', 'a', null, 'member_id')[ 'data' ],
  165. 'deal_num' => $order_model->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'promotion_type', '<>', '' ], [ 'pay_status', '=', 1 ] ], 'order_id', 'a', null, 'member_id')[ 'data' ],
  166. 'order_num' => $order_model->getOrderCount([ [ 'site_id', '=', $this->site_id ], [ 'promotion_type', '<>', '' ], [ 'pay_status', '=', 1 ] ], 'order_id')[ 'data' ],
  167. 'order_money' => $order_model->getOrderMoneySum([ [ 'site_id', '=', $this->site_id ], [ 'promotion_type', '<>', '' ], [ 'pay_status', '=', 1 ] ])[ 'data' ],
  168. 'grant_point' => round($account_model->getMemberAccountSum([ [ 'site_id', '=', $this->site_id ], [ 'account_data', '>', 0 ], [ 'from_type', 'not in', [ 'adjust', 'refund', 'pointexchangerefund', 'presale_refund' ] ] ], 'account_data')[ 'data' ])
  169. ];
  170. return success(0, '', $data);
  171. }
  172. }
  173. /**
  174. * 常用功能设置
  175. */
  176. public function commonAddonSetting()
  177. {
  178. if (request()->isAjax()) {
  179. $addon_model = new Addon();
  180. $res = $addon_model->setAddonQuickMenuConfig([
  181. 'site_id' => $this->site_id,
  182. 'app_module' => $this->app_module,
  183. 'addon' => input('addon', ''),
  184. 'type' => input('type', 'promotion')
  185. ]);
  186. return $res;
  187. }
  188. }
  189. /**
  190. * 活动专区页配置
  191. * @return mixed
  192. */
  193. public function zoneConfig()
  194. {
  195. $promotion_model = new PrmotionModel();
  196. if (request()->isAjax()) {
  197. $data = [
  198. 'name' => input('name', ''),
  199. 'title' => input('title', ''),
  200. 'bg_color' => input('bg_color', ''), // 背景色
  201. ];
  202. $res = $promotion_model->setPromotionZoneConfig($data, $this->site_id, $this->app_module);
  203. return $res;
  204. } else {
  205. $promotion_zone_list = event('PromotionZoneConfig');
  206. $this->assign('promotion_zone_list', $promotion_zone_list);
  207. $promotion_config_list = []; // 活动专区页面配置列表
  208. $config = []; // 第一个活动页面配置
  209. if (!empty($promotion_zone_list)) {
  210. foreach ($promotion_zone_list as $k => $v) {
  211. $promotion_config_list[ $v[ 'name' ] ] = $promotion_model->getPromotionZoneConfig($v[ 'name' ], $this->site_id, $this->app_module)[ 'data' ][ 'value' ];
  212. if ($k == 0) {
  213. $config = $promotion_config_list[ $v[ 'name' ] ];
  214. }
  215. }
  216. }
  217. $this->assign("config", $config);
  218. $this->assign("promotion_config_list", $promotion_config_list);
  219. return $this->fetch("promotion/zone_config");
  220. }
  221. }
  222. }