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