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