StoreOrder.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. namespace We7\Table\Site;
  7. class StoreOrder extends \We7Table {
  8. protected $tableName = 'site_store_order';
  9. protected $primaryKey = 'id';
  10. protected $field = array(
  11. 'orderid',
  12. 'goodsid',
  13. 'duration',
  14. 'buyer',
  15. 'buyerid',
  16. 'amount',
  17. 'type',
  18. 'changeprice',
  19. 'createtime',
  20. 'uniacid',
  21. 'endtime',
  22. 'wxapp',
  23. 'is_wish'
  24. );
  25. protected $default = array(
  26. 'orderid' => '',
  27. 'goodsid' => '',
  28. 'duration' => '',
  29. 'buyer' => '',
  30. 'buyerid' => '',
  31. 'amount' => '',
  32. 'type' => '',
  33. 'changeprice' => 0,
  34. 'createtime' => '',
  35. 'uniacid' => '',
  36. 'endtime' => 0,
  37. 'wxapp' => 0,
  38. 'is_wish' => 0
  39. );
  40. public function getAllByGoodsId($goodsid) {
  41. return $this->query->where('goodsid', $goodsid)->getall();
  42. }
  43. public function getStatisticsInfoByDate($starttime, $endtime) {
  44. $total_orders = $this->query->select('COUNT(id)')->where(array('createtime >=' => $starttime, 'createtime <=' => $endtime,))->getcolumn();
  45. $total_amounts = $this->query->select('SUM(amount)')->where(array('createtime >=' => $starttime, 'createtime <=' => $endtime, 'type' => STORE_ORDER_FINISH))->getcolumn();
  46. return array('total_orders' => $total_orders, 'total_amounts' => $total_amounts);
  47. }
  48. public function getQueryJoinGoodsTable($order_type = 0, $goods_type = 0) {
  49. $query = $this->query
  50. ->from($this->tableName, 'a')
  51. ->leftjoin('site_store_goods', 'b')
  52. ->on('a.goodsid', 'b.id');
  53. if ($order_type > 0) {
  54. $query->where('a.type', $order_type);
  55. }
  56. if ($goods_type > 0) {
  57. $query->where('b.type', $goods_type);
  58. }
  59. return $query;
  60. }
  61. public function getUserBuyNumByType($uid, $type) {
  62. $account_all_type = uni_account_type();
  63. $account_all_type_sign = uni_account_type_sign();
  64. foreach($account_all_type as $account_type) {
  65. if ($account_type['type_sign'] == $type) {
  66. $store_type_number = $account_type['store_type_number'];
  67. break;
  68. }
  69. }
  70. $count = $this->getQueryJoinGoodsTable(STORE_ORDER_FINISH, array($store_type_number, STORE_TYPE_ACCOUNT_PACKAGE))
  71. ->select("b.{$type}_num")
  72. ->where('a.buyerid', intval($uid))
  73. ->getall();
  74. if (empty($count)) {
  75. return 0;
  76. } else {
  77. $count = array_sum(array_column($count, "{$type}_num"));
  78. $deleted_account = table('site_store_create_account')->getUserDeleteNum($uid, $account_all_type_sign[$type]['contain_type']);
  79. return max(0, $count - $deleted_account);
  80. }
  81. }
  82. public function getApiOrderByUniacid($uniacid) {
  83. return $this->getQueryJoinGoodsTable(STORE_ORDER_FINISH, STORE_TYPE_API)
  84. ->select('a.duration, b.api_num, b.price')
  85. ->where('a.uniacid', intval($uniacid))
  86. ->getall();
  87. }
  88. public function getUserBuyPackage($uniacid) {
  89. return $this->getQueryJoinGoodsTable(STORE_ORDER_FINISH, STORE_TYPE_PACKAGE)
  90. ->where(function ($query) use ($uniacid) {
  91. $query->where('a.uniacid', $uniacid)->whereor('a.wxapp', $uniacid);
  92. })->getall('module_group');
  93. }
  94. public function searchAccountBuyGoods($uniacid, $type) {
  95. $this->query->from('site_store_goods', 'g')
  96. ->leftjoin('site_store_order', 'r')
  97. ->on(array('g.id' => 'r.goodsid'))
  98. ->where('g.type', $type)
  99. ->where('r.type', STORE_ORDER_FINISH);
  100. if ($type == STORE_TYPE_API) {
  101. $number_list = $this->query->where('r.uniacid', $uniacid)->select('(g.api_num * r.duration) as number')->getall('number');
  102. return array_sum(array_keys($number_list));
  103. } else{
  104. $this->query->where(function ($query) use ($uniacid) {
  105. $query->where('r.uniacid', $uniacid)->whereor('r.wxapp', $uniacid);
  106. });
  107. load()->model('store');
  108. $all_type = store_goods_type_info();
  109. if ($all_type[$type]['group'] == 'module') {
  110. $keyfield = 'module';
  111. } else {
  112. $type_name = array(
  113. STORE_TYPE_PACKAGE => 'module_group',
  114. );
  115. $keyfield = empty($type_name[$type]) ? '' : $type_name[$type];
  116. }
  117. return $this->query->getall($keyfield);
  118. }
  119. }
  120. public function searchWithEndtime() {
  121. $this->query->where('r.endtime >=', time());
  122. return $this;
  123. }
  124. }