Sellerpromotionmgdiscount.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. namespace app\home\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. use think\facade\Db;
  6. /**
  7. * ============================================================================
  8. *
  9. * ============================================================================
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Sellerpromotionmgdiscount extends BaseSeller
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize();
  21. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionmgdiscount.lang.php');
  22. if (intval(config('ds_config.mgdiscount_allow')) !== 1) {
  23. $this->error(lang('mgdiscount_unavailable'), 'seller/index');
  24. }
  25. }
  26. /**
  27. * 店铺的基本设置
  28. */
  29. public function mgdiscount_store()
  30. {
  31. $mgdiscountquota_model = model('pmgdiscountquota');
  32. //当前的和系统设置的会员等级进行比对
  33. if (!request()->isPost()) {
  34. if (check_platform_store()) {
  35. View::assign('isPlatformStore', true);
  36. } else {
  37. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  38. View::assign('current_mgdiscount_quota', $current_mgdiscount_quota);
  39. }
  40. //当前店铺设置的会员等级对应的折扣
  41. $store = Db::name('store')->where('store_id', session('store_id'))->find();
  42. View::assign('mgdiscount_store_arr', $this->_get_mgdiscount_arr($store['store_mgdiscount']));
  43. View::assign('store', $store);
  44. $this->setSellerCurMenu('Sellermgdiscount');
  45. $this->setSellerCurItem('mgdiscount_store');
  46. return View::fetch($this->template_dir . 'mgdiscount_store');
  47. } else {
  48. $member_model = model('member');
  49. //系统等级设置
  50. $membergrade_arr = $member_model->getMemberGradeArr();
  51. $result_1 = array();
  52. $pre_level_discount = 0;
  53. foreach ($membergrade_arr as $key => $value) {
  54. $current_level_discount = floatval($_POST['mgdiscount_store'][$key]['level_discount'] * 10);
  55. //限制会员等级高的会员享受更低折扣
  56. if ($pre_level_discount == 0) {
  57. $pre_level_discount = $current_level_discount;
  58. }
  59. if ($current_level_discount > $pre_level_discount) {
  60. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  61. }
  62. $pre_level_discount = $current_level_discount;
  63. if ($current_level_discount < 1 || $current_level_discount > 100) {
  64. ds_json_encode(10001, lang('current_level_discount_range_error'));
  65. }
  66. $result_1[$key]['level_discount'] = $current_level_discount / 10;
  67. $result_1[$key]['level_name'] = $value['level_name'];
  68. }
  69. $data = array(
  70. 'store_mgdiscount' => serialize($result_1),
  71. 'store_mgdiscount_state' => intval(input('post.store_mgdiscount_state'))
  72. );
  73. $result = Db::name('store')->where('store_id', session('store_id'))->update($data);
  74. if ($result) {
  75. ds_json_encode(10000, lang('ds_common_op_succ'));
  76. } else {
  77. ds_json_encode(10001, lang('ds_common_op_fail'));
  78. }
  79. }
  80. }
  81. //显示不同商品享受的会员等级折扣
  82. public function mgdiscount_goods()
  83. {
  84. $goods_model = model('goods');
  85. $condition[] = array('goods_mgdiscount', '<>', '');
  86. $condition[] = array('store_id', '=', session('store_id'));
  87. $goods_list = $goods_model->getGoodsCommonOnlineList($condition);
  88. foreach ($goods_list as $key => $goods) {
  89. $goods_list[$key]['goods_mgdiscount_arr'] = $this->_get_mgdiscount_arr($goods['goods_mgdiscount']);
  90. }
  91. View::assign('show_page', $goods_model->page_info->render());
  92. View::assign('goods_list', $goods_list);
  93. /* 设置卖家当前菜单 */
  94. $this->setSellerCurMenu('Sellermgdiscount');
  95. $this->setSellerCurItem('mgdiscount_goods');
  96. return View::fetch($this->template_dir . 'mgdiscount_goods');
  97. }
  98. /**
  99. * 通过系统会员等级和现有数据比对得出数值
  100. * @param type $mgdiscount_arr_temp
  101. * @return type
  102. */
  103. private function _get_mgdiscount_arr($mgdiscount_arr_temp)
  104. {
  105. $mgdiscount_arr_temp = @unserialize($mgdiscount_arr_temp);
  106. $member_model = model('member');
  107. //系统等级设置
  108. $membergrade_arr = $member_model->getMemberGradeArr();
  109. $mgdiscount_arr = array();
  110. foreach ($membergrade_arr as $key => $value) {
  111. $mgdiscount_arr[$key] = $value;
  112. $mgdiscount_arr[$key]['level_discount'] = isset($mgdiscount_arr_temp[$key]['level_discount']) ? $mgdiscount_arr_temp[$key]['level_discount'] : 10;
  113. }
  114. return $mgdiscount_arr;
  115. }
  116. //新增现有商品的折扣
  117. public function mgdiscount_goods_add()
  118. {
  119. $member_model = model('member');
  120. //系统等级设置
  121. $membergrade_arr = $member_model->getMemberGradeArr();
  122. if (!request()->isPost()) {
  123. View::assign('mgdiscount_goods_arr', $membergrade_arr);
  124. $this->setSellerCurMenu('Sellermgdiscount');
  125. $this->setSellerCurItem('mgdiscount_goods_add');
  126. return View::fetch($this->template_dir . 'mgdiscount_goods_add');
  127. } else {
  128. if (!check_platform_store()) {
  129. //获取当前套餐
  130. $mgdiscountquota_model = model('pmgdiscountquota');
  131. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  132. if (empty($current_mgdiscount_quota)) {
  133. if (intval(config('ds_config.mgdiscount_price')) != 0) {
  134. ds_json_encode(10001, lang('please_buy_package_first'));
  135. } else {
  136. $current_mgdiscount_quota = array('mgdiscountquota_starttime' => TIMESTAMP, 'mgdiscountquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  137. }
  138. }
  139. $quota_start_time = intval($current_mgdiscount_quota['mgdiscountquota_starttime']);
  140. $quota_end_time = intval($current_mgdiscount_quota['mgdiscountquota_endtime']);
  141. if (TIMESTAMP < $quota_start_time) {
  142. ds_json_encode(10001, sprintf(lang('mgdiscount_add_start_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_starttime'])));
  143. }
  144. if (TIMESTAMP > $quota_end_time) {
  145. ds_json_encode(10001, sprintf(lang('mgdiscount_add_end_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_endtime'])));
  146. }
  147. }
  148. //获取提交的数据
  149. $goods_id = intval(input('post.mgdiscount_goods_id'));
  150. if (empty($goods_id)) {
  151. ds_json_encode(10001, lang('param_error'));
  152. }
  153. $goods_model = model('goods');
  154. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  155. if (empty($goods_info) || $goods_info['store_id'] != session('store_id')) {
  156. ds_json_encode(10001, lang('param_error'));
  157. }
  158. $data = array();
  159. $pre_level_discount = 0;
  160. foreach ($membergrade_arr as $key => $value) {
  161. $current_level_discount = floatval($_POST['mgdiscount_goods'][$key]['level_discount'] * 10);
  162. //限制会员等级高的会员享受更低折扣
  163. if ($pre_level_discount == 0) {
  164. $pre_level_discount = $current_level_discount;
  165. }
  166. if ($current_level_discount > $pre_level_discount) {
  167. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  168. }
  169. $pre_level_discount = $current_level_discount;
  170. if ($current_level_discount < 1 || $current_level_discount > 100) {
  171. ds_json_encode(10001, lang('current_level_discount_range_error'));
  172. }
  173. $data[$key]['level_discount'] = $current_level_discount / 10;
  174. $data[$key]['level_name'] = $value['level_name'];
  175. }
  176. $condition = array();
  177. $condition[] = array('goods_commonid', '=', $goods_info['goods_commonid']);
  178. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => serialize($data)), $condition);
  179. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => serialize($data)), $condition);
  180. if ($result && $result1) {
  181. $this->recordSellerlog('添加会员等级折扣,公共商品ID:' . $goods_info['goods_commonid']);
  182. ds_json_encode(10000, lang('mgdiscount_add_success'));
  183. } else {
  184. ds_json_encode(10001, lang('mgdiscount_add_fail'));
  185. }
  186. }
  187. }
  188. function mgdiscount_goods_edit()
  189. {
  190. //获取提交的数据
  191. $goods_commonid = intval(input('param.goods_commonid'));
  192. if (empty($goods_commonid)) {
  193. ds_json_encode(10001, lang('param_error'));
  194. }
  195. $goods_model = model('goods');
  196. $goodscommon_info = $goods_model->getGoodsCommonInfoByID($goods_commonid);
  197. if (empty($goodscommon_info) || $goodscommon_info['store_id'] != session('store_id')) {
  198. ds_json_encode(10001, lang('goods_not_exist'));
  199. }
  200. if (!request()->isPost()) {
  201. View::assign('goodscommon_info', $goodscommon_info);
  202. View::assign('mgdiscount_goods_arr', $this->_get_mgdiscount_arr($goodscommon_info['goods_mgdiscount']));
  203. $this->setSellerCurMenu('Sellermgdiscount');
  204. $this->setSellerCurItem('mgdiscount_goods_add');
  205. return View::fetch($this->template_dir . 'mgdiscount_goods_add');
  206. } else {
  207. if (!check_platform_store()) {
  208. //获取当前套餐
  209. $mgdiscountquota_model = model('pmgdiscountquota');
  210. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  211. if (empty($current_mgdiscount_quota)) {
  212. if (intval(config('ds_config.mgdiscount_price')) != 0) {
  213. ds_json_encode(10001, lang('please_buy_package_first'));
  214. } else {
  215. $current_mgdiscount_quota = array('mgdiscountquota_starttime' => TIMESTAMP, 'mgdiscountquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  216. }
  217. }
  218. $quota_start_time = intval($current_mgdiscount_quota['mgdiscountquota_starttime']);
  219. $quota_end_time = intval($current_mgdiscount_quota['mgdiscountquota_endtime']);
  220. if ($quota_start_time < $quota_start_time) {
  221. ds_json_encode(10001, sprintf(lang('mgdiscount_add_start_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_starttime'])));
  222. }
  223. if ($quota_start_time > $quota_end_time) {
  224. ds_json_encode(10001, sprintf(lang('mgdiscount_add_end_time_explain'), date('Y-m-d', $current_mgdiscount_quota['mgdiscountquota_endtime'])));
  225. }
  226. }
  227. $member_model = model('member');
  228. //系统等级设置
  229. $membergrade_arr = $member_model->getMemberGradeArr();
  230. $data = array();
  231. $pre_level_discount = 0;
  232. foreach ($membergrade_arr as $key => $value) {
  233. $current_level_discount = intval($_POST['mgdiscount_goods'][$key]['level_discount'] * 10);
  234. //限制会员等级高的会员享受更低折扣
  235. if ($pre_level_discount == 0) {
  236. $pre_level_discount = $current_level_discount;
  237. }
  238. if ($current_level_discount > $pre_level_discount) {
  239. ds_json_encode(10001, sprintf(lang('current_level_discount_error'), $value['level_name'], $pre_level_discount / 10));
  240. }
  241. $pre_level_discount = $current_level_discount;
  242. if ($current_level_discount < 1 || $current_level_discount > 100) {
  243. ds_json_encode(10001, lang('current_level_discount_range_error'));
  244. }
  245. $data[$key]['level_discount'] = $current_level_discount / 10;
  246. $data[$key]['level_name'] = $value['level_name'];
  247. }
  248. $condition = array();
  249. $condition[] = array('goods_commonid', '=', $goods_commonid);
  250. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => serialize($data)), $condition);
  251. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => serialize($data)), $condition);
  252. if ($result && $result1) {
  253. $this->recordSellerlog('添加会员等级折扣,公共商品ID:' . $goods_commonid);
  254. ds_json_encode(10000, lang('mgdiscount_add_success'));
  255. } else {
  256. ds_json_encode(10001, lang('mgdiscount_add_fail'));
  257. }
  258. }
  259. }
  260. /**
  261. * 删除
  262. */
  263. public function mgdiscount_del()
  264. {
  265. $goods_commonid = intval(input('param.goods_commonid'));
  266. if (empty($goods_commonid)) {
  267. ds_json_encode(10001, lang('param_error'));
  268. }
  269. $condition = array();
  270. $condition[] = array('goods_commonid', '=', $goods_commonid);
  271. $goods_model = model('goods');
  272. $result = $goods_model->editGoodscommon(array('goods_mgdiscount' => ''), $condition);
  273. $result1 = $goods_model->editGoods(array('goods_mgdiscount' => ''), $condition);
  274. if ($result && $result1) {
  275. ds_json_encode(10000, lang('ds_common_op_succ'));
  276. } else {
  277. ds_json_encode(10001, lang('ds_common_op_fail'));
  278. }
  279. }
  280. /**
  281. * 会员等级折扣套餐购买
  282. * */
  283. public function mgdiscount_quota_add()
  284. {
  285. //输出导航
  286. $this->setSellerCurMenu('Sellermgdiscount');
  287. $this->setSellerCurItem('mgdiscount_quota_add');
  288. return View::fetch($this->template_dir . 'mgdiscount_quota_add');
  289. }
  290. /**
  291. * 会员等级折扣套餐购买保存
  292. * */
  293. public function mgdiscount_quota_add_save()
  294. {
  295. if (intval(config('ds_config.mgdiscount_price')) == 0) {
  296. ds_json_encode(10001, lang('param_error'));
  297. }
  298. $mgdiscount_quota_quantity = intval(input('post.mgdiscount_quota_quantity'));
  299. if ($mgdiscount_quota_quantity <= 0 || $mgdiscount_quota_quantity > 12) {
  300. ds_json_encode(10001, lang('mgdiscount_quota_quantity_error'));
  301. }
  302. //获取当前价格
  303. $current_price = intval(config('ds_config.mgdiscount_price'));
  304. //获取该用户已有套餐
  305. $mgdiscountquota_model = model('pmgdiscountquota');
  306. $current_mgdiscount_quota = $mgdiscountquota_model->getMgdiscountquotaCurrent(session('store_id'));
  307. $mgdiscount_add_time = 86400 * 30 * $mgdiscount_quota_quantity;
  308. if (empty($current_mgdiscount_quota)) {
  309. //生成套餐
  310. $param = array();
  311. $param['member_id'] = session('member_id');
  312. $param['member_name'] = session('member_name');
  313. $param['store_id'] = session('store_id');
  314. $param['store_name'] = session('store_name');
  315. $param['mgdiscountquota_starttime'] = TIMESTAMP;
  316. $param['mgdiscountquota_endtime'] = TIMESTAMP + $mgdiscount_add_time;
  317. $mgdiscountquota_model->addMgdiscountquota($param);
  318. } else {
  319. $param = array();
  320. $param['mgdiscountquota_endtime'] = Db::raw('mgdiscountquota_endtime+' . $mgdiscount_add_time);
  321. $mgdiscountquota_model->editMgdiscountquota($param, array('mgdiscountquota_id' => $current_mgdiscount_quota['mgdiscountquota_id']));
  322. }
  323. //记录店铺费用
  324. $this->recordStorecost($current_price * $mgdiscount_quota_quantity, '购买会员等级折扣');
  325. $this->recordSellerlog('购买' . $mgdiscount_quota_quantity . '份会员等级折扣套餐,单价' . $current_price . lang('ds_yuan'));
  326. ds_json_encode(10000, lang('mgdiscount_quota_add_success'));
  327. }
  328. /**
  329. * 选择活动商品
  330. * */
  331. public function search_goods()
  332. {
  333. $goods_model = model('goods');
  334. $condition = array();
  335. $condition[] = array('store_id', '=', session('store_id'));
  336. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  337. $goods_list = $goods_model->getGoodsCommonListForPromotion($condition, '*', 8, 'groupbuy');
  338. View::assign('goods_list', $goods_list);
  339. View::assign('show_page', $goods_model->page_info->render());
  340. echo View::fetch($this->template_dir . 'search_goods');
  341. exit;
  342. }
  343. public function mgdiscount_goods_info()
  344. {
  345. $goods_commonid = intval(input('param.goods_commonid'));
  346. $data = array();
  347. $data['result'] = true;
  348. //判断此商品是否已经参加会员等级折扣,
  349. $result = $this->_check_allow_mgdiscount($goods_commonid);
  350. if ($result['result'] != TRUE) {
  351. echo json_encode($result);
  352. die;
  353. }
  354. //获取商品具体信息用于显示
  355. $goods_model = model('goods');
  356. $condition = array();
  357. $condition[] = array('goods_commonid', '=', $goods_commonid);
  358. $goods_list = $goods_model->getGoodsOnlineList($condition);
  359. if (empty($goods_list)) {
  360. $data['result'] = false;
  361. $data['message'] = lang('param_error');
  362. echo json_encode($data);
  363. die;
  364. }
  365. $goods_info = $goods_list[0];
  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_mgdiscount($goods_commonid)
  378. {
  379. $condition = array();
  380. $goodscommon_info = model('goods')->getGoodsCommonInfoByID($goods_commonid);
  381. $result['result'] = TRUE;
  382. if ($goodscommon_info['goods_mgdiscount'] != '') {
  383. $result['result'] = FALSE;
  384. $result['message'] = lang('goods_discount_already_set');
  385. }
  386. return $result;
  387. }
  388. /**
  389. * 用户中心右边,小导航
  390. *
  391. * @param string $menu_type 导航类型
  392. * @param string $name 当前导航的name
  393. * @param array $array 附加菜单
  394. * @return
  395. */
  396. protected function getSellerItemList()
  397. {
  398. $menu_array = array(
  399. array(
  400. 'name' => 'mgdiscount_store',
  401. 'text' => lang('mgdiscount_store'),
  402. 'url' => (string) url('Sellerpromotionmgdiscount/mgdiscount_store')
  403. ),
  404. array(
  405. 'name' => 'mgdiscount_goods',
  406. 'text' => lang('mgdiscount_goods'),
  407. 'url' => (string) url('Sellerpromotionmgdiscount/mgdiscount_goods')
  408. ),
  409. );
  410. return $menu_array;
  411. }
  412. }