Sellerpromotionwholesale.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. * DSMall多用户商城
  9. * ============================================================================
  10. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  11. * 网站地址: http://www.csdeshang.com
  12. * ----------------------------------------------------------------------------
  13. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  14. * 不允许对程序代码以任何形式任何目的的再发布。
  15. * ============================================================================
  16. * 控制器
  17. */
  18. class Sellerpromotionwholesale extends BaseSeller {
  19. public function initialize() {
  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. $wholesalequota_model = model('wholesalequota');
  28. $wholesale_model = model('wholesale');
  29. if (check_platform_store()) {
  30. View::assign('isPlatformStore', true);
  31. } else {
  32. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  33. View::assign('current_wholesale_quota', $current_wholesale_quota);
  34. }
  35. $condition = array();
  36. $condition[] = array('store_id', '=', session('store_id'));
  37. if ((input('param.wholesale_name'))) {
  38. $condition[] = array('goods_name', 'like', '%' . input('param.wholesale_name') . '%');
  39. }
  40. if ((input('param.state'))) {
  41. $condition[] = array('wholesale_state', '=', intval(input('param.state')));
  42. }
  43. $wholesale_list = $wholesale_model->getWholesaleList($condition, 10, 'wholesale_state desc, wholesale_end_time desc');
  44. View::assign('wholesale_list', $wholesale_list);
  45. View::assign('show_page', $wholesale_model->page_info->render());
  46. View::assign('wholesale_state_array', $wholesale_model->getWholesaleStateArray());
  47. $this->setSellerCurMenu('Sellerpromotionwholesale');
  48. $this->setSellerCurItem('wholesale_list');
  49. return View::fetch($this->template_dir . 'index');
  50. }
  51. /**
  52. * 添加批发活动
  53. * */
  54. public function wholesale_add() {
  55. if (check_platform_store()) {
  56. View::assign('isPlatformStore', true);
  57. } else {
  58. View::assign('isPlatformStore', false);
  59. $wholesalequota_model = model('wholesalequota');
  60. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  61. if (empty($current_wholesale_quota)) {
  62. if (intval(config('ds_config.promotion_wholesale_price')) != 0) {
  63. $this->error(lang('wholesale_quota_current_error1'));
  64. } else {
  65. $current_wholesale_quota = array('wholesalequota_starttime' => TIMESTAMP, 'wholesalequota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  66. }
  67. }
  68. View::assign('current_wholesale_quota', $current_wholesale_quota);
  69. }
  70. //输出导航
  71. $this->setSellerCurMenu('Sellerpromotionwholesale');
  72. $this->setSellerCurItem('wholesale_add');
  73. return View::fetch($this->template_dir . 'wholesale_add');
  74. }
  75. /**
  76. * 保存添加的批发活动
  77. * */
  78. public function wholesale_save() {
  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. $temp = explode('|', $val);
  162. $goods_id = $temp[0];
  163. $temp = explode('_', $temp[1]);
  164. $goods_model = model('goods');
  165. $goods_info = $goods_model->getGoodsInfoByID($goods_id);
  166. if ($goods_info && $goods_info['goods_verify'] == 1 && $goods_info['goods_state'] == 1) {
  167. if (empty($temp)) {
  168. throw new \think\Exception($goods_info['goods_name'] . lang('wholesale_price_empty'), 10006);
  169. }
  170. $wholesalegoods_price = array();
  171. foreach ($temp as $v) {
  172. $m_temp = explode('-', $v);
  173. $num = intval($m_temp[0]);
  174. $price = floatval($m_temp[1]);
  175. if ($num <= 0 || $price <= 0) {
  176. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  177. }
  178. $wholesalegoods_price[] = array(
  179. 'num' => $num,
  180. 'price' => $price,
  181. );
  182. }
  183. $wholesalegoods_price = $this->arraySort($wholesalegoods_price, 'num', 'asc');
  184. $wholesalegoods_price = array_values($wholesalegoods_price);
  185. for ($i = 0; $i < count($wholesalegoods_price); $i++) {
  186. if ($i < (count($wholesalegoods_price) - 1)) {
  187. $wholesalegoods_price[$i]['num_text'] = $wholesalegoods_price[$i]['num'] . '-' . $wholesalegoods_price[$i + 1]['num'];
  188. } else {
  189. $wholesalegoods_price[$i]['num_text'] = '≥' . $wholesalegoods_price[$i]['num'];
  190. }
  191. if ($i > 0) {
  192. if ($wholesalegoods_price[$i]['price'] >= $wholesalegoods_price[$i - 1]['price']) {
  193. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  194. }
  195. if ($wholesalegoods_price[$i]['num'] == $wholesalegoods_price[$i - 1]['num']) {
  196. throw new \think\Exception(lang('wholesale_set_error'), 10006);
  197. }
  198. }
  199. }
  200. if ($wholesalegoods_price[0]['price'] > $goods_info['goods_price']) {
  201. throw new \think\Exception($goods_info['goods_name'] . lang('wholesale_price_error'), 10006);
  202. }
  203. $wholesalegoods_data = array(
  204. 'goods_id' => $goods_info['goods_id'],
  205. 'goods_commonid' => $goods_info['goods_commonid'],
  206. 'store_id' => $goods_info['store_id'],
  207. 'goods_name' => $goods_info['goods_name'],
  208. 'goods_price' => $goods_info['goods_price'],
  209. 'goods_image' => $goods_info['goods_image'],
  210. 'wholesalegoods_price' => serialize($wholesalegoods_price),
  211. );
  212. return $wholesalegoods_data;
  213. } else {
  214. throw new \think\Exception(lang('goods_not_exist'), 10006);
  215. }
  216. }
  217. /**
  218. * 多维数组排序(多用于文件数组数据)
  219. *
  220. * @param array $array
  221. * @param array $cols
  222. * @return array
  223. *
  224. */
  225. private function arraySort($array, $keys, $sort = 'asc') {
  226. $newArr = $valArr = array();
  227. foreach ($array as $key => $value) {
  228. $valArr[$key] = $value[$keys];
  229. }
  230. ($sort == 'asc') ? asort($valArr) : arsort($valArr);
  231. reset($valArr);
  232. foreach ($valArr as $key => $value) {
  233. $newArr[$key] = $array[$key];
  234. }
  235. return $newArr;
  236. }
  237. /**
  238. * 编辑批发活动
  239. * */
  240. public function wholesale_edit() {
  241. if (check_platform_store()) {
  242. View::assign('isPlatformStore', true);
  243. } else {
  244. View::assign('isPlatformStore', false);
  245. }
  246. $wholesale_model = model('wholesale');
  247. $wholesale_info = $wholesale_model->getWholesaleInfoByID(input('param.wholesale_id'));
  248. if (empty($wholesale_info) || !$wholesale_info['editable']) {
  249. $this->error(lang('param_error'));
  250. }
  251. View::assign('wholesale_info', $wholesale_info);
  252. //获取批发商品列表
  253. $goods_model = model('goods');
  254. $condition = array();
  255. $condition[] = array('goods_commonid', '=', $wholesale_info['goods_commonid']);
  256. $goods_list = $goods_model->getGoodsOnlineList($condition);
  257. $wholesalegoods_model = model('wholesalegoods');
  258. $wholesalegoods_list = array();
  259. foreach ($goods_list as $key => $val) {
  260. $val['goods_image'] = goods_thumb($val, 240);
  261. $condition = array();
  262. $condition[] = array('wholesale_id', '=', $wholesale_info['wholesale_id']);
  263. $condition[] = array('goods_id', '=', $val['goods_id']);
  264. $wholesalegoods_info = $wholesalegoods_model->getWholesalegoodsInfo($condition);
  265. if (!$wholesalegoods_info) {
  266. $val['inactive'] = 1;
  267. } else {
  268. $val['wholesale_info'] = unserialize($wholesalegoods_info['wholesalegoods_price']);
  269. $val['goods_lock'] = 0;
  270. }
  271. $wholesalegoods_list[] = $val;
  272. }
  273. View::assign('wholesalegoods_list', json_encode($wholesalegoods_list));
  274. //输出导航
  275. $this->setSellerCurMenu('Sellerpromotionwholesale');
  276. $this->setSellerCurItem('wholesale_edit');
  277. return View::fetch($this->template_dir . 'wholesale_add');
  278. }
  279. /**
  280. * 编辑保存批发活动
  281. * */
  282. public function wholesale_edit_save() {
  283. $wholesale_id = input('param.wholesale_id');
  284. $wholesale_model = model('wholesale');
  285. $wholesalegoods_model = model('wholesalegoods');
  286. $wholesale_if_mix = intval(input('param.wholesale_if_mix'));
  287. $wholesale_json = explode(',', input('param.wholesale_json'));
  288. if (!$wholesale_id || empty($wholesale_json)) {
  289. ds_json_encode(10001, lang('param_error'));
  290. }
  291. $wholesale_info = $wholesale_model->getWholesaleInfoByID($wholesale_id);
  292. if (empty($wholesale_info) || !$wholesale_info['editable']) {
  293. $this->error(lang('param_error'));
  294. }
  295. Db::startTrans();
  296. try {
  297. //生成活动
  298. $param = array();
  299. $param['wholesale_if_mix'] = $wholesale_if_mix;
  300. $wholesale_model->editWholesale($param, array(array('wholesale_id', '=', $wholesale_id)));
  301. $goods_ids = array();
  302. foreach ($wholesale_json as $val) {
  303. $wholesalegoods_data = $this->getWholesaleGoods($val);
  304. $goods_ids[] = $wholesalegoods_data['goods_id'];
  305. $wholesalegoods_info = $wholesalegoods_model->getWholesalegoodsInfo(array(array('wholesale_id', '=', $wholesale_id), array('goods_id', '=', $wholesalegoods_data['goods_id'])));
  306. if ($wholesalegoods_info) {
  307. $wholesalegoods_model->editWholesalegoods($wholesalegoods_data, array(array('wholesalegoods_id', '=', $wholesalegoods_info['wholesalegoods_id'])));
  308. } else {
  309. $wholesalegoods_data['wholesale_id'] = $wholesale_id;
  310. $wholesalegoods_data['wholesale_starttime'] = $wholesale_info['wholesale_starttime'];
  311. $wholesalegoods_data['wholesale_end_time'] = $wholesale_info['wholesale_end_time'];
  312. $wholesalegoods_model->addWholesalegoods($wholesalegoods_data);
  313. }
  314. }
  315. $wholesalegoods_ids = Db::name('wholesalegoods')->where(array(array('wholesale_id', '=', $wholesale_id), array('goods_id', 'not in', $goods_ids)))->column('wholesalegoods_id');
  316. if (!empty($wholesalegoods_ids)) {
  317. $wholesalegoods_model->delWholesalegoods(array(array('wholesalegoods_id', 'in', $wholesalegoods_ids)));
  318. }
  319. } catch (\Exception $e) {
  320. Db::rollback();
  321. ds_json_encode(10001, $e->getMessage());
  322. }
  323. Db::commit();
  324. $this->recordSellerlog(lang('edit_limited_time_discount_activity') . $wholesale_info['goods_name'] . lang('activity_number') . $wholesale_id);
  325. ds_json_encode(10000, lang('ds_common_op_succ'));
  326. }
  327. /**
  328. * 批发活动删除
  329. * */
  330. public function wholesale_del() {
  331. $wholesale_id = intval(input('param.wholesale_id'));
  332. $wholesale_model = model('wholesale');
  333. $data = array();
  334. $data['result'] = true;
  335. $wholesale_info = $wholesale_model->getWholesaleInfoByID($wholesale_id, session('store_id'));
  336. if (!$wholesale_info) {
  337. ds_json_encode(10001, lang('param_error'));
  338. }
  339. $result = $wholesale_model->delWholesale(array('wholesale_id' => $wholesale_id));
  340. if ($result) {
  341. $this->recordSellerlog(lang('delete_limited_time_discount_activity') . $wholesale_info['goods_name'] . lang('activity_number') . $wholesale_id);
  342. ds_json_encode(10000, lang('ds_common_op_succ'));
  343. } else {
  344. ds_json_encode(10001, lang('ds_common_op_fail'));
  345. }
  346. }
  347. /**
  348. * 批发套餐购买
  349. * */
  350. public function wholesale_quota_add() {
  351. //输出导航
  352. $this->setSellerCurMenu('Sellerpromotionwholesale');
  353. $this->setSellerCurItem('wholesale_quota_add');
  354. return View::fetch($this->template_dir . 'wholesale_quota_add');
  355. }
  356. /**
  357. * 批发套餐购买保存
  358. * */
  359. public function wholesale_quota_add_save() {
  360. if (intval(config('ds_config.promotion_wholesale_price')) == 0) {
  361. ds_json_encode(10001, lang('param_error'));
  362. }
  363. $wholesale_quota_quantity = intval(input('post.wholesale_quota_quantity'));
  364. if ($wholesale_quota_quantity <= 0 || $wholesale_quota_quantity > 12) {
  365. ds_json_encode(10001, lang('wholesale_quota_quantity_error'));
  366. }
  367. //获取当前价格
  368. $current_price = intval(config('ds_config.promotion_wholesale_price'));
  369. //获取该用户已有套餐
  370. $wholesalequota_model = model('wholesalequota');
  371. $current_wholesale_quota = $wholesalequota_model->getWholesalequotaCurrent(session('store_id'));
  372. $wholesale_add_time = 86400 * 30 * $wholesale_quota_quantity;
  373. if (empty($current_wholesale_quota)) {
  374. //生成套餐
  375. $param = array();
  376. $param['member_id'] = session('member_id');
  377. $param['member_name'] = session('member_name');
  378. $param['store_id'] = session('store_id');
  379. $param['store_name'] = session('store_name');
  380. $param['wholesalequota_starttime'] = TIMESTAMP;
  381. $param['wholesalequota_endtime'] = TIMESTAMP + $wholesale_add_time;
  382. $wholesalequota_model->addWholesalequota($param);
  383. } else {
  384. $param = array();
  385. $param['wholesalequota_endtime'] = Db::raw('wholesalequota_endtime+' . $wholesale_add_time);
  386. $wholesalequota_model->editWholesalequota($param, array('wholesalequota_id' => $current_wholesale_quota['wholesalequota_id']));
  387. }
  388. //记录店铺费用
  389. $this->recordStorecost($current_price * $wholesale_quota_quantity, lang('buy_limited_time_discount'));
  390. $this->recordSellerlog(lang('buy') . $wholesale_quota_quantity . lang('limited_time_discount_package') . $current_price . lang('ds_yuan'));
  391. ds_json_encode(10000, lang('wholesale_quota_add_success'));
  392. }
  393. /**
  394. * 选择活动商品
  395. * */
  396. public function goods_select() {
  397. $goods_model = model('goods');
  398. $condition = array();
  399. $condition[] = array('goods.store_id', '=', session('store_id'));
  400. $condition[] = array('goods.goods_name', 'like', '%' . input('param.goods_name') . '%');
  401. $goods_list = $goods_model->getGoodsListForPromotion($condition, 'goods.goods_id,goods.goods_commonid,goods.goods_name,goods.goods_image,goods.goods_price', 10, 'wholesale');
  402. View::assign('goods_list', $goods_list);
  403. View::assign('show_page', $goods_model->page_info->render());
  404. echo View::fetch($this->template_dir . 'goods_select');
  405. }
  406. public function goods_info() {
  407. $goods_commonid = intval(input('param.goods_commonid'));
  408. $data = array();
  409. $data['result'] = true;
  410. $goods_model = model('goods');
  411. $condition = array();
  412. $condition[] = array('goods_commonid', '=', $goods_commonid);
  413. $goods_list = $goods_model->getGoodsOnlineList($condition);
  414. if (empty($goods_list)) {
  415. $data['result'] = false;
  416. $data['message'] = lang('param_error');
  417. echo json_encode($data);
  418. die;
  419. }
  420. foreach ($goods_list as $key => $val) {
  421. $goods_list[$key]['goods_image'] = goods_thumb($val, 240);
  422. }
  423. $data['goods_list'] = $goods_list;
  424. echo json_encode($data);
  425. die;
  426. }
  427. /**
  428. * 用户中心右边,小导航
  429. *
  430. * @param string $menu_type 导航类型
  431. * @param string $name 当前导航的name
  432. * @param array $array 附加菜单
  433. * @return
  434. */
  435. protected function getSellerItemList() {
  436. $menu_array = array(
  437. array(
  438. 'name' => 'wholesale_list', 'text' => lang('promotion_active_list'),
  439. 'url' => (string) url('Sellerpromotionwholesale/index')
  440. ),
  441. );
  442. switch (request()->action()) {
  443. case 'wholesale_add':
  444. $menu_array[] = array(
  445. 'name' => 'wholesale_add', 'text' => lang('promotion_join_active'),
  446. 'url' => (string) url('Sellerpromotionwholesale/wholesale_add')
  447. );
  448. break;
  449. case 'wholesale_edit':
  450. $menu_array[] = array(
  451. 'name' => 'wholesale_edit', 'text' => lang('editing_activity'), 'url' => 'javascript:;'
  452. );
  453. break;
  454. case 'wholesale_quota_add':
  455. $menu_array[] = array(
  456. 'name' => 'wholesale_quota_add', 'text' => lang('promotion_buy_product'),
  457. 'url' => (string) url('Sellerpromotionwholesale/wholesale_quota_add')
  458. );
  459. break;
  460. case 'wholesale_manage':
  461. $menu_array[] = array(
  462. 'name' => 'wholesale_manage', 'text' => lang('promotion_goods_manage'),
  463. 'url' => (string) url('Sellerpromotionwholesale/wholesale_manage', ['wholesale_id' => input('param.wholesale_id')])
  464. );
  465. break;
  466. }
  467. return $menu_array;
  468. }
  469. }