request->cookies->getValue('user_id', 2);
$result = array();
$res = BaseUser::find()->select('id,main_corp_id,user_name')->where(['id' => $user_id, 'cancel_flag' => 0])->asArray()->one();
if (!$res) {
$result['code'] = '1';
$result['info'] = '无效用户';
return $result;
}
$real_main_corp_id = ($main_corp_id == -1 ? $res['main_corp_id'] : $main_corp_id);
if ($real_main_corp_id == 0) { //如果main_corp_id为0,下单时视为蜘蛛国旅(运营主体为1)处理
$real_main_corp_id = 1;
}
//1,根据prod_id,main_corp_id判断是否是代售
$res2 = OperaTickets::find()
->select('a.ticket_id,a.line_id,b.main_corp_id')
->from('opera_tickets as a')
->leftJoin('opera_line as b', 'a.line_id=b.line_id')
->where(['a.ticket_id' => $prod_id])
->asArray()->one();
if (!$res2) {
$result['code'] = '1';
$result['info'] = '票种信息错误或不可售';
return $result;
}
$line_id = $res2['line_id'];
$ori_main_corp_id = $res2['main_corp_id'];//线路所属运营主体
$is_agent = 0;//是否是代售
$agent_level = 1;//销售层级
if ($ori_main_corp_id != $real_main_corp_id) {
$is_agent = 1;
}
$ticket_agent_list = array();
$org_id = $this->getLineOrgId($prod_id);//票种所在线路 的原始供应商id
$org_name = $this->getSupplierName($org_id);//票种所在线路 的原始供应商名称
$list = array('prod_id' => $prod_id, 'line_id' => $line_id, 'supplier_id' => $supplier_id, 'is_agent' => $is_agent, 'ori_main_corp_id' => $ori_main_corp_id, 'self_main_corp_id' => $real_main_corp_id, 'agent_depth' => $agent_level);
//如果是自营非代收,返回信息
if ($is_agent == 0) {
$base_price_one = $this->getOwnerCost($prod_id);
$ticket_agent_one = array(
'prod_top_org_id' => $org_id,
'prod_supply_org_name' => $org_name,
'outside_sale_org_id' => $supplier_id,
'outside_sale_org_name' => $this->getSupplierName($supplier_id),
'outside_order_no' => 'unknown',
'sub_channel_id' => $supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => $real_main_corp_id,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => '',
'agent_level' => 1,
'base_price' => $base_price_one,
'order_price' => 'unknown'
);
$ticket_agent_list[1] = $ticket_agent_one;
$list['ticket_agent_list'] = $ticket_agent_list;
$result['code'] = '0';
$result['info'] = '获取票种代售信息成功(自营票种)';
$result['list'] = $list;
return $result;
}
//2,判断代售票种是否可售,如果可售,获取相关信息
#2.1 获取该运营主体所对应的渠道id
$corp_supplier_id = $this->getCorpSupplierId($real_main_corp_id);//所属运营主体对应的渠道
if (!$corp_supplier_id) {
$result['code'] = '1';
$result['info'] = '该运营主体无对应渠道';
$result['error_info'] = $res['info'];
return $result;
}
#2.2 判断代售票种是否可售
$res = OperaTicketsAgent::find()->select('a.ticket_id,a.line_id,a.from_main_corp_id,a.to_main_corp_id,c.authority_status,c.prod_price as spec_prod_price,a.cost_price,a.prod_price')
->from(OperaTicketsAgent::tableName() . ' as a')
->leftJoin(OperaTicketsDistrib::tableName() . ' as b', 'a.ticket_id=b.ticket_id and b.supplier_id=' . $supplier_id)
->leftJoin(OperaTicketsDistrib::tableName() . ' as c', 'a.ticket_id=c.ticket_id and c.supplier_id=' . $corp_supplier_id)
->where([
'and',
'a.ticket_id= ' . $prod_id,
'a.to_main_corp_id=' . $real_main_corp_id,
'a.authority_status=1',
'a.is_onsale=1',
'a.cancel_flag=0',
['or', 'b.cancel_flag=0', 'b.cancel_flag is null'],
['or', 'c.cancel_flag=0', 'c.cancel_flag is null'],
['or', 'b.authority_status=1', 'b.authority_status is null'],
['or', 'c.authority_status=1', 'c.authority_status is null']
])->limit(1);
$ticket_info_sql = $res->createCommand()->getRawSql();
$ticket_info = $res->asArray()->one();
if (!$ticket_info) {
$list['agent_depth'] = $agent_level;
$result['code'] = '1';
$result['info'] = '代售票种' . $prod_id . '不可售';
$result['list'] = $list;
return $result;
}
#部分公用信息
$res1 = $this->getCorpSupplierId(1);//取运营主体【蜘蛛国旅】对应的渠道id
$res2 = $this->getCorpSupplierId(1, 187);//取运营主体【蜘蛛国旅】对应的供应商id
if (!$res1 || !$res2) {
$result['code'] = '1';
$result['info'] = '运营主体对应渠道或供应商出错(spider)';
$result['list'] = $list;
return $result;
}
$spider_supplier_id = $res1;//运营主体【蜘蛛国旅】对应的渠道id
$spider_org_id = $res2;//运营主体【蜘蛛国旅】对应的供应商id
$spider_org_name = $this->getSupplierName($spider_org_id);//运营主体【蜘蛛国旅】对应的供应商名称
//3,获取代售具体信息
#--------------------- 3.1 两级结构 (1)蜘蛛代售其他运营主体票种,运营主体a=>蜘蛛 (2)其他运营主体代售蜘蛛的票种,蜘蛛=>运营主体b ---------
if ($real_main_corp_id == 1 || $ori_main_corp_id == 1) {
$agent_level = 2;
#获取agent_level=1的成本 (是上级票种提供方的分销价)
$base_price_one = $ticket_info['spec_prod_price'] ? $ticket_info['spec_prod_price'] : $ticket_info['cost_price'];
$org_id_one = $this->getCorpSupplierId($ori_main_corp_id, 187);//上级运营渠道所对应的供应商id
if (!$org_id_one) {
$result['code'] = '1';
$result['info'] = '运营主体对应渠道或供应商出错(spider)';
return $result;
}
$org_name_one = $this->getSupplierName($org_id_one);//上级运营渠道所对应的供应商名称
#获取agent_level=2的成本(实际应为用车成本,此处取预估成本)
$base_price_two = $this->getOwnerCost($prod_id);
#获取agent_level=1的相关信息 为'unknown'的项,在后续下单流程补充
$ticket_agent_one = array(
'prod_top_org_id' => $org_id_one,
'prod_supply_org_name' => $org_name_one,
'outside_sale_org_id' => $supplier_id,
'outside_sale_org_name' => $this->getSupplierName($supplier_id),
'outside_order_no' => 'unknown',
'sub_channel_id' => $supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => $real_main_corp_id,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => 'unknown',
'agent_level' => 1,
'base_price' => $base_price_one,
'order_price' => 'unknown'
);
#获取agent_level=2的相关信息 为'unknown'的项,在后续下单流程补充
$ticket_agent_two = array(
'prod_top_org_id' => $org_id,
'prod_supply_org_name' => $org_name,
'outside_sale_org_id' => $corp_supplier_id,
'sales_man' => $this->getSupplierSalesName($corp_supplier_id),
'outside_sale_org_name' => $this->getSupplierName($corp_supplier_id),
'outside_order_no' => 'unknown',
'sub_channel_id' => $supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => $ori_main_corp_id,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => 'unknown',
'agent_level' => 2,
'base_price' => $base_price_two,
'order_price' => $base_price_one
);
$ticket_agent_list[1] = $ticket_agent_one;
$ticket_agent_list[2] = $ticket_agent_two;
#3.2 ------------------- 三级结构,运营主体a=>蜘蛛=>运营主体b -------------------------------------------------------
} else {
$agent_level = 3;
#获取agent_level=1的成本 (是票种上级2提供的分销价)
$base_price_one = $ticket_info['spec_prod_price'] ? $ticket_info['spec_prod_price'] : $ticket_info['cost_price'];
#获取agent_level=2的成本 (是票种上级3提供的分销价)
$org_id_two = $this->getCorpSupplierId($ori_main_corp_id, 187);//上级运营渠道所对应的供应商id
if (!$org_id_two) {
$result['code'] = '1';
$result['info'] = '运营主体对应渠道或供应商出错(spider)';
return $result;
}
$org_name_two = $this->getSupplierName($org_id_two);//上级运营渠道所对应的供应商名称
$mid_ticket = OperaTicketsAgent::find()->select('a.ticket_id,a.line_id,a.from_main_corp_id,a.to_main_corp_id,b.authority_status,b.prod_price as spec_prod_price,a.cost_price,a.prod_price')
->from(OperaTicketsAgent::tableName() . ' as a')
->leftJoin(OperaTicketsDistrib::tableName() . ' as b', 'a.ticket_id=b.ticket_id and b.supplier_id=' . $spider_supplier_id)
->where([
'and',
'a.ticket_id= ' . $prod_id,
'a.to_main_corp_id=1',
'a.authority_status=1',
'a.is_onsale=1',
'a.cancel_flag=0',
['or', 'b.cancel_flag=0', 'b.cancel_flag is null'],
['or', 'b.authority_status=1', 'b.authority_status is null']
])->asArray()->limit(1)->one();
if (!$mid_ticket) {
$list['agent_depth'] = $agent_level;
$result['code'] = '1';
$result['info'] = '代售票种' . $prod_id . '不可售(没有得到正确授权)';
$result['list'] = $list;
return $result;
}
$base_price_two = $mid_ticket['spec_prod_price'] ? $mid_ticket['spec_prod_price'] : $mid_ticket['cost_price'];
#获取agent_level=3的成本(实际应为用车成本,此处取预估成本)
$base_price_three = $this->getOwnerCost($prod_id);
#获取agent_level=1的相关信息 为'unknown'的项,在后续下单流程补充
$ticket_agent_one = array(
'prod_top_org_id' => $spider_org_id,
'prod_supply_org_name' => $spider_org_name,
'outside_sale_org_id' => $supplier_id,
'outside_sale_org_name' => $this->getSupplierName($supplier_id),
'outside_order_no' => 'unknown',
'sub_channel_id' => $supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => $real_main_corp_id,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => 'unknown',
'agent_level' => 1,
'base_price' => $base_price_one,
'order_price' => 'unknown'
);
#获取agent_level=2的相关信息 为'unknown'的项,在后续下单流程补充
$ticket_agent_two = array(
'prod_top_org_id' => $org_id_two,
'prod_supply_org_name' => $org_name_two,
'outside_sale_org_id' => $corp_supplier_id,
'outside_sale_org_name' => $this->getSupplierName($corp_supplier_id),
'sales_man' => $this->getSupplierSalesName($corp_supplier_id),
'outside_order_no' => 'unknown',
'sub_channel_id' => $supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => 1,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => 'unknown',
'agent_level' => 2,
'base_price' => $base_price_two,
'order_price' => $base_price_one
);
#获取agent_level=3的相关信息 为'unknown'的项,在后续下单流程补充
$ticket_agent_three = array(
'prod_top_org_id' => $org_id,
'prod_supply_org_name' => $org_name,
'outside_sale_org_id' => 1,
'outside_sale_org_name' => $this->getSupplierName(1),
'sales_man' => $this->getSupplierSalesName(1),
'outside_order_no' => 'unknown',
'sub_channel_id' => $corp_supplier_id,
'sub_channel_no' => 'unknown',
'main_corp_id' => $ori_main_corp_id,
'source_main_corp_id' => $ori_main_corp_id,
'sale_path' => 'unknown',
'agent_level' => 3,
'base_price' => $base_price_three,
'order_price' => $base_price_two
);
$ticket_agent_list[1] = $ticket_agent_one;
$ticket_agent_list[2] = $ticket_agent_two;
$ticket_agent_list[3] = $ticket_agent_three;
}
//4,构造返回数组
$list['agent_depth'] = $agent_level;
$list['ticket_agent_list'] = $ticket_agent_list;
$result['code'] = '0';
$result['info'] = '获取票种代售信息成功(代售票种)';
$result['list'] = $list;
return $result;
}
/**
* Function Description:得到运营主体相对应的渠道id或者供应商id
* Function Name: getCorpSupplierId
* @param $main_corp_id
* @param $type 301渠道商id,187供应商id
* @return array
*
* @author 温依莅
*/
public function getCorpSupplierId($main_corp_id, $type = 301)
{
$type_name = $type == 301 ? '渠道商' : '供应商';
$res = BaseSupplier::find()->select('id,supplier_name')->where(['reflect_main_corp' => $main_corp_id, 'supplier_type' => $type, 'is_disabled' => 0, 'cancel_flag' => 0])->limit(1)->asArray()->one();
$result = array();
// if (!$res) {
// $result['code'] = '1';
// $result['info'] = '该运营主体无对应' . $type_name;
// return $result;
// }
$corp_supplier_id = $res['id'];//所属运营主体对应的渠道/供应商
// $result['code'] = '0';
// $result['info'] = '返回对应' . $type_name . '成功';
// $result['id'] = $corp_supplier_id;
// return $result;
return $corp_supplier_id;
}
/**
* Function Description:得到票种拥有者的票种预估成本
* Function Name: getOwnerCost
* @param $prod_id
*
* @return int
*
* @author 温依莅
*/
public function getOwnerCost($prod_id)
{
$res = OperaTickets::find()->select('ticket_id,cost_price')->where(['ticket_id' => $prod_id, 'cancel_flag' => 0])->limit(1)->asArray()->one();
return $res['cost_price'];
}
/**
* Function Description:取得票种所在线路的供应商id
* Function Name: getLineOrgId
* @param $prod_id
*
* @return int
*
* @author 温依莅
*/
public function getLineOrgId($prod_id)
{
$res = OperaTickets::find()
->select('a.ticket_id,b.org_id')
->from(OperaTickets::tableName() . ' as a')
->leftJoin(OperaLine::tableName() . ' as b', 'a.line_id=b.line_id')
->where([
'ticket_id' => $prod_id
])->limit(1)->asArray()->one();
return $res['org_id'];
}
/**
* Function Description:根据id获取供应商(渠道商)名称
* Function Name: getSupplierName
* @param $supplier_id
*
* @return string
*
* @author 温依莅
*/
public function getSupplierName($supplier_id)
{
$res = BaseSupplier::find()->select('id,supplier_name')->where(['id' => $supplier_id])->limit(1)->asArray()->one();
return $res['supplier_name'];
}
/**
* Function Description:根据id获取供应商(渠道商)业务员名称
* Function Name: getSupplierSalesName
* @param $supplier_id
*
* @return string
*
* @author 温依莅
*/
public function getSupplierSalesName($supplier_id)
{
$res = BaseSupplier::find()->select('id,supplier_name,sales_man')->where(['id' => $supplier_id])->limit(1)->asArray()->one();
return $res['sales_man'];
}
/**
* Function Description:判断生成代收票种数据
* Function Name: getFinalProductList
* @param array $prod_list_arr 票种数据数组
* @param int $outside_sale_org_id 渠道id
* @param int $outside_order_no 渠道名称
* @param int $user_id 下单用户id
*
* @return array
*
* @author 温依莅
*/
public function getFinalProductList($prod_list_arr, $outside_sale_org_id, $outside_order_no, $user_id)
{
$result = array();
$prod_list_final = $prod_list_arr;
//1,初步将代收信息整合进票种数组
foreach ($prod_list_arr as $k => $v) {
$res = $this->getTicketAgentInfo($v['ticket_id'], $outside_sale_org_id, $user_id);
if ($res['code'] != '0') {
$result['code'] = 1;
$result['info'] = $res['info'];
return $result;
}
$list = $res['list'];
#prod_list_final里ticket对应的值存 销售层级1的数据
$prod_list_final[$k]['is_agent'] = $list['is_agent'];//是否是代理票种
$prod_list_final[$k]['agent_depth'] = $list['agent_depth'];//销售层数
$prod_list_final[$k]['prod_top_org_id'] = $list['ticket_agent_list'][1]['prod_top_org_id'];//供应商id
$prod_list_final[$k]['prod_supply_org_name'] = $list['ticket_agent_list'][1]['prod_supply_org_name'];//供应商姓名
$prod_list_final[$k]['outside_sale_org_id'] = $outside_sale_org_id;//渠道商id
$prod_list_final[$k]['outside_order_no'] = $outside_order_no;//渠道订单号
$prod_list_final[$k]['sub_channel_id'] = $outside_sale_org_id;//下级渠道id
$prod_list_final[$k]['sub_channel_no'] = $outside_order_no;//下级渠道订单号
$prod_list_final[$k]['main_corp_id'] = $list['ticket_agent_list'][1]['main_corp_id'];//订单所属运营主体
$prod_list_final[$k]['source_main_corp_id'] = $list['ticket_agent_list'][1]['source_main_corp_id'];//线路所属运营主体
$prod_list_final[$k]['sale_path'] = $list['ticket_agent_list'][1]['sale_path'];//销售路径(代售才有,自营为空)
$prod_list_final[$k]['agent_level'] = $list['ticket_agent_list'][1]['agent_level'];//售卖层级:终端1,上一级2,再上一级3
$prod_list_final[$k]['base_price'] = $list['ticket_agent_list'][1]['base_price'];//子订单成本
$prod_list_final[$k]['total_base_price'] = $prod_list_final[$k]['base_price'] * $prod_list_final[$k]['people_num'];//主订单成本
#根据销售层级得到代收数组
$prod_list_final[$k]['agent_list'] = $list['ticket_agent_list'];
}
//2,生成订单号:其中主订单id直接嵌入数组相关字段,子订单给出起始id,完成base_unique_id2表的更新
#2.1,计算BaseUniqueId 需要增加的值(往返程的主订单和子订单的id个数),,更新唯一键值
$add_count = 0;
$main_id_count = 0;
foreach ($prod_list_final as $k => $v) {
$add_count += ($v['people_num'] + 1) * $v['agent_depth'];
$main_id_count += $v['agent_depth'];
}
$temp = BaseUniqueId2::find()->select('unique_id')->where(['id' => 1])->asArray()->one();
$base_main_id = $temp['unique_id'] + 0;
$son_id_begin = $base_main_id + $main_id_count + 1;//子订单起始id
$time=time().rand(0,999999);
$res = BaseUniqueId2::updateAll(['UNIQUE_ID' => $base_main_id + $add_count + 1,'UPDATE_TIME'=>$time], ['id' => 1]);
if (!$res) {
$result['code'] = '1';
$result['info'] = 'BaseUniqueId更新失败';
return $result;
}
#2.2,生成订单号,按需求重组进prod_list_final数组里
$main_order_id_arr = array();//主订单号数组
foreach ($prod_list_final as $k => $v) {
$agent_depth = $v['agent_depth'];
$id_arr = $this->getPointNum($base_main_id, $agent_depth);//array(1001,1002,1003)
$base_main_id += $agent_depth;//增加初始值
$id_str = implode(',', $id_arr);//1001,1002,1003
$main_order_id_arr[$k] = $id_arr;
$prod_list_final[$k]['order_id'] = $id_arr[1];
$prod_list_final[$k]['sale_path'] = $v['is_agent'] ? $id_str : '';//如果是代售票种,写入销售路径,否则为空字符串
foreach ($v['agent_list'] as $ak => $av) {
$prod_list_final[$k]['agent_list'][$ak]['order_id'] = $id_arr[$ak];//主订单号
$prod_list_final[$k]['agent_list'][$ak]['sale_path'] = $prod_list_final[$k]['sale_path'];//销售路径
if ($ak == 1) { //agent_level_1
$prod_list_final[$k]['agent_list'][$ak]['outside_order_no'] = $prod_list_final[$k]['outside_order_no'];//agent_level_1 渠道订单号
$prod_list_final[$k]['agent_list'][$ak]['sub_channel_no'] = $prod_list_final[$k]['sub_channel_no'];//agent_level_1 下级订单号
$prod_list_final[$k]['agent_list'][$ak]['order_price'] = $prod_list_final[$k]['order_price'];//agent_level_1 子订单售价
$prod_list_final[$k]['agent_list'][$ak]['total_order_price'] = $prod_list_final[$k]['total_order_price'];//agent_level_1 主订单售价
$prod_list_final[$k]['agent_list'][$ak]['total_base_price'] = $prod_list_final[$k]['total_base_price'];//agent_level_1 主订单成本
} else if ($ak == 2) { //agent_level_2
$prod_list_final[$k]['agent_list'][$ak]['outside_order_no'] = 'SPIDER-' . $id_arr[1];//agent_level_2 渠道订单号
$prod_list_final[$k]['agent_list'][$ak]['sub_channel_no'] = $prod_list_final[$k]['outside_order_no'];//agent_level_2 下级订单号
$prod_list_final[$k]['agent_list'][$ak]['total_order_price'] = $av['order_price'] * $prod_list_final[$k]['people_num'];//agent_level_2 主订单售价
$prod_list_final[$k]['agent_list'][$ak]['total_base_price'] = $av['base_price'] * $prod_list_final[$k]['people_num'];//agent_level_2 主订单成本
} else if ($ak == 3) { //agent_level_3
$prod_list_final[$k]['agent_list'][$ak]['outside_order_no'] = 'SPIDER-' . $id_arr[2];//agent_level_3 渠道订单号
$prod_list_final[$k]['agent_list'][$ak]['sub_channel_no'] = 'SPIDER-' . $id_arr[1];//agent_level_3 下级订单号
$prod_list_final[$k]['agent_list'][$ak]['total_order_price'] = $av['order_price'] * $prod_list_final[$k]['people_num'];//agent_level_3 主订单售价
$prod_list_final[$k]['agent_list'][$ak]['total_base_price'] = $av['base_price'] * $prod_list_final[$k]['people_num'];//agent_level_3 主订单成本
}
}
}
$result['code'] = 0;
$result['info'] = '代售信息订单号整合成功';
$result['prod_list'] = $prod_list_final;
$result['main_order_id_arr'] = $main_order_id_arr;
$result['son_id_begin'] = $son_id_begin;
return $result;
}
/**
* Function Description:单巴士下单接口
* Function Name: submitBusOrder
* @param array $prod_list_arr 产品列表
* @param array $seat_arr 座位
* @param array $order_info_arr 订单的其他信息
* @param array $customer_info_arr 乘客
* @param array $order_append_arr 附加产品
* @param int $is_send_msg 是否发短信
*
* @return mixed
*
* @author 张帅
*/
public function submitBusOrder($prod_list_arr, $seat_arr, $order_info_arr, $customer_info_arr, $order_append_arr, $is_send_msg, $buy_insurance = 0)
{
//一,判断生成 代售票种数据:同时生成好订单的id(其中主订单id直接嵌入数组关联字段,子订单给出起始id);计算更新base_unique_id2值
$res = $this->getFinalProductList($prod_list_arr, $order_info_arr['outside_sale_org_id'], $order_info_arr['outside_order_no'], $order_info_arr['user_id']);
if ($res['code'] != '0') {
$result['code'] = '1';
$result['info'] = $res['info'];
return $result;
}
$prod_list = $res['prod_list'];
$main_order_id_arr = $res['main_order_id_arr'];//生成好的主订单号(包含代售主订单号)
$son_id_begin = $res['son_id_begin'];//子订单起始订单号
//二,生成需要操作数据库的信息
#region 提交订单操作数据库所需数据
$main_order = [];//主订单数据
$main_order_key = [];//提交主订单时所需key值
$son_order = [];//子订单数据
$son_order_key = [];//提交子订单时所需key值
$run_x_arr = [];//run_x
$run_bus_prod_stock_arr = [];//run_bus,run_prod,run_stock更新数据
$main_order_id = [];//订单id数组
$pay_main_arr = [];//主支付记录数据
$pay_main_key = [];//主支付记录键值
$order_price = 0;
$son_order_id = $son_id_begin;//子订单起始订单号
#endregion
//如有半角双引号,替换成半角空格
$customer_info_arr['customer_memo'] = str_replace('"', " ", $customer_info_arr['customer_memo']);
#region 整理数据
foreach ($prod_list as $key => $vel) {
$run_x_one = [];//run_x_one
#region 主订单数据
$main_order_val = [
'ID' => $vel['order_id'],
'MAIN_CREATE_USER_ID' => $order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,//'下单组织机构所属一级机构ID',
'PROD_TOP_ORG_ID' => $vel['prod_top_org_id'],//供应商id,
'ORDER_ID' => $vel['order_id'],
'ORDER_DESCRIPTION' => $vel['ticket_name'] . ',' . $vel['run_date'] . ',' . $vel['people_num'] . '|' . $vel['people_num'],//'最细颗粒度的订单描述',
'OUTSIDE_ORDER_NO' => $order_info_arr['outside_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $vel['order_id'],//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => 0,
'PARENT_PROD_ID' => $vel['line_id'],
'PRINCIPAL_ID'=>$vel['principal_id'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $vel['total_order_price'],//'订单价格',
'ORDER_PROD_TYPE' => 81,//'订单类型',
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],//'上车站检票口资源ID',
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],//'上车站检票口资源名称',
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],//'上车站发车时间距发车日零点分钟数',
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],//'下车站发车时间距发车日零点分钟数',
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],//'订单当前状态',
'IF_LAST_PROD' => 1,//'是否最细颗粒度的判断',
'if_gift' => $buy_insurance,//'是否是赠票,0不是赠票,1是赠票',
'ORDER_LEVEL' => 0,//'Order Level',
'OUTSIDE_SALE_ORG_ID' => $order_info_arr['outside_sale_org_id'],//渠道id
'SALES_MAN' => $order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['total_receiver_value'],
'PROD_SUPPLY_ORG_NAME' => $vel['prod_supply_org_name'],//供应商名字
'SUB_CHANNEL_ID' => $vel['sub_channel_id'],//下级渠道id
'SUB_CHANNEL_NO' => $vel['sub_channel_no'],//下级渠道订单号
'MAIN_CORP_ID' => $vel['main_corp_id'],//订单所属运营主体
'SOURCE_MAIN_CORP_ID' => $vel['source_main_corp_id'],//线路所属运营主体
'SALE_PATH' => $vel['sale_path'],//销售路径
'AGENT_LEVEL' => $vel['agent_level'],//售卖层级:终端1,上一级2,再上一级3
'BASE_PRICE' => $vel['total_base_price'] //成本价
];
#endregion
$single_price = $vel['total_order_price'];
$order_price += $vel['total_order_price'];
#region 收集主订单的id
if ($vel['if_back'] == 1) {
$main_order_id[0] = $vel['order_id'];//收集主订单的id
} else {
$main_order_id[1] = $vel['order_id'];//收集主订单的id
}
#endregion
#region 主订单key值
if (count($main_order_key) == 0) {
$main_order_key = array_keys($main_order_val);
}
#endregion
#region 主订单数组
//1,agent_level_1的主订单
$main_order[] = $main_order_val;
//2,☆☆☆ 如果是代售,插入代售的相关主订单 ☆☆☆
if ($vel['is_agent'] == 1) {
$agent_list = $vel['agent_list'];
unset($agent_list[1]);//去掉销售层级1的主订单,前面已插入
foreach ($agent_list as $ak => $av) {
$main_order_agent = [
'ID' => $av['order_id'],
'MAIN_CREATE_USER_ID' => 2,//$order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => 2,//$order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,//'下单组织机构所属一级机构ID',
'PROD_TOP_ORG_ID' => $av['prod_top_org_id'],//供应商id,
'ORDER_ID' => $av['order_id'],
'ORDER_DESCRIPTION' => $vel['ticket_name'] . ',' . $vel['run_date'] . ',' . $vel['people_num'] . '|' . $vel['people_num'],//'最细颗粒度的订单描述',
'OUTSIDE_ORDER_NO' => $av['outside_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $vel['order_id'],//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => 0,
'PARENT_PROD_ID' => $vel['line_id'],
'PRINCIPAL_ID'=>$vel['principal_id'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $av['total_order_price'],//'订单价格',
'ORDER_PROD_TYPE' => 81,//'订单类型',
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],//'上车站检票口资源ID',
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],//'上车站检票口资源名称',
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],//'上车站发车时间距发车日零点分钟数',
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],//'下车站发车时间距发车日零点分钟数',
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],//'订单当前状态',
'IF_LAST_PROD' => 1,//'是否最细颗粒度的判断',
'if_gift' => $buy_insurance,//'是否是赠票,0不是赠票,1是赠票',
'ORDER_LEVEL' => 0,//'Order Level',
'OUTSIDE_SALE_ORG_ID' => $av['outside_sale_org_id'],//渠道id
'SALES_MAN' => $av['sales_man'],//$order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['total_receiver_value'],
'PROD_SUPPLY_ORG_NAME' => $av['prod_supply_org_name'],//供应商名字
'SUB_CHANNEL_ID' => $av['sub_channel_id'],//下级渠道id
'SUB_CHANNEL_NO' => $av['sub_channel_no'],//下级渠道订单号
'MAIN_CORP_ID' => $av['main_corp_id'],//订单所属运营主体
'SOURCE_MAIN_CORP_ID' => $av['source_main_corp_id'],//线路所属运营主体
'SALE_PATH' => $av['sale_path'],//销售路径
'AGENT_LEVEL' => $av['agent_level'],//售卖层级:终端1,上一级2,再上一级3
'BASE_PRICE' => $av['total_base_price'] //成本价
];
$main_order[] = $main_order_agent;
}
}
#endregion
#region 根据人数遍历每个子订单的个数
foreach ($seat_arr[$vel['ticket_id']] as $seat_key => $seat_vel) {
$son_level_one_id = $son_order_id;
#region 子订单数据
$this->run_id = $vel['run_id'];
$this->bus_order_id = $seat_vel['bus_order_id'];
$son_order_val = [
'ID' => $son_order_id,
'MAIN_CREATE_USER_ID' => $order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,
'PROD_TOP_ORG_ID' => $vel['prod_top_org_id'],//供应商id,
'ORDER_ID' => $son_order_id,
'OUTSIDE_ORDER_NO' => $order_info_arr['outside_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $vel['order_id'],//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => $vel['order_id'],
'PROD_ID' => $vel['ticket_id'],
'PARENT_PROD_ID' => $vel['line_id'],
'PRINCIPAL_ID'=>$vel['principal_id'],
'PROD_NAME' => $vel['ticket_name'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $vel['order_price'],
'ORDER_PROD_TYPE' => 82,
'RUN_ID' => $vel['run_id'],
'RUN_DATE' => $vel['run_date'],
'RUN_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),//'班次首发日期参数,发车日期至2016-01-01天数,算法使用',
'RUN_TIME' => $vel['run_time'],
'RUN_BUS_ORDER_ID' => $seat_vel['bus_order_id'],//'班次车号,RUN_BUS.BUS_ORDER_ID',
'RUN_BUS_SEAT_TYPE' => $vel['seat_type'],//'座位类型,DICT_TYPE.ID,普通座、贵宾座等',
'RUN_BUS_SEAT_SEQ_ID' => $seat_vel['seat_seq_id'],//'座位内部序列ID',
'RUN_BUS_SEAT_NAME' => $seat_vel['seat_name'],
'PROD_START_STATION_RES_ID' => $vel['start_res_id'],
'PROD_START_STATION_RES_NAME' => $vel['start_res_name'],
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],
'PROD_START_STATION_SEQ_ID' => $vel['start_seq_id'],
'PROD_START_STATION_DATE' => $vel['run_date'],
'PROD_START_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_START_STATION_TIME' => $vel['start_time'],
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],
'PROD_START_STATION_AREA_ID' => $vel['start_area_id'],
'PROD_START_STATION_AREA_NAME' => $vel['start_area_name'],
'PROD_END_STATION_RES_ID' => $vel['end_res_id'],
'PROD_END_STATION_RES_NAME' => $vel['end_res_name'],
'PROD_END_STATION_SEQ_ID' => $vel['end_seq_id'],
'PROD_END_STATION_DATE' => $vel['run_date'],
'PROD_END_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_END_STATION_TIME' => $vel['end_time'],
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],
'PROD_END_STATION_AREA_ID' => $vel['end_area_id'],
'PROD_END_STATION_AREA_NAME' => $vel['end_area_name'],
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],
'if_gift' => $buy_insurance,//'是否是赠票,0不是赠票,1是赠票',
'ORDER_LEVEL' => 0,
'OUTSIDE_SALE_ORG_ID' => $order_info_arr['outside_sale_org_id'],
'SALES_MAN' => $order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['receiver_value'],
'PROD_SUPPLY_ORG_NAME' => $vel['prod_supply_org_name'],//供应商名字
'SUB_CHANNEL_ID' => $vel['sub_channel_id'],//下级渠道id
'SUB_CHANNEL_NO' => $vel['sub_channel_no'],//下级渠道订单号
'MAIN_CORP_ID' => $vel['main_corp_id'],//订单所属运营主体
'SOURCE_MAIN_CORP_ID' => $vel['source_main_corp_id'],//线路所属运营主体
'SALE_PATH' => $vel['sale_path'],//销售路径
'AGENT_LEVEL' => $vel['agent_level'],//售卖层级:终端1,上一级2,再上一级3
'BASE_PRICE' => $vel['base_price'] //成本价
];
#endregion
$son_order_id++;//子订单号递增
#region 子订单key值
if (count($son_order_key) == 0) {
$son_order_key = array_keys($son_order_val);
}
#endregion
#region 子订单数组
//1,agent_level_1的子订单
$son_order[] = $son_order_val;
//2,☆☆☆ 如果是代售,插入代售的相关主订单 ☆☆☆
if ($vel['is_agent'] == 1) {
$agent_list_copy = $vel['agent_list'];
unset($agent_list_copy[1]);//去掉销售层级1的主订单,前面已插入
foreach ($agent_list_copy as $akp => $avp) {
$son_order_agent = [
'ID' => $son_order_id,
'MAIN_CREATE_USER_ID' => 2,//$order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => 2,//$order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,
'PROD_TOP_ORG_ID' => $avp['prod_top_org_id'],//供应商id,
'ORDER_ID' => $son_order_id,
'OUTSIDE_ORDER_NO' => $avp['outside_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $vel['order_id'],//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => $avp['order_id'],
'PROD_ID' => $vel['ticket_id'],
'PARENT_PROD_ID' => $vel['line_id'],
'PRINCIPAL_ID'=>$vel['principal_id'],
'PROD_NAME' => $vel['ticket_name'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $avp['order_price'],
'ORDER_PROD_TYPE' => 82,
'RUN_ID' => $vel['run_id'],
'RUN_DATE' => $vel['run_date'],
'RUN_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),//'班次首发日期参数,发车日期至2016-01-01天数,算法使用',
'RUN_TIME' => $vel['run_time'],
'RUN_BUS_ORDER_ID' => $seat_vel['bus_order_id'],//'班次车号,RUN_BUS.BUS_ORDER_ID',
'RUN_BUS_SEAT_TYPE' => $vel['seat_type'],//'座位类型,DICT_TYPE.ID,普通座、贵宾座等',
'RUN_BUS_SEAT_SEQ_ID' => $seat_vel['seat_seq_id'],//'座位内部序列ID',
'RUN_BUS_SEAT_NAME' => $seat_vel['seat_name'],
'PROD_START_STATION_RES_ID' => $vel['start_res_id'],
'PROD_START_STATION_RES_NAME' => $vel['start_res_name'],
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],
'PROD_START_STATION_SEQ_ID' => $vel['start_seq_id'],
'PROD_START_STATION_DATE' => $vel['run_date'],
'PROD_START_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_START_STATION_TIME' => $vel['start_time'],
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],
'PROD_START_STATION_AREA_ID' => $vel['start_area_id'],
'PROD_START_STATION_AREA_NAME' => $vel['start_area_name'],
'PROD_END_STATION_RES_ID' => $vel['end_res_id'],
'PROD_END_STATION_RES_NAME' => $vel['end_res_name'],
'PROD_END_STATION_SEQ_ID' => $vel['end_seq_id'],
'PROD_END_STATION_DATE' => $vel['run_date'],
'PROD_END_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_END_STATION_TIME' => $vel['end_time'],
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],
'PROD_END_STATION_AREA_ID' => $vel['end_area_id'],
'PROD_END_STATION_AREA_NAME' => $vel['end_area_name'],
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],
'if_gift' => $buy_insurance,//'是否是赠票,0不是赠票,1是赠票',
'ORDER_LEVEL' => 0,
'OUTSIDE_SALE_ORG_ID' => $avp['outside_sale_org_id'],
'SALES_MAN' => $avp['sales_man'],//$order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['receiver_value'],
'PROD_SUPPLY_ORG_NAME' => $avp['prod_supply_org_name'],//供应商名字
'SUB_CHANNEL_ID' => $avp['sub_channel_id'],//下级渠道id
'SUB_CHANNEL_NO' => $avp['sub_channel_no'],//下级渠道订单号
'MAIN_CORP_ID' => $avp['main_corp_id'],//订单所属运营主体
'SOURCE_MAIN_CORP_ID' => $avp['source_main_corp_id'],//线路所属运营主体
'SALE_PATH' => $avp['sale_path'],//销售路径
'AGENT_LEVEL' => $avp['agent_level'],//售卖层级:终端1,上一级2,再上一级3
'BASE_PRICE' => $avp['base_price'] //成本价
];
$son_order[] = $son_order_agent;
$son_order_id++;//子订单号递增
}
}
#endregion
#region run_bus,run_prod,run_stock更新数据
if (!isset($run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']])) {
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['num'] = $vel['people_num'];//数量
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['run_id'] = $vel['run_id'];//班次id
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['bus_order_id'] = $seat_vel['bus_order_id'];//车次号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['ticket_id'] = $vel['ticket_id'];//票种id
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['start_seq_id'] = $vel['start_seq_id'];//开始站顺序号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['end_seq_id'] = $vel['end_seq_id'];//结束站顺序号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['seat_type'] = $vel['seat_type'];//座位类型
}
#endregion
#region 更新run_x配置数据
$run_x_one[$seat_key]['run_x_id'] = $seat_vel['run_x_id'];
$run_x_one[$seat_key]['run_date'] = substr($vel['run_date'], 0, 7);
$run_x_one[$seat_key]['run_date'] = str_replace('-', '', $run_x_one[$seat_key]['run_date']);//run_x表的日期后缀
$run_x_one[$seat_key]['order_main_id'] = $son_level_one_id;//子订单号
$run_x_one[$seat_key]['order_main_prod_id'] = $vel['line_id'];//线路id
$run_x_one[$seat_key]['order_prod_id'] = $vel['ticket_id'];//票种id
#endregion
}
#endregion
#region 更新run_x配置数据
$run_x_arr[$key] = $run_x_one;
#endregion
}
#endregion
#region 主支付记录数据数组
foreach ($main_order_id as $key => $vel) {
#region 主支付记录
$pay_main_one = [
'ID' => $main_order_id[0],
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'PAY_TOTAL' => $single_price,
'ORDER_ID' => $vel,
];
#endregion
$pay_main_arr[] = $pay_main_one;//主支付记录数组
#region 主支付记录key值
if (count($pay_main_key) == 0) {
$pay_main_key = array_keys($pay_main_one);
}
#endregion
}
#endregion
//三,正式执行,插入更新数据库
$transaction = Yii::$app->db->beginTransaction();
try {
#region 1.占座 更新run_x表
foreach ($run_x_arr as $run_x_key => $run_x_vel) {
foreach ($run_x_vel as $key => $vel) {
$update_sql = 'update run_' . $vel['run_date'] . '
set order_main_id = ' . $vel['order_main_id'] . ',
order_main_prod_id = ' . $vel['order_main_prod_id'] . ',
order_prod_id = ' . $vel['order_prod_id'] . ',
seat_status = 2,
order_lock_user_id = ' . $order_info_arr['user_id'] . ',
order_lock_time = \'' . date('Y-m-d H:i:s') . '\'
where id in (' . $vel['run_x_id'] . ')';
$res = Yii::$app->db->createCommand($update_sql)->execute();
if (!$res) {
throw new Exception('占位失败');
}
}
}
#endregion
#region 2.插入主订单
$res = Yii::$app->db->createCommand()->batchInsert(OrderMain::tableName(), $main_order_key, $main_order)->execute();
if (!$res) {
throw new Exception('主订单提交失败');
}
#endregion
#region 3.插入子订单
$res = Yii::$app->db->createCommand()->batchInsert(OrderMain::tableName(), $son_order_key, $son_order)->execute();
if (!$res) {
throw new Exception('子订单提交失败');
}
#endregion
#region 4.更新run_prod,run_bus,run_stock
foreach ($run_bus_prod_stock_arr as $key => $vel) {
#region (1).更新run_prod
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['=', 'prod_id', $vel['ticket_id']],
['=', 'cancel_flag', 0],
];
$res = RunProd::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_prod失败');
}
#endregion
#region (2).更新run_bus
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['=', 'bus_order_id', $vel['bus_order_id']],
['=', 'cancel_flag', 0],
];
$res = RunBus::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_bus失败');
}
#endregion
#region (3).更新run_stock
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['>=', 'seq_id', $vel['start_seq_id']],
['<', 'seq_id', $vel['end_seq_id']],
['=', 'seat_type', $vel['seat_type']],
['=', 'cancel_flag', 0],
];
$res = RunStock::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_stock失败');
}
#endregion
}
#endregion
//==================这里加判断,只有是已支付状态,才插入支付记录=============
// if ($order_info_arr['order_pay_status'] == 1) {
#region 5.插入主支付记录
$res = Yii::$app->db->createCommand()->batchInsert(OrderPayMain::tableName(), $pay_main_key, $pay_main_arr)->execute();
if (!$res) {
throw new Exception('插入主支付记录失败');
}
#endregion
#region 6.插入支付明细
$order_pay_detail = new OrderPayDetail();
$order_pay_detail->attributes = [
'ID' => $main_order_id[0],
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'PAY_MAIN_ID' => $main_order_id[0],
'PAY_TYPE_ID_1' => $order_info_arr['pay_type'],
'PAY_MONEY' => $order_price,
];
$res = $order_pay_detail->insert();
if (!$res) {
throw new Exception('插入支付明细失败');
}
#endregion
// }
#region 8.如果是往返 建立往返关系
if (count($main_order_id) == 2) {
$to_from = new ToFrom();
$to_from->attributes = [
'to_orderid' => $main_order_id[0],
'back_orderid' => $main_order_id[1],
'CREATE_TIME' => date('Y-m-d H:i:s'),
];
$res = $to_from->insert();
if (!$res) {
throw new Exception('建立往返关系失败');
}
}
#endregion
#region 9.插入酒店门票价格
if ($order_append_arr['gate_price'] != 0 || $order_append_arr['hotel_price'] != 0) {
$order_extra_info = new OrderExtraInfo();
$order_extra_info->attributes = [
'order_id' => $main_order_id[0],
'hotel_price' => $order_append_arr['hotel_price'],
'gate_price' => $order_append_arr['gate_price'],
];
$res = $order_extra_info->insert();
if (!$res) {
throw new Exception('酒店门票价格提交失败' . serialize($order_extra_info->getErrors()));
}
}
#endregion
#region 10.插入日志
$opera_time = date('Y-m-d H:i:s');
foreach ($prod_list as $key => $vel) {
foreach ($vel['agent_list'] as $ak => $av) {
$order_opera_log = new OrderOperaLog();
$order_opera_log->attributes = [
'order_id' => $av['order_id'],
'opera_user_id' => $av['agent_level'] == 1 ? $order_info_arr['user_id'] : 2,
'opera_type' => 1,
'opera_time' => $opera_time,
'opera_detail' => '创建订单,出发日期' . $vel['run_date'] . ',订单渠道' . $order_info_arr['org_name'] . ',单价' . $av['order_price'] . ',总额' . $av['total_order_price'] . ',联系人信息:' . $customer_info_arr['customer_name'] . ',' . $customer_info_arr['customer_mobile'] . ',' . $customer_info_arr['customer_id_no'] . ',备注信息:' . $customer_info_arr['customer_memo'] . ',乘客地址:' . $customer_info_arr['customer_address'],
'member_id' => $order_info_arr['member_id'],
'opera_platform' => '统一下单',
];
$res = $order_opera_log->insert();
if (!$res) {
throw new Exception('插入日志失败');
}
}
}
#endregion
$transaction->commit();
//$transaction->rollBack();
#region 13.修改bus_cost库存和单价
if ($this->run_id != 0 && $this->bus_order_id != 0) {
$res = self::updateBusCost($this->run_id, $this->bus_order_id);
if (!file_exists(__DIR__ . '/../log/bus_cost')) {
mkdir(__DIR__ . '/../log/bus_cost');
}
file_put_contents(__DIR__ . '/../log/bus_cost/' . date("Y-m-d") . '.log', date("Y-m-d H:i:s") . json_encode($res) . PHP_EOL, FILE_APPEND);
}
#endregion
#region 11.发送短信
/*$order_run_time = array();
foreach($prod_list as $sk=>$sv){
$order_run_time[$sv['order_id']] = $sv['run_date'];
}*/
if ($order_info_arr['order_pay_status'] == 1) {
foreach ($main_order_id as $key => $vel) {
//这里对发送短信时间做一个判断,满足条件的才发送短信
$time_diff = $this->get_time_diff($vel);
if ($time_diff >= 0) {
//如果已支付,添加出票成功短信 到数据库
if ($order_info_arr['order_pay_status'] == 1) {
// $tomorrow_day = date("Y-m-d", strtotime("+1 days"));
// $current_time = date("H:i");
// $order_run_day = $order_run_time[$vel];
// if ($order_run_day > $tomorrow_day || ($order_run_day == $order_run_day && $current_time < "18:30")) {
$url = 'http://' . CS1_DOMAIN . '/api/msg/order-send-success-msg';
$sendInfo['order_id'] = $vel;
Msg::httpRequestForasyn($url, $sendInfo);
// }
}
if ($is_send_msg == 1) {
$url = 'http://' . CS1_DOMAIN . '/api/msg/order-send-msg';
$sendInfo['order_id'] = $vel;
$sendInfo['msg_type'] = 'cs';
Msg::httpRequestForasyn($url, $sendInfo);
}
}
}
}
#endregion
#region 12.添加订单状态
foreach ($main_order_id_arr as $key => $vel) {
foreach ($vel as $ik => $iv) {
$url = 'http://' . CS_DOMAIN . '/st-xm/Api/add_order_status_log.php';
$post_data['order_id'] = $iv;
if ($order_info_arr['order_pay_status'] == 1) {
$post_data['order_status'] = '451,452,453';
} else {
$post_data['order_status'] = '451';
}
Msg::httpRequestForasyn($url, $post_data);
}
}
#endregion
$result['code'] = '0';
$result['info'] = '下单成功';
$result['data'] = implode(',', $main_order_id);
} catch (Exception $e) {
# 回滚事务
$transaction->rollBack();
$result['code'] = '1';
$result['info'] = '下单失败';
$result['error'] = $e->getMessage();
}
return $result;
}
/**
* Function Description:获取短信发送时间差(巴士班次时间-当前时间)
* Function Name: get_time_diff
* @param int $order_id 巴士主订单
*
* @return mixed
*
* @author 温依莅
*/
public function get_time_diff($order_id)
{
$now = time();
//$sql="select b.run_date,b.RUN_MINUTES,(UNIX_TIMESTAMP(b.run_date)+b.run_minutes*60) as stamp from order_main a left join run_main b on a.run_id=b.run_id where a.PARENT_ORDER_ID=$order_id limit 1";
//$sql = "select b.run_date,b.RUN_MINUTES,(UNIX_TIMESTAMP(b.run_date)) as stamp from order_main a left join run_main b on a.run_id=b.run_id where a.PARENT_ORDER_ID=$order_id limit 1";
$res = OrderMain::find()->select([
'b.run_date',
'b.run_minutes',
'stamp' => new Expression('UNIX_TIMESTAMP(b.run_date)'),
])->from('order_main a')
->leftJoin('run_main b', 'a.run_id=b.run_id')
->where(['a.parent_order_id' => $order_id])
->asArray()
->one();
$run_stamp = $res['stamp'];
return ($run_stamp + 3600 * 24) - $now;
}
/**
* Function Description:整理产品数据
* Function Name: getBusProductList
* @param string $prod_str 选购产品,格式为{去程/返程标志(1:去程 2:返程),去程/返程班次ID,票种ID,票种单价,预定数量,车号}...
*
* @return array
*
* @author 张帅
*/
public function getBusProductList($prod_str)
{
$result = [];
$prod_str = substr($prod_str, 1, -1);
$prod_arr = explode('}{', $prod_str);
foreach ($prod_arr as $key => $vel) {
$vel = explode(',', $vel);
$result[$vel[2]]['if_back'] = $vel[0];//1:去程 2:返程
$result[$vel[2]]['run_id'] = $vel[1];//班次id
$result[$vel[2]]['ticket_id'] = $vel[2];//票种id
//新增判断 如果票种不在上下车可收范围内 则直接不可下单
$arr = $this->getInOutTickets($vel[1]);
if (!in_array($vel[2], $arr)) {
return [];
}
$result[$vel[2]]['price'] = $vel[3];//单价
$result[$vel[2]]['num'] = $vel[4];//数量
$result[$vel[2]]['bus_order_id'] = isset($vel[5]) ? $vel[5] : 0;//车号
}
return $result;
}
/**
* Function Description:获取准确的票种信息
* Function Name: getInOutTickets
* @param $run_id
*
* @return array
*
* @author LUOCJ
*/
public function getInOutTickets($run_id)
{
$sql_line = "select prod_id as line_id from run_station where run_id ={$run_id}";
$line_id = Yii::$app->db->createCommand($sql_line)->queryAll();
$sql = "select ticket_id from opera_tickets ot left join run_station rsa on ot.START_STATION_RES_ID = rsa.STATION_RES_ID and rsa.run_id = {$run_id} left join run_station rsb on ot.END_STATION_RES_ID = rsb.STATION_RES_ID and rsb.run_id = {$run_id} where ot.line_id = {$line_id[0]['line_id']} and rsa.STATION_INOUT_TYPE<>114 and rsb.STATION_INOUT_TYPE <>114";
$data = Yii::$app->db->createCommand($sql)->queryAll();
$res = [];
foreach ($data as $k => $v) {
$res[] = $v['ticket_id'];
}
return $res;
}
/**
* Function Description:获取订单数据
* Function Name: getOrderInfoArray
* @param string $order_info_str 订单基础信息{下单用户,会员号,渠道id,渠道订单号,是否自动退票(0否1是),支付方式(授信275),是否支付(0否1是),业务员}
*
* @return mixed
*
* @author 张帅
*/
public function getOrderInfoArray($order_info_str)
{
$order_info_str = substr($order_info_str, 1, -1);
$order_info_arr = explode(',', $order_info_str);
$result['user_id'] = (!isset($order_info_arr[0]) || empty($order_info_arr[0])) ? 1 : $order_info_arr[0];//下单用户id
$result['member_id'] = (!isset($order_info_arr[1]) || empty($order_info_arr[1])) ? 0 : $order_info_arr[1];//会员id
$result['outside_sale_org_id'] = (!isset($order_info_arr[2]) || empty($order_info_arr[2])) ? 0 : $order_info_arr[2];//外部下单id
$result['outside_order_no'] = (!isset($order_info_arr[3]) || empty($order_info_arr[3])) ? '' : $order_info_arr[3];//外部下单号
$result['order_book_status'] = (!isset($order_info_arr[4]) || $order_info_arr[4] === '') ? 1 : $order_info_arr[4];//订单预订状态
$result['pay_type'] = (!isset($order_info_arr[5]) || empty($order_info_arr[5])) ? 275 : $order_info_arr[5];//支付方式(授信,微信)
$result['order_pay_status'] = (!isset($order_info_arr[6]) || empty($order_info_arr[6])) ? 0 : $order_info_arr[6];//是否支付
$result['sales_man'] = (!isset($order_info_arr[7]) || empty($order_info_arr[7])) ? '' : $order_info_arr[7];//业务员
$supplier_info = BaseSupplier::find()->select('sales_man,supplier_name')->where(['id' => $result['outside_sale_org_id']])->asArray()->one();
if ($result['sales_man'] == '') {
$result['sales_man'] = empty($supplier_info['sales_man']) ? '' : $supplier_info['sales_man'];
}
$result['org_name'] = empty($supplier_info['supplier_name']) ? '' : $supplier_info['supplier_name'];
$result['order_status'] = $result['order_pay_status'] == 0 ? 145 : 146;
return $result;
}
/**
* Function Description:获取乘客信息
* Function Name: getCustomerInfoArray
* @param string $customer_info_str 顾客信息,格式为{联系人,联系电话,z证件类型,联系人身份证,备注}
*
* @return mixed
*
* @author 张帅
*/
public function getCustomerInfoArray($customer_info_str)
{
$customer_info_str = substr($customer_info_str, 1, -1);
$customer_info_arr = explode(',', $customer_info_str);
$result['customer_name'] = (!isset($customer_info_arr[0]) || empty($customer_info_arr[0])) ? '' : $customer_info_arr[0];//乘客姓名
$result['customer_mobile'] = (!isset($customer_info_arr[1]) || empty($customer_info_arr[1])) ? '' : $customer_info_arr[1];//乘客电话
$result['customer_id_type'] = (!isset($customer_info_arr[2]) || empty($customer_info_arr[2])) ? 150 : $customer_info_arr[2];//乘客证件类型
$result['customer_id_no'] = (!isset($customer_info_arr[3]) || empty($customer_info_arr[3])) ? '' : $customer_info_arr[3];//乘客证件号
$result['customer_memo'] = (!isset($customer_info_arr[4]) || empty($customer_info_arr[4])) ? '' : $customer_info_arr[4];//乘客备注
$result['customer_address'] = (!isset($customer_info_arr[5]) || empty($customer_info_arr[5])) ? '' : $customer_info_arr[5];//乘客地址
return $result;
}
/**
* Function Description:订单附加产品
* Function Name: getOrderAppendArray
* @param string $order_append_str 订单附加产品,格式为{门票价格,酒店价格}
*
* @return mixed
*
* @author 张帅
*/
public function getOrderAppendArray($order_append_str)
{
$order_append_str = substr($order_append_str, 1, -1);
$order_append_arr = explode(',', $order_append_str);
$result['gate_price'] = (!isset($order_append_arr[0]) || empty($order_append_arr[0])) ? '0' : $order_append_arr[0];//门票价格
$result['hotel_price'] = (!isset($order_append_arr[1]) || empty($order_append_arr[1])) ? '0' : $order_append_arr[1];//酒店价格
return $result;
}
/**
* Function Description:根据班次和票种id获取产品详情列表
* Function Name: getBusProductListByRunTicket
* @param array $run_ticket_arr 产品详情列表
*
* @return array|\yii\db\ActiveRecord[]
*
* @author 张帅
*/
public function getBusProductListByRunTicket($run_ticket_arr)
{
#region 1.班次票种筛选条件
$and_where = [
'or',
];
foreach ($run_ticket_arr as $key => $vel) {
$and_where[] = [
'and',
['=', 'rp.run_id', $vel['run_id']],
['=', 'rp.prod_id', $vel['ticket_id']],
];
}
#endregion
#region 2.查询结果
$result = RunProd::find()
->select([
'run_id' => 'rm.run_id',//班次id
'run_date' => 'rm.run_date',//出发日期
'run_time' => 'rm.run_time',//出发时间
'run_minutes' => 'rm.run_minutes',//出发分钟数
'ticket_id' => 'rp.prod_id',//票种id
'ticket_name' => 't.ticket_name',//票种名称
'seat_type' => 't.seat_type',//座位类型
'human_type' => 't.human_type',//人群属性
'line_id' => 't.line_id',//线路id
'line_name' => 'l.line_name',//线路名称
'start_res_id' => 't.start_station_res_id',//上车站id
'start_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.start_station_res_id')->limit(1),//上车站name
'start_area_id' => 't.start_station_area_id',//出发地id
'start_area_name' => BaseArea::find()->select('area_name')->where('id = t.start_station_area_id')->limit(1),//出发地name
'start_seq_id' => 's1.station_order_id',//开始站顺序号
'start_time' => 's1.start_time',//开始时间
'start_minutes' => 's1.start_minutes',//开始分钟数
'checkport_res_id' => 's1.checkport_res_id',//检票口id
'checkport_res_name' => BaseResource::find()->select('res_name')->where('res_id = s1.checkport_res_id')->limit(1),//检票口name
'end_res_id' => 't.end_station_res_id',//下车站id
'end_res_name' => BaseResource::find()->select('res_name')->where('res_id = t.end_station_res_id')->limit(1),//下车站name
'end_area_id' => 't.end_station_area_id',//目的地id
'end_area_name' => BaseArea::find()->select('area_name')->where('id = t.end_station_area_id')->limit(1),//目的地name
'end_seq_id' => 's2.station_order_id',//结束站顺序号
'end_time' => 's2.start_time',//结束时间
'end_minutes' => 's2.start_minutes',//结束分钟数
'prod_price' => 't.prod_price',//分销价
'cus_price' => 't.cus_price',//零售价
'principal_id' => 'l.run_duty_id',//运营负责人
])
->from(RunProd::tableName() . ' as rp')
->leftJoin(RunMain::tableName() . ' as rm', 'rp.run_id = rm.run_id')
->leftJoin(OperaTickets::tableName() . ' as t', 'rp.prod_id = t.ticket_id')
->leftJoin(OperaLine::tableName() . ' as l', 't.line_id = l.line_id')
->leftJoin(RunStation::tableName() . ' as s1', 'rm.run_id = s1.run_id and l.line_id = s1.prod_id and t.start_station_res_id = s1.station_res_id')
->leftJoin(RunStation::tableName() . ' as s2', 'rm.run_id = s2.run_id and l.line_id = s2.prod_id and t.end_station_res_id = s2.station_res_id')
->where([
'and',
['=', 'rp.cancel_flag', 0],
['=', 't.cancel_flag', 0],
['=', 't.is_onsale', 1],
])
->andWhere($and_where);
$result_sql = $result->createCommand()->getRawSql();
$final = $result->indexBy('ticket_id')->asArray()->all();
#endregion
return $final;
}
/**
* Function Description:检查run_x中是否有班次
* Function Name: checkRunX
* @param array $date_run_arr 日期班次数组
*
* @return bool
*
* @author 张帅
*/
public function checkRunX($date_run_arr)
{
#region 没有班次直接返回错误
if (count($date_run_arr) == 0) {
return false;
}
#endregion
#region 判断每个班次是否存在run_x
foreach ($date_run_arr as $key => $vel) {
$sql = '' . 'select
count(*) as total_count
from run_' . $key . '
where run_id in (' . implode(',', $vel) . ')
group by run_id';
$res = Yii::$app->db->createCommand($sql)->queryAll();
#region 如果有不存在班次的直接返回错误
if (count($res) != count($vel)) {
return false;
}
foreach ($res as $res_key => $res_vel) {
if ($res_vel['total_count'] == 0) {
return false;
}
}
#endregion
}
#endregion
return true;
}
/**
* Function Description:获取所有的座位
* Function Name: getRunBunSeat
* @param array $run_bus_arr 班次信息
*
* @return array|bool
*
* @author 张帅
*/
public function getRunBusSeat($run_bus_arr)
{
$result = [];
foreach ($run_bus_arr as $key => $vel) {
$sql_and = '';
if ($vel['bus_order_id'] != 0) {
$sql_and = ' and bus_order_id = ' . $vel['bus_order_id'];
}
$sql = '' . 'select
group_concat(id) as run_x_id,run_id,bus_order_id,seat_x,seat_y,seat_type,seat_seq_id,seat_name,count(id) as seat_count
from
run_' . $vel['run_date'] . '
where
run_id = ' . $vel['run_id'] . '
and order_id >= ' . $vel['start_seq_id'] . '
and order_id < ' . $vel['end_seq_id'] . '
and seat_type = ' . $vel['seat_type'] . $sql_and . '
and cancel_flag = 0
and seat_status = 0
group by
run_id,bus_order_id,seat_type,seat_seq_id,seat_name
order by seat_seq_id ASC';
$res = Yii::$app->db->createCommand($sql)->queryAll();
if (count($res) == 0) {
return false;
}
//分车归理数据
$res_one = [];
foreach ($res as $res_key => $res_vel) {
if ($res_vel['seat_count'] == $vel['seat_count']) {
$res_one[$res_vel['bus_order_id']][$res_vel['seat_seq_id']] = $res_vel;
}
}
//挑出可以放订单的车次,否则返回错误
$result_one = [];
foreach ($res_one as $res_key => $res_vel) {
if (count($res_vel) >= $vel['num']) {
$result_one = $res_vel;
break;
}
}
if (count($result_one) == 0) {
return false;
}
//通过票种索引塞入数据
$result[$vel['ticket_id']] = $result_one;
}
return $result;
}
/**
* Function Description:获取座位信息
* Function Name: getSeatArray
* @param array $run_bus_arr 班次车次数组
* @param array $prod_arr 产品数据
*
* @return array
*
* @author 张帅
*/
public function getSeatArray($run_bus_arr, $prod_arr)
{
$result = [];
foreach ($run_bus_arr as $key => $vel) {
$seat_no = [];
$i = 0;
#region 连座筛选
foreach ($vel as $key1 => $vel2) {
$seat_no[$key1 - $i][$key1] = $vel2;
$i++;
}
#endregion
#region 挑选座位
$run_seat_value = [];
foreach ($seat_no as $key1 => $vel2) {
if (count($vel2) >= $prod_arr[$key]['num']) {
$run_seat_value = array_slice($vel2, 0, $prod_arr[$key]['num'], true);
break;
}
}
if (count($run_seat_value) == 0) {
$run_seat_value = array_slice($vel, 0, $prod_arr[$key]['num'], true);
}
#endregion
$result[$key] = $run_seat_value;
}
return $result;
}
/**
* Function Description:获取指定数量的连续数字
* Function Name: getPointNum
* @param int $start 开始数值
* @param int $num 取数字数量
* @return array
* @author 温依莅
*/
public function getPointNum($start, $num)
{
$start += 0;
$num += 0;//转为数字
$list = array();
for ($i = 1; $i <= $num; $i++) {
$list[$i] = ++$start;
}
return $list;
}
/**
* Function Description:原无代售逻辑下单接口
* Function Name: submitBusOrderBack
* @param array $prod_list 产品列表
* @param array $seat_arr 座位
* @param array $order_info_arr 订单的其他信息
* @param array $customer_info_arr 乘客
* @param array $order_append_arr 附加产品
* @param int $is_send_msg 是否发短信
*
* @return mixed
*
* @author 张帅
*/
public function submitBusOrderBack($prod_list, $seat_arr, $order_info_arr, $customer_info_arr, $order_append_arr, $is_send_msg)
{
#region 获取最大唯一值,先赋给支付id,然后递增
$base_main_id = BaseUniqueId2::find()->select('unique_id')->where(['id' => 1])->asArray()->one();
//※更新唯一键值
#计算BaseUniqueId 需要 增加的值(往返程的主订单和子订单的id个数)
$add_count = 0;
foreach ($prod_list as $k => $v) {
$add_count += $v['people_num'] + 1;
}
$time=time().rand(0,999999);
$res = BaseUniqueId2::updateAll(['UNIQUE_ID' => $base_main_id['unique_id'] + $add_count + 1,'UPDATE_TIME'=>$time], ['id' => 1]);
if (!$res) {
$result['code'] = '1';
$result['info'] = '下单失败';
$result['error'] = 'BaseUniqueId更新失败';
return $result;
}
$pay_main_id = $base_main_id['unique_id'] + 1;
$parent_order_id = $pay_main_id;
#endregion
#region 提交订单操作数据库所需数据
$main_order = [];//主订单数据
$main_order_key = [];//提交主订单时所需key值
$son_order = [];//子订单数据
$son_order_key = [];//提交子订单时所需key值
$run_x_arr = [];//run_x
$run_bus_prod_stock_arr = [];//run_bus,run_prod,run_stock更新数据
$main_order_id = [];//订单id数组
$pay_main_arr = [];//主支付记录数据
$pay_main_key = [];//主支付记录键值
$order_price = 0;
$son_order_id = 0;
#endregion
#region 整理数据
foreach ($prod_list as $key => $vel) {
$run_x_one = [];//run_x_one
$prod_list[$key]['order_id'] = $parent_order_id;//订单号
#region 主订单数据
$main_order_val = [
'ID' => $parent_order_id,
'MAIN_CREATE_USER_ID' => $order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,//'下单组织机构所属一级机构ID',
'PROD_TOP_ORG_ID' => 0,//'产品所属一级机构ID',
'ORDER_ID' => $parent_order_id,
'ORDER_DESCRIPTION' => $vel['ticket_name'] . ',' . $vel['run_date'] . ',' . $vel['people_num'] . '|' . $vel['people_num'],//'最细颗粒度的订单描述',
'OUTSIDE_ORDER_NO' => $order_info_arr['outside_sale_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $pay_main_id,//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => 0,
'PARENT_PROD_ID' => $vel['line_id'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $vel['total_price'],//'订单价格',
'ORDER_PROD_TYPE' => 81,//'订单类型',
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],//'上车站检票口资源ID',
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],//'上车站检票口资源名称',
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],//'上车站发车时间距发车日零点分钟数',
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],//'下车站发车时间距发车日零点分钟数',
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],//'订单当前状态',
'IF_LAST_PROD' => 1,//'是否最细颗粒度的判断',
'ORDER_LEVEL' => 0,//'Order Level',
'OUTSIDE_SALE_ORG_ID' => $order_info_arr['outside_sale_org_id'],//
'SALES_MAN' => $order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['total_receiver_value'],
];
#endregion
$single_price = $vel['total_price'];
$order_price += $vel['total_price'];
#region 收集主订单的id
if ($vel['if_back'] == 1) {
$main_order_id[0] = $parent_order_id;//收集主订单的id
} else {
$main_order_id[1] = $parent_order_id;//收集主订单的id
}
#endregion
#region 主订单key值
if (count($main_order_key) == 0) {
$main_order_key = array_keys($main_order_val);
}
#endregion
$son_order_id = $parent_order_id;//子订单id号
#region 根据人数遍历每个子订单的个数
foreach ($seat_arr[$vel['ticket_id']] as $seat_key => $seat_vel) {
$son_order_id++;//子订单号递增
#region 子订单数据
$son_order_val = [
'ID' => $son_order_id,
'MAIN_CREATE_USER_ID' => $order_info_arr['user_id'],//'代下单用户ID,BASE_USER.ID,用户A代替用户B下单,则MAIN_CREATE_USER_ID=A,CREATE_USER_ID=B',
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALE_ORG_ID' => 0,//'分销商机构ID',
'SALE_TOP_ORG_ID' => 0,
'PROD_TOP_ORG_ID' => 0,
'ORDER_ID' => $son_order_id,
'OUTSIDE_ORDER_NO' => $order_info_arr['outside_sale_order_no'],//'分销商订单编号',
'ORDER_VALID_STATUS' => 1,//'订单有效性状态,0失效订单、1有效订单',
'ORDER_BOOK_STATUS' => $order_info_arr['order_book_status'],//'订单预订状态,0非预订、1有时限、2永久锁位预订',
'ORDER_PAY_STATUS' => $order_info_arr['order_pay_status'],//'订单是否已支付,0未支付,1已支付',
'ORDER_PAY_MAIN_ID' => $pay_main_id,//'支付记录主ID,ORDER_PAY_MAIN.ID',
'ORDER_PAY_USER_ID' => $order_info_arr['user_id'],//'支付操作用户ID,BASE_USER.ID',
'ORDER_PAY_TIME' => date('Y-m-d H:i:s'),//'支付操作时间',
'PARENT_ORDER_ID' => $parent_order_id,
'PROD_ID' => $vel['ticket_id'],
'PARENT_PROD_ID' => $vel['line_id'],
'PROD_NAME' => $vel['ticket_name'],
'PARENT_PROD_NAME' => $vel['line_name'],
'ORDER_PRICE' => $vel['order_price'],
'ORDER_PROD_TYPE' => 82,
'RUN_ID' => $vel['run_id'],
'RUN_DATE' => $vel['run_date'],
'RUN_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),//'班次首发日期参数,发车日期至2016-01-01天数,算法使用',
'RUN_TIME' => $vel['run_time'],
'RUN_BUS_ORDER_ID' => $seat_vel['bus_order_id'],//'班次车号,RUN_BUS.BUS_ORDER_ID',
'RUN_BUS_SEAT_TYPE' => $vel['seat_type'],//'座位类型,DICT_TYPE.ID,普通座、贵宾座等',
'RUN_BUS_SEAT_SEQ_ID' => $seat_vel['seat_seq_id'],//'座位内部序列ID',
'RUN_BUS_SEAT_NAME' => $seat_vel['seat_name'],
'PROD_START_STATION_RES_ID' => $vel['start_res_id'],
'PROD_START_STATION_RES_NAME' => $vel['start_res_name'],
'PROD_START_STATION_CHECKPORT_RES_ID' => $vel['checkport_res_id'],
'PROD_START_STATION_CHECKPORT_RES_NAME' => empty($vel['checkport_res_name']) ? '' : $vel['checkport_res_name'],
'PROD_START_STATION_SEQ_ID' => $vel['start_seq_id'],
'PROD_START_STATION_DATE' => $vel['run_date'],
'PROD_START_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_START_STATION_TIME' => $vel['start_time'],
'PROD_START_STATION_TIME_MINUTES' => $vel['start_minutes'],
'PROD_START_STATION_AREA_ID' => $vel['start_area_id'],
'PROD_START_STATION_AREA_NAME' => $vel['start_area_name'],
'PROD_END_STATION_RES_ID' => $vel['end_res_id'],
'PROD_END_STATION_RES_NAME' => $vel['end_res_name'],
'PROD_END_STATION_SEQ_ID' => $vel['end_seq_id'],
'PROD_END_STATION_DATE' => $vel['run_date'],
'PROD_END_STATION_DATE_NUM' => (strtotime($vel['run_date']) - strtotime('2016-01-01')) / (60 * 60 * 24),
'PROD_END_STATION_TIME' => $vel['end_time'],
'PROD_END_STATION_TIME_MINUTES' => $vel['end_minutes'],
'PROD_END_STATION_AREA_ID' => $vel['end_area_id'],
'PROD_END_STATION_AREA_NAME' => $vel['end_area_name'],
'CUSTOMER_NAME' => $customer_info_arr['customer_name'],//'客人姓名',
'CUSTOMER_SEX' => 1,//'客人性别,0女1男',
'CUSTOMER_MOBILE' => $customer_info_arr['customer_mobile'],//'客人手机',
'CUSTOMER_MEMO' => $customer_info_arr['customer_memo'],//'订单备注',
'CUSTOMER_ADDRESS' => $customer_info_arr['customer_address'],//'顾客地址' 新增by luocj 2017-08-23,
'CUSTOMER_ID_TYPE' => $customer_info_arr['customer_id_type'],
'CUSTOMER_ID_NO' => $customer_info_arr['customer_id_no'],
'MEMBER_ID' => $order_info_arr['member_id'],
'ORDER_STATUS' => $order_info_arr['order_status'],
'if_gift' => 0,//'是否是赠票,0不是赠票,1是赠票',
'ORDER_LEVEL' => 0,
'OUTSIDE_SALE_ORG_ID' => $order_info_arr['outside_sale_org_id'],
'SALES_MAN' => $order_info_arr['sales_man'],
'RECEIVE_VALUE' => $vel['receiver_value'],
];
#endregion
#region 子订单key值
if (count($son_order_key) == 0) {
$son_order_key = array_keys($son_order_val);
}
#endregion
#region run_bus,run_prod,run_stock更新数据
if (!isset($run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']])) {
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['num'] = $vel['people_num'];//数量
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['run_id'] = $vel['run_id'];//班次id
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['bus_order_id'] = $seat_vel['bus_order_id'];//车次号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['ticket_id'] = $vel['ticket_id'];//票种id
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['start_seq_id'] = $vel['start_seq_id'];//开始站顺序号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['end_seq_id'] = $vel['end_seq_id'];//结束站顺序号
$run_bus_prod_stock_arr[$vel['run_id'] . '-' . $seat_vel['bus_order_id']]['seat_type'] = $vel['seat_type'];//座位类型
}
#endregion
#region 更新run_x配置数据
$run_x_one[$seat_key]['run_x_id'] = $seat_vel['run_x_id'];
$run_x_one[$seat_key]['run_date'] = substr($vel['run_date'], 0, 7);
$run_x_one[$seat_key]['run_date'] = str_replace('-', '', $run_x_one[$seat_key]['run_date']);//run_x表的日期后缀
$run_x_one[$seat_key]['order_main_id'] = $son_order_id;//主订单号
$run_x_one[$seat_key]['order_main_prod_id'] = $vel['line_id'];//线路id
$run_x_one[$seat_key]['order_prod_id'] = $vel['ticket_id'];//票种id
#endregion
#region 子订单数组
$son_order[] = $son_order_val;
#endregion
}
#endregion
#region 更新run_x配置数据
$run_x_arr[$key] = $run_x_one;
#endregion
$parent_order_id = $son_order_id + 1;
#region 主订单数组
$main_order[] = $main_order_val;
#endregion
}
#endregion
$unique_id = $son_order_id;//唯一主键
#region 主支付记录数据数组
foreach ($main_order_id as $key => $vel) {
#region 主支付记录
$pay_main_one = [
'ID' => $pay_main_id,
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'PAY_TOTAL' => $single_price,
'ORDER_ID' => $vel,
];
#endregion
$pay_main_arr[] = $pay_main_one;//主支付记录数组
#region 主支付记录key值
if (count($pay_main_key) == 0) {
$pay_main_key = array_keys($pay_main_one);
}
#endregion
}
#endregion
$transaction = Yii::$app->db->beginTransaction();
try {
#region 1.占座 更新run_x表
foreach ($run_x_arr as $run_x_key => $run_x_vel) {
foreach ($run_x_vel as $key => $vel) {
$update_sql = 'update run_' . $vel['run_date'] . '
set order_main_id = ' . $vel['order_main_id'] . ',
order_main_prod_id = ' . $vel['order_main_prod_id'] . ',
order_prod_id = ' . $vel['order_prod_id'] . ',
seat_status = 2,
order_lock_user_id = ' . $order_info_arr['user_id'] . ',
order_lock_time = \'' . date('Y-m-d H:i:s') . '\'
where id in (' . $vel['run_x_id'] . ')';
$res = Yii::$app->db->createCommand($update_sql)->execute();
if (!$res) {
throw new Exception('占位失败');
}
}
}
#endregion
#region 2.插入主订单
$res = Yii::$app->db->createCommand()->batchInsert(OrderMain::tableName(), $main_order_key, $main_order)->execute();
if (!$res) {
throw new Exception('主订单提交失败');
}
#endregion
#region 3.插入子订单
$res = Yii::$app->db->createCommand()->batchInsert(OrderMain::tableName(), $son_order_key, $son_order)->execute();
if (!$res) {
throw new Exception('子订单提交失败');
}
#endregion
#region 4.更新run_prod,run_bus,run_stock
foreach ($run_bus_prod_stock_arr as $key => $vel) {
#region (1).更新run_prod
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['=', 'prod_id', $vel['ticket_id']],
['=', 'cancel_flag', 0],
];
$res = RunProd::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_prod失败');
}
#endregion
#region (2).更新run_bus
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['=', 'bus_order_id', $vel['bus_order_id']],
['=', 'cancel_flag', 0],
];
$res = RunBus::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_bus失败');
}
#endregion
#region (3).更新run_stock
$update_value = [
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'SALED_COUNT' => new Expression('SALED_COUNT+' . $vel['num']),
];
$update_where = [
'and',
['=', 'run_id', $vel['run_id']],
['>=', 'seq_id', $vel['start_seq_id']],
['<', 'seq_id', $vel['end_seq_id']],
['=', 'seat_type', $vel['seat_type']],
['=', 'cancel_flag', 0],
];
$res = RunStock::updateAll($update_value, $update_where);
if (!$res) {
throw new Exception('更新run_stock失败');
}
#endregion
}
#endregion
//==================这里加判断,只有是已支付状态,才插入支付记录=============
if ($order_info_arr['order_pay_status'] == 1) {
#region 5.插入主支付记录
$res = Yii::$app->db->createCommand()->batchInsert(OrderPayMain::tableName(), $pay_main_key, $pay_main_arr)->execute();
if (!$res) {
throw new Exception('插入主支付记录失败');
}
#endregion
#region 6.插入支付明细
$order_pay_detail = new OrderPayDetail();
$order_pay_detail->attributes = [
'ID' => $pay_main_id,
'CREATE_USER_ID' => $order_info_arr['user_id'],
'CREATE_TIME' => date('Y-m-d H:i:s'),
'UPDATE_USER_ID' => $order_info_arr['user_id'],
'UPDATE_TIME' => date('Y-m-d H:i:s'),
'PAY_MAIN_ID' => $pay_main_id,
'PAY_TYPE_ID_1' => $order_info_arr['pay_type'],
'PAY_MONEY' => $order_price,
];
$res = $order_pay_detail->insert();
if (!$res) {
throw new Exception('插入支付明细失败');
}
#endregion
}
/*#region 7.更新唯一键值
$res = BaseUniqueId2::updateAll(['UNIQUE_ID' => $unique_id], ['id' => 1]);
if (!$res) {
throw new Exception('更新唯一键值失败');
}
#endregion*/
#region 8.如果是往返 建立往返关系
if (count($main_order_id) == 2) {
$to_from = new ToFrom();
$to_from->attributes = [
'to_orderid' => $main_order_id[0],
'back_orderid' => $main_order_id[1],
'CREATE_TIME' => date('Y-m-d H:i:s'),
];
$res = $to_from->insert();
if (!$res) {
throw new Exception('建立往返关系失败');
}
}
#endregion
#region 9.插入酒店门票价格
if ($order_append_arr['gate_price'] != 0 || $order_append_arr['hotel_price'] != 0) {
$order_extra_info = new OrderExtraInfo();
$order_extra_info->attributes = [
'order_id' => $main_order_id[0],
'hotel_price' => $order_append_arr['hotel_price'],
'gate_price' => $order_append_arr['gate_price'],
];
$res = $order_extra_info->insert();
if (!$res) {
throw new Exception('酒店门票价格提交失败' . serialize($order_extra_info->getErrors()));
}
}
#endregion
#region 10.插入日志
$order_run_time = array();
$opera_time = date('Y-m-d H:i:s');
foreach ($prod_list as $key => $vel) {
$order_opera_log = new OrderOperaLog();
$order_opera_log->attributes = [
'order_id' => $vel['order_id'],
'opera_user_id' => $order_info_arr['user_id'],
'opera_type' => 1,
'opera_time' => $opera_time,
'opera_detail' => '创建订单,出发日期' . $vel['run_date'] . ',订单渠道' . $order_info_arr['org_name'] . ',单价' . $vel['order_price'] . ',总额' . $vel['total_price'] . ',联系人信息:' . $customer_info_arr['customer_name'] . ',' . $customer_info_arr['customer_mobile'] . ',' . $customer_info_arr['customer_id_no'] . ',备注信息:' . $customer_info_arr['customer_memo'] . ',乘客地址:' . $customer_info_arr['customer_address'],
'member_id' => $order_info_arr['member_id'],
'opera_platform' => '统一下单',
];
$order_run_time[$vel['order_id']] = $vel['run_date'];
$res = $order_opera_log->insert();
if (!$res) {
throw new Exception('插入日志失败');
}
}
#endregion
//$transaction->commit();
$transaction->rollBack();
#region 11.发送短信
if ($order_info_arr['order_pay_status'] == 1 && $is_send_msg == 1) {
foreach ($main_order_id as $key => $vel) {
//这里对发送短信时间做一个判断,满足条件的才发送短信
$time_diff = $this->get_time_diff($vel);
if ($time_diff >= 0) {
$url = 'http://' . CS1_DOMAIN . '/api/msg/order-send-msg';
$sendInfo['order_id'] = $vel;
$sendInfo['msg_type'] = 'cs';
Msg::httpRequestForasyn($url, $sendInfo);
}
}
}
#endregion
#region 12.添加订单状态
if ($order_info_arr['order_pay_status'] == 1) {
//添加出票成功短信
$tomorrow_day = date("Y-m-d", strtotime("+1 days"));
$current_time = date("H:i");
foreach ($main_order_id as $key => $vel) {
$order_run_day = $order_run_time[$vel];
//if( $order_run_day > $tomorrow_day || ( $order_run_day == $order_run_day && $current_time < "18:30" ) ) {
$url = 'http://' . CS1_DOMAIN . '/api/msg/order-send-success-msg';
$sendInfo['order_id'] = $vel;
Msg::httpRequestForasyn($url, $sendInfo);
//}
$url = 'http://' . CS_DOMAIN . '/st-xm/Api/add_order_status_log.php';
$post_data['order_id'] = $vel;
if ($order_info_arr['order_pay_status'] == 1) {
$post_data['order_status'] = '451,452,453';
} else {
$post_data['order_status'] = '451';
}
Msg::httpRequestForasyn($url, $post_data);
}
}
#endregion
$result['code'] = '0';
$result['info'] = '下单成功';
$result['data'] = implode(',', $main_order_id);
} catch (Exception $e) {
# 回滚事务
$transaction->rollBack();
$result['code'] = '1';
$result['info'] = '下单失败';
$result['error'] = $e->getMessage();
}
return $result;
}
/**
* Function Description:获取代收价
* Function Name: getBusProductListReceiver
* @param array $prod_list 产品列表
* @param string $receiver_value_str 代收信息
* @param array $prod_arr 产品简易数据
*
* @return mixed
*
* @author 张帅
*/
public function getBusProductListReceiver($prod_list, $receiver_value_str, $prod_arr)
{
$result = [];
$receiver_value_str = substr($receiver_value_str, 1, -1);
$receiver_value_arr = explode(',', $receiver_value_str);//代收价格信息
$receiver_value = (!isset($receiver_value_arr[0]) || empty($receiver_value_arr[0])) ? 0 : $receiver_value_arr[0];//代收单价
$total_receiver_value = (!isset($receiver_value_arr[1]) || empty($receiver_value_arr[1])) ? 0 : $receiver_value_arr[1];//代收总价
$receiver_value = $receiver_value / count($prod_list);
$total_receiver_value = $total_receiver_value / count($prod_list);
//将订单单价,人数,总价,往返标志,代收单价,代收总价放入产品列表中
foreach ($prod_list as $key => $vel) {
if ($total_receiver_value != 0) {
$prod_list[$key]['total_receiver_value'] = round($total_receiver_value, 2);
$prod_list[$key]['receiver_value'] = round($total_receiver_value / $prod_arr[$key]['num'], 2);
} else {
$prod_list[$key]['total_receiver_value'] = round($receiver_value * $prod_arr[$key]['num'], 2);
$prod_list[$key]['receiver_value'] = round($receiver_value, 2);
}
$prod_list[$key]['order_price'] = $prod_arr[$key]['price'];
$prod_list[$key]['total_order_price'] = $prod_arr[$key]['price'] * $prod_arr[$key]['num'];
$prod_list[$key]['people_num'] = $prod_arr[$key]['num'];
$prod_list[$key]['if_back'] = $prod_arr[$key]['if_back'];
}
//将产品安往返排序
foreach ($prod_arr as $key => $vel) {
$result[$key] = $prod_list[$key];
}
return $result;
}
public static function updateBusCost($run_id, $bus_order_id)
{
$data = ['code' => '0', 'info' => 'bus_cost表数据修改成功'];
$bus_cost_model = BusCost::find()->where(['RUN_ID' => $run_id, 'BUS_ORDER_ID' => $bus_order_id, 'COST_TYPE' => 478, 'CREATE_USER_ID' => 2])->orderBy('CREATE_TIME ASC')->one();
$run_bus_model = BusCost::findOne(['RUN_ID' => $run_id, 'BUS_ORDER_ID' => $bus_order_id]);
if (!$bus_cost_model || !$run_bus_model) {
$data['code'] = '1';
$data['info'] = '找不到bus_cost 或run_bus 对应的记录';
return $data;
}
$bus_cost_model->SALED_COUNT = $run_bus_model->SALED_COUNT;
$bus_cost_model->COST_PRICE = round($bus_cost_model->TOTAL_COST_PRICE / $bus_cost_model->SALED_COUNT, 2);
$bus_cost_model->save(false);
return $data;
}
/**
* Function Description:增加自由行的保险订单
* Function Name: addFreeWalkInsurance
* @param $order_info_arr
* @param $bus_order_id
* @param $insurance
* @param $tourist_id
*
*
* @author 田玲菲
*/
public static function addFreeWalkInsurance($order_info_arr, $bus_order_id, $insurance, $tourist_id)
{
$config = Yii::$app->params;
$order_id_arr = explode(',', $bus_order_id); //其实是自由行产品(组合产品)的title_id
foreach ($order_id_arr as $order_id) {
if (empty($order_id)) {
continue;
}
$orderTitle = new OrderTitle();
$getOrderInfo = $orderTitle->getTitleInfoById($order_id);
try {
//订单信息
$oderInfo = [
$order_info_arr['user_id'],//下单用户
$order_info_arr['member_id'],//会员号
$order_info_arr['outside_sale_org_id'],//渠道ID
$order_info_arr['outside_order_no'],//渠道订单号
0,//是否自动退票
$order_info_arr['pay_type'],//支付方式
1,//是否支付
$order_info_arr['sales_man'],//业务员
];
$operaTouristCommon = new OperaTouristCommon();
$result = $operaTouristCommon->getDays($tourist_id);
$days = $result['day_num'];
$main_prod_id = $config['tourist_insurance']['main_prod_id'];//主票种D
$data = [
#请求头
'auth_code' => Yii::$app->request->post('auth_code'),
'user_key' => Yii::$app->request->post('user_key'),
'user' => Yii::$app->request->post('user'),
'user_id' => Yii::$app->request->post('user_id'),
'request_time' => Yii::$app->request->post('request_time'),
#请求内容
'main_prod_id' => $main_prod_id,//主票种D
'run_date' => $getOrderInfo['run_date'],//出行日期
'prod_str' => '',//购买票种列表
'order_info_str' => '{' . implode(',', $oderInfo) . '}',//订单信息
'customer_info_str' => '',
'action' => 'submit_ticket_order'
];
#设置购买票种列表
$operaProdRun = new OperaProductRun();
$getInfo = $operaProdRun->getRunInfoByProdIdAndDate($config['tourist_insurance']['sun_prod_id'][$days - 1], $getOrderInfo['run_date']);
$data['prod_str'] = '{' . implode(',', [$config['tourist_insurance']['sun_prod_id'][$days - 1], $getInfo['prod_price'], 1]) . '}';
#循环下单
$addMpFail = 0;
$addMpSu = [];
foreach ($insurance as $val) {
#设置联系人信息
$memo = $getOrderInfo['order_title_prod_name'] . '旅意险';//备注信息
$memo .= '
' . $getOrderInfo['prod_start_station_time'] . '|';
$customer_info = [
$val['0'], $getOrderInfo['cus_phone'], '150', $val['1'], $memo
];
$data['customer_info_str'] = '{' . implode(',', $customer_info) . '}';
#发起保险单下单功能
$addMp = Utils::httpRequest('http://' . CS1_DOMAIN . '/api/submit-order', $data);
$addMp = json_decode($addMp, true);
if (!isset($addMp['code']) || $addMp['code'] != 0) {//当保险单下单失败
$addMpFail++;
} else {
#保险单下单成功,进行订单绑定
$oderInsurance = new OrderInsuranceConnect();
$insuranceDate = [
'main_order_id' => $order_id, //此处是title_id
'insurance_order_id' => $addMp['data'],
];
$flag = $oderInsurance->addOne($insuranceDate);
if ($flag == false) {#绑定失败
$addMpFail++;
} else {
$addMpSu[] = $addMp['data'];
}
}
}
static:: sendInsuranceMsg($addMpSu, $insurance, $order_id, $getOrderInfo, $addMpFail, 4);
} catch (Exception $e) {
//如果程序异常 发送短信通知
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
$msg = "自由行订单title_id为({$order_id})带来了新的保险订单。注意:出现保险下单失败,请去自由行订单详情页确认保险人的具体信息并搬单。";
Msg::sendTelMsg($phone, $msg);
}
}
}
}
}
/**
* Des:添加保险单
* Name: addInsurance
* @param $order_info_arr
* @param $bus_order_id
* @param $insurance
* @author 倪宗锋
*/
public static function addInsurance($order_info_arr, $bus_order_id, $insurance)
{
$config = Yii::$app->params;
$order_id_arr = explode(',', $bus_order_id);
foreach ($order_id_arr as $order_id) {
if (empty($order_id)) {
continue;
}
$orderMain = new OrderMain();
$getOrderInfo = $orderMain->getOrderInfoForGetSeat($order_id);
try {
//订单信息
$oderInfo = [
$order_info_arr['user_id'],//下单用户
$order_info_arr['member_id'],//会员号
$order_info_arr['outside_sale_org_id'],//渠道ID
$order_info_arr['outside_order_no'],//渠道订单号
0,//是否自动退票
$order_info_arr['pay_type'],//支付方式
1,//是否支付
$order_info_arr['sales_man'],//业务员
];
$main_prod_id = $config['insurance']['main_prod_id'];//主票种D
$data = [
#请求头
'auth_code' => Yii::$app->request->post('auth_code'),
'user_key' => Yii::$app->request->post('user_key'),
'user' => Yii::$app->request->post('user'),
'user_id' => Yii::$app->request->post('user_id'),
'request_time' => Yii::$app->request->post('request_time'),
#请求内容
'main_prod_id' => $main_prod_id,//主票种D
'run_date' => $getOrderInfo['run_date'],//出行日期
'prod_str' => '',//购买票种列表
'order_info_str' => '{' . implode(',', $oderInfo) . '}',//订单信息
'customer_info_str' => '',
'action' => 'submit_ticket_order'
];
#设置购买票种列表
$operaProdRun = new OperaProductRun();
$getInfo = $operaProdRun->getRunInfoByProdIdAndDate($config['insurance']['sun_prod_id'], $getOrderInfo['run_date']);
$data['prod_str'] = '{' . implode(',', [$config['insurance']['sun_prod_id'], $getInfo['prod_price'], 1]) . '}';
#循环下单
$addMpFail = 0;
$addMpSu = [];
//投保人信息
$policy_holder = [
'name'=>$insurance[0][0],
'cardid'=>$insurance[0][1]
];
foreach ($insurance as $val){
$name = $val[0];
$cardid = $val[1];
$cardobj=Util::getIDCardInfo($cardid);
if($cardobj['old']>=18){
$policy_holder['name']=$name;
$policy_holder['cardid']=$cardid;
break;
}
}
foreach ($insurance as $val) {
#设置联系人信息
$memo = $getOrderInfo['start_area_name'] . '-->' . $getOrderInfo['end_area_name'];//备注信息
$memo .= '
' . $getOrderInfo['prod_start_station_time'] . '|' . $getOrderInfo['run_id'];
$customer_info = [
$val['0'], $getOrderInfo['customer_mobile'], '150', $val['1'], $memo
];
$data['customer_info_str'] = '{' . implode(',', $customer_info) . '}';
#发起保险单下单功能
$addMp = Utils::httpRequest('http://' . CS1_DOMAIN . '/api/submit-order', $data);
$addMp = json_decode($addMp, true);
if (!isset($addMp['code']) || $addMp['code'] != 0) {//当保险单下单失败
$addMpFail++;
} else {
#保险单下单成功,进行订单绑定
$oderInsurance = new OrderInsuranceConnect();
$insuranceDate = [
'main_order_id' => $order_id,
'insurance_order_id' => $addMp['data'],
];
$flag = $oderInsurance->addOne($insuranceDate);
if ($flag == false) {#绑定失败
$addMpFail++;
} else {
$addMpSu[] = $addMp['data'];
#保险下单成功后,调用保险第三方接口进行下单
$insuranceParam=[];
$insuranceParam['start_date']=$getOrderInfo['run_date'];
$insuranceParam['end_date']=$getOrderInfo['run_date'];
$insuranceParam['start_time'] = $getOrderInfo['prod_start_station_time'].':00';
$insuranceParam['passenger_cardid']=$val[1];
$insuranceParam['passenger_name']=$val[0];
$insuranceParam['contacts_phone']=$getOrderInfo['customer_mobile'];
$insuranceParam['order_id']=$insuranceDate['insurance_order_id'];
$insuranceParam['prod_type']='1'; //1乘客意外险 2旅行意外险
$insuranceParam['policy_holder_name'] = $policy_holder['name'];
$insuranceParam['policy_holder_cardid'] = $policy_holder['cardid'];
$curl = new CurlInterface($insuranceParam, 5);
$verifyResult = $curl->execute('http://'. CS1_DOMAIN. '/api/sunshine-insurance/verify-order','POST');
$verifyResult = json_decode($verifyResult,true);
if(!$verifyResult['flag']){
//不允许下单
//如果在保险第三方平台下单失败,则发送短信通知
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
$msg = "保险下单直连失败,请及时确认并搬单。订单号:".$insuranceDate['insurance_order_id'];
Msg::sendTelMsg($phone, $msg);
}
}
}else{
$insuranceResult = $curl->execute('http://'. CS1_DOMAIN. '/api/sunshine-insurance/create-order', 'POST');
$insuranceResult = json_decode($insuranceResult,true);
if($insuranceResult['flag']){
//成功
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
$msg = "保险下单直连成功success,请及时确认并搬单。订单号:".$insuranceDate['insurance_order_id'];
Msg::sendTelMsg($phone, $msg);
}
}
}else{
//失败
//如果在保险第三方平台下单失败,则发送短信通知
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
$msg = "保险下单直连失败,请及时确认并搬单。订单号:".$insuranceDate['insurance_order_id'];
Msg::sendTelMsg($phone, $msg);
}
}
}
}
}
}
}
static:: sendInsuranceMsg($addMpSu, $insurance, $order_id, $getOrderInfo, $addMpFail);
} catch (Exception $e) {
//如果程序异常 发送短信通知
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
$msg = "巴士订单({$order_id})带来了新的保险订单。注意:出现保险下单失败,请去巴士订单详情页确认保险人的具体信息并搬单。";
Msg::sendTelMsg($phone, $msg);
}
}
}
}
}
/**
* Des:发送保险单短信
* Name: sendInsuranceMsg
* @param $addMpSu
* @param $insurance
* @param $order_id
* @param $getOrderInfo
* @param $type
* @author 倪宗锋
*/
public static function sendInsuranceMsg($addMpSu, $insurance, $order_id, $getOrderInfo, $addMpFail, $type = '2')
{
$runTime = strtotime($getOrderInfo['prod_start_station_date'] . ' ' . $getOrderInfo['prod_start_station_time']);//出发日期时间戳
$createTime = strtotime($getOrderInfo['create_time']);//下单时间
$msg = '';
/**判断是否紧急订单*/
$jinji = false;
//1、下单时间 为 晚上18点到晚上24点间 且出发日期为当天或第二天早上10点之前的订单
if ($jinji == false) {
$runtimeCh = $runTime >= strtotime(date('Y-m-d 00:00')) && $runTime < strtotime(date("Y-m-d 10:00", strtotime("+1 day")));//出发日期为当天或第二天早上10点之前的订单
$creatTimeCh = $createTime >= strtotime(date('Y-m-d 18:00')) && $createTime < strtotime(date("Y-m-d 00:00", strtotime("+1 day")));//下单时间 为 晚上18点到晚上24点间
if ($runtimeCh && $creatTimeCh) {
$remainingTime = '距离发车还有' . ceil((($runTime - time()) / 60)) . '分钟';
$msg = '【紧急订单!!】' . $remainingTime;
$jinji = true;
}
}
//2、下单时间为早上0点到9点,且出发时间为当天早上10点之前的订单
if ($jinji == false) {
$runtimeCh = $runTime >= strtotime(date('Y-m-d 00:00')) && $runTime < strtotime(date("Y-m-d 10:00"));//出发时间为当天早上10点之前的订单
$creatTimeCh = $createTime >= strtotime(date('Y-m-d 00:00')) && $createTime < strtotime(date("Y-m-d 09:00"));//下单时间为早上0点到9点
if ($runtimeCh && $creatTimeCh) {
$remainingTime = '距离发车还有' . ceil((($runTime - time()) / 60)) . '分钟';
$msg = '【紧急订单!!】' . $remainingTime;
$jinji = true;
}
}
//3、下单时间距离发车时间小于60分钟 的订单
if ($jinji == false) {
if ($runTime < time() + 3600 && $runTime > time()) {
$remainingTime = '距离发车还有' . ceil((($runTime - time()) / 60)) . '分钟';
$msg = '【紧急订单!!】' . $remainingTime;
}
}
/**发送短信通知*/
$config = Yii::$app->params;
if (empty($config['YunYinPhone']) == false) {
foreach ($config['YunYinPhone'] as $phone) {
if ($type == 4) {//巴士自由行订单
if (count($addMpSu) == count($insurance)) {
$msg .= "自由行订单({$order_id})带来了新的保险订单。系统订单号:" . implode(',', $addMpSu) . "。请及时确认及搬单。";
} else {
$msg .= "自由行订单({$order_id})带来了新的保险订单。系统订单号:" . implode(',', $addMpSu) . "。※注意:出现保险下单失败,失败数{$addMpFail}※ 请及时确认及搬单。";
}
} else {
if (count($addMpSu) == count($insurance)) {
$msg .= "巴士订单({$order_id})带来了新的保险订单,每单保险3元。系统订单号:" . implode(',', $addMpSu) . "。请及时确认及搬单。";
} else {
$msg .= "巴士订单({$order_id})带来了新的保险订单,每单保险3元。系统订单号:" . implode(',', $addMpSu) . "。※注意:出现保险下单失败,失败数{$addMpFail}※ 请及时确认及搬单。";
}
}
Msg::sendTelMsg($phone, $msg);
}
}
}
/**
* Function Description:插入乘车人身份信息
* Function Name: IstPassengerInfo
* @param $order_id
* @param $passenger_info
*
* @return bool
*
* @author 娄梦宁
*/
public function IstPassengerInfo($order_id, $passenger_info)
{
try {
$order_main = new OrderMain();
$order_customer_info = new OrderCustomerInfo();
$order_son_info = $order_main->getSonOrderArr($order_id);
foreach ($order_son_info as $key => $val) {
$value = [
'order_main_id' => $val['order_id'],
'order_parent_id' => $order_id,
'cus_name' => $passenger_info[$key]['passenger_name'],
'cus_id_type' => 1,
'cus_id_no' => $passenger_info[$key]['passenger_cardid'],
'create_time' => date('Y-m-d H:i:s')
];
$clone = clone $order_customer_info;
$clone->attributes = $value;
$clone->insert();
}
return true;
} catch (\yii\db\Exception $e) {
return false;
}
}
}