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