IndexForm.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace app\modules\client\models\v1\integralAppreciation;
  3. use app\models\Goods;
  4. use app\models\IntegralAppreciationGoods;
  5. use app\models\IntegralAppreciationPool;
  6. use app\models\IntegralAppreciationPoolSub;
  7. use app\models\IntegralAppreciationUser;
  8. use app\models\IntegralAppreciationUserIntegralLog;
  9. use app\models\Option;
  10. class IndexForm extends \yii\base\Model
  11. {
  12. public $store_id;
  13. public $user;
  14. public $type;
  15. //近一月
  16. const TYPE_MONTH = 0;
  17. //近半年
  18. const TYPE_HALF_YEAR = 1;
  19. //近一年
  20. const TYPE_YEAR = 2;
  21. //近三年
  22. const TYPE_THREE_YEAR = 3;
  23. public static $typeArray = [
  24. self::TYPE_MONTH => '近一月',
  25. self::TYPE_HALF_YEAR => '近半年',
  26. self::TYPE_YEAR => '近一年',
  27. self::TYPE_THREE_YEAR => '近三年',
  28. ];
  29. public function rules()
  30. {
  31. return [
  32. [['store_id', 'type'], 'integer'],
  33. [['type'], 'in', 'range' => [
  34. self::TYPE_MONTH,
  35. self::TYPE_HALF_YEAR,
  36. self::TYPE_YEAR,
  37. self::TYPE_THREE_YEAR,
  38. ]],
  39. ];
  40. }
  41. //首页数据
  42. public function index() {
  43. try {
  44. $user = $this->user;
  45. $store_id = $this->store_id;
  46. $type = $this->type;
  47. $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
  48. $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
  49. //展示增值积分
  50. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  51. $pool = IntegralAppreciationPool::findOne(['store_id' => $store_id]);
  52. $query = IntegralAppreciationPoolSub::find()->where([
  53. 'store_id' => $store_id
  54. ])->select([
  55. 'month' => new \yii\db\Expression("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y/%m/%d')"),
  56. 'integral_price' => new \yii\db\Expression('ROUND(max(after_integral_price), 2)')
  57. ]);
  58. switch ($type) {
  59. case self::TYPE_HALF_YEAR:
  60. $start_time = strtotime('-6 month');
  61. break;
  62. case self::TYPE_YEAR:
  63. $start_time = strtotime('-1 year');
  64. break;
  65. case self::TYPE_THREE_YEAR:
  66. $start_time = strtotime('-3 year');
  67. break;
  68. default:
  69. $start_time = strtotime('-1 month');
  70. break;
  71. }
  72. $end_time = time();
  73. $query->andWhere(['>=', 'created_at', $start_time]);
  74. $query->andWhere(['<=', 'created_at', $end_time]);
  75. $dateData_ = $query->groupBy('month')->orderBy('id ASC')->asArray()->all();
  76. $dateData = [
  77. 'categories' => array_column($dateData_, 'month'),
  78. 'series' => [[
  79. 'name' => '价格',
  80. 'data' => array_column($dateData_, 'integral_price'),
  81. ]]
  82. ];
  83. $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  84. $data = [
  85. 'user_info' => [
  86. 'avatar' => $user->avatar_url,
  87. 'nickname' => $user->nickname,
  88. ],
  89. 'integral' => $integralAppreciationUser->integral,
  90. 'price_change_log' => $dateData,
  91. 'today_integral_price' => $pool->integral_price ?? ($integral_appreciation_setting['integral_init_price'] ?: 0),
  92. 'today' => date('Y/m/d'),
  93. 'type' => self::$typeArray,
  94. 'integral_project_name' => $integral_appreciation_setting['integral_project_name'] ?: '增值积分中心',
  95. 'integral_bg' => $integral_appreciation_setting['integral_bg'] ?: $baseUrl . '/web/v1/statics/clientImg/integral_appreciation/integral_bg.png',
  96. 'integral_custom_name' => $integral_appreciation_setting['integral_custom_name'] ?: '增值积分',
  97. 'integral_appreciation_setting'=>$integral_appreciation_setting
  98. ];
  99. return [
  100. 'code' => 0,
  101. 'msg' => '',
  102. 'data' => $data
  103. ];
  104. } catch (\Exception $e) {
  105. return [
  106. 'code' => 1,
  107. 'msg' => $e->getMessage()
  108. ];
  109. }
  110. }
  111. //商品列表
  112. public function goodsList() {
  113. $store_id = $this->store_id;
  114. $query = IntegralAppreciationGoods::find()->alias('ig')
  115. ->leftJoin(['g' => Goods::tableName()], 'ig.goods_id = g.id')
  116. ->where(['ig.is_delete' => 0, 'g.is_delete' => 0, 'ig.store_id' => $store_id]);
  117. $query->orderBy('ig.id DESC')
  118. ->select('ig.id, ig.goods_id, g.name, g.cover_pic, g.price, ig.reflux_amount');
  119. $list = pagination_make($query);
  120. return [
  121. 'code' => 0,
  122. 'msg' => '',
  123. 'data' => $list
  124. ];
  125. }
  126. }