Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

267 linhas
7.2 KiB

  1. <?php
  2. /**
  3. * 数据库表类 order_info
  4. * ============================================================================
  5. * * 版权所有 蜘蛛出行 * *
  6. * 网站地址: http://www.zhizhuchuxing.com
  7. * ----------------------------------------------------------------------------
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
  9. * 使用;不允许对程序代码以任何形式任何目的的再发布。
  10. * ============================================================================
  11. * Author By: 倪宗锋
  12. * PhpStorm LoginController.php
  13. * Create By 2017/06/15 11:02 $
  14. */
  15. namespace common\models;
  16. use yii\db\ActiveRecord;
  17. use yii\db\Exception;
  18. use yii\db\Expression;
  19. /**
  20. * 数据库表类 order_info.
  21. * @property integer $id
  22. * @property integer $order_id
  23. * @property integer $prod_id
  24. * @property integer $count
  25. * @property string $unit_price
  26. * @property string $total_price
  27. * @property integer $delete_flag
  28. * @property integer $commission
  29. */
  30. class OrderInfo extends ActiveRecord
  31. {
  32. /**
  33. * @inheritdoc
  34. */
  35. public static function tableName()
  36. {
  37. return 'order_info';
  38. }
  39. /**
  40. * @inheritdoc
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['order_id', 'prod_id', 'count', 'delete_flag'], 'integer'],
  46. [['unit_price', 'total_price', 'commission'], 'number'],
  47. [['prod_name'], 'string', 'max' => 255],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'order_id' => 'Order ID',
  58. 'prod_id' => 'Prod ID',
  59. 'count' => 'Count',
  60. 'unit_price' => 'Unit Price',
  61. 'total_price' => 'Total Price',
  62. 'delete_flag' => 'Delete Flag',
  63. 'commission' => 'Commission',
  64. 'prod_name' => 'Prod Name',
  65. ];
  66. }
  67. /**
  68. * Des:获取订单佣金
  69. * Name: getOrderCommission
  70. * @param $orderId
  71. * @return int
  72. * @author 倪宗锋
  73. */
  74. public function getOrderCommission($orderId)
  75. {
  76. $select = [
  77. new Expression("IFNULL(SUM(b.commission*a.count),0) 'commission'"),
  78. 'order_id'
  79. ];
  80. try {
  81. $result = self::find()->select($select)
  82. ->from(self::tableName() . ' as a')
  83. ->innerJoin(ProdMain::tableName() . ' as b', 'a.prod_id = b.prod_id')
  84. ->where(['=', 'a.order_id', $orderId])
  85. ->asArray()
  86. ->one();
  87. } catch (Exception $e) {
  88. $result['commission'] = 0;
  89. }
  90. return $result['commission'];
  91. }
  92. /**
  93. * Function Description:插入子订单
  94. * Function Name: insertInfo
  95. * @param $prod_arr array
  96. * @param $oderId
  97. * @param $category_id
  98. *
  99. * @return bool
  100. *
  101. * @author 娄梦宁
  102. */
  103. public function insertInfo($prod_arr, $oderId, $category)
  104. {
  105. if ($category['category_id'] == 2 && $category['sign'] || $category['category_id'] == 4) {
  106. $sql = "INSERT into order_info(order_id,prod_name, total_price, count, unit_price, prod_id,commission)
  107. SELECT {$oderId},prod_name, {$prod_arr['prod_price']}*{$prod_arr['prod_count']} price,
  108. {$prod_arr['prod_count']},{$prod_arr['prod_price']},prod_id,commission
  109. from prod_main
  110. where prod_id ={$prod_arr['prod_id']}";
  111. } else {
  112. $sql = "INSERT into order_info(order_id,prod_name, total_price, count, unit_price, prod_id,commission)
  113. SELECT {$oderId},prod_name, prod_price*{$prod_arr['prod_count']} price,
  114. {$prod_arr['prod_count']},prod_price,prod_id,commission
  115. from prod_main
  116. where prod_id ={$prod_arr['prod_id']}";
  117. }
  118. $res = $this->getDb()->createCommand($sql)->execute();
  119. if (!$res) {
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * Des:获取订单子产品数组
  126. * Name: getProdArrayByOrderId
  127. * @param $orderId
  128. * @return array
  129. * @author 倪宗锋
  130. */
  131. public function getProdArrayByOrderId($orderId)
  132. {
  133. $where = [
  134. 'and',
  135. ['=', 'order_id', $orderId],
  136. ['=', 'delete_flag', 0]
  137. ];
  138. $select = [
  139. 'order_id',
  140. 'prod_id',
  141. 'count',
  142. 'unit_price',
  143. 'total_price',
  144. 'commission',
  145. 'prod_name',
  146. ];
  147. $getProd = self::find()->select($select)
  148. ->where($where)
  149. ->asArray()
  150. ->all();
  151. if (empty($getProd[0])) {
  152. return [];
  153. }
  154. return $getProd;
  155. }
  156. /**
  157. * Des:获取订单子产品数组
  158. * Name: getProdArrayByOrderId
  159. * @param $orderIds
  160. * @return array
  161. * @author 倪宗锋
  162. */
  163. public function getProdArrForPay($orderIds)
  164. {
  165. $where = [
  166. 'and',
  167. ['in', 'order_id', $orderIds],
  168. ['=', 'delete_flag', 0]
  169. ];
  170. $select = [
  171. 'cnt' => new Expression("SUM(count)"),
  172. 'unit_price' => new Expression("ROUND(SUM(total_price)/SUM(count),2)"),
  173. 'prod_name',
  174. ];
  175. $getProd = self::find()->select($select)
  176. ->where($where)
  177. ->asArray()
  178. ->groupBy('prod_name')
  179. ->all();
  180. if (empty($getProd[0])) {
  181. return [];
  182. }
  183. return $getProd;
  184. }
  185. /**
  186. * Function Description:处理价格
  187. * Function Name: dealFloat
  188. * @param $float
  189. *
  190. * @return float
  191. *
  192. * @author LUOCJ
  193. */
  194. public function dealFloat($float)
  195. {
  196. $int = floor($float);
  197. if ($float - $int == 0) {
  198. $float = $int;
  199. }
  200. return $float;
  201. }
  202. /**
  203. * Function Description:获取订单车票信息
  204. * Function Name: getOrderPriceDetail
  205. * @param $order_id
  206. *
  207. * @return array|ActiveRecord[]
  208. *
  209. * @author LUOCJ
  210. */
  211. public function getOrderPriceDetail($order_id)
  212. {
  213. //查询订单乘车人数
  214. $where = ['and'];
  215. $where[] = ['=', 'order_id', $order_id];
  216. $where[] = ['=', 'delete_flag', 0];
  217. $info = self::find()->select('count,unit_price')->from(self::tableName())->where($where)->groupBy('prod_id')->asArray()->all();
  218. if (count($info) > 0) {
  219. foreach ($info as &$v) {
  220. $v['unit_price'] = $this->dealFloat($v['unit_price']);
  221. }
  222. }
  223. return $info;
  224. }
  225. /**
  226. * Des:获取产品数组
  227. * Name: getFreeWalkProdArr
  228. * @param $order_id
  229. * @return array
  230. * @author 倪宗锋
  231. */
  232. public function geProdArr($order_id)
  233. {
  234. $select = [
  235. 'prod_id' => 'b.bus_id',
  236. 'prod_name' => 'b.prod_name',
  237. 'prod_num' => 'a.count'
  238. ];
  239. $where = ['=', 'a.order_id', $order_id];
  240. $getProdArr = self::find()->select($select)
  241. ->from(self::tableName() . ' a')
  242. ->innerJoin(ProdMain::tableName() . ' b', 'a.prod_id = b.prod_id')
  243. ->where($where)
  244. ->asArray(true)
  245. ->all();
  246. return $getProdArr;
  247. }
  248. }