Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

306 řádky
10 KiB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\Exception;
  5. use yii\db\Expression;
  6. /**
  7. * This is the model class for table "bus_ticket".
  8. *
  9. * @property integer $ticket_id
  10. * @property integer $start_area_id
  11. * @property string $start_area_name
  12. * @property integer $end_area_id
  13. * @property string $end_area_name
  14. * @property integer $start_res_id
  15. * @property string $start_res_name
  16. * @property integer $end_res_id
  17. * @property string $end_res_name
  18. * @property integer $line_id
  19. * @property integer $line_type
  20. * @property string $prod_price
  21. * @property string $line_name
  22. */
  23. class BusTicket extends \yii\db\ActiveRecord
  24. {
  25. /**
  26. * @inheritdoc
  27. */
  28. public static function tableName()
  29. {
  30. return 'bus_ticket';
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function rules()
  36. {
  37. return [
  38. [['ticket_id'], 'required'],
  39. [['ticket_id', 'start_area_id', 'end_area_id', 'start_res_id', 'end_res_id', 'line_id', 'line_type'], 'integer'],
  40. [['prod_price', 'start_res_longitude', 'start_res_latitude', 'end_res_longitude', 'end_res_latitude'], 'number'],
  41. [['start_area_name', 'end_area_name', 'start_res_name', 'end_res_name'], 'string', 'max' => 50],
  42. [['line_name'], 'string', 'max' => 255],
  43. [['ticket_id'], 'unique'],
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'ticket_id' => 'Ticket ID',
  53. 'start_area_id' => 'Start Area ID',
  54. 'start_area_name' => 'Start Area Name',
  55. 'end_area_id' => 'End Area ID',
  56. 'end_area_name' => 'End Area Name',
  57. 'start_res_id' => 'Start Res ID',
  58. 'start_res_name' => 'Start Res Name',
  59. 'end_res_id' => 'End Res ID',
  60. 'end_res_name' => 'End Res Name',
  61. 'line_id' => 'Line ID',
  62. 'line_type' => 'Line Type',
  63. 'prod_price' => 'Prod Price',
  64. 'line_name' => 'Line Name',
  65. 'start_LONGITUDE' => 'start_LONGITUDE',
  66. 'start_LATITUDE' => 'start_LATITUDE',
  67. 'end_LONGITUDE' => 'end_LONGITUDE',
  68. 'end_LATITUDE' => 'end_LATITUDE'
  69. ];
  70. }
  71. public function insertBus($ticket_arr)
  72. {
  73. foreach ($ticket_arr as $val) {
  74. $this->isNewRecord = true;
  75. $this->setAttributes($val);
  76. $this->save();
  77. }
  78. }
  79. /**
  80. * Function Description:新增可售票种的操作
  81. * Function Name: istBusTicket
  82. * @param $ticket_arr
  83. *
  84. * @return bool
  85. *
  86. * @author 娄梦宁
  87. */
  88. public function istBusTicket($ticket_arr)
  89. {
  90. $prod_category_1 = new ProdCategory();
  91. $prod_main_1 = new ProdMain();
  92. foreach ($ticket_arr as $val) {
  93. $transaction = Yii::$app->db->beginTransaction();
  94. try {
  95. //***************************************插入bus_ticket表**************************************************
  96. $this->isNewRecord = true;
  97. $this->setAttributes($val);
  98. $res = $this->save();
  99. if ($res === false) {
  100. $transaction->rollBack();
  101. continue;
  102. }
  103. //***************************************插入prod_category表**************************************************
  104. $tmp_pro_name = $val['line_name'] . "({$val['start_res_name']}-{$val['end_res_name']})";
  105. $value = [
  106. 'category_id' => '1',
  107. 'pro_cate_name' => $tmp_pro_name,
  108. 'bus_ticket_id' => $val['ticket_id'],
  109. 'bus_line_type' => $val['line_type'],
  110. 'create_time' => date('Y-m-d H:i:s'),
  111. 'show_price' => $val['prod_price'],
  112. 'commission' => 5,
  113. ];
  114. $prod_category = clone $prod_category_1;
  115. $prod_category->attributes = $value;
  116. $prod_res = $prod_category->insert();
  117. if ($prod_res === false) {
  118. $transaction->rollBack();
  119. continue;
  120. }
  121. //***************************************插入prod_main表**************************************************
  122. $value_adult = [
  123. 'prod_cate_id' => $prod_category->pro_cate_id,
  124. 'prod_price' => $val['prod_price'],
  125. 'bus_id' => $val['ticket_id'],
  126. 'prod_name' => '成人票',
  127. 'create_time' => date('Y-m-d H:i:s')
  128. ];
  129. $prod_main = clone $prod_main_1;
  130. $prod_main->attributes = $value_adult;
  131. $prod_main_res = $prod_main->insert();
  132. if ($prod_main_res === false) {
  133. $transaction->rollBack();
  134. continue;
  135. }
  136. $value_child = [
  137. 'prod_cate_id' => $prod_category->pro_cate_id,
  138. 'prod_price' => $val['prod_price'],
  139. 'bus_id' => $val['ticket_id'],
  140. 'prod_name' => '儿童票',
  141. 'create_time' => date('Y-m-d H:i:s')
  142. ];
  143. $prod_main_2 = clone $prod_main_1;
  144. $prod_main_2->attributes = $value_child;
  145. $prod_main_res_2 = $prod_main_2->insert();
  146. if ($prod_main_res_2 === false) {
  147. $transaction->rollBack();
  148. continue;
  149. }
  150. $transaction->commit();
  151. } catch (Exception $e) {
  152. $transaction->rollBack();
  153. }
  154. }
  155. return true;
  156. }
  157. /**
  158. * Function Description:cs系统取消的票种处理
  159. * Function Name: delBusTicket
  160. * @param $ticket_arr
  161. *
  162. * @return bool
  163. *
  164. * @author 娄梦宁
  165. */
  166. public function delBusTicket($ticket_arr)
  167. {
  168. $prod_category = new ProdCategory();
  169. $prod_main = new ProdMain();
  170. foreach ($ticket_arr as $val) {
  171. $transaction = Yii::$app->db->beginTransaction();
  172. //物理删除bus_ticket表
  173. $this->deleteAll(['ticket_id' => $val['ticket_id']]);
  174. //***************************************改变prod_category表状态位**************************************************
  175. $count = $prod_category::updateAll(['delete_flag' => 1], ['=', 'bus_ticket_id', $val['ticket_id']]);
  176. if (!$count) {
  177. $transaction->rollBack();
  178. continue;
  179. }
  180. //***************************************改变prod_main表状态位**************************************************
  181. $count = $prod_main::updateAll(['delete_flag' => 1], ['=', 'bus_id', $val['ticket_id']]);
  182. if (!$count) {
  183. $transaction->rollBack();
  184. continue;
  185. }
  186. $transaction->commit();
  187. }
  188. return true;
  189. }
  190. /**
  191. * Des:激活已售卖的产品
  192. * Name: assocBusTicket
  193. * @param $ticket_arr
  194. * @return bool
  195. * @throws \yii\db\Exception
  196. * @author 倪宗锋
  197. */
  198. public function assocBusTicket($ticket_arr)
  199. {
  200. $transaction = Yii::$app->db->beginTransaction();
  201. $prod_category = new ProdCategory();
  202. foreach ($ticket_arr as $val) {
  203. //***************************************修改bus_ticket表现有记录**************************************************
  204. self::updateAll($val, ['=', 'ticket_id', $val['ticket_id']]);
  205. //***************************************改变prod_category表状态位**************************************************
  206. $prod_category::updateAll(['delete_flag' => 0], ['=', 'bus_ticket_id', $val['ticket_id']]);//不论是否成功
  207. }
  208. $transaction->commit();
  209. return true;
  210. }
  211. /**
  212. * Function Description:查返程票种id对应的产品id
  213. * Function Name: getBackTicketId
  214. * @param $ticket_id
  215. *
  216. * @return mixed
  217. *
  218. * @author 娄梦宁
  219. */
  220. public function getBackTicketId($ticket_id)
  221. {
  222. $result = self::find()->select(['c.pro_cate_id'])
  223. ->from(self::tableName() . ' as a')
  224. ->innerJoin('bus_ticket b', 'a.end_res_name=b.start_res_name and a.start_res_name=b.end_res_name')
  225. ->innerJoin('prod_category c', 'a.ticket_id=c.bus_ticket_id')
  226. ->where(['b.ticket_id' => $ticket_id])
  227. ->orderBy('a.ticket_id desc,c.pro_cate_id desc')
  228. ->asArray()
  229. ->one();
  230. return $result['pro_cate_id'];
  231. }
  232. /*
  233. * 获取当前数据库所存票种信息
  234. */
  235. public function getBusArr()
  236. {
  237. $result = self::find()
  238. ->from(self::tableName())
  239. ->indexBy('ticket_id')
  240. ->asArray()
  241. ->all();
  242. return $result;
  243. }
  244. /**
  245. * Des:
  246. * Name: getTicketAndLineByIDs
  247. * @param $ticketIds array
  248. * @return array
  249. * @author 倪宗锋
  250. */
  251. public function getTicketAndLineByIDs($ticketIds)
  252. {
  253. $where = [
  254. 'and',
  255. ['in', 'ticket_id', $ticketIds],
  256. ['=', 'b.delete_flag', 0]
  257. ];
  258. $result = self::find()->select(['a.start_res_name', 'a.start_res_id', new Expression("GROUP_CONCAT(CONCAT(ticket_id,'|',a.end_res_id,'|',a.end_res_name,'|',b.pro_cate_id) separator '||') FxTicket")])
  259. ->from(self::tableName() . ' as a')
  260. ->innerJoin(ProdCategory::tableName() . ' b', 'a.ticket_id = b.bus_ticket_id')
  261. ->where($where)
  262. ->groupBy('a.start_res_id')
  263. ->indexBy('start_res_id')
  264. ->asArray()
  265. ->all();
  266. if (empty($result)) {
  267. return [];
  268. }
  269. foreach ($result as $key => $val) {//拼接成以起始站为中心的数组
  270. $result[$key]['end_station_list'] = [];
  271. $ticketArr = explode('||', $val['FxTicket']);
  272. foreach ($ticketArr as $val1) {
  273. $theArr = explode('|', $val1);
  274. if (count($theArr) == 4) {
  275. $theArr1['ticket_id'] = $theArr[0];
  276. $theArr1['end_res_id'] = $theArr[1];
  277. $theArr1['end_res_name'] = $theArr[2];
  278. $theArr1['pro_cate_id'] = $theArr[3];
  279. $result[$key]['end_station_list'][$theArr[0]] = $theArr1;
  280. }
  281. }
  282. unset($result[$key]['FxTicket']);
  283. }
  284. return $result;
  285. }
  286. }