Sellerpromotionbargain.php 20 KB

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