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