You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

397 line
15 KiB

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zs
  5. * Date: 2016/10/15
  6. * Time: 15:43
  7. * 销售规则
  8. */
  9. class promotionRule extends base
  10. {
  11. /**
  12. * 获取促销规则配置信息
  13. * @param $param
  14. */
  15. public function getRuleBaseInfo($param)
  16. {
  17. $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
  18. $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
  19. $line_type = isset($param['line_type'])?trim($param['line_type']):false;//线路类型
  20. if(!$line_id || !$ticket_id || !$line_type)
  21. {
  22. $json['code'] = '2';
  23. $json['info'] = '缺少必要参数';
  24. return $json;
  25. }
  26. //获取可以添加的促销渠道
  27. $channel_list = $this->getAddChannel($line_type);
  28. $discount_type = array(array('id'=>332,'type_name'=>'往返优惠'));
  29. $discount_content = array(array('id'=>333,'type_name'=>'立减'));
  30. $discount_time = array(array('id'=>0,'type_name'=>'不限')/*,array('id'=>1,'type_name'=>'节假日'),array('id'=>2,'type_name'=>'工作日')*/);
  31. $json['code'] = '0';
  32. $json['info'] = '添加基本配置信息成功';
  33. $json['discount_type'] = $discount_type;
  34. $json['discount_content'] = $discount_content;
  35. $json['discount_time'] = $discount_time;
  36. $json['channel_list'] = $channel_list;
  37. return $json;
  38. }
  39. /**
  40. * 获取可以添加的促销渠道
  41. * @param $line_id
  42. * @param $ticket_id
  43. * @param $line_type
  44. */
  45. private function getAddChannel($line_type)
  46. {
  47. $user_id = $this->user_id;
  48. $search_user_sql = " SELECT main_corp_id FROM base_user WHERE id={$user_id} ";
  49. $user_info = $this->query($search_user_sql);
  50. $main_corp_id = 0;
  51. if( $user_info != false && isset($user_info["main_corp_id"])) {
  52. $main_corp_id = $user_info["main_corp_id"];
  53. }
  54. $extra_search_sql = "";
  55. if( $main_corp_id != 0 ) {
  56. $extra_search_sql = " AND b.main_corp_id IN (0,{$main_corp_id}) ";
  57. }
  58. $sql = "SELECT
  59. a.supplier_id as channel_id,
  60. b.supplier_name
  61. FROM
  62. base_supplier_sale AS a
  63. INNER JOIN base_supplier AS b ON a.supplier_id = b.id
  64. WHERE
  65. a.parent_type = 310
  66. AND a.product_type in(0," . $line_type . ")
  67. AND a.cancel_flag = 0
  68. AND b.cancel_flag = 0
  69. AND b.is_disabled = 0 {$extra_search_sql}
  70. GROUP BY
  71. a.supplier_id";
  72. $channel_list = $this->query($sql);
  73. return $channel_list;
  74. //AND a.supplier_id not in (SELECT supplier_id from opera_tickets_discount where cancel_flag = 0 AND line_id = " . $line_id . " AND ticket_id in (" . $ticket_id . "))
  75. }
  76. /**
  77. * 获取促销规则列表
  78. */
  79. public function getPromotionRuleList($param)
  80. {
  81. $page_size = isset($param['page_size'])?trim($param['page_size']):false;
  82. $current_page = isset($param['current_page'])?trim($param['current_page']):false;
  83. $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
  84. $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
  85. $start_row = ($current_page-1)*$page_size;
  86. if(!$line_id || !$ticket_id || !$page_size || !$current_page)
  87. {
  88. $json['code'] = '2';
  89. $json['info'] = '缺少必要参数';
  90. return $json;
  91. }
  92. $sql = "SELECT
  93. group_concat(id) as promotion_id,
  94. discount_type,
  95. (SELECT type_name from dict_type where id = discount_type) as discount_type_name,
  96. off_value,
  97. discount_day_type,
  98. CASE discount_day_type WHEN 0 THEN '不限' WHEN 1 THEN '节假日' ELSE '工作日' END AS discount_day_type_name,
  99. supplier_id as channel_id,
  100. (SELECT supplier_name from base_supplier where id = channel_id AND cancel_flag = 0) as supplier_name,
  101. is_onsale
  102. FROM
  103. opera_tickets_discount
  104. WHERE
  105. cancel_flag = 0
  106. AND line_id = " . $line_id . "
  107. AND ticket_id IN (" . $ticket_id . ")
  108. GROUP BY supplier_id,discount_type
  109. LIMIT " . $start_row . "," . $page_size;
  110. $rule_list = $this->query($sql);
  111. if(count($rule_list) == 0)
  112. {
  113. $json['code'] = '0';
  114. $json['info'] = '返回促销规则列表成功';
  115. $json['rule_list'] = array();
  116. $json['page']['page_size'] = $page_size;
  117. $json['page']['current_page'] = $current_page;
  118. $json['page']['total_count'] = '0';
  119. $json['page']['total_page'] = '0';
  120. return $json;
  121. }
  122. $count_sql = "SELECT
  123. count(supplier_id) as total_count
  124. FROM
  125. opera_tickets_discount
  126. WHERE
  127. cancel_flag = 0
  128. AND line_id = " . $line_id . "
  129. AND ticket_id IN (" . $ticket_id . ")
  130. GROUP BY supplier_id,discount_type";
  131. $total_count = $this->query($count_sql);
  132. $total_count = $total_count[0]['total_count'];
  133. $total_page = ceil($total_count/$page_size);
  134. $json['code'] = '0';
  135. $json['info'] = '返回促销规则列表成功';
  136. $json['rule_list'] = $rule_list;
  137. $json['page']['page_size'] = $page_size;
  138. $json['page']['current_page'] = $current_page;
  139. $json['page']['total_count'] = $total_count;
  140. $json['page']['total_page'] = $total_page;
  141. return $json;
  142. }
  143. /**
  144. * 添加促销规则
  145. */
  146. public function addPromotionRule($param)
  147. {
  148. $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
  149. $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
  150. $discount_type = isset($param['discount_type'])?trim($param['discount_type']):false;//促销类型
  151. $off_value = isset($param['off_value'])?trim($param['off_value']):false;//促销内容
  152. $discount_day_type = isset($param['discount_day_type'])?trim($param['discount_day_type']):false;//促销时间
  153. $channel_id = isset($param['channel_id'])?trim($param['channel_id']):false;//促销渠道
  154. $channel_id = rtrim($channel_id, ',');
  155. $user_id = $this->user_id;
  156. $create_time = date("Y-m-d H:i:s",time());
  157. if(!$line_id || !$ticket_id || $discount_type === false || $off_value === false || $discount_day_type === false || !$channel_id)
  158. {
  159. $json['code'] = '2';
  160. $json['info'] = '缺少必要参数';
  161. return $json;
  162. }
  163. $ticket_id_array = explode(",",$ticket_id);
  164. $channel_id_array = explode(",",$channel_id);
  165. foreach($ticket_id_array as $ticket_key => $ticket_val)
  166. {
  167. foreach($channel_id_array as $channel_key => $channel_val)
  168. {
  169. $check_str_array[] = "(discount_type = " . $discount_type . " and supplier_id = " . $channel_val . " and ticket_id = " . $ticket_val . ")";
  170. }
  171. }
  172. $check_str = "(" . implode(" or ",$check_str_array) . ")";
  173. $check_sql = "SELECT discount_type,
  174. (SELECT type_name FROM dict_type WHERE id = discount_type) AS discount_type_name,
  175. supplier_id,
  176. (SELECT supplier_name FROM base_supplier WHERE id = supplier_id) AS supplier_name,
  177. ticket_id
  178. FROM opera_tickets_discount
  179. WHERE cancel_flag = 0
  180. AND " . $check_str;
  181. $check_array = $this->query($check_sql);
  182. if(count($check_array) > 0)
  183. {
  184. $info_array = array();
  185. foreach($check_array as $k => $v)
  186. {
  187. if(!in_array($v['supplier_name'] . "的" . $v['discount_type_name'],$info_array))
  188. {
  189. $info_array[] = $v['supplier_name'] . "的" . $v['discount_type_name'];
  190. }
  191. }
  192. $json['code'] = '1';
  193. $json['info'] = implode(",",$info_array) . "促销规则已添加过";
  194. return $json;
  195. }
  196. $insert_sql = "INSERT INTO opera_tickets_discount(
  197. create_user_id,
  198. create_time,
  199. update_user_id,
  200. update_time,
  201. cancel_flag,
  202. line_id,
  203. ticket_id,
  204. supplier_id,
  205. discount_type,
  206. discount_day_type,
  207. off_value,
  208. is_onsale
  209. ) VALUES";
  210. foreach($ticket_id_array as $ticket_key => $ticket_val)
  211. {
  212. foreach($channel_id_array as $channel_key => $channel_val)
  213. {
  214. $insert_array[] = "(" . $user_id . ",'" . $create_time . "'," . $user_id . ",'" . $create_time . "',0," . $line_id . "," . $ticket_val . "," . $channel_val . "," . $discount_type . "," . $discount_day_type . ",'" . $off_value . "',1)";
  215. }
  216. }
  217. $insert_sql .= implode(",",$insert_array);
  218. zzcsUtils::writeLog($insert_sql);
  219. $result = $this->exec($insert_sql);
  220. if($result)
  221. {
  222. $json['code'] = '0';
  223. $json['info'] = '添加促销规则成功';
  224. }else
  225. {
  226. $json['code'] = '1';
  227. $json['info'] = '数据库原因,添加促销规则失败';
  228. }
  229. return $json;
  230. }
  231. /**
  232. * 改变规则状态
  233. * @param $param
  234. */
  235. public function ruleStatus($param)
  236. {
  237. $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
  238. $status = isset($param['status'])?trim($param['status']):false;//销售规则停用,启用状态
  239. $user_id = $this->user_id;
  240. $create_time = date("Y-m-d H:i:s",time());
  241. if(!$promotion_id || $status === false)
  242. {
  243. $json['code'] = '2';
  244. $json['info'] = '缺少必要参数';
  245. return $json;
  246. }
  247. if($status == 'stop')
  248. {
  249. $status_info = 0;
  250. }
  251. else
  252. {
  253. $status_info = 1;
  254. }
  255. $sql = "UPDATE opera_tickets_discount
  256. SET update_user_id = " . $user_id . ",
  257. update_time = '" . $create_time . "',
  258. is_onsale = " . $status_info . "
  259. WHERE
  260. cancel_flag = 0
  261. AND id in (" . $promotion_id . ")";
  262. $result = $this->exec($sql);
  263. zzcsUtils::writeLog($sql);
  264. if($result)
  265. {
  266. $json['code'] = '0';
  267. $json['info'] = '更改促销状态成功';
  268. }else
  269. {
  270. $json['code'] = '1';
  271. $json['info'] = '数据库原因,更改促销状态失败';
  272. }
  273. return $json;
  274. }
  275. /**
  276. * 获取页面配置
  277. * @param $param
  278. */
  279. public function getPromotionBase($param)
  280. {
  281. $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
  282. $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
  283. if(!$line_id || !$ticket_id)
  284. {
  285. $json['code'] = '2';
  286. $json['info'] = '缺少必要参数';
  287. return $json;
  288. }
  289. $sql = "SELECT
  290. seat_type,
  291. (SELECT type_name FROM dict_type WHERE id = seat_type) AS seat_type_name,
  292. human_type,
  293. (SELECT type_name FROM dict_type WHERE id = human_type ) AS human_type_name,
  294. prod_price,
  295. cus_price
  296. FROM
  297. opera_tickets
  298. WHERE
  299. cancel_flag = 0
  300. AND line_id = " . $line_id . "
  301. AND ticket_id IN (" . $ticket_id . ")
  302. GROUP BY line_id";
  303. $base = $this->query($sql);
  304. $json['code'] = '0';
  305. $json['info'] = '获取基础信息成功';
  306. $json['base'] = $base[0];
  307. return $json;
  308. }
  309. /**
  310. * 删除促销规则
  311. */
  312. public function delPromotion($param)
  313. {
  314. $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
  315. $user_id = $this->user_id;
  316. $create_time = date("Y-m-d H:i:s",time());
  317. if(!$promotion_id)
  318. {
  319. $json['code'] = '2';
  320. $json['info'] = '缺少必要参数';
  321. return $json;
  322. }
  323. $sql = "UPDATE opera_tickets_discount
  324. SET update_user_id = " . $user_id . ",
  325. update_time = '" . $create_time . "',
  326. cancel_flag = 1
  327. WHERE
  328. cancel_flag = 0
  329. AND id in (" . $promotion_id . ")";
  330. zzcsUtils::writeLog($sql);
  331. $result = $this->exec($sql);
  332. if($result)
  333. {
  334. $json['code'] = '0';
  335. $json['info'] = '删除促销规则成功';
  336. }else
  337. {
  338. $json['code'] = '1';
  339. $json['info'] = '数据库原因,删除促销规则失败';
  340. }
  341. return $json;
  342. }
  343. /**
  344. * 修改促销规则
  345. * @return
  346. */
  347. public function updatePromotion($param)
  348. {
  349. $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
  350. $off_value = isset($param['off_value'])?trim($param['off_value']):false;//促销内容
  351. $discount_day_type = isset($param['discount_day_type'])?trim($param['discount_day_type']):false;//促销时间
  352. $user_id = $this->user_id;
  353. $create_time = date("Y-m-d H:i:s",time());
  354. if(!$promotion_id || $off_value === false || $discount_day_type === false)
  355. {
  356. $json['code'] = '2';
  357. $json['info'] = '缺少必要参数';
  358. return $json;
  359. }
  360. $sql = "UPDATE opera_tickets_discount
  361. SET update_user_id = " . $user_id . ",
  362. update_time = '" . $create_time . "',
  363. off_value = '" . $off_value . "',
  364. discount_day_type = " . $discount_day_type . "
  365. WHERE
  366. cancel_flag = 0
  367. AND id in (" . $promotion_id . ")";
  368. zzcsUtils::writeLog($sql);
  369. $result = $this->exec($sql);
  370. if($result)
  371. {
  372. $json['code'] = '0';
  373. $json['info'] = '修改促销规则成功';
  374. }else
  375. {
  376. $json['code'] = '1';
  377. $json['info'] = '数据库原因,修改促销规则失败';
  378. }
  379. return $json;
  380. }
  381. }
  382. ?>