Statistics.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\db\Query;
  6. use yii\helpers\ArrayHelper;
  7. /**
  8. * @property array $subscriptions
  9. * */
  10. class Statistics extends Model
  11. {
  12. public $month;
  13. private $colors = [];
  14. public function init()
  15. {
  16. parent::init();
  17. foreach(Category::find()->all() as $category){
  18. $this->colors[$category->id] = $this->getRandomColor();
  19. }
  20. }
  21. public function rules()
  22. {
  23. return [
  24. [['month'], 'date', 'format' => 'php:Y.m'],
  25. [['month'], 'default', 'value' => date('Y.m', time())],
  26. ];
  27. }
  28. public function attributeLabels()
  29. {
  30. return [
  31. 'month' => Yii::t('app', 'Month'),
  32. ];
  33. }
  34. public function searchUsers($params = []){
  35. $this->load($params);
  36. if(!$this->validate()){
  37. return [];
  38. }
  39. $select = "DAY(from_unixtime(created_at)) as day, COUNT(*) as count ";
  40. $data = (new Query())->select($select)
  41. ->from('user')
  42. ->andWhere(['month(FROM_UNIXTIME(created_at))' => explode('.', $this->month)[1]])
  43. ->andWhere(['year(FROM_UNIXTIME(created_at))' => explode('.', $this->month)[0]])
  44. ->groupBy('day')
  45. ->all();
  46. $countDayInMonth = cal_days_in_month(CAL_GREGORIAN, explode('.', $this->month)[1],explode('.', $this->month)[0]);
  47. $data = fill(ArrayHelper::map($data,'day','count'), $countDayInMonth);
  48. return $data;
  49. }
  50. public function searchAuctions($params = []){
  51. $this->load($params);
  52. if(!$this->validate()){
  53. return [];
  54. }
  55. $data = [];
  56. foreach(Auctions::statusNames() as $i=>$label){
  57. $data[] = [
  58. 'data' => $this->selectAuctions($i),
  59. 'label' => $label,
  60. 'borderColor' => $this->getRandomColor(),
  61. 'pointBackgroundColor' => $this->getRandomColor(),
  62. 'pointBorderColor' => "#fff",
  63. 'pointHoverBackgroundColor' => "#fff",
  64. 'pointHoverBorderColor' => $this->getRandomColor(),
  65. ];
  66. }
  67. return $data;
  68. }
  69. public function searchCategory($params = []){
  70. $this->load($params);
  71. if(!$this->validate()){
  72. return [];
  73. }
  74. $data = [];
  75. $categories = Category::find()
  76. ->leftJoin('lots', '`lots`.`category_id` = `category`.`id`')
  77. ->where('lots.id is not null')
  78. ->groupBy(['category_id'])
  79. ->all();
  80. foreach ($categories as $category){
  81. $data['data'][] = $this->selectCategory($category->id);
  82. $data['labels'][] = $category->name;
  83. $data['backgroundColor'][] = $this->colors[$category->id];
  84. }
  85. return $data;
  86. }
  87. public function getSubscriptions(){
  88. $categories = Category::find()
  89. ->leftJoin('subscriptions', '`subscriptions`.`category_id` = `category`.`id`')
  90. ->where('subscriptions.id is not null')
  91. ->all();
  92. $subscriptions = Subscriptions::find()
  93. ->select(['category_id', 'count' => 'count(user_id)'])
  94. ->asArray()
  95. ->groupBy('category_id')->all();
  96. foreach($subscriptions as $i => $subscription){
  97. $data['data'][] = $subscription['count'];
  98. $data['labels'][] = $categories[$i]->name;
  99. $data['backgroundColor'][] = $this->colors[$categories[$i]->id];
  100. }
  101. return $data;
  102. }
  103. public function userBiddings(){
  104. $bidStatuses = ['Поданные', 'Одобренные', 'Отклоненные'];
  105. $data = [];
  106. $biddingData = Bidding::find()->select(['status', 'count' => 'count(user_id)'])
  107. ->groupBy(['status'])
  108. ->where(['user_id' => Yii::$app->user->id])
  109. ->asArray()
  110. ->all();
  111. foreach ($biddingData as $i => $item) {
  112. $data['data'][] = $item['count'];
  113. $data['labels'][] = $bidStatuses[$item['status']];
  114. $data['backgroundColor'][] = $this->getRandomColor();
  115. }
  116. return $data;
  117. }
  118. public function userMailing()
  119. {
  120. $categories = Category::find()
  121. ->leftJoin('subscriptions', '`subscriptions`.`category_id` = `category`.`id`')
  122. ->where(['user_id' => Yii::$app->user->id])
  123. ->all();
  124. $userMailing = Subscriptions::find()
  125. ->select(['category_id', 'count' => 'count(category_id)'])
  126. ->groupBy(['category_id'])
  127. ->where(['user_id' => Yii::$app->user->id])
  128. ->asArray()
  129. ->all();
  130. foreach($userMailing as $i => $mailing){
  131. $data['data'][] = $mailing['count'];
  132. $data['labels'][] = $categories[$i]->name;
  133. $data['backgroundColor'][] = $this->colors[$categories[$i]->id];
  134. }
  135. return $data;
  136. }
  137. /* public function searchUserAuctions(){
  138. $aucStatuses = ['Все','Выигранные','Отклоненные'];
  139. $data = [];
  140. $auctions = Auctions::find()
  141. ->where(['user_id' => Yii::$app->user->id])
  142. ->asArray()
  143. ->all();
  144. return $data;
  145. }*/
  146. private function selectAuctions($type){
  147. $data = [];
  148. $select = "DAY(date_start) as day, COUNT(*) as count ";
  149. $data = (new Query())->select($select)
  150. ->from('auctions')
  151. ->andWhere(['month(date_start)' => explode('.', $this->month)[1]])
  152. ->andWhere(['status' => $type])
  153. ->andWhere(['year(date_start)' => explode('.', $this->month)[0]])
  154. ->groupBy('day')
  155. ->all();
  156. $countDayInMonth = cal_days_in_month(CAL_GREGORIAN, explode('.', $this->month)[1],explode('.', $this->month)[0]);
  157. $data = fill(ArrayHelper::map($data,'day','count'), $countDayInMonth);
  158. return $data;
  159. }
  160. private function selectCategory($type){
  161. $data = [];
  162. $select = "COUNT(*) as count ";
  163. $data = (new Query())->select($select)
  164. ->from('auctions')
  165. ->leftJoin('lots', '`lots`.`id` = `auctions`.`lot_id`')
  166. ->leftJoin('category', '`category`.`id` = `lots`.`category_id`')
  167. ->andWhere(['month(date_start)' => explode('.', $this->month)[1]])
  168. ->andWhere(['lots.category_id' => $type])
  169. ->andWhere('category.id is not null')
  170. ->andWhere(['year(date_start)' => explode('.', $this->month)[0]])
  171. ->count();
  172. return $data;
  173. }
  174. private function getRandomColor(){
  175. $letters = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'];
  176. $color = '#';
  177. for ($i = 0; $i < 6; $i++) {
  178. $color .= $letters[random_int(0, 15)];
  179. }
  180. return $color;
  181. }
  182. }