Sellerpromotionmansong.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 Sellerpromotionmansong 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') . '/sellerpromotionmansong.lang.php');
  22. //检查满就送是否开启
  23. if (intval(config('ds_config.promotion_allow')) !== 1) {
  24. $this->error(lang('promotion_unavailable'), 'seller/index');
  25. }
  26. }
  27. /**
  28. * 发布的满就送活动列表
  29. * */
  30. public function index()
  31. {
  32. $mansongquota_model = model('pmansongquota');
  33. $pmansong_model = model('pmansong');
  34. $isPlatformStore = check_platform_store() ? true : false;
  35. View::assign('isPlatformStore', $isPlatformStore);
  36. $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
  37. View::assign('current_mansong_quota', $current_mansong_quota);
  38. $condition = array();
  39. $condition[] = array('store_id', '=', session('store_id'));
  40. if ((input('param.mansong_name'))) {
  41. $condition[] = array('mansong_name', 'like', '%' . input('param.mansong_name') . '%');
  42. }
  43. if ((input('param.state'))) {
  44. $condition[] = array('mansong_state', '=', intval(input('param.state')));
  45. }
  46. $mansong_list = $pmansong_model->getMansongList($condition, 10, 'mansong_state desc, mansong_endtime desc');
  47. View::assign('mansong_list', $mansong_list);
  48. View::assign('show_page', $pmansong_model->page_info->render());
  49. View::assign('mansong_state_array', $pmansong_model->getMansongStateArray());
  50. $this->setSellerCurMenu('Sellerpromotionmansong');
  51. $this->setSellerCurItem('mansong_list');
  52. return View::fetch($this->template_dir . 'index');
  53. }
  54. /**
  55. * 添加满就送活动
  56. * */
  57. public function mansong_add()
  58. {
  59. $mansongquota_model = model('pmansongquota');
  60. $pmansong_model = model('pmansong');
  61. $start_time = $pmansong_model->getMansongNewStartTime(session('store_id'));
  62. $end_time = TIMESTAMP;
  63. $isPlatformStore = check_platform_store() ? true : false;
  64. View::assign('isPlatformStore', $isPlatformStore);
  65. if (!$isPlatformStore) {
  66. //检查当前套餐是否可用
  67. $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
  68. if (empty($current_mansong_quota)) {
  69. if (intval(config('ds_config.promotion_mansong_price')) != 0) {
  70. $this->error(lang('mansong_quota_current_error'));
  71. } else {
  72. $current_mansong_quota = array('mansongquota_starttime' => TIMESTAMP, 'mansongquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  73. }
  74. }
  75. if (empty($start_time)) {
  76. $start_time = $current_mansong_quota['mansongquota_starttime'];
  77. }
  78. $end_time = $current_mansong_quota['mansongquota_endtime'];
  79. }
  80. if (empty($start_time))
  81. $start_time = TIMESTAMP;
  82. View::assign('start_time', $start_time);
  83. View::assign('end_time', $end_time);
  84. //输出导航
  85. $this->setSellerCurMenu('Sellerpromotionmansong');
  86. $this->setSellerCurItem('mansong_add');
  87. return View::fetch($this->template_dir . 'mansong_add');
  88. }
  89. /**
  90. * 保存添加的满就送活动
  91. * */
  92. public function mansong_save()
  93. {
  94. $mansong_name = trim(input('post.mansong_name'));
  95. $start_time = strtotime(input('post.start_time'));
  96. $end_time = strtotime(input('post.end_time'));
  97. $mansongquota_model = model('pmansongquota');
  98. $pmansong_model = model('pmansong');
  99. $mansongrule_model = model('pmansongrule');
  100. if ($start_time >= $end_time) {
  101. ds_json_encode(10001, lang('greater_than_start_time'));
  102. }
  103. if (empty($mansong_name)) {
  104. ds_json_encode(10001, lang('mansong_name_error'));
  105. }
  106. $start_time_limit = $pmansong_model->getMansongNewStartTime(session('store_id'));
  107. if (!empty($start_time_limit) && $start_time_limit > $start_time) {
  108. $start_time = $start_time_limit;
  109. }
  110. if (!check_platform_store()) {
  111. //检查当前套餐是否可用
  112. $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
  113. if (empty($current_mansong_quota)) {
  114. if (intval(config('ds_config.promotion_mansong_price')) != 0) {
  115. ds_json_encode(10001, lang('mansong_quota_current_error'));
  116. } else {
  117. $current_mansong_quota = array('mansongquota_starttime' => TIMESTAMP, 'mansongquota_endtime' => TIMESTAMP + 86400 * 30); //没有套餐时,最多一个月
  118. }
  119. }
  120. //验证输入
  121. $quota_start_time = intval($current_mansong_quota['mansongquota_starttime']);
  122. $quota_end_time = intval($current_mansong_quota['mansongquota_endtime']);
  123. if ($start_time < $quota_start_time) {
  124. ds_json_encode(10001, sprintf(lang('mansong_add_start_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_starttime'])));
  125. }
  126. if ($end_time > $quota_end_time) {
  127. ds_json_encode(10001, sprintf(lang('mansong_add_end_time_explain'), date('Y-m-d', $current_mansong_quota['mansongquota_endtime'])));
  128. }
  129. }
  130. if ($end_time < TIMESTAMP) {
  131. ds_json_encode(10001, sprintf(lang('mansong_add_end_time_explain'), date('Y-m-d')));
  132. }
  133. if (empty(input('post.mansong_rule/a'))) {
  134. ds_json_encode(10001, lang('mansong_rule_empty'));
  135. }
  136. $param = array();
  137. $param['mansong_name'] = $mansong_name;
  138. $param['mansong_starttime'] = $start_time;
  139. $param['mansong_endtime'] = $end_time;
  140. $param['store_id'] = session('store_id');
  141. $param['store_name'] = session('store_name');
  142. $param['member_id'] = session('member_id');
  143. $param['member_name'] = session('member_name');
  144. $param['mansongquota_id'] = isset($current_mansong_quota['mansongquota_id']) ? $current_mansong_quota['mansongquota_id'] : 0;
  145. $param['mansong_remark'] = trim(input('post.remark'));
  146. $mansong_id = $pmansong_model->addMansong($param);
  147. if ($mansong_id) {
  148. $mansong_rule_array = array();
  149. foreach (input('post.mansong_rule/a') as $value) {
  150. list($price, $discount, $goods_id) = explode(',', $value);
  151. $mansong_rule = array();
  152. $mansong_rule['mansong_id'] = $mansong_id;
  153. $mansong_rule['mansongrule_price'] = $price;
  154. $mansong_rule['mansongrule_discount'] = $discount;
  155. $mansong_rule['goods_id'] = $goods_id;
  156. $mansong_rule_array[] = $mansong_rule;
  157. }
  158. //生成规则
  159. $result = $mansongrule_model->addMansongruleArray($mansong_rule_array);
  160. $this->recordSellerlog('添加满即送活动,活动名称:' . $mansong_name);
  161. // 自动发布动态
  162. // mansong_name,start_time,end_time,store_id
  163. $data_array = array();
  164. $data_array['mansong_name'] = $param['mansong_name'];
  165. $data_array['mansong_starttime'] = $param['mansong_starttime'];
  166. $data_array['mansong_endtime'] = $param['mansong_endtime'];
  167. $data_array['store_id'] = session('store_id');
  168. $this->storeAutoShare($data_array, 'mansong');
  169. ds_json_encode(10000, lang('mansong_add_success'));
  170. } else {
  171. ds_json_encode(10001, lang('mansong_add_fail'));
  172. }
  173. }
  174. /**
  175. * 满就送活动详细信息
  176. * */
  177. public function mansong_detail()
  178. {
  179. $mansong_id = intval(input('param.mansong_id'));
  180. $pmansong_model = model('pmansong');
  181. $mansongrule_model = model('pmansongrule');
  182. $mansong_info = $pmansong_model->getMansongInfoByID($mansong_id, session('store_id'));
  183. if (empty($mansong_info)) {
  184. $this->error(lang('param_error'));
  185. }
  186. View::assign('mansong_info', $mansong_info);
  187. $param = array();
  188. $param['mansong_id'] = $mansong_id;
  189. $rule_list = $mansongrule_model->getMansongruleListByID($mansong_id);
  190. View::assign('rule_list', $rule_list);
  191. //输出导航
  192. $this->setSellerCurMenu('Sellerpromotionmansong');
  193. return View::fetch($this->template_dir . 'mansong_detail');
  194. }
  195. /**
  196. * 满就送活动删除
  197. * */
  198. public function mansong_del()
  199. {
  200. $mansong_id = intval(input('post.mansong_id'));
  201. $pmansong_model = model('pmansong');
  202. $mansong_info = $pmansong_model->getMansongInfoByID($mansong_id, session('store_id'));
  203. if (empty($mansong_info)) {
  204. ds_json_encode(10001, lang('param_error'));
  205. }
  206. $condition = array();
  207. $condition[] = array('mansong_id', '=', $mansong_id);
  208. $result = $pmansong_model->delMansong($condition);
  209. if ($result) {
  210. $this->recordSellerlog('删除满即送活动,活动名称:' . $mansong_info['mansong_name']);
  211. ds_json_encode(10000, lang('ds_common_op_succ'));
  212. } else {
  213. ds_json_encode(10001, lang('ds_common_op_fail'));
  214. }
  215. }
  216. /**
  217. * 满就送套餐购买
  218. * */
  219. public function mansong_quota_add()
  220. {
  221. $this->setSellerCurMenu('Sellerpromotionmansong');
  222. $this->setSellerCurItem('mansong_quota_add');
  223. return View::fetch($this->template_dir . 'quota_add');
  224. }
  225. /**
  226. * 满就送套餐购买保存
  227. * */
  228. public function mansong_quota_add_save()
  229. {
  230. if (intval(config('ds_config.promotion_mansong_price')) == 0) {
  231. ds_json_encode(10001, lang('param_error'));
  232. }
  233. $mansong_quota_quantity = intval(input('post.mansong_quota_quantity'));
  234. if ($mansong_quota_quantity <= 0 || $mansong_quota_quantity > 12) {
  235. ds_json_encode(10001, lang('mansong_quota_quantity_error'));
  236. }
  237. //获取当前价格
  238. $current_price = intval(config('ds_config.promotion_mansong_price'));
  239. //获取该用户已有套餐
  240. $mansongquota_model = model('pmansongquota');
  241. $current_mansong_quota = $mansongquota_model->getMansongquotaCurrent(session('store_id'));
  242. $mansong_add_time = 86400 * 30 * $mansong_quota_quantity;
  243. if (empty($current_mansong_quota)) {
  244. //生成套餐
  245. $param = array();
  246. $param['member_id'] = session('member_id');
  247. $param['member_name'] = session('member_name');
  248. $param['store_id'] = session('store_id');
  249. $param['store_name'] = session('store_name');
  250. $param['mansongquota_starttime'] = TIMESTAMP;
  251. $param['mansongquota_endtime'] = TIMESTAMP + $mansong_add_time;
  252. $mansongquota_model->addMansongquota($param);
  253. } else {
  254. $param = array();
  255. $param['mansongquota_endtime'] = Db::raw('mansongquota_endtime+' . $mansong_add_time);
  256. $mansongquota_model->editMansongquota($param, array('mansongquota_id' => $current_mansong_quota['mansongquota_id']));
  257. }
  258. //记录店铺费用
  259. $this->recordStorecost($current_price * $mansong_quota_quantity, '购买满即送');
  260. $this->recordSellerlog('购买' . $mansong_quota_quantity . '份满即送套餐,单价' . $current_price . lang('ds_yuan'));
  261. ds_json_encode(10000, lang('mansong_quota_add_success'));
  262. }
  263. /**
  264. * 选择活动商品
  265. * */
  266. public function search_goods()
  267. {
  268. $goods_model = model('goods');
  269. $condition = array();
  270. $condition[] = array('store_id', '=', session('store_id'));
  271. $condition[] = array('goods_name', 'like', '%' . input('param.goods_name') . '%');
  272. $goods_list = $goods_model->getGeneralGoodsList($condition, '*', 8);
  273. View::assign('goods_list', $goods_list);
  274. View::assign('show_page', $goods_model->page_info->render());
  275. echo View::fetch($this->template_dir . 'mansong_goods');
  276. }
  277. /**
  278. * 用户中心右边,小导航
  279. *
  280. * @param string $menu_type 导航类型
  281. * @param string $name 当前导航的name
  282. * @param array $array 附加菜单
  283. * @return
  284. */
  285. protected function getSellerItemList()
  286. {
  287. $menu_array = array(
  288. array(
  289. 'name' => 'mansong_list', 'text' => lang('promotion_active_list'),
  290. 'url' => (string) url('Sellerpromotionmansong/index')
  291. ),
  292. );
  293. switch (request()->action()) {
  294. case 'mansong_add':
  295. $menu_array[] = array(
  296. 'name' => 'mansong_add', 'text' => lang('promotion_join_active'),
  297. 'url' => (string) url('Sellerpromotionmansong/mansong_add')
  298. );
  299. break;
  300. case 'mansong_quota_add':
  301. $menu_array[] = array(
  302. 'name' => 'mansong_quota_add', 'text' => lang('promotion_buy_product'),
  303. 'url' => (string) url('Sellerpromotionmansong/mansong_quota_add')
  304. );
  305. break;
  306. case 'mansong_detail':
  307. $menu_array[] = array(
  308. 'name' => 'mansong_detail', 'text' => lang('mansong_active_content'),
  309. 'url' => (string) url('Sellerpromotionmansong/mansong_detail', array('mansong_id' => input('param.mansong_id')))
  310. );
  311. break;
  312. }
  313. return $menu_array;
  314. }
  315. }