Sellerpromotionwholesale.php 23 KB

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