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