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.
 
 
 
 
 
 

298 lines
8.3 KiB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord;
  5. use yii\db\Expression;
  6. /**
  7. * This is the model class for table "prod_main".
  8. *
  9. * @property integer $prod_id
  10. * @property integer $prod_cate_id
  11. * @property string $prod_name
  12. * @property string $prod_price
  13. * @property string $create_user
  14. * @property string $create_time
  15. * @property string $update_time
  16. * @property integer $delete_flag
  17. * @property string $update_user
  18. * @property string $bus_id
  19. */
  20. class ProdMain extends ActiveRecord
  21. {
  22. /**
  23. * @inheritdoc
  24. */
  25. public static function tableName()
  26. {
  27. return 'prod_main';
  28. }
  29. /**
  30. * @inheritdoc
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['prod_cate_id', 'prod_price'], 'required'],
  36. [['prod_cate_id', 'delete_flag'], 'integer'],
  37. [['prod_price', 'commission'], 'number'],
  38. [['create_time', 'update_time'], 'safe'],
  39. [['prod_name'], 'string', 'max' => 100],
  40. [['create_user', 'update_user'], 'string', 'max' => 50],
  41. [['bus_id'], 'string', 'max' => 255],
  42. ];
  43. }
  44. /**
  45. * @inheritdoc
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'prod_id' => 'Prod ID',
  51. 'prod_cate_id' => 'Prod Cate ID',
  52. 'prod_name' => 'Prod Name',
  53. 'prod_price' => 'Prod Price',
  54. 'create_user' => 'Create User',
  55. 'create_time' => 'Create Time',
  56. 'update_time' => 'Update Time',
  57. 'delete_flag' => 'Delete Flag',
  58. 'update_user' => 'Update User',
  59. 'bus_id' => 'Bus ID',
  60. ];
  61. }
  62. /**
  63. * Function Description:产品详情获取子产品数组
  64. * Function Name: getProdArr
  65. * @param $pro_cate_id
  66. *
  67. * @return array|null|\yii\db\ActiveRecord
  68. *
  69. * @author 娄梦宁
  70. */
  71. public function getProdArr($pro_cate_id)
  72. {
  73. $result = self::find()->select(['prod_name', 'prod_price', 'prod_id', 'bus_id', '(select 100) as prod_count', 'commission'])
  74. ->from(self::tableName())
  75. ->where(['and', ['=', 'prod_cate_id', $pro_cate_id], ['=', 'delete_flag', 0]])
  76. ->asArray()
  77. ->all();
  78. return $result;
  79. }
  80. /*
  81. * Des:设置产品佣金 (根据prod_cate_id)
  82. * Name: setProdCommissionByCateId
  83. * @param $param
  84. * @return int
  85. * @author 付泓程
  86. */
  87. public function setProdCommissionByCateId($param)
  88. {
  89. $commission = $param['commission'];
  90. $prod_cate_id = $param['prod_cate_id'];
  91. $count = $this->updateAll(['commission' => $commission], ['and', ['=', 'prod_cate_id', $prod_cate_id], ['=', 'delete_flag', '0']]);
  92. return $count;
  93. }
  94. /*
  95. * Des:设置产品佣金 (根据prod_id)
  96. * Name: setProdCommissionByProdId
  97. * @param $param
  98. * @return int
  99. * @author 付泓程
  100. */
  101. public function setProdCommissionByProdId($param)
  102. {
  103. $commission = $param['commission'];
  104. $prod_id = $param['prod_id'];
  105. $count = $this->updateAll(['commission' => $commission], ['and', ['=', 'prod_id', $prod_id], ['=', 'delete_flag', '0']]);
  106. return $count;
  107. }
  108. /**
  109. * Function Description:子产品表添加(只能添加非巴士产品)
  110. * Function Name: addProdMain
  111. * @param $param
  112. *
  113. * @return bool
  114. *
  115. * @author 付泓程
  116. */
  117. public function addProdMain($param)
  118. {
  119. $transaction = Yii::$app->db->beginTransaction();
  120. if ($param['category_id'] != 1) {
  121. foreach ($param['price_type'] as $val) {
  122. $values = [
  123. 'prod_cate_id' => $param['prod_cate_id'],
  124. 'prod_name' => $val['price_type_name'],
  125. 'prod_price' => $val['price_type_price'],
  126. 'create_time' => date('Y-m-d H:i:s'),
  127. ];
  128. $prod_main = clone $this;
  129. $prod_main->attributes = $values;
  130. $res = $prod_main->insert();
  131. if (!$res) {
  132. $transaction->rollBack();
  133. return false;
  134. }
  135. }
  136. }
  137. $transaction->commit();
  138. return true;
  139. }
  140. /*
  141. * 获取产品的子产品数组
  142. */
  143. public function getIdArr($pro_cate_id)
  144. {
  145. $arr = self::find()->select('prod_id')->from(self::tableName())->where(['prod_cate_id' => $pro_cate_id])->indexBy('prod_id')->asArray()->all();
  146. return $arr;
  147. }
  148. /*
  149. * 修改子产品表
  150. */
  151. public function uptCmsProd($prod_arr, $pro_cate_id)
  152. {
  153. if ($prod_arr['prod_id'] == 0) {
  154. $value = [
  155. 'prod_name' => $prod_arr['price_type_name'],
  156. 'prod_price' => $prod_arr['price_type_price'],
  157. 'prod_cate_id' => $pro_cate_id,
  158. 'create_time' => date('Y-m-d H:i:s')
  159. ];
  160. $this->attributes = $value;
  161. $res = $this->insert();
  162. if (!$res) {
  163. return false;
  164. }
  165. } else {
  166. $count = self::updateAll(['prod_name' => $prod_arr['price_type_name'], 'prod_price' => $prod_arr['price_type_price']], ['=', 'prod_id', $prod_arr['prod_id']]);
  167. if ($count === false) {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. public function delCount($prod_id)
  174. {
  175. $count = self::updateAll(['delete_flag' => 1], ['=', 'prod_id', $prod_id]);
  176. return $count;
  177. }
  178. /**
  179. * Des:获取订单总价格
  180. * Name: getTotalMoney
  181. * @param $params array 多为数组 产品ID和数量
  182. * @return array
  183. * @author 倪宗锋
  184. */
  185. public function getTotalMoney($params)
  186. {
  187. $prod_id = [];
  188. $sqlCase = '';
  189. foreach ($params as $val) {
  190. $sqlCase .= " WHEN {$val['prod_id']} THEN prod_price*{$val['prod_count']} ";
  191. $prod_id[] = $val['prod_id'];
  192. }
  193. $prodId = implode(',', $prod_id);
  194. $sql = "SELECT
  195. SUM(CASE prod_id
  196. {$sqlCase}
  197. ELSE 0
  198. END)
  199. from prod_main
  200. where prod_id in ({$prodId})
  201. ";
  202. $result = Yii::$app->db->createCommand($sql)->queryScalar();
  203. return $result;
  204. }
  205. /**
  206. * Function Description:获取票种id
  207. * Function Name: getTicketId
  208. * @param $pro_cate_id
  209. *
  210. * @return array|null|\yii\db\ActiveRecord
  211. *
  212. * @author 娄梦宁
  213. */
  214. public function getTicketId($pro_cate_id)
  215. {
  216. $where = ['and'];
  217. $where[] = ['=', 'prod_cate_id', $pro_cate_id];
  218. $where[] = ['=', 'delete_flag', 0];
  219. $result = self::find()->select(['bus_id', 'prod_price', 'prod_id', 'prod_name'])
  220. ->from(self::tableName())
  221. ->where($where)
  222. ->orderBy('prod_id')
  223. ->asArray()
  224. ->all();
  225. return $result;
  226. }
  227. /**
  228. * Function Description:查询起始站点
  229. * Function Name: getStation
  230. * @param $prod_id
  231. *
  232. * @return mixed
  233. *
  234. * @author 娄梦宁
  235. */
  236. public function getStation($prod_id)
  237. {
  238. $result = self::find()->select(["concat(start_res_name,' - ',end_res_name) as station_str"])
  239. ->from(self::tableName() . ' as a')
  240. ->leftJoin('bus_ticket as b', 'a.bus_id=b.ticket_id')
  241. ->where(['a.prod_id' => $prod_id])
  242. ->asArray()
  243. ->one();
  244. return $result['station_str'];
  245. }
  246. /**
  247. * Des:获取巴士自由行cs产品标志、名称及数量
  248. * Name: getProdForCsFreeWalk
  249. * @param $params
  250. * @return array
  251. * @author 倪宗锋
  252. */
  253. public function getProdForCsFreeWalk($params)
  254. {
  255. if (count($params) == 0) {
  256. return [];
  257. }
  258. $prod_id = [];
  259. $sqlCase = 'CASE prod_id';
  260. foreach ($params as $val) {
  261. $sqlCase .= " WHEN {$val['prod_id']} THEN {$val['prod_count']} ";
  262. $prod_id[] = $val['prod_id'];
  263. }
  264. $sqlCase .= "ELSE 0 END";
  265. $select = [
  266. 'prod_id' => 'bus_id',
  267. 'prod_name',
  268. 'prod_num' => new Expression($sqlCase)
  269. ];
  270. $where = ['in', 'prod_id', $prod_id];
  271. $getResult = self::find()->select($select)
  272. ->where($where)
  273. ->asArray()
  274. ->all();
  275. if (empty($getResult['0'])) {
  276. return [];
  277. }
  278. return $getResult;
  279. }
  280. }