Sellerpromotionpintuan.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * 卖家拼团管理
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. use think\facade\Db;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. *
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Sellerpromotionpintuan extends BaseSeller
  20. {
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionpintuan.lang.php');
  25. if (intval(config('ds_config.promotion_allow')) !== 1) {
  26. $this->error(lang('promotion_unavailable'), 'seller/index');
  27. }
  28. }
  29. public function index()
  30. {
  31. $pintuanquota_model = model('ppintuanquota');
  32. $ppintuan_model = model('ppintuan');
  33. if (check_platform_store()) {
  34. View::assign('isPlatformStore', true);
  35. } else {
  36. $current_pintuan_quota = $pintuanquota_model->getPintuanquotaCurrent(session('store_id'));
  37. View::assign('current_pintuan_quota', $current_pintuan_quota);
  38. }
  39. $condition = array();
  40. $condition[] = array('store_id', '=', session('store_id'));
  41. if ((input('param.pintuan_name'))) {
  42. $condition[] = array('pintuan_name', 'like', '%' . input('param.pintuan_name') . '%');
  43. }
  44. if (input('param.state') != '') {
  45. $condition[] = array('pintuan_state', '=', intval(input('param.state')));
  46. }
  47. $pintuan_list = $ppintuan_model->getPintuanList($condition, 10, 'pintuan_state desc, pintuan_end_time desc');
  48. View::assign('pintuan_list', $pintuan_list);
  49. View::assign('show_page', $ppintuan_model->page_info->render());
  50. View::assign('pintuan_state_array', $ppintuan_model->getPintuanStateArray());
  51. $this->setSellerCurMenu('Sellerpromotionpintuan');
  52. $this->setSellerCurItem('pintuan_list');
  53. return View::fetch($this->template_dir . 'index');
  54. }
  55. /**
  56. * 添加拼团活动
  57. * */
  58. public function pintuan_add()
  59. {
  60. if (!request()->isPost()) {
  61. if (check_platform_store()) {
  62. View::assign('isPlatformStore', true);
  63. } else {
  64. View::assign('isPlatformStore', false);
  65. $pintuanquota_model = model('ppintuanquota');
  66. $current_pintuan_quota = $pintuanquota_model->getPintuanquotaCurrent(session('store_id'));
  67. if (empty($current_pintuan_quota)) {
  68. if (intval(config('ds_config.promotion_pintuan_price')) != 0) {
  69. $this->error(lang('pintuan_quota_current_error1'));
  70. } else {
  71. $current_pintuan_quota = array('pintuanquota_starttime' => TIMESTAMP, 'pintuanquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  72. }
  73. }
  74. View::assign('current_pintuan_quota', $current_pintuan_quota);
  75. }
  76. //输出导航
  77. $this->setSellerCurMenu('Sellerpromotionpintuan');
  78. $this->setSellerCurItem('pintuan_add');
  79. return View::fetch($this->template_dir . 'pintuan_add');
  80. } else {
  81. //验证输入
  82. $pintuan_name = trim(input('post.pintuan_name'));
  83. $start_time = strtotime(input('post.start_time'));
  84. $end_time = strtotime(input('post.end_time'));
  85. $pintuan_limit_number = intval(input('post.pintuan_limit_number'));
  86. if ($pintuan_limit_number <= 1) {
  87. $pintuan_limit_number = 2;
  88. }
  89. //成团时限
  90. $pintuan_limit_hour = intval(input('post.pintuan_limit_hour'));
  91. if ($pintuan_limit_hour <= 0) {
  92. $pintuan_limit_hour = 1;
  93. }
  94. //购买限制
  95. $pintuan_limit_quantity = intval(input('post.pintuan_limit_quantity'));
  96. if ($pintuan_limit_quantity <= 0) {
  97. $pintuan_limit_quantity = 1;
  98. }
  99. //购买折扣
  100. $pintuan_zhe = intval(input('post.pintuan_zhe'));
  101. if ($pintuan_zhe <= 0 || $pintuan_zhe >= 10) {
  102. $pintuan_zhe = 1;
  103. }
  104. if (empty($pintuan_name)) {
  105. ds_json_encode(10001, lang('pintuan_name_error'));
  106. }
  107. if ($start_time >= $end_time) {
  108. ds_json_encode(10001, lang('greater_than_start_time'));
  109. }
  110. if (!check_platform_store()) {
  111. //获取当前套餐
  112. $pintuanquota_model = model('ppintuanquota');
  113. $current_pintuan_quota = $pintuanquota_model->getPintuanquotaCurrent(session('store_id'));
  114. if (empty($current_pintuan_quota)) {
  115. if (intval(config('ds_config.promotion_pintuan_price')) != 0) {
  116. ds_json_encode(10001, lang('please_buy_package_first'));
  117. } else {
  118. $current_pintuan_quota = array('pintuanquota_starttime' => TIMESTAMP, 'pintuanquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  119. }
  120. }
  121. $quota_start_time = intval($current_pintuan_quota['pintuanquota_starttime']);
  122. $quota_end_time = intval($current_pintuan_quota['pintuanquota_endtime']);
  123. if ($start_time < $quota_start_time) {
  124. ds_json_encode(10001, sprintf(lang('pintuan_add_start_time_explain'), date('Y-m-d', $current_pintuan_quota['pintuanquota_starttime'])));
  125. }
  126. if ($end_time > $quota_end_time) {
  127. ds_json_encode(10001, sprintf(lang('pintuan_add_end_time_explain'), date('Y-m-d', $current_pintuan_quota['pintuanquota_endtime'])));
  128. }
  129. }
  130. if ($end_time < TIMESTAMP) {
  131. ds_json_encode(10001, sprintf(lang('pintuan_add_end_time_explain'), date('Y-m-d')));
  132. }
  133. //获取提交的数据
  134. $goods_id = intval(input('post.pintuan_goods_id'));
  135. if (empty($goods_id)) {
  136. ds_json_encode(10001, lang('param_error'));
  137. }
  138. $goods_model = model('goods');
  139. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  140. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  141. ds_json_encode(10001, lang('param_error'));
  142. }
  143. //判断此商品是否在拼团中,拼团中的商品是不可以参加活动
  144. $result = $this->_check_allow_pintuan($goods_info['goods_commonid']);
  145. if ($result != TRUE) {
  146. ds_json_encode(10001, $result['message']);
  147. }
  148. //生成活动
  149. $ppintuan_model = model('ppintuan');
  150. $param = array();
  151. $param['pintuan_name'] = $pintuan_name;
  152. $param['pintuanquota_id'] = isset($current_pintuan_quota['pintuanquota_id']) ? $current_pintuan_quota['pintuanquota_id'] : 0;
  153. $param['pintuan_starttime'] = $start_time;
  154. $param['pintuan_end_time'] = $end_time;
  155. $param['store_id'] = session('store_id');
  156. $param['store_name'] = session('store_name');
  157. $param['member_id'] = session('member_id');
  158. $param['member_name'] = session('member_name');
  159. $param['pintuan_zhe'] = $pintuan_zhe;
  160. $param['pintuan_limit_number'] = $pintuan_limit_number;
  161. $param['pintuan_limit_hour'] = $pintuan_limit_hour;
  162. $param['pintuan_limit_quantity'] = $pintuan_limit_quantity;
  163. $param['pintuan_goods_id'] = $goods_info['goods_id'];
  164. $param['pintuan_is_virtual'] = $goods_info['is_virtual'];
  165. $param['pintuan_goods_price'] = $goods_info['goods_price'];
  166. $param['pintuan_goods_name'] = $goods_info['goods_name'];
  167. $param['pintuan_goods_commonid'] = $goods_info['goods_commonid'];
  168. $param['pintuan_image'] = $goods_info['goods_image'];
  169. $result = $ppintuan_model->addPintuan($param);
  170. if ($result) {
  171. $this->recordSellerlog(lang('add_group_activities') . $pintuan_name . lang('activity_number') . $result);
  172. $ppintuan_model->_dGoodsPintuanCache($goods_info['goods_commonid']); #清除缓存
  173. ds_json_encode(10000, lang('pintuan_add_success'));
  174. } else {
  175. ds_json_encode(10001, lang('pintuan_add_fail'));
  176. }
  177. }
  178. }
  179. /**
  180. * 编辑拼团活动
  181. * */
  182. public function pintuan_edit()
  183. {
  184. if (!request()->isPost()) {
  185. if (check_platform_store()) {
  186. View::assign('isPlatformStore', true);
  187. } else {
  188. View::assign('isPlatformStore', false);
  189. }
  190. $ppintuan_model = model('ppintuan');
  191. $pintuan_info = $ppintuan_model->getPintuanInfoByID(input('param.pintuan_id'));
  192. if (empty($pintuan_info) || !$pintuan_info['editable']) {
  193. $this->error(lang('param_error'));
  194. }
  195. View::assign('pintuan_info', $pintuan_info);
  196. //输出导航
  197. $this->setSellerCurMenu('Sellerpromotionpintuan');
  198. $this->setSellerCurItem('pintuan_edit');
  199. return View::fetch($this->template_dir . 'pintuan_add');
  200. } else {
  201. $pintuan_id = input('param.pintuan_id');
  202. $ppintuan_model = model('ppintuan');
  203. $pintuan_info = $ppintuan_model->getPintuanInfoByID($pintuan_id, session('store_id'));
  204. if (empty($pintuan_info) || !$pintuan_info['editable']) {
  205. $this->error(lang('param_error'));
  206. }
  207. //验证输入
  208. $pintuan_name = trim(input('post.pintuan_name'));
  209. if (empty($pintuan_name)) {
  210. ds_json_encode(10001, lang('pintuan_name_error'));
  211. }
  212. $pintuan_limit_number = intval(input('post.pintuan_limit_number'));
  213. if ($pintuan_limit_number <= 1) {
  214. $pintuan_limit_number = 2;
  215. }
  216. //成团时限
  217. $pintuan_limit_hour = intval(input('post.pintuan_limit_hour'));
  218. if ($pintuan_limit_hour <= 0) {
  219. $pintuan_limit_hour = 1;
  220. }
  221. //购买限制
  222. $pintuan_limit_quantity = intval(input('post.pintuan_limit_quantity'));
  223. if ($pintuan_limit_quantity <= 0) {
  224. $pintuan_limit_quantity = 1;
  225. }
  226. //购买折扣
  227. $pintuan_zhe = intval(input('post.pintuan_zhe'));
  228. if ($pintuan_zhe <= 0 || $pintuan_zhe >= 10) {
  229. $pintuan_zhe = 1;
  230. }
  231. //生成活动
  232. $param = array();
  233. $param['pintuan_name'] = $pintuan_name;
  234. $param['pintuan_zhe'] = $pintuan_zhe;
  235. $param['pintuan_limit_number'] = $pintuan_limit_number;
  236. $param['pintuan_limit_hour'] = $pintuan_limit_hour;
  237. $param['pintuan_limit_quantity'] = $pintuan_limit_quantity;
  238. $result = $ppintuan_model->editPintuan($param, array('pintuan_id' => $pintuan_id));
  239. if ($result) {
  240. $this->recordSellerlog(lang('edit_group_activities') . $pintuan_name . lang('activity_number') . $pintuan_id);
  241. $ppintuan_model->_dGoodsPintuanCache($pintuan_info['pintuan_goods_commonid']); #清除缓存
  242. ds_json_encode(10000, lang('ds_common_op_succ'));
  243. } else {
  244. ds_json_encode(10001, lang('ds_common_op_fail'));
  245. }
  246. }
  247. }
  248. /**
  249. * 拼团活动 提前结束
  250. */
  251. public function pintuan_end()
  252. {
  253. $pintuan_id = intval(input('post.pintuan_id'));
  254. $ppintuan_model = model('ppintuan');
  255. $pintuan_info = $ppintuan_model->getPintuanInfoByID($pintuan_id, session('store_id'));
  256. if (!$pintuan_info) {
  257. ds_json_encode(10001, lang('param_error'));
  258. }
  259. /**
  260. * 指定拼团活动结束
  261. */
  262. $result = $ppintuan_model->endPintuan(array('pintuan_id' => $pintuan_id));
  263. if ($result) {
  264. $this->recordSellerlog(lang('group_activities_end_early') . $pintuan_info['pintuan_name'] . lang('activity_number') . $pintuan_id);
  265. $ppintuan_model->_dGoodsPintuanCache($pintuan_info['pintuan_goods_commonid']); #清除缓存
  266. ds_json_encode(10000, lang('ds_common_op_succ'));
  267. } else {
  268. ds_json_encode(10001, lang('ds_common_op_fail'));
  269. }
  270. }
  271. /**
  272. * 查看拼团开团信息
  273. * @return type
  274. */
  275. public function pintuan_manage()
  276. {
  277. $ppintuan_model = model('ppintuan');
  278. $ppintuangroup_model = model('ppintuangroup');
  279. $ppintuanorder_model = model('ppintuanorder');
  280. $pintuan_id = intval(input('param.pintuan_id'));
  281. //判断此拼团是否属于店铺
  282. $ppintuan = $ppintuan_model->getPintuanInfo(array('store_id' => session('store_id'), 'pintuan_id' => $pintuan_id));
  283. if (empty($ppintuan)) {
  284. $this->error(lang('param_error'));
  285. }
  286. $condition = array();
  287. $condition[] = array('pintuan_id', '=', $pintuan_id);
  288. if (input('param.pintuangroup_state')) {
  289. $condition[] = array('pintuangroup_state', '=', intval(input('param.pintuangroup_state')));
  290. }
  291. $ppintuangroup_list = $ppintuangroup_model->getPpintuangroupList($condition, 10); #获取开团信息
  292. foreach ($ppintuangroup_list as $key => $ppintuangroup) {
  293. //获取开团订单下的参团订单
  294. $condition = array();
  295. $condition[] = array('pintuangroup_id', '=', $ppintuangroup['pintuangroup_id']);
  296. $ppintuangroup_list[$key]['order_list'] = $ppintuanorder_model->getPpintuanorderList($condition);
  297. }
  298. View::assign('show_page', $ppintuangroup_model->page_info->render());
  299. View::assign('pintuangroup_list', $ppintuangroup_list);
  300. View::assign('pintuangroup_state_array', $ppintuangroup_model->getPintuangroupStateArray());
  301. $this->setSellerCurMenu('Sellerpromotionpintuan');
  302. $this->setSellerCurItem('pintuan_manage');
  303. return View::fetch($this->template_dir . 'pintuan_manage');
  304. }
  305. /**
  306. * 拼团套餐购买
  307. * */
  308. public function pintuan_quota_add()
  309. {
  310. //输出导航
  311. $this->setSellerCurMenu('Sellerpromotionpintuan');
  312. $this->setSellerCurItem('pintuan_quota_add');
  313. return View::fetch($this->template_dir . 'pintuan_quota_add');
  314. }
  315. /**
  316. * 拼团套餐购买保存
  317. * */
  318. public function pintuan_quota_add_save()
  319. {
  320. if (intval(config('ds_config.promotion_pintuan_price')) == 0) {
  321. ds_json_encode(10001, lang('param_error'));
  322. }
  323. $pintuan_quota_quantity = intval(input('post.pintuan_quota_quantity'));
  324. if ($pintuan_quota_quantity <= 0 || $pintuan_quota_quantity > 12) {
  325. ds_json_encode(10001, lang('pintuan_quota_quantity_error'));
  326. }
  327. //获取当前价格
  328. $current_price = intval(config('ds_config.promotion_pintuan_price'));
  329. //获取该用户已有套餐
  330. $pintuanquota_model = model('ppintuanquota');
  331. $current_pintuan_quota = $pintuanquota_model->getPintuanquotaCurrent(session('store_id'));
  332. $pintuan_add_time = 86400 * 30 * $pintuan_quota_quantity;
  333. if (empty($current_pintuan_quota)) {
  334. //生成套餐
  335. $param = array();
  336. $param['member_id'] = session('member_id');
  337. $param['member_name'] = session('member_name');
  338. $param['store_id'] = session('store_id');
  339. $param['store_name'] = session('store_name');
  340. $param['pintuanquota_starttime'] = TIMESTAMP;
  341. $param['pintuanquota_endtime'] = TIMESTAMP + $pintuan_add_time;
  342. $pintuanquota_model->addPintuanquota($param);
  343. } else {
  344. $param = array();
  345. $param['pintuanquota_endtime'] = Db::raw('pintuanquota_endtime+' . $pintuan_add_time);
  346. $pintuanquota_model->editPintuanquota($param, array('pintuanquota_id' => $current_pintuan_quota['pintuanquota_id']));
  347. }
  348. //记录店铺费用
  349. $this->recordStorecost($current_price * $pintuan_quota_quantity, lang('buy_spell_group'));
  350. $this->recordSellerlog(lang('buy') . $pintuan_quota_quantity . lang('combo_pack') . $current_price . lang('ds_yuan'));
  351. ds_json_encode(10000, lang('pintuan_quota_add_success'));
  352. }
  353. /**
  354. * 选择活动商品
  355. * */
  356. public function search_goods()
  357. {
  358. $goods_model = model('goods');
  359. $condition = array();
  360. $condition[] = array('goods_lock', '=', 0);
  361. $condition[] = array('goods_state', '=', 1);
  362. $condition[] = array('goods_verify', '=', 1);
  363. $condition[] = array('store_id', '=', session('store_id'));
  364. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  365. $goods_list = $goods_model->getGoodsCommonListForPromotion($condition, '*', 8, '');
  366. View::assign('goods_list', $goods_list);
  367. View::assign('show_page', $goods_model->page_info->render());
  368. echo View::fetch($this->template_dir . 'search_goods');
  369. exit;
  370. }
  371. public function pintuan_goods_info()
  372. {
  373. $goods_commonid = intval(input('param.goods_commonid'));
  374. $data = array();
  375. $data['result'] = true;
  376. //判断此商品是否已经参加拼团,
  377. $result = $this->_check_allow_pintuan($goods_commonid);
  378. if ($result['result'] != TRUE) {
  379. echo json_encode($result);
  380. die;
  381. }
  382. //获取商品具体信息用于显示
  383. $goods_model = model('goods');
  384. $condition = array();
  385. $condition[] = array('goods_commonid', '=', $goods_commonid);
  386. $goods_list = $goods_model->getGoodsOnlineList($condition);
  387. if (empty($goods_list)) {
  388. $data['result'] = false;
  389. $data['message'] = lang('param_error');
  390. echo json_encode($data);
  391. die;
  392. }
  393. $goods_info = $goods_list[0];
  394. $data['goods_id'] = $goods_info['goods_id'];
  395. $data['goods_name'] = $goods_info['goods_name'];
  396. $data['goods_price'] = $goods_info['goods_price'];
  397. $data['goods_image'] = goods_thumb($goods_info, 240);
  398. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  399. echo json_encode($data);
  400. die;
  401. }
  402. /*
  403. * 判断此商品是否已经参加拼团
  404. */
  405. private function _check_allow_pintuan($goods_commonid)
  406. {
  407. $condition = array();
  408. $condition[] = array('pintuan_goods_commonid', '=', $goods_commonid);
  409. $condition[] = array('pintuan_state', '=', 1);
  410. $pintuan = model('ppintuan')->getPintuanInfo($condition);
  411. $result['result'] = TRUE;
  412. if (!empty($pintuan)) {
  413. $result['result'] = FALSE;
  414. $result['message'] = lang('goods_are_syndicate');
  415. }
  416. return $result;
  417. }
  418. protected function getSellerItemList()
  419. {
  420. $menu_array = array(
  421. array(
  422. 'name' => 'pintuan_list', 'text' => lang('pintuan_active_list'),
  423. 'url' => (string) url('Sellerpromotionpintuan/index')
  424. ),
  425. );
  426. switch (request()->action()) {
  427. case 'pintuan_add':
  428. $menu_array[] = array(
  429. 'name' => 'pintuan_add', 'text' => lang('pintuan_add'),
  430. 'url' => (string) url('Sellerpromotionpintuan/pintuan_add')
  431. );
  432. break;
  433. case 'pintuan_edit':
  434. $menu_array[] = array(
  435. 'name' => 'pintuan_edit', 'text' => lang('pintuan_edit'), 'url' => 'javascript:;'
  436. );
  437. break;
  438. case 'pintuan_quota_add':
  439. $menu_array[] = array(
  440. 'name' => 'pintuan_quota_add', 'text' => lang('pintuan_quota_add'),
  441. 'url' => (string) url('Sellerpromotionpintuan/pintuan_quota_add')
  442. );
  443. break;
  444. case 'pintuan_manage':
  445. $menu_array[] = array(
  446. 'name' => 'pintuan_manage', 'text' => lang('pintuan_manage'),
  447. 'url' => (string) url('Sellerpromotionpintuan/pintuan_manage', ['pintuan_id' => input('param.pintuan_id')])
  448. );
  449. break;
  450. }
  451. return $menu_array;
  452. }
  453. }