|
- <?php
- /**
- *
- * ============================================================================
- * * 版权所有 蜘蛛出行 * *
- * 网站地址: http://www.zhizhuchuxing.com
- * ----------------------------------------------------------------------------
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
- * 使用;不允许对程序代码以任何形式任何目的的再发布。
- * ============================================================================
- * Author By: 温依莅
- * PhpStorm DynamicBus.php 动态巴士 票种,班次,订单生成
- * Create By 2017/7/19 15:00 $
- */
-
- namespace backend\modules\api\logic;
-
-
- use backend\modules\api\models\BaseUser;
- use backend\modules\api\models\OperaTickets;
- use backend\modules\api\models\OperaTicketsGroup;
- use backend\modules\api\models\OperaLine;
- use backend\modules\api\models\RunMain;
- use backend\modules\api\models\RunProd;
- use backend\modules\api\models\RunStation;
- use backend\modules\api\models\OperaStation;
-
- use backend\modules\api\models\BaseTailorStation;
-
- use common\models\Msg;
- use yii\db\Query;
- use yii\base\Exception;
- use yii\db\Expression;
-
- use Yii;
-
- /**
- * 动态巴士
- */
- class DynamicBus extends Query
- {
- /**
- * Function Description:生成动态巴士票种
- * Function Name: generateTickets
- * @param $line_id
- *
- * @return array
- *
- * @author 温依莅
- */
- public function generateTickets($line_id)
- {
- $result = array();
- //1,根据线路id,获取动态巴士产品信息
- $line_res = OperaLine::getDynamicInfo($line_id);
- if (!$line_res) {
- $result['code'] = '1';
- $result['info'] = '该动态巴士线路无效';
- return $result;
- }
-
- //2,根据线路站点(上下车)得到需要可添加的票种类型
- $station_res = OperaStation::getTicketsInfo($line_id);
- if (!$station_res) {
- $result['code'] = '1';
- $result['info'] = '该线路无站点信息';
- return $result;
- }
- $ticket_station = array();
- foreach ($station_res as $sk => $sv) {
- if ($sv['inout_type'] == 108 || $sv['inout_type'] == 109) {
- foreach ($station_res as $ek => $ev) {
- if (($ev['inout_type'] == 109 || $ev['inout_type'] == 110) && ($sv['order_id'] < $ev['order_id'])) {
- $arr['line_id'] = $ev['line_id'];
- $arr['start_res_id'] = $sv['res_id'];
- $arr['start_res_name'] = $sv['res_name'];
- $arr['start_area_id'] = $sv['area_id'];
- $arr['start_order_id'] = $sv['order_id'];
- $arr['start_inout_type'] = $sv['inout_type'];
- $arr['start_start_minutes'] = $sv['start_minutes'];
-
- $arr['end_res_id'] = $ev['res_id'];
- $arr['end_res_name'] = $ev['res_name'];
- $arr['end_area_id'] = $ev['area_id'];
- $arr['end_order_id'] = $ev['order_id'];
- $arr['end_inout_type'] = $ev['inout_type'];
- $arr['end_start_minutes'] = $ev['start_minutes'];
-
- $ticket_station[$sv['res_id'] . '-' . $ev['res_id']] = $arr;
- }
- }
-
- }
- }
- if (!$ticket_station) {
- $result['code'] = '1';
- $result['info'] = '该线路无法生成票种';
- return $result;
- }
- //3,判断opera_tickets表已有票种,根据票种上下车站点对要添加的票种做去重处理
- $existed_tickets = OperaTickets::getExistedTickets($line_id);
- $add_tickets = array();//需要添加的票种站点信息
- foreach ($ticket_station as $tk => $tv) {
- if (!isset($existed_tickets[$tk])) {
- $add_tickets[$tk] = $tv;
- }
- }
- if (empty($add_tickets)) {
- $result['code'] = '1';
- $result['info'] = '无需要添加票种';
- return $result;
- }
- //4,插入opera_tickets_group和opera_tickets
- #4.1部分准备数据
- $user_id = Yii::$app->request->cookies->getValue('user_id', 2);
- $time = date('Y-m-d H:i:s');
- $extra_arr = array('ticket_type' => 1, 'ticket_name' => '普通座票', 'seat_type' => 72, 'human_type' => 159);//部分票种默认信息
-
- #4.2插入数据
- $transaction = Yii::$app->db->beginTransaction();
- try {
- #4.2.1 先插入opera_tickets表
- $group_ticket_id = array();//添加的票种id集合
- $group_station_id = array();
- $group_station_name = array();
- foreach ($add_tickets as $k => $v) {
- $opera_tickets_one = [
- 'CREATE_USER_ID' => $user_id,
- 'CREATE_TIME' => $time,
- 'UPDATE_USER_ID' => $user_id,
- 'UPDATE_TIME' => $time,
- 'CANCEL_FLAG' => 0,
- 'LINE_ID' => $line_id,
- 'TICKET_NAME' => $extra_arr['ticket_name'],
- 'TICKET_TYPE' => $extra_arr['ticket_type'],
- 'START_STATION_RES_ID' => $v['start_res_id'],
- 'END_STATION_RES_ID' => $v['end_res_id'],
- 'START_STATION_AREA_ID' => $v['start_area_id'],
- 'END_STATION_AREA_ID' => $v['end_area_id'],
- 'SEAT_TYPE' => $extra_arr['seat_type'],
- 'HUMAN_TYPE' => $extra_arr['human_type'],
- 'PROD_PRICE' => $line_res['prod_price'],
- 'CUS_PRICE' => $line_res['cus_price'],
- 'COST_PRICE' => $line_res['cost_price'],
- 'IS_ONSALE' => 1,
- ];
- $new_ticket = new OperaTickets();
- $new_ticket->attributes = $opera_tickets_one;
- if (!$new_ticket->insert()) {
- throw new Exception(serialize($new_ticket->getErrors()));
- }
- $group_ticket_id[] = $new_ticket->TICKET_ID;
- $group_station_id[] = $v['start_res_id'] . '-' . $v['end_res_id'];
- $group_station_name[] = $v['start_res_name'] . '-' . $v['end_res_name'];
- }
- #4.2.2 再插入opera_tickets_group表
- $opera_tickets_group_arr = array(
- 'CREATE_USER_ID' => $user_id,
- 'CREATE_TIME' => $time,
- 'UPDATE_USER_ID' => $user_id,
- 'UPDATE_TIME' => $time,
- 'CANCEL_FLAG' => 0,
- 'LINE_ID' => $line_id,
- 'TICKET_ID' => implode(',', $group_ticket_id),
- 'STATION_ID' => implode(',', $group_station_id),
- 'STATION_NAME' => implode(',', $group_station_name),
- 'SEAT_TYPE' => $extra_arr['seat_type'],
- 'HUMAN_TYPE' => $extra_arr['human_type'],
- 'PROD_PRICE' => $line_res['prod_price'],
- 'CUS_PRICE' => $line_res['cus_price'],
- 'COST_PRICE' => $line_res['cost_price'],
- );
- $ticket_group_obj = new OperaTicketsGroup();
- $ticket_group_obj->attributes = $opera_tickets_group_arr;
- if (!$ticket_group_obj->insert()) {
- throw new Exception(serialize($ticket_group_obj->getErrors()));
- }
-
- } catch (Exception $e) {
- # 回滚事务
- $transaction->rollBack();
- $result['code'] = '1';
- $result['info'] = '动态巴士票种添加失败,原因:' . $e->getMessage();
- return $result;
- }
-
- $transaction->commit();
- $result['code'] = '0';
- $result['info'] = '动态巴士票种添加成功';
- $result['list'] = array('ticket_arr' => $group_ticket_id);
- return $result;
- }
-
- /**
- * Function Description:生成动态巴士班次
- * Function Name: generateRunInfo
- * @param int $line_id 线路id
- * @param string $start_time 班次出发时间
- * @return mixed
- *
- * @author 温依莅
- */
- public function generateRunInfo($line_id, $start_time)
- {
- $result = array();
- $user_id = Yii::$app->request->cookies->getValue('user_id', 2);
- //0,判断班次时间格式是否正确
- if (!preg_match('/^(0[0-9]|1[0-9]|2[0-3])\:(0[0-9]|[1-5][0-9])$/', $start_time)) {
- $result['code'] = '1';
- $result['info'] = '产品出发时间格式不正确';
- return $result;
- }
- //1,根据动态巴士线路id 获取班次添加相关参数
- $run_param = array(
- 'user_id' => $user_id,
- 'lineid' => $line_id,//线路id 448655
- 'starttime' => '', //2017-07-26 班次最早日期
- 'endtime' => '', //2017-07-29 班次最晚日期
- 'week' => '2345671', //星期限制
- 'allstation' => '', //{9092,0,108,0}{9300,10,108,0}{100437,60,110,0} //{站点res_id,分钟数[距离0点],上下类型[108只上109上下110只下114不停],0}
- 'allcar' => '', //{1126,1,156,288,628,477,188}{221,1,156,288,400,478,56} //{车型类型,数量,156可选157不可选,车品牌名,供应商,计价方式[477包车计价478车座计价],价格}
- 'allticket' => '', //{3249,100437,72,159,0}{3326,100437,72,159,0} //{开始地区id,结束地区id,座位类型,人群类型}
- 'sellday' => 7, //预售天数【无实际作用】
- );
- $run_extra_arr = array( //班次添加部分默认参数
- 'bus_brand' => 288, //车辆品牌 288 金龙
- 'supplier_id' => 628, //车辆供应商 628 恒栋
- 'bus_money' => 500, //包车价格 默认500
- 'bus_type_19' => 1126, //19座的base_resource.res_id
- 'bus_type_53' => 1004, //53座的base_resource.res_id
- 'count_type' => 477, //计价方式 dict_type.id 477包车计价,478车座计价
- 'seat_type' => 72, //座位类型 72普通座
- 'human_type' => 159 //人群属性 159成人
- );
- #1.1获取班次日期
- $run_date = BaseTailorStation::getDynamicRunDate($line_id);
- if (!$run_date) {
- $result['code'] = '1';
- $result['info'] = '该动态巴士线路无有效乘客';
- return $result;
- }
- $run_param['starttime'] = $run_date;
- $run_param['endtime'] = $run_date;
- #1.2获取【班次站点信息allstation】 和 【站点区域分组 allticket】
- $station_res = OperaStation::getTicketsInfo($line_id);
- if (!$station_res) {
- $result['code'] = '1';
- $result['info'] = '该线路无站点信息';
- return $result;
- }
- #1.2.1获取【站点参数allstation】
- $allstation = '';
- $base_minutes = ceil((strtotime($run_date . ' ' . $start_time . ':00') - strtotime($run_date)) / 60); //获取基准分钟数
-
- foreach ($station_res as $k => $v) {
- $allstation .= '{' . $v['res_id'] . ',' . ($v['start_minutes'] + $base_minutes) . ',' . $v['inout_type'] . ',' . '0' . '}';
- }
- $run_param['allstation'] = $allstation;
- #1.2.2获取【站点区域分组参数 allticket】
- $ticket_area = array();
- foreach ($station_res as $sk => $sv) {
- if ($sv['inout_type'] == 108 || $sv['inout_type'] == 109) {
- foreach ($station_res as $ek => $ev) {
- if (($ev['inout_type'] == 109 || $ev['inout_type'] == 110) && ($sv['order_id'] < $ev['order_id'])) {
- $arr['start_area_id'] = $sv['area_id']; //起始区域id
- $arr['end_area_id'] = $ev['area_id']; //结束区域id
- $arr['seat_type'] = $run_extra_arr['seat_type'];//座位类型
- $arr['human_type'] = $run_extra_arr['human_type'];//人群属性
- $ticket_area[$sv['area_id'] . '-' . $ev['area_id']] = $arr;
- }
- }
- }
- }
- if (!$ticket_area) {
- $result['code'] = '1';
- $result['info'] = '该线路无有效票种【上下车区域判定】';
- return $result;
- }
- foreach ($ticket_area as $k => $v) {
- $run_param['allticket'] .= '{' . $v['start_area_id'] . ',' . $v['end_area_id'] . ',' . $v['seat_type'] . ',' . $v['human_type'] . ',' . '0' . '}';
- }
- #1.3获取车次车信息【allcar:订单总人数小于19人派 19座恒栋包车500元,大于19人派53座的恒栋包车500元】
- #1.3.1从动态巴士用户信息表获取该线路的总人数
- $person_num = BaseTailorStation::getTotalNum($line_id);
- if (!$person_num) {
- $result['code'] = '1';
- $result['info'] = '该动态线路无有效用户';
- return $result;
- }
- #1.3.2根据人数生成车次的选用车辆信息
- if ($person_num > 53) {
- $result['code'] = '1';
- $result['info'] = '超过最大53人,请联系管理员';
- return $result;
- }
- if ($person_num <= 19) {
- $run_param['allcar'] = '{' . $run_extra_arr['bus_type_19'] . ',' . '1' . ',' . '156' . ',' . $run_extra_arr['bus_brand'] . ',' . $run_extra_arr['supplier_id'] . ',' . $run_extra_arr['count_type'] . ',' . $run_extra_arr['bus_money'] . '}';
- } else {
- $run_param['allcar'] = '{' . $run_extra_arr['bus_type_53'] . ',' . '1' . ',' . '156' . ',' . $run_extra_arr['bus_brand'] . ',' . $run_extra_arr['supplier_id'] . ',' . $run_extra_arr['count_type'] . ',' . $run_extra_arr['bus_money'] . '}';
- }
- //2,调用巴士班次添加接口添加班次
- $sql = 'call sp_bus_run_new_save_tailor(' . $run_param['user_id'] . ',' . $run_param['lineid'] . ',' . $run_param['starttime'] . ',' . $run_param['endtime'] . ',' . $run_param['week'] . ',' . $run_param['allstation'] . ',' . $run_param['allcar'] . ',' . $run_param['allticket'] . ',' . $run_param['sellday'] . ')';
- //$this->writeLog($sql, 'sp_bus_run_new_save');//写入log
- $pres_obj = Yii::$app->db->createCommand("call sp_bus_run_new_save_tailor(:para_user_id,:para_lineid,:para_starttime,:para_endtime,:para_week,:para_allstation,:para_allcar,:para_allticket,:para_sellday)")
- ->bindValues([
- ":para_user_id" => $run_param['user_id'] + 0,
- ":para_lineid" => $run_param['lineid'],
- ":para_starttime" => $run_param['starttime'],
- ":para_endtime" => $run_param['endtime'],
- ":para_week" => $run_param['week'],
- ":para_allstation" => $run_param['allstation'],
- ":para_allcar" => $run_param['allcar'],
- ":para_allticket" => $run_param['allticket'],
- ":para_sellday" => $run_param['sellday'],
- ]);
- $query_sql = $pres_obj->getRawSql();
- $this->writeLog($query_sql, 'sp_bus_run_new_save');//写入log
- $pres = $pres_obj->queryOne();
- if ($pres['errcode'] != 0) {
- $result['code'] = '1';
- $result['info'] = '添加班次失败【' . $pres['errinfo'] . '】';
- return $result;
- }
-
- $result['code'] = '0';
- $result['info'] = '动态巴士班次添加成功';
- $result['list'] = array('run_param' => $run_param);
- return $result;
- }
-
- /**
- * Function Description:生成动态巴士订单
- * Function Name: generateOrders
- * @param int $line_id 线路id
- * @param $start_time
- *
- * @return array
- *
- * @author 温依莅
- */
- public function generateOrders($line_id, $start_time)
- {
- $result = array();
- $user_id = Yii::$app->request->cookies->getValue('user_id', 2);
- $main_corp_id = BaseUser::getMainCorpId($user_id);
- if (!$main_corp_id) {
- $result['code'] = '1';
- $result['info'] = '当前用户运营主体有误';
- return $result;
- }
- //1,根据line_id和start_time获取下单需要参数
- $order_extra_arr = array( //订单生成部分参数
- 'user_id' => $user_id, //用户id
- 'member_id' => $user_id, //会员id
- 'order_book_status' => 0, //是否是预定 1是0否
- 'pay_type' => 275, //支付方式 275 授信支付
- 'customer_id_type' => 150, //证件类型 150身份证
- 'order_pay_status' => 1, //支付状态 1已支付 0待支付
- 'is_send_msg' => 1, // 是否发送短信 1是0否
- 'sale_man' => '', // 业务员
- );
- #1.1获取该班次run_id
- $run_date = BaseTailorStation::getDynamicRunDate($line_id);
- $run_id = RunMain::getRunIdByLine($line_id, $run_date, $start_time);
- if (!$run_id) {
- $result['code'] = '1';
- $result['info'] = '该线路无有效班次';
- return $result;
- }
- #1.2遍历获取每单的:乘客,票种,渠道信息
- #1.2.1 获取该线路的乘客信息
- $customer_arr = BaseTailorStation::getDynamicCustomerInfo($line_id, $main_corp_id);
- if (!$customer_arr) {
- $result['code'] = '1';
- $result['info'] = '该线路无符合条件下单用户';
- return $result;
- }
- //$this->writeLog(json_encode($customer_arr));
- #1.2.2 获取票种渠道等信息,并检验数据
- $customer_final = array();
- foreach ($customer_arr as $k => $v) {
- //数据校验
- if (!$v['inout_type'] || !$v['address_res_id'] || !$v['product_res_id'] || !$v['product_id'] || !$v['org_id'] || $v['person_num'] < 1) {
- $result['code'] = '1';
- $result['info'] = '乘客-' . $v['name'] . $v['tel'] . ' 数据不完整';
- return $result;
- }
- if ($v['inout_type'] != 108 && $v['inout_type'] != 110) {
- $result['code'] = '1';
- $result['info'] = '乘客-' . $v['name'] . $v['tel'] . ' 的站点类型数据库有误';
- return $result;
- }
- //生成票种渠道等信息
- $start_res_id = $v['inout_type'] == 108 ? $v['address_res_id'] : $v['product_res_id'];
- $end_res_id = $v['inout_type'] == 110 ? $v['address_res_id'] : $v['product_res_id'];
- $ticket_arr = OperaTickets::getTicketByRes($line_id, $start_res_id, $end_res_id);
- if (!$ticket_arr) {
- $result['code'] = '1';
- $result['info'] = '乘客-' . $v['name'] . $v['tel'] . ' 的上下车站点无有效票种';
- return $result;
- }
- $v['ticket_id'] = $ticket_arr['ticket_id'];
- $v['cost_price'] = $ticket_arr['cost_price'];
- $v['prod_price'] = $ticket_arr['prod_price'];
- $v['cus_price'] = $ticket_arr['cus_price'];
- $customer_final[$k] = $v;
- }
- //2,批量下单,返回结果
- $order_result = array();//批量下单结果数组
- foreach ($customer_final as $k => $v) {
- #2.1整理下单参数
- $prod_str = '{' . '1' . ',' . $run_id . ',' . $v['ticket_id'] . ',' . $v['prod_price'] . ',' . $v['person_num'] . '}';
- $order_info_str = '{' . $order_extra_arr['user_id'] . ',' . $order_extra_arr['member_id'] . ',' . $v['org_id'] . ',' . $v['org_no'] . ',' . $order_extra_arr['order_book_status'] . ',' . $order_extra_arr['pay_type'] . ',' . $order_extra_arr['order_pay_status'] . ',' . $order_extra_arr['sale_man'] . '}';
- $customer_info_str = '{' . $v['name'] . ',' . $v['tel'] . ',' . $order_extra_arr['customer_id_type'] . ',' . $v['id_card'] . ',' . $v['memo'] . ',' . $v['address'] . '}';
- #2.2整合最终下单参数
- $url = 'http://' . CS1_DOMAIN . '/api/submit-order';
- $send_data = array();
- $send_data['action'] = 'submit_bus_order';
- $send_data['prod_str'] = $prod_str;//购买票种列表 格式为{去程/返程标志(1:去程 2:返程),去程/返程班次ID,票种ID,票种单价,预定数量,车号}...
- $send_data['order_info_str'] = $order_info_str;//'订单信息 {下单用户,会员号,渠道id,渠道订单号,是否自动退票(0否1是),支付方式(授信275),是否支付(0否1是),业务员}'
- $send_data['customer_info_str'] = $customer_info_str;//联系人信息,格式为{联系人,联系电话,z证件类型,联系人身份证,备注}
- $send_data['order_append_str'] = '{0,0}';//订单附加 ,格式为{门票价格,酒店价格}
- $send_data['receiver_value_str'] = '{0,0}';//代收,格式为{代收单价,代收总价}
- $send_data['is_send_msg'] = 1;//是否发短信(0否1是) 默认1
- $json_res = Msg::httpRequest($url, $send_data);
- $res_arr = json_decode($json_res, true);
- #2.3 结果插入数组
- $tmp_res = array();
- $tmp_res['name'] = $v['name'];
- $tmp_res['tel'] = $v['tel'];
- $tmp_res['code'] = $res_arr['code'];
- $tmp_res['customer_param'] = $v; //下单用户信息参数
- $tmp_res['order_param'] = $send_data;//下单参数
- $tmp_res['return_info'] = $res_arr; //下单返回值参数
- $order_result[$v['id'] . '-' . $v['name'] . '-' . $v['tel']] = $tmp_res;
- //$this->writeLog(json_encode($tmp_res), 'single_submit_order');//写入log
- #2.4 下单如果成功,将订单号反写入
- $base_tailor_obj = BaseTailorStation::findOne(['id' => $v['id']]);
-
- if ($res_arr['code'] == '0' && $base_tailor_obj) {
- $base_tailor_obj->order_id = $res_arr['data'];
- $up_res = $base_tailor_obj->update();
- //$this->writeLog($v['id'].'-order_id:'.$base_tailor_obj->order_id, 'base_tailor_obj');//写入log
- if ($up_res === false) {
- $this->writeLog(json_encode($base_tailor_obj->getErrors()), 'rewrite_order');//写入log
- }
- }
-
- }
-
- //3,整理返回结果
- $count_num = 0;
- $total_num = count($customer_final);
- $info = '';
- $code = '0';
- foreach ($order_result as $ok => $ov) {
- if ($ov['code'] == '0') {
- $count_num++;
- }
- }
- if ($count_num == $total_num) {
- $info = '下单成功';
- } else if ($count_num == 0) {
- $code = '1';
- $info = '下单失败';
- } else if ($count_num < $total_num) {
- $code = '1';
- $info = "部分订单下单失败:【全部共$total_num 个订单,下单成功$count_num 个订单】";
- } else {
- $code = '1';
- $info = '未知错误';
- }
- $result['code'] = $code;
- $result['info'] = $info;
- $result['list'] = $order_result;
- //$this->writeLog(json_encode($order_result), 'submit_dynamic_order');//写入log
- return $result;
- }
-
- /**
- * User: wangxj
- *
- * 保存日志
- *
- * @param $action
- * @param $string
- */
- public function writeLog($string, $action = 'record')
- {
- $string = date('Y-m-d H:i:s') . " $action " . PHP_EOL . $string . PHP_EOL;
- if (!file_exists(__DIR__ . '/../log/dynamic')) {
- mkdir(__DIR__ . '/../log/dynamic', 0777, true);
- }
- file_put_contents(__DIR__ . '/../log/dynamic/' . date('Y-m-d') . '.log', $string, FILE_APPEND);
- }
-
- }
|