|
- <?php
-
- /**
- * Created by PhpStorm.
- * User: zs
- * Date: 2016/10/15
- * Time: 15:43
- * 销售规则
- */
- class promotionRule extends base
- {
- /**
- * 获取促销规则配置信息
- * @param $param
- */
- public function getRuleBaseInfo($param)
- {
- $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
- $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
- $line_type = isset($param['line_type'])?trim($param['line_type']):false;//线路类型
- if(!$line_id || !$ticket_id || !$line_type)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
-
- //获取可以添加的促销渠道
- $channel_list = $this->getAddChannel($line_type);
- $discount_type = array(array('id'=>332,'type_name'=>'往返优惠'));
- $discount_content = array(array('id'=>333,'type_name'=>'立减'));
- $discount_time = array(array('id'=>0,'type_name'=>'不限')/*,array('id'=>1,'type_name'=>'节假日'),array('id'=>2,'type_name'=>'工作日')*/);
- $json['code'] = '0';
- $json['info'] = '添加基本配置信息成功';
- $json['discount_type'] = $discount_type;
- $json['discount_content'] = $discount_content;
- $json['discount_time'] = $discount_time;
- $json['channel_list'] = $channel_list;
- return $json;
- }
-
- /**
- * 获取可以添加的促销渠道
- * @param $line_id
- * @param $ticket_id
- * @param $line_type
- */
- private function getAddChannel($line_type)
- {
- $user_id = $this->user_id;
- $search_user_sql = " SELECT main_corp_id FROM base_user WHERE id={$user_id} ";
- $user_info = $this->query($search_user_sql);
- $main_corp_id = 0;
- if( $user_info != false && isset($user_info["main_corp_id"])) {
- $main_corp_id = $user_info["main_corp_id"];
- }
- $extra_search_sql = "";
- if( $main_corp_id != 0 ) {
- $extra_search_sql = " AND b.main_corp_id IN (0,{$main_corp_id}) ";
- }
-
- $sql = "SELECT
- a.supplier_id as channel_id,
- b.supplier_name
- FROM
- base_supplier_sale AS a
- INNER JOIN base_supplier AS b ON a.supplier_id = b.id
- WHERE
- a.parent_type = 310
- AND a.product_type in(0," . $line_type . ")
- AND a.cancel_flag = 0
- AND b.cancel_flag = 0
- AND b.is_disabled = 0 {$extra_search_sql}
- GROUP BY
- a.supplier_id";
- $channel_list = $this->query($sql);
- return $channel_list;
-
- //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 . "))
-
- }
-
- /**
- * 获取促销规则列表
- */
- public function getPromotionRuleList($param)
- {
- $page_size = isset($param['page_size'])?trim($param['page_size']):false;
- $current_page = isset($param['current_page'])?trim($param['current_page']):false;
- $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
- $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
- $start_row = ($current_page-1)*$page_size;
- if(!$line_id || !$ticket_id || !$page_size || !$current_page)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- $sql = "SELECT
- group_concat(id) as promotion_id,
- discount_type,
- (SELECT type_name from dict_type where id = discount_type) as discount_type_name,
- off_value,
- discount_day_type,
- CASE discount_day_type WHEN 0 THEN '不限' WHEN 1 THEN '节假日' ELSE '工作日' END AS discount_day_type_name,
- supplier_id as channel_id,
- (SELECT supplier_name from base_supplier where id = channel_id AND cancel_flag = 0) as supplier_name,
- is_onsale
- FROM
- opera_tickets_discount
- WHERE
- cancel_flag = 0
- AND line_id = " . $line_id . "
- AND ticket_id IN (" . $ticket_id . ")
- GROUP BY supplier_id,discount_type
- LIMIT " . $start_row . "," . $page_size;
- $rule_list = $this->query($sql);
- if(count($rule_list) == 0)
- {
- $json['code'] = '0';
- $json['info'] = '返回促销规则列表成功';
- $json['rule_list'] = array();
- $json['page']['page_size'] = $page_size;
- $json['page']['current_page'] = $current_page;
- $json['page']['total_count'] = '0';
- $json['page']['total_page'] = '0';
- return $json;
- }
-
- $count_sql = "SELECT
- count(supplier_id) as total_count
- FROM
- opera_tickets_discount
- WHERE
- cancel_flag = 0
- AND line_id = " . $line_id . "
- AND ticket_id IN (" . $ticket_id . ")
- GROUP BY supplier_id,discount_type";
- $total_count = $this->query($count_sql);
- $total_count = $total_count[0]['total_count'];
- $total_page = ceil($total_count/$page_size);
- $json['code'] = '0';
- $json['info'] = '返回促销规则列表成功';
- $json['rule_list'] = $rule_list;
- $json['page']['page_size'] = $page_size;
- $json['page']['current_page'] = $current_page;
- $json['page']['total_count'] = $total_count;
- $json['page']['total_page'] = $total_page;
- return $json;
- }
-
- /**
- * 添加促销规则
- */
- public function addPromotionRule($param)
- {
- $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
- $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
- $discount_type = isset($param['discount_type'])?trim($param['discount_type']):false;//促销类型
- $off_value = isset($param['off_value'])?trim($param['off_value']):false;//促销内容
- $discount_day_type = isset($param['discount_day_type'])?trim($param['discount_day_type']):false;//促销时间
- $channel_id = isset($param['channel_id'])?trim($param['channel_id']):false;//促销渠道
- $channel_id = rtrim($channel_id, ',');
- $user_id = $this->user_id;
- $create_time = date("Y-m-d H:i:s",time());
- if(!$line_id || !$ticket_id || $discount_type === false || $off_value === false || $discount_day_type === false || !$channel_id)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- $ticket_id_array = explode(",",$ticket_id);
- $channel_id_array = explode(",",$channel_id);
- foreach($ticket_id_array as $ticket_key => $ticket_val)
- {
- foreach($channel_id_array as $channel_key => $channel_val)
- {
- $check_str_array[] = "(discount_type = " . $discount_type . " and supplier_id = " . $channel_val . " and ticket_id = " . $ticket_val . ")";
- }
- }
- $check_str = "(" . implode(" or ",$check_str_array) . ")";
- $check_sql = "SELECT discount_type,
- (SELECT type_name FROM dict_type WHERE id = discount_type) AS discount_type_name,
- supplier_id,
- (SELECT supplier_name FROM base_supplier WHERE id = supplier_id) AS supplier_name,
- ticket_id
- FROM opera_tickets_discount
- WHERE cancel_flag = 0
- AND " . $check_str;
- $check_array = $this->query($check_sql);
- if(count($check_array) > 0)
- {
- $info_array = array();
- foreach($check_array as $k => $v)
- {
- if(!in_array($v['supplier_name'] . "的" . $v['discount_type_name'],$info_array))
- {
- $info_array[] = $v['supplier_name'] . "的" . $v['discount_type_name'];
- }
- }
- $json['code'] = '1';
- $json['info'] = implode(",",$info_array) . "促销规则已添加过";
- return $json;
- }
-
- $insert_sql = "INSERT INTO opera_tickets_discount(
- create_user_id,
- create_time,
- update_user_id,
- update_time,
- cancel_flag,
- line_id,
- ticket_id,
- supplier_id,
- discount_type,
- discount_day_type,
- off_value,
- is_onsale
- ) VALUES";
- foreach($ticket_id_array as $ticket_key => $ticket_val)
- {
- foreach($channel_id_array as $channel_key => $channel_val)
- {
- $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)";
- }
- }
- $insert_sql .= implode(",",$insert_array);
- zzcsUtils::writeLog($insert_sql);
- $result = $this->exec($insert_sql);
- if($result)
- {
- $json['code'] = '0';
- $json['info'] = '添加促销规则成功';
- }else
- {
- $json['code'] = '1';
- $json['info'] = '数据库原因,添加促销规则失败';
- }
- return $json;
- }
-
- /**
- * 改变规则状态
- * @param $param
- */
- public function ruleStatus($param)
- {
- $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
- $status = isset($param['status'])?trim($param['status']):false;//销售规则停用,启用状态
- $user_id = $this->user_id;
- $create_time = date("Y-m-d H:i:s",time());
- if(!$promotion_id || $status === false)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- if($status == 'stop')
- {
- $status_info = 0;
- }
- else
- {
- $status_info = 1;
- }
- $sql = "UPDATE opera_tickets_discount
- SET update_user_id = " . $user_id . ",
- update_time = '" . $create_time . "',
- is_onsale = " . $status_info . "
- WHERE
- cancel_flag = 0
- AND id in (" . $promotion_id . ")";
- $result = $this->exec($sql);
- zzcsUtils::writeLog($sql);
- if($result)
- {
- $json['code'] = '0';
- $json['info'] = '更改促销状态成功';
- }else
- {
- $json['code'] = '1';
- $json['info'] = '数据库原因,更改促销状态失败';
- }
- return $json;
- }
-
- /**
- * 获取页面配置
- * @param $param
- */
- public function getPromotionBase($param)
- {
- $line_id = isset($param['line_id'])?trim($param['line_id']):false;//152251
- $ticket_id = isset($param['ticket_id'])?trim($param['ticket_id']):false;//票种id
- if(!$line_id || !$ticket_id)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- $sql = "SELECT
- seat_type,
- (SELECT type_name FROM dict_type WHERE id = seat_type) AS seat_type_name,
- human_type,
- (SELECT type_name FROM dict_type WHERE id = human_type ) AS human_type_name,
- prod_price,
- cus_price
- FROM
- opera_tickets
- WHERE
- cancel_flag = 0
- AND line_id = " . $line_id . "
- AND ticket_id IN (" . $ticket_id . ")
- GROUP BY line_id";
- $base = $this->query($sql);
- $json['code'] = '0';
- $json['info'] = '获取基础信息成功';
- $json['base'] = $base[0];
- return $json;
- }
-
- /**
- * 删除促销规则
- */
- public function delPromotion($param)
- {
- $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
-
- $user_id = $this->user_id;
- $create_time = date("Y-m-d H:i:s",time());
- if(!$promotion_id)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- $sql = "UPDATE opera_tickets_discount
- SET update_user_id = " . $user_id . ",
- update_time = '" . $create_time . "',
- cancel_flag = 1
- WHERE
- cancel_flag = 0
- AND id in (" . $promotion_id . ")";
- zzcsUtils::writeLog($sql);
- $result = $this->exec($sql);
- if($result)
- {
- $json['code'] = '0';
- $json['info'] = '删除促销规则成功';
- }else
- {
- $json['code'] = '1';
- $json['info'] = '数据库原因,删除促销规则失败';
- }
- return $json;
- }
-
- /**
- * 修改促销规则
- * @return
- */
- public function updatePromotion($param)
- {
- $promotion_id = isset($param['promotion_id'])?trim($param['promotion_id']):false;
- $off_value = isset($param['off_value'])?trim($param['off_value']):false;//促销内容
- $discount_day_type = isset($param['discount_day_type'])?trim($param['discount_day_type']):false;//促销时间
- $user_id = $this->user_id;
- $create_time = date("Y-m-d H:i:s",time());
- if(!$promotion_id || $off_value === false || $discount_day_type === false)
- {
- $json['code'] = '2';
- $json['info'] = '缺少必要参数';
- return $json;
- }
- $sql = "UPDATE opera_tickets_discount
- SET update_user_id = " . $user_id . ",
- update_time = '" . $create_time . "',
- off_value = '" . $off_value . "',
- discount_day_type = " . $discount_day_type . "
- WHERE
- cancel_flag = 0
- AND id in (" . $promotion_id . ")";
- zzcsUtils::writeLog($sql);
- $result = $this->exec($sql);
- if($result)
- {
- $json['code'] = '0';
- $json['info'] = '修改促销规则成功';
- }else
- {
- $json['code'] = '1';
- $json['info'] = '数据库原因,修改促销规则失败';
- }
- return $json;
- }
- }
- ?>
|