Sellerpromotionwholesale.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 Sellerpromotionwholesale extends BaseSeller
  14. {
  15. public function initialize()
  16. {
  17. parent::initialize(); // TODO: Change the autogenerated stub
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/sellerpromotionwholesale.lang.php');
  19. if (intval(config('ds_config.promotion_allow')) !== 1) {
  20. $this->error(lang('promotion_unavailable'), 'seller/index');
  21. }
  22. }
  23. public function index()
  24. {
  25. $wholesalequota_model = model('wholesalequota');
  26. $wholesale_model = model('wholesale');
  27. if (check_platform_store()) {
  28. View::assign('isPlatformStore', true);
  29. } else {
  30. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  31. View::assign('current_wholesale_quota', $current_wholesale_quota);
  32. }
  33. $condition = array();
  34. $condition[] = array('store_id', '=', session('store_id'));
  35. if ((input('param.wholesale_name'))) {
  36. $condition[] = array('goods_name', 'like', '%' . input('param.wholesale_name') . '%');
  37. }
  38. if ((input('param.state'))) {
  39. $condition[] = array('wholesale_state', '=', intval(input('param.state')));
  40. }
  41. $wholesale_list = $wholesale_model->getWholesaleList($condition, 10, 'wholesale_state desc, wholesale_end_time desc');
  42. View::assign('wholesale_list', $wholesale_list);
  43. View::assign('show_page', $wholesale_model->page_info->render());
  44. View::assign('wholesale_state_array', $wholesale_model->getWholesaleStateArray());
  45. $this->setSellerCurMenu('Sellerpromotionwholesale');
  46. $this->setSellerCurItem('wholesale_list');
  47. return View::fetch($this->template_dir . 'index');
  48. }
  49. /**
  50. * 添加批发活动
  51. * */
  52. public function wholesale_add()
  53. {
  54. if (check_platform_store()) {
  55. View::assign('isPlatformStore', true);
  56. } else {
  57. View::assign('isPlatformStore', false);
  58. $wholesalequota_model = model('wholesalequota');
  59. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  60. if (empty($current_wholesale_quota)) {
  61. if (intval(config('ds_config.promotion_wholesale_price')) != 0) {
  62. $this->error(lang('wholesale_quota_current_error1'));
  63. } else {
  64. $current_wholesale_quota = array('wholesalequota_starttime' => TIMESTAMP, 'wholesalequota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  65. }
  66. }
  67. View::assign('current_wholesale_quota', $current_wholesale_quota);
  68. }
  69. //输出导航
  70. $this->setSellerCurMenu('Sellerpromotionwholesale');
  71. $this->setSellerCurItem('wholesale_add');
  72. return View::fetch($this->template_dir . 'wholesale_add');
  73. }
  74. /**
  75. * 保存添加的批发活动
  76. * */
  77. public function wholesale_save()
  78. {
  79. //验证输入
  80. $wholesale_if_mix = intval(input('param.wholesale_if_mix'));
  81. $goods_commonid = intval(input('param.goods_commonid'));
  82. $start_time = strtotime(input('post.start_time'));
  83. $end_time = strtotime(input('post.end_time'));
  84. $wholesale_json = explode(',', input('param.wholesale_json'));
  85. if (!$goods_commonid || empty($wholesale_json)) {
  86. ds_json_encode(10001, lang('param_error'));
  87. }
  88. if ($start_time >= $end_time) {
  89. ds_json_encode(10001, lang('greater_than_start_time'));
  90. }
  91. if (!check_platform_store()) {
  92. //获取当前套餐
  93. $wholesalequota_model = model('wholesalequota');
  94. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  95. if (empty($current_wholesale_quota)) {
  96. if (intval(config('ds_config.promotion_wholesale_price')) != 0) {
  97. ds_json_encode(10001, lang('please_buy_package_first'));
  98. } else {
  99. $current_wholesale_quota = array('wholesalequota_starttime' => TIMESTAMP, 'wholesalequota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  100. }
  101. }
  102. $quota_start_time = intval($current_wholesale_quota['wholesalequota_starttime']);
  103. $quota_end_time = intval($current_wholesale_quota['wholesalequota_endtime']);
  104. if ($start_time < $quota_start_time) {
  105. ds_json_encode(10001, sprintf(lang('wholesale_add_start_time_explain'), date('Y-m-d', $current_wholesale_quota['wholesalequota_starttime'])));
  106. }
  107. if ($end_time > $quota_end_time) {
  108. ds_json_encode(10001, sprintf(lang('wholesale_add_end_time_explain'), date('Y-m-d', $current_wholesale_quota['wholesalequota_endtime'])));
  109. }
  110. }
  111. if ($end_time < TIMESTAMP) {
  112. ds_json_encode(10001, sprintf(lang('wholesale_add_end_time_explain'), date('Y-m-d')));
  113. }
  114. $goods_model = model('goods');
  115. $wholesale_model = model('wholesale');
  116. $wholesalegoods_model = model('wholesalegoods');
  117. Db::startTrans();
  118. try {
  119. $goodscommon_info = $goods_model->getGoodsCommonInfoByID($goods_commonid);
  120. if (!$goodscommon_info || $goodscommon_info['store_id'] != session('store_id') || $goodscommon_info['goods_verify'] != 1 || $goodscommon_info['goods_state'] != 1 || $goodscommon_info['goods_lock']) {
  121. throw new \think\Exception(lang('goods_not_exist'), 10006);
  122. }
  123. $param = array();
  124. $param['wholesalequota_id'] = isset($current_wholesale_quota['wholesalequota_id']) ? $current_wholesale_quota['wholesalequota_id'] : 0;
  125. $param['wholesale_starttime'] = $start_time;
  126. $param['wholesale_end_time'] = $end_time;
  127. $param['goods_commonid'] = $goods_commonid;
  128. $param['goods_name'] = $goodscommon_info['goods_name'];
  129. $param['wholesale_if_mix'] = $wholesale_if_mix;
  130. $param['store_id'] = session('store_id');
  131. $param['store_name'] = session('store_name');
  132. $param['member_id'] = session('member_id');
  133. $param['member_name'] = session('member_name');
  134. $wholesale_id = $wholesale_model->addWholesale($param);
  135. foreach ($wholesale_json as $val) {
  136. $wholesalegoods_data = $this->getWholesaleGoods($val);
  137. $wholesalegoods_data['wholesale_id'] = $wholesale_id;
  138. $wholesalegoods_data['wholesale_starttime'] = $start_time;
  139. $wholesalegoods_data['wholesale_end_time'] = $end_time;
  140. $wholesalegoods_model->addWholesalegoods($wholesalegoods_data);
  141. }
  142. } catch (\Exception $e) {
  143. Db::rollback();
  144. ds_json_encode(10001, $e->getMessage());
  145. }
  146. Db::commit();
  147. // 添加计划任务
  148. $this->addcron(array('cron_exetime' => $end_time, 'cron_value' => serialize(intval($wholesale_id)), 'cron_type' => 'editExpireWholesale'), true);
  149. $this->recordSellerlog(lang('add_limited_time_discount_activity') . $goodscommon_info['goods_name'] . lang('activity_number') . $wholesale_id);
  150. ds_json_encode(10000, lang('ds_common_op_succ'));
  151. }
  152. /**
  153. * 获取批发商品
  154. *
  155. * @param array $val
  156. * @param type $goods_commonid
  157. * @return array
  158. *
  159. */
  160. private function getWholesaleGoods($val)
  161. {
  162. $temp = explode('|', $val);
  163. $goods_id = $temp[0];
  164. $temp = explode('_', $temp[1]);
  165. $goods_model = model('goods');
  166. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  167. if ($goods_info && $goods_info['goods_verify'] == 1 && $goods_info['goods_state'] == 1) {
  168. if (empty($temp)) {
  169. throw new \think\Exception($goods_info['goods_name'] . lang('wholesale_price_empty'), 10006);
  170. }
  171. $wholesalegoods_price = array();
  172. foreach ($temp as $v) {
  173. $m_temp = explode('-', $v);
  174. $num = intval($m_temp[0]);
  175. $price = floatval($m_temp[1]);
  176. if ($num <= 0 || $price <= 0) {
  177. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  178. }
  179. $wholesalegoods_price[] = array(
  180. 'num' => $num,
  181. 'price' => $price,
  182. );
  183. }
  184. $wholesalegoods_price = $this->arraySort($wholesalegoods_price, 'num', 'asc');
  185. $wholesalegoods_price = array_values($wholesalegoods_price);
  186. for ($i = 0; $i < count($wholesalegoods_price); $i++) {
  187. if ($i < (count($wholesalegoods_price) - 1)) {
  188. $wholesalegoods_price[$i]['num_text'] = $wholesalegoods_price[$i]['num'] . '-' . $wholesalegoods_price[$i + 1]['num'];
  189. } else {
  190. $wholesalegoods_price[$i]['num_text'] = '≥' . $wholesalegoods_price[$i]['num'];
  191. }
  192. if ($i > 0) {
  193. if ($wholesalegoods_price[$i]['price'] >= $wholesalegoods_price[$i - 1]['price']) {
  194. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  195. }
  196. if ($wholesalegoods_price[$i]['num'] == $wholesalegoods_price[$i - 1]['num']) {
  197. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  198. }
  199. }
  200. }
  201. if ($wholesalegoods_price[0]['price'] > $goods_info['goods_price']) {
  202. throw new \think\Exception($goods_info['goods_name'] . lang('wholesale_price_error'), 10006);
  203. }
  204. $wholesalegoods_data = array(
  205. 'goods_id' => $goods_info['goods_id'],
  206. 'goods_commonid' => $goods_info['goods_commonid'],
  207. 'store_id' => $goods_info['store_id'],
  208. 'goods_name' => $goods_info['goods_name'],
  209. 'goods_price' => $goods_info['goods_price'],
  210. 'goods_image' => $goods_info['goods_image'],
  211. 'wholesalegoods_price' => serialize($wholesalegoods_price),
  212. );
  213. return $wholesalegoods_data;
  214. } else {
  215. throw new \think\Exception(lang('goods_not_exist'), 10006);
  216. }
  217. }
  218. /**
  219. * 多维数组排序(多用于文件数组数据)
  220. *
  221. * @param array $array
  222. * @param array $cols
  223. * @return array
  224. *
  225. */
  226. private function arraySort($array, $keys, $sort = 'asc')
  227. {
  228. $newArr = $valArr = array();
  229. foreach ($array as $key => $value) {
  230. $valArr[$key] = $value[$keys];
  231. }
  232. ($sort == 'asc') ? asort($valArr) : arsort($valArr);
  233. reset($valArr);
  234. foreach ($valArr as $key => $value) {
  235. $newArr[$key] = $array[$key];
  236. }
  237. return $newArr;
  238. }
  239. /**
  240. * 编辑批发活动
  241. * */
  242. public function wholesale_edit()
  243. {
  244. if (check_platform_store()) {
  245. View::assign('isPlatformStore', true);
  246. } else {
  247. View::assign('isPlatformStore', false);
  248. }
  249. $wholesale_model = model('wholesale');
  250. $wholesale_info = $wholesale_model->getWholesaleInfoByID(input('param.wholesale_id'));
  251. if (empty($wholesale_info) || !$wholesale_info['editable']) {
  252. $this->error(lang('param_error'));
  253. }
  254. View::assign('wholesale_info', $wholesale_info);
  255. //获取批发商品列表
  256. $goods_model = model('goods');
  257. $condition = array();
  258. $condition[] = array('goods_commonid', '=', $wholesale_info['goods_commonid']);
  259. $goods_list = $goods_model->getGoodsOnlineList($condition);
  260. $wholesalegoods_model = model('wholesalegoods');
  261. $wholesalegoods_list = array();
  262. foreach ($goods_list as $key => $val) {
  263. $val['goods_image'] = goods_thumb($val, 240);
  264. $condition = array();
  265. $condition[] = array('wholesale_id', '=', $wholesale_info['wholesale_id']);
  266. $condition[] = array('goods_id', '=', $val['goods_id']);
  267. $wholesalegoods_info = $wholesalegoods_model->getWholesalegoodsInfo($condition);
  268. if (!$wholesalegoods_info) {
  269. $val['inactive'] = 1;
  270. } else {
  271. $val['wholesale_info'] = unserialize($wholesalegoods_info['wholesalegoods_price']);
  272. $val['goods_lock'] = 0;
  273. }
  274. $wholesalegoods_list[] = $val;
  275. }
  276. View::assign('wholesalegoods_list', json_encode($wholesalegoods_list));
  277. //输出导航
  278. $this->setSellerCurMenu('Sellerpromotionwholesale');
  279. $this->setSellerCurItem('wholesale_edit');
  280. return View::fetch($this->template_dir . 'wholesale_add');
  281. }
  282. /**
  283. * 编辑保存批发活动
  284. * */
  285. public function wholesale_edit_save()
  286. {
  287. $wholesale_id = input('param.wholesale_id');
  288. $wholesale_model = model('wholesale');
  289. $wholesalegoods_model = model('wholesalegoods');
  290. $wholesale_if_mix = intval(input('param.wholesale_if_mix'));
  291. $wholesale_json = explode(',', input('param.wholesale_json'));
  292. if (!$wholesale_id || empty($wholesale_json)) {
  293. ds_json_encode(10001, lang('param_error'));
  294. }
  295. $wholesale_info = $wholesale_model->getWholesaleInfoByID($wholesale_id);
  296. if (empty($wholesale_info) || !$wholesale_info['editable']) {
  297. $this->error(lang('param_error'));
  298. }
  299. Db::startTrans();
  300. try {
  301. //生成活动
  302. $param = array();
  303. $param['wholesale_if_mix'] = $wholesale_if_mix;
  304. $wholesale_model->editWholesale($param, array(array('wholesale_id', '=', $wholesale_id)));
  305. $goods_ids = array();
  306. foreach ($wholesale_json as $val) {
  307. $wholesalegoods_data = $this->getWholesaleGoods($val);
  308. $goods_ids[] = $wholesalegoods_data['goods_id'];
  309. $wholesalegoods_info = $wholesalegoods_model->getWholesalegoodsInfo(array(array('wholesale_id', '=', $wholesale_id), array('goods_id', '=', $wholesalegoods_data['goods_id'])));
  310. if ($wholesalegoods_info) {
  311. $wholesalegoods_model->editWholesalegoods($wholesalegoods_data, array(array('wholesalegoods_id', '=', $wholesalegoods_info['wholesalegoods_id'])));
  312. } else {
  313. $wholesalegoods_data['wholesale_id'] = $wholesale_id;
  314. $wholesalegoods_data['wholesale_starttime'] = $wholesale_info['wholesale_starttime'];
  315. $wholesalegoods_data['wholesale_end_time'] = $wholesale_info['wholesale_end_time'];
  316. $wholesalegoods_model->addWholesalegoods($wholesalegoods_data);
  317. }
  318. }
  319. $wholesalegoods_ids = Db::name('wholesalegoods')->where(array(array('wholesale_id', '=', $wholesale_id), array('goods_id', 'not in', $goods_ids)))->column('wholesalegoods_id');
  320. if (!empty($wholesalegoods_ids)) {
  321. $wholesalegoods_model->delWholesalegoods(array(array('wholesalegoods_id', 'in', $wholesalegoods_ids)));
  322. }
  323. } catch (\Exception $e) {
  324. Db::rollback();
  325. ds_json_encode(10001, $e->getMessage());
  326. }
  327. Db::commit();
  328. $this->recordSellerlog(lang('edit_limited_time_discount_activity') . $wholesale_info['goods_name'] . lang('activity_number') . $wholesale_id);
  329. ds_json_encode(10000, lang('ds_common_op_succ'));
  330. }
  331. /**
  332. * 批发活动删除
  333. * */
  334. public function wholesale_del()
  335. {
  336. $wholesale_id = intval(input('param.wholesale_id'));
  337. $wholesale_model = model('wholesale');
  338. $data = array();
  339. $data['result'] = true;
  340. $wholesale_info = $wholesale_model->getWholesaleInfoByID($wholesale_id, session('store_id'));
  341. if (!$wholesale_info) {
  342. ds_json_encode(10001, lang('param_error'));
  343. }
  344. $result = $wholesale_model->delWholesale(array('wholesale_id' => $wholesale_id));
  345. if ($result) {
  346. $this->recordSellerlog(lang('delete_limited_time_discount_activity') . $wholesale_info['goods_name'] . lang('activity_number') . $wholesale_id);
  347. ds_json_encode(10000, lang('ds_common_op_succ'));
  348. } else {
  349. ds_json_encode(10001, lang('ds_common_op_fail'));
  350. }
  351. }
  352. /**
  353. * 批发套餐购买
  354. * */
  355. public function wholesale_quota_add()
  356. {
  357. //输出导航
  358. $this->setSellerCurMenu('Sellerpromotionwholesale');
  359. $this->setSellerCurItem('wholesale_quota_add');
  360. return View::fetch($this->template_dir . 'wholesale_quota_add');
  361. }
  362. /**
  363. * 批发套餐购买保存
  364. * */
  365. public function wholesale_quota_add_save()
  366. {
  367. if (intval(config('ds_config.promotion_wholesale_price')) == 0) {
  368. ds_json_encode(10001, lang('param_error'));
  369. }
  370. $wholesale_quota_quantity = intval(input('post.wholesale_quota_quantity'));
  371. if ($wholesale_quota_quantity <= 0 || $wholesale_quota_quantity > 12) {
  372. ds_json_encode(10001, lang('wholesale_quota_quantity_error'));
  373. }
  374. //获取当前价格
  375. $current_price = intval(config('ds_config.promotion_wholesale_price'));
  376. //获取该用户已有套餐
  377. $wholesalequota_model = model('wholesalequota');
  378. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  379. $wholesale_add_time = 86400 * 30 * $wholesale_quota_quantity;
  380. if (empty($current_wholesale_quota)) {
  381. //生成套餐
  382. $param = array();
  383. $param['member_id'] = session('member_id');
  384. $param['member_name'] = session('member_name');
  385. $param['store_id'] = session('store_id');
  386. $param['store_name'] = session('store_name');
  387. $param['wholesalequota_starttime'] = TIMESTAMP;
  388. $param['wholesalequota_endtime'] = TIMESTAMP + $wholesale_add_time;
  389. $wholesalequota_model->addWholesalequota($param);
  390. } else {
  391. $param = array();
  392. $param['wholesalequota_endtime'] = Db::raw('wholesalequota_endtime+' . $wholesale_add_time);
  393. $wholesalequota_model->editWholesalequota($param, array('wholesalequota_id' => $current_wholesale_quota['wholesalequota_id']));
  394. }
  395. //记录店铺费用
  396. $this->recordStorecost($current_price * $wholesale_quota_quantity, lang('buy_limited_time_discount'));
  397. $this->recordSellerlog(lang('buy') . $wholesale_quota_quantity . lang('limited_time_discount_package') . $current_price . lang('ds_yuan'));
  398. ds_json_encode(10000, lang('wholesale_quota_add_success'));
  399. }
  400. /**
  401. * 选择活动商品
  402. * */
  403. public function goods_select()
  404. {
  405. $goods_model = model('goods');
  406. $condition = array();
  407. $condition[] = array('goods.store_id', '=', session('store_id'));
  408. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  409. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 10, 'wholesale');
  410. View::assign('goods_list', $goods_list);
  411. View::assign('show_page', $goods_model->page_info->render());
  412. echo View::fetch($this->template_dir . 'goods_select');
  413. }
  414. public function goods_info()
  415. {
  416. $goods_commonid = intval(input('param.goods_commonid'));
  417. $data = array();
  418. $data['result'] = true;
  419. $goods_model = model('goods');
  420. $condition = array();
  421. $condition[] = array('goods_commonid', '=', $goods_commonid);
  422. $goods_list = $goods_model->getGoodsOnlineList($condition);
  423. if (empty($goods_list)) {
  424. $data['result'] = false;
  425. $data['message'] = lang('param_error');
  426. echo json_encode($data);
  427. die;
  428. }
  429. foreach ($goods_list as $key => $val) {
  430. $goods_list[$key]['goods_image'] = goods_thumb($val, 240);
  431. }
  432. $data['goods_list'] = $goods_list;
  433. echo json_encode($data);
  434. die;
  435. }
  436. /**
  437. * 用户中心右边,小导航
  438. *
  439. * @param string $menu_type 导航类型
  440. * @param string $name 当前导航的name
  441. * @param array $array 附加菜单
  442. * @return
  443. */
  444. protected function getSellerItemList()
  445. {
  446. $menu_array = array(
  447. array(
  448. 'name' => 'wholesale_list', 'text' => lang('promotion_active_list'),
  449. 'url' => (string) url('Sellerpromotionwholesale/index')
  450. ),
  451. );
  452. switch (request()->action()) {
  453. case 'wholesale_add':
  454. $menu_array[] = array(
  455. 'name' => 'wholesale_add', 'text' => lang('promotion_join_active'),
  456. 'url' => (string) url('Sellerpromotionwholesale/wholesale_add')
  457. );
  458. break;
  459. case 'wholesale_edit':
  460. $menu_array[] = array(
  461. 'name' => 'wholesale_edit', 'text' => lang('editing_activity'), 'url' => 'javascript:;'
  462. );
  463. break;
  464. case 'wholesale_quota_add':
  465. $menu_array[] = array(
  466. 'name' => 'wholesale_quota_add', 'text' => lang('promotion_buy_product'),
  467. 'url' => (string) url('Sellerpromotionwholesale/wholesale_quota_add')
  468. );
  469. break;
  470. case 'wholesale_manage':
  471. $menu_array[] = array(
  472. 'name' => 'wholesale_manage', 'text' => lang('promotion_goods_manage'),
  473. 'url' => (string) url('Sellerpromotionwholesale/wholesale_manage', ['wholesale_id' => input('param.wholesale_id')])
  474. );
  475. break;
  476. }
  477. return $menu_array;
  478. }
  479. }