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

227 rivejä
6.3 KiB

  1. <?php
  2. namespace backend\modules\api\models;
  3. use Yii;
  4. use yii\db\Expression;
  5. /**
  6. * This is the model class for table "outside_run".
  7. *
  8. * @property integer $id
  9. * @property integer $org_id
  10. * @property string $run_code
  11. * @property string $prod_code
  12. * @property string $prod_name
  13. * @property string $line_code
  14. * @property string $line_name
  15. * @property string $start_date
  16. * @property string $start_time
  17. * @property string $start_area
  18. * @property string $end_area
  19. * @property string $start_res
  20. * @property string $end_res
  21. * @property string $cus_price
  22. * @property string $mid_price
  23. * @property integer $remain_count
  24. * @property integer $cancel_flag
  25. * @property string $update_time
  26. */
  27. class OutsideRun extends \yii\db\ActiveRecord
  28. {
  29. /**
  30. * @inheritdoc
  31. */
  32. public static function tableName()
  33. {
  34. return 'outside_run';
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['org_id', 'remain_count', 'cancel_flag'], 'integer'],
  43. [['cus_price', 'mid_price'], 'number'],
  44. [['run_code', 'prod_code', 'prod_name', 'line_name', 'start_area', 'end_area', 'start_res', 'end_res'], 'string', 'max' => 50],
  45. [['line_code'], 'string', 'max' => 30],
  46. [['start_date', 'start_time'], 'string', 'max' => 10],
  47. [['update_time'], 'string', 'max' => 20],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'org_id' => 'Org ID',
  58. 'run_code' => 'Run Code',
  59. 'prod_code' => 'Prod Code',
  60. 'prod_name' => 'Prod Name',
  61. 'line_code' => 'Line Code',
  62. 'line_name' => 'Line Name',
  63. 'start_date' => 'Start Date',
  64. 'start_time' => 'Start Time',
  65. 'start_area' => 'Start Area',
  66. 'end_area' => 'End Area',
  67. 'start_res' => 'Start Res',
  68. 'end_res' => 'End Res',
  69. 'cus_price' => 'Cus Price',
  70. 'mid_price' => 'Mid Price',
  71. 'remain_count' => 'Remain Count',
  72. 'cancel_flag' => 'Cancel Flag',
  73. 'update_time' => 'Update Time',
  74. ];
  75. }
  76. /**
  77. * Des:获取出发站点数组
  78. * Name: getStartCity
  79. * @return array
  80. * @author 倪宗锋
  81. */
  82. public function getStartCity()
  83. {
  84. $where = [
  85. 'and',
  86. ['=', 'cancel_flag', 0],
  87. ['>=', 'start_date', new Expression("DATE(NOW())")]
  88. ];
  89. $list = self::find()->select(['start_area'])
  90. ->distinct('start_area')
  91. ->where($where)
  92. ->asArray()
  93. ->all();
  94. if (!is_array($list)) {
  95. $list = [];
  96. }
  97. return $list;
  98. }
  99. /**
  100. * Des:获取结束站点
  101. * Name: getEndArea
  102. * @param $startArea
  103. * @return array
  104. * @author 倪宗锋
  105. */
  106. public function getEndAreaByStartArea($startArea)
  107. {
  108. $select = [
  109. 'preSaleStart' => new Expression("MIN(start_date)"),
  110. 'preSaleEnd' => new Expression("MAX(start_date)"),
  111. 'area_name' => new Expression("end_area")
  112. ];
  113. $where = [
  114. 'and',
  115. ['=', 'cancel_flag', 0],
  116. ['=', 'start_area', str_replace('市', '', $startArea)],
  117. ['>=', 'start_date', new Expression("DATE(NOW())")]
  118. ];
  119. $getList = self::find()->select($select)
  120. ->where($where)
  121. ->groupBy('end_area')
  122. ->asArray()
  123. ->all();
  124. if (is_array($getList) == false) {
  125. $getList = [];
  126. }
  127. return $getList;
  128. }
  129. /***
  130. * Des:携程对接获取线路列表
  131. * Name: getLineList
  132. * @param $date
  133. * @param $fromCity
  134. * @param $toCity
  135. * @return array
  136. * @author 倪宗锋
  137. */
  138. public function getLineList($date, $fromCity, $toCity)
  139. {
  140. $select = [
  141. 'run_code',
  142. 'prod_code',
  143. 'line_code',
  144. 'line_name' => new Expression("CONCAT(start_res,'-',end_res)"),
  145. 'start_time',
  146. 'start_area',
  147. 'end_area',
  148. 'line_id' => new Expression("concat(SUBSTR(prod_code FROM locate('T',prod_code,locate('T',prod_code)+1)+1),line_code)"),
  149. 'price' => new Expression("MIN(cus_price)"),
  150. 'start_res',
  151. 'end_res',
  152. 'start_res_long',
  153. 'end_res_long',
  154. 'start_res_lat',
  155. 'end_res_lat',
  156. 'start_res_address',
  157. 'end_res_address',
  158. 'start_time' => new Expression("GROUP_CONCAT(CONCAT(start_time,'-',remain_count))")
  159. ];
  160. $where = [
  161. 'and',
  162. ['=', 'start_area', $fromCity],
  163. ['=', 'end_area', $toCity],
  164. ['=', 'start_date', $date],
  165. ['=', 'cancel_flag', 0]
  166. ];
  167. $list = self::find()->select($select)
  168. ->where($where)
  169. ->groupBy('line_id')
  170. ->asArray()
  171. ->all();
  172. if (is_array($list) == false) {
  173. return [];
  174. }
  175. return $list;
  176. }
  177. /**
  178. * Des:携程 汽车票对接 获取 run_id
  179. * Name: getRunId
  180. * @param $params
  181. * @return array
  182. * @author 倪宗锋
  183. */
  184. public function getInfo($params)
  185. {
  186. $lineIDEx = explode('NJYYT', $params['lineId']);
  187. $lineId = $lineIDEx['1'];//线路ID
  188. $ticketId = $lineIDEx['0'];//票种ID
  189. $select = [
  190. 'run_id' => new Expression("SUBSTR(run_code FROM locate('NJYYT',run_code)+5)"),
  191. 'remain_count'
  192. ];
  193. $where = [
  194. 'and',
  195. ['=', 'cancel_flag', 0],
  196. ['=', 'line_code', 'NJYYT' . $lineId],
  197. ['like', 'prod_code', '%T' . $ticketId, false],
  198. ['=', 'start_time', $params['fromTime']],
  199. ['=', 'start_date', $params['fromDate']]
  200. ];
  201. $info = self::find()->select($select)
  202. ->where($where)
  203. ->asArray()
  204. ->one();
  205. if (empty($info['run_id'])) {
  206. $run_id = 0;
  207. } else {
  208. $run_id = $info['run_id'];
  209. }
  210. return [
  211. 'run_id' => $run_id,
  212. 'line_id' => $lineIDEx['1'],
  213. 'ticket_id' => $ticketId,
  214. 'count' => $info['remain_count'] ? $info['remain_count'] : 0
  215. ];
  216. }
  217. }