Sellerpromotionbargain.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 Sellerpromotionbargain extends BaseSeller
  20. {
  21. public function initialize()
  22. {
  23. parent::initialize();
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionbargain.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. $bargainquota_model = model('pbargainquota');
  32. $pbargain_model = model('pbargain');
  33. if (check_platform_store()) {
  34. View::assign('isPlatformStore', true);
  35. } else {
  36. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  37. View::assign('current_bargain_quota', $current_bargain_quota);
  38. }
  39. $condition = array();
  40. $condition[] = array('store_id', '=', $this->store_info['store_id']);
  41. if ((input('param.bargain_name'))) {
  42. $condition[] = array('bargain_name', 'like', '%' . input('param.bargain_name') . '%');
  43. }
  44. if (input('param.state') != '' && in_array(input('param.state'), array(0, 1, 2, 3))) {
  45. $condition[] = array('bargain_state', '=', intval(input('param.state')));
  46. }
  47. $bargain_list = $pbargain_model->getBargainList($condition, 10, 'bargain_id desc');
  48. foreach ($bargain_list as $key => $val) {
  49. $bargain_list[$key]['bargain_state_text'] = $pbargain_model->getBargainStateText($val);
  50. $bargain_list[$key] = array_merge($bargain_list[$key], $pbargain_model->getBargainBtn($val));
  51. }
  52. View::assign('bargain_list', $bargain_list);
  53. View::assign('show_page', $pbargain_model->page_info->render());
  54. View::assign('bargain_state_array', $pbargain_model->getBargainStateArray());
  55. $this->setSellerCurMenu('Sellerpromotionbargain');
  56. $this->setSellerCurItem('bargain_list');
  57. return View::fetch($this->template_dir . 'index');
  58. }
  59. /**
  60. * 添加砍价活动
  61. * */
  62. public function bargain_add()
  63. {
  64. if (!request()->isPost()) {
  65. if (check_platform_store()) {
  66. View::assign('isPlatformStore', true);
  67. } else {
  68. View::assign('isPlatformStore', false);
  69. $bargainquota_model = model('pbargainquota');
  70. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  71. if (empty($current_bargain_quota)) {
  72. if (intval(config('ds_config.promotion_bargain_price')) != 0) {
  73. $this->error(lang('bargain_quota_current_error1'));
  74. } else {
  75. $current_bargain_quota = array('bargainquota_starttime' => TIMESTAMP, 'bargainquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  76. }
  77. }
  78. View::assign('current_bargain_quota', $current_bargain_quota);
  79. }
  80. //输出导航
  81. $this->setSellerCurMenu('Sellerpromotionbargain');
  82. $this->setSellerCurItem('bargain_add');
  83. return View::fetch($this->template_dir . 'bargain_add');
  84. } else {
  85. $data = $this->post_data();
  86. if (!$this->_check_allow_bargain($data['bargain_goods_id'])) {
  87. ds_json_encode(10001, lang('sellerpromotionbargain_goods_not_allow'));
  88. }
  89. $data['member_id'] = $this->store_info['member_id'];
  90. $data['member_name'] = $this->store_info['member_name'];
  91. $data['store_id'] = $this->store_info['store_id'];
  92. $data['store_name'] = $this->store_info['store_name'];
  93. if (!check_platform_store()) {
  94. //获取当前套餐
  95. $bargainquota_model = model('pbargainquota');
  96. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  97. if (empty($current_bargain_quota)) {
  98. if (intval(config('ds_config.promotion_bargain_price')) != 0) {
  99. ds_json_encode(10001, lang('please_buy_package_first'));
  100. } else {
  101. $current_bargain_quota = array('bargainquota_starttime' => TIMESTAMP, 'bargainquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  102. }
  103. }
  104. $quota_start_time = intval($current_bargain_quota['bargainquota_starttime']);
  105. $quota_end_time = intval($current_bargain_quota['bargainquota_endtime']);
  106. if ($data['bargain_begintime'] < $quota_start_time) {
  107. ds_json_encode(10001, sprintf(lang('bargain_add_start_time_explain'), date('Y-m-d', $current_bargain_quota['bargainquota_starttime'])));
  108. }
  109. if ($data['bargain_endtime'] > $quota_end_time) {
  110. ds_json_encode(10001, sprintf(lang('bargain_add_end_time_explain'), date('Y-m-d', $current_bargain_quota['bargainquota_endtime'])));
  111. }
  112. }
  113. //生成活动
  114. $pbargain_model = model('pbargain');
  115. $result = $pbargain_model->addBargain($data);
  116. if ($result) {
  117. $this->recordSellerlog(lang('add_group_activities') . $data['bargain_name'] . lang('activity_number') . $result);
  118. $pbargain_model->_dGoodsBargainCache($data['bargain_goods_id']); #清除缓存
  119. ds_json_encode(10000, lang('bargain_add_success'));
  120. } else {
  121. ds_json_encode(10001, lang('bargain_add_fail'));
  122. }
  123. }
  124. }
  125. public function post_data()
  126. {
  127. $data = array(
  128. 'bargain_name' => input('post.bargain_name'),
  129. 'bargain_limit' => input('post.bargain_limit'),
  130. 'bargain_time' => input('post.bargain_time'),
  131. 'bargain_floorprice' => input('post.bargain_floorprice'),
  132. 'bargain_total' => input('post.bargain_total'),
  133. 'bargain_max' => input('post.bargain_max'),
  134. 'bargain_remark' => input('post.bargain_remark'),
  135. 'bargain_begintime' => input('post.start_time'),
  136. 'bargain_endtime' => input('post.end_time'),
  137. );
  138. $pbargain_validate = ds_validate('pbargain');
  139. if (!$pbargain_validate->scene('pbargin_save')->check($data)) {
  140. ds_json_encode(10001, $pbargain_validate->getError());
  141. }
  142. //获取提交的数据
  143. $goods_id = intval(input('post.bargain_goods_id'));
  144. if (empty($goods_id)) {
  145. ds_json_encode(10001, lang('param_error'));
  146. }
  147. $goods_model = model('goods');
  148. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  149. if (empty($goods_info) || $goods_info['store_id'] != $this->store_info['store_id']) {
  150. ds_json_encode(10001, lang('param_error'));
  151. }
  152. $data['bargain_begintime'] = strtotime($data['bargain_begintime']);
  153. $data['bargain_endtime'] = strtotime($data['bargain_endtime']);
  154. if ($data['bargain_endtime'] <= TIMESTAMP) {
  155. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_endtime_error'));
  156. }
  157. if ($data['bargain_begintime'] <= TIMESTAMP) {
  158. $data['bargain_state'] = 2;
  159. } else {
  160. $data['bargain_state'] = 1;
  161. }
  162. $data['bargain_goods_id'] = $goods_info['goods_id'];
  163. $data['bargain_goods_name'] = $goods_info['goods_name'];
  164. $data['bargain_goods_price'] = $goods_info['goods_price'];
  165. $data['bargain_goods_image'] = $goods_info['goods_image'];
  166. $data['bargain_goods_commonid'] = $goods_info['goods_commonid'];
  167. //砍价差价*100必须大于底价刀数(因为1刀做少0.01元)
  168. if (($data['bargain_goods_price'] - $data['bargain_floorprice']) * 100 < $data['bargain_total']) {
  169. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_total_error'));
  170. }
  171. if (($data['bargain_goods_price'] - $data['bargain_floorprice']) > ($data['bargain_total'] * $data['bargain_max'])) {
  172. ds_json_encode(10001, lang('sellerpromotionbargain_bargain_limit_error'));
  173. }
  174. return $data;
  175. }
  176. /**
  177. * 编辑砍价活动
  178. * */
  179. public function bargain_edit()
  180. {
  181. $bargain_id = input('param.bargain_id');
  182. $pbargain_model = model('pbargain');
  183. $bargain_info = $pbargain_model->getBargainInfoByID($bargain_id, $this->store_info['store_id']);
  184. $btn = $pbargain_model->getBargainBtn($bargain_info);
  185. if ($btn) {
  186. $bargain_info = array_merge($bargain_info, $btn);
  187. }
  188. if (!request()->isPost()) {
  189. if (check_platform_store()) {
  190. View::assign('isPlatformStore', true);
  191. } else {
  192. View::assign('isPlatformStore', false);
  193. }
  194. if (empty($bargain_info) || !$bargain_info['editable']) {
  195. $this->error(lang('param_error'));
  196. }
  197. View::assign('bargain_info', $bargain_info);
  198. //输出导航
  199. $this->setSellerCurMenu('Sellerpromotionbargain');
  200. $this->setSellerCurItem('bargain_edit');
  201. return View::fetch($this->template_dir . 'bargain_add');
  202. } else {
  203. if (empty($bargain_info) || !$bargain_info['editable']) {
  204. ds_json_encode(10001, lang('param_error'));
  205. }
  206. $data = $this->post_data();
  207. if (!$this->_check_allow_bargain($data['bargain_goods_id'], $bargain_info['bargain_id'])) {
  208. ds_json_encode(10001, lang('sellerpromotionbargain_goods_not_allow'));
  209. }
  210. $result = $pbargain_model->editBargain($data, array('bargain_id' => $bargain_id));
  211. if ($result) {
  212. $this->recordSellerlog(lang('edit_group_activities') . $data['bargain_name'] . lang('activity_number') . $bargain_id);
  213. $pbargain_model->_dGoodsBargainCache($data['bargain_goods_id']); #清除缓存
  214. ds_json_encode(10000, lang('ds_common_op_succ'));
  215. } else {
  216. ds_json_encode(10001, lang('ds_common_op_fail'));
  217. }
  218. }
  219. }
  220. /**
  221. * 砍价活动 取消
  222. */
  223. public function bargain_end()
  224. {
  225. $bargain_id = intval(input('post.bargain_id'));
  226. $pbargain_model = model('pbargain');
  227. $bargain_info = $pbargain_model->getBargainInfoByID($bargain_id, $this->store_info['store_id']);
  228. if (!$bargain_info) {
  229. ds_json_encode(10001, lang('param_error'));
  230. }
  231. $btn = $pbargain_model->getBargainBtn($bargain_info);
  232. if (!$btn['editable']) {
  233. ds_json_encode(10001, lang('param_error'));
  234. }
  235. /**
  236. * 指定砍价活动结束
  237. */
  238. $condition = array();
  239. $condition[] = array('bargain_id', '=', $bargain_id);
  240. $condition[] = array('bargain_state', '=', 1);
  241. $result = $pbargain_model->cancelBargain($condition);
  242. if ($result) {
  243. $this->recordSellerlog(lang('group_activities_end_early') . $bargain_info['bargain_name'] . lang('activity_number') . $bargain_id);
  244. $pbargain_model->_dGoodsBargainCache($bargain_info['bargain_goods_id']); #清除缓存
  245. ds_json_encode(10000, lang('ds_common_op_succ'));
  246. } else {
  247. ds_json_encode(10001, lang('ds_common_op_fail'));
  248. }
  249. }
  250. /**
  251. * 商品砍价订单列表
  252. */
  253. public function bargain_order()
  254. {
  255. $pbargain_model = model('pbargain');
  256. $pbargainorder_model = model('pbargainorder');
  257. $bargain_id = intval(input('param.bargain_id'));
  258. //判断此砍价是否属于店铺
  259. $pbargain = $pbargain_model->getBargainInfo(array('store_id' => $this->store_info['store_id'], 'bargain_id' => $bargain_id));
  260. if (empty($pbargain)) {
  261. $this->error(lang('param_error'));
  262. }
  263. $condition = array();
  264. $condition[] = array('bargain_id', '=', $bargain_id);
  265. if (input('param.bargainorder_state') != '') {
  266. $condition[] = array('bargainorder_state', '=', intval(input('param.bargainorder_state')));
  267. }
  268. $pbargainorder_list = $pbargainorder_model->getPbargainorderList($condition, 10);
  269. View::assign('show_page', $pbargainorder_model->page_info->render());
  270. View::assign('pbargainorder_list', $pbargainorder_list);
  271. View::assign('bargainorder_state_array', $pbargainorder_model->getBargainorderStateArray());
  272. $this->setSellerCurMenu('Sellerpromotionbargain');
  273. $this->setSellerCurItem('bargain_order');
  274. return View::fetch($this->template_dir . 'bargain_order');
  275. }
  276. /**
  277. * 商品砍价记录列表
  278. */
  279. public function bargain_log()
  280. {
  281. $pbargainlog_model = model('pbargainlog');
  282. $bargainorder_id = intval(input('param.bargainorder_id'));
  283. $condition = array();
  284. $condition[] = array('bargainorder_id', '=', $bargainorder_id);
  285. $pbargainlog_list = $pbargainlog_model->getPbargainlogList($condition, 10); #获取砍价记录信息
  286. View::assign('show_page', $pbargainlog_model->page_info->render());
  287. View::assign('pbargainlog_list', $pbargainlog_list);
  288. $this->setSellerCurMenu('Sellerpromotionbargain');
  289. $this->setSellerCurItem('bargain_log');
  290. return View::fetch($this->template_dir . 'bargain_log');
  291. }
  292. /**
  293. * 砍价套餐购买
  294. * */
  295. public function bargain_quota_add()
  296. {
  297. //输出导航
  298. $this->setSellerCurMenu('Sellerpromotionbargain');
  299. $this->setSellerCurItem('bargain_quota_add');
  300. return View::fetch($this->template_dir . 'bargain_quota_add');
  301. }
  302. /**
  303. * 砍价套餐购买保存
  304. * */
  305. public function bargain_quota_add_save()
  306. {
  307. if (intval(config('ds_config.promotion_bargain_price')) == 0) {
  308. ds_json_encode(10001, lang('param_error'));
  309. }
  310. $bargain_quota_quantity = intval(input('post.bargain_quota_quantity'));
  311. if ($bargain_quota_quantity <= 0 || $bargain_quota_quantity > 12) {
  312. ds_json_encode(10001, lang('bargain_quota_quantity_error'));
  313. }
  314. //获取当前价格
  315. $current_price = intval(config('ds_config.promotion_bargain_price'));
  316. //获取该用户已有套餐
  317. $bargainquota_model = model('pbargainquota');
  318. $current_bargain_quota = $bargainquota_model->getBargainquotaCurrent($this->store_info['store_id']);
  319. $bargain_add_time = 86400 * 30 * $bargain_quota_quantity;
  320. if (empty($current_bargain_quota)) {
  321. //生成套餐
  322. $param = array();
  323. $param['member_id'] = session('member_id');
  324. $param['member_name'] = session('member_name');
  325. $param['store_id'] = $this->store_info['store_id'];
  326. $param['store_name'] = session('store_name');
  327. $param['bargainquota_starttime'] = TIMESTAMP;
  328. $param['bargainquota_endtime'] = TIMESTAMP + $bargain_add_time;
  329. $bargainquota_model->addBargainquota($param);
  330. } else {
  331. $param = array();
  332. $param['bargainquota_endtime'] = Db::raw('bargainquota_endtime+' . $bargain_add_time);
  333. $bargainquota_model->editBargainquota($param, array('bargainquota_id' => $current_bargain_quota['bargainquota_id']));
  334. }
  335. //记录店铺费用
  336. $this->recordStorecost($current_price * $bargain_quota_quantity, lang('buy_spell_group'));
  337. $this->recordSellerlog(lang('buy') . $bargain_quota_quantity . lang('combo_pack') . $current_price . lang('ds_yuan'));
  338. ds_json_encode(10000, lang('bargain_quota_add_success'));
  339. }
  340. /**
  341. * 选择活动商品
  342. * */
  343. public function search_goods()
  344. {
  345. $goods_model = model('goods');
  346. $condition = array();
  347. $condition[] = array('goods.store_id', '=', $this->store_info['store_id']);
  348. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  349. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 8, 'bargain');
  350. View::assign('goods_list', $goods_list);
  351. View::assign('show_page', $goods_model->page_info->render());
  352. echo View::fetch($this->template_dir . 'search_goods');
  353. exit;
  354. }
  355. public function bargain_goods_info()
  356. {
  357. $goods_id = intval(input('param.goods_id'));
  358. $data = array();
  359. $data['result'] = true;
  360. //获取商品具体信息用于显示
  361. $goods_model = model('goods');
  362. $goods_info = $goods_model->getGoodsOnlineInfoByID($goods_id);
  363. if (empty($goods_info)) {
  364. $data['result'] = false;
  365. $data['message'] = lang('param_error');
  366. echo json_encode($data);
  367. die;
  368. }
  369. $data['goods_id'] = $goods_info['goods_id'];
  370. $data['goods_name'] = $goods_info['goods_name'];
  371. $data['goods_price'] = $goods_info['goods_price'];
  372. $data['goods_image'] = goods_thumb($goods_info, 240);
  373. $data['goods_href'] = (string) url('Goods/index', array('goods_id' => $goods_info['goods_id']));
  374. echo json_encode($data);
  375. die;
  376. }
  377. /*
  378. * 判断此商品是否已经参加砍价
  379. */
  380. private function _check_allow_bargain($goods_id, $bargain_id = 0)
  381. {
  382. $condition = array();
  383. $condition[] = array('bargain_goods_id', '=', $goods_id);
  384. $condition[] = array('bargain_state', '=', 1);
  385. if ($bargain_id) {
  386. $condition[] = array('bargain_id', '<>', $bargain_id);
  387. }
  388. $bargain = model('pbargain')->getBargainInfo($condition);
  389. if ($bargain) {
  390. return false;
  391. } else {
  392. return true;
  393. }
  394. }
  395. protected function getSellerItemList()
  396. {
  397. $menu_array = array(
  398. array(
  399. 'name' => 'bargain_list', 'text' => lang('bargain_active_list'),
  400. 'url' => (string) url('Sellerpromotionbargain/index')
  401. ),
  402. );
  403. switch (request()->action()) {
  404. case 'bargain_add':
  405. $menu_array[] = array(
  406. 'name' => 'bargain_add', 'text' => lang('bargain_add'),
  407. 'url' => (string) url('Sellerpromotionbargain/bargain_add')
  408. );
  409. break;
  410. case 'bargain_edit':
  411. $menu_array[] = array(
  412. 'name' => 'bargain_edit', 'text' => lang('bargain_edit'), 'url' => 'javascript:;'
  413. );
  414. break;
  415. case 'bargain_quota_add':
  416. $menu_array[] = array(
  417. 'name' => 'bargain_quota_add', 'text' => lang('bargain_quota_add'),
  418. 'url' => (string) url('Sellerpromotionbargain/bargain_quota_add')
  419. );
  420. break;
  421. case 'bargain_order':
  422. $menu_array[] = array(
  423. 'name' => 'bargain_order', 'text' => lang('bargainorder'),
  424. 'url' => (string) url('Sellerpromotionbargain/bargain_order', ['bargain_id' => input('param.bargain_id')])
  425. );
  426. break;
  427. case 'bargain_log':
  428. $menu_array[] = array(
  429. 'name' => 'bargain_log', 'text' => lang('pbargainlog'),
  430. 'url' => (string) url('Sellerpromotionbargain/bargain_log', ['bargainorder_id' => input('param.bargainorder_id')])
  431. );
  432. break;
  433. }
  434. return $menu_array;
  435. }
  436. }