AnalysisDataForm.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Cart;
  9. use app\models\CommonOperation;
  10. use app\models\Goods;
  11. use app\models\Mch;
  12. use app\models\Order;
  13. use app\models\OrderDetail;
  14. use app\models\User;
  15. use app\models\UserViews;
  16. use yii\base\Model;
  17. use yii\helpers\Json;
  18. class AnalysisDataForm extends Model
  19. {
  20. public $store_id;
  21. public $user_id;
  22. public $mch_id;
  23. /**
  24. * @var CommonOperation $model
  25. */
  26. public $model;
  27. public $text;
  28. public $start_time;
  29. public $end_time;
  30. /**
  31. * @return array
  32. */
  33. public function getData()
  34. {
  35. // 当前时间 2021-05-14 09:07:38
  36. $current_time = time();
  37. // 截止到当前时间的一周 2021-05-07 09:07:38~2021-05-14 09:07:38
  38. $current_before_week_time = strtotime("-1 week", $current_time);
  39. // 再往前推一周
  40. $far_before_week_time = strtotime("-1 week", $current_before_week_time);
  41. // 截止到当前时间到一天
  42. $current_before_day_time = strtotime("-1 day", $current_time);
  43. // 再往前推一天
  44. $far_before_day_time = strtotime("-1 day", $current_before_day_time);
  45. \Yii::error([
  46. 'current_time ++++++ ' . date('Y-m-d H:i:s', $current_time),
  47. 'current_before_week_time ++++++ ' . date('Y-m-d H:i:s', $current_before_week_time),
  48. 'far_before_week_time ++++++ ' . date('Y-m-d H:i:s', $far_before_week_time),
  49. 'current_before_day_time ++++++ ' . date('Y-m-d H:i:s', $current_before_day_time),
  50. 'far_before_day_time ++++++ ' . date('Y-m-d H:i:s', $far_before_day_time)
  51. ]);
  52. /***** 销售额 START *****/
  53. // 所有订单的总销售额
  54. $total_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->sum('pay_price');
  55. // 截止到当前时间的本周销售额 开始截止时间为:$current_before_week_time ~ $current_time
  56. $this_week_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $current_before_week_time])->andWhere(['<=', 'pay_time', $current_time])->sum('pay_price');
  57. // 截止到当前时间前一周时间点的上周销售额 开始截止时间为:$far_before_week_time ~ $current_before_week_time
  58. $before_week_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $far_before_week_time])->andWhere(['<=', 'pay_time', $current_before_week_time])->sum('pay_price');
  59. if ($before_week_sell_money) {
  60. // 销售额周环比(与上一周相比)当前一周:$current_before_week_time ~ $current_time,上一周:$far_before_week_time ~ $current_before_week_time
  61. $this_week_sell_percentage = ($this_week_sell_money - $before_week_sell_money) / $before_week_sell_money;
  62. // 销售额周环比百分数
  63. $this_week_sell_basis_percent = sprintf("%01.2f", $this_week_sell_percentage * 100).'%';
  64. } else {
  65. $this_week_sell_basis_percent = '-';
  66. }
  67. // 日销售额 当天00:00:00 ~ 当前时间
  68. $this_time_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', strtotime(date('Y-m-d 00:00:00', time()))])->andWhere(['<=', 'pay_time', $current_time])->sum('pay_price');
  69. // 截止到当前时间的本天销售额 开始截止时间为:$current_before_day_time ~ $current_time
  70. $this_day_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $current_before_day_time])->andWhere(['<=', 'pay_time', $current_time])->sum('pay_price');
  71. // 截止到当前时间前一天时间点的上天销售额 开始截止时间为:$far_before_day_time ~ $current_before_day_time
  72. $before_day_sell_money = Order::find()->where(['store_id' => $this->store_id, 'is_pay' => Order::IS_PAY_TRUE, 'mch_id' => $this->mch_id])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $far_before_day_time])->andWhere(['<=', 'pay_time', $current_before_day_time])->sum('pay_price');
  73. if ($before_day_sell_money) {
  74. // 销售额日环比(与上一天相比)当前一天:$current_before_day_time ~ $current_time,上一周:$far_before_day_time ~ $current_before_day_time
  75. $this_day_sell_percentage = ($this_day_sell_money - $before_day_sell_money) / $before_day_sell_money;
  76. // 销售额日环比百分数
  77. $this_day_sell_basis_percent = sprintf("%01.2f", $this_day_sell_percentage * 100).'%';
  78. } else {
  79. $this_day_sell_basis_percent = '-';
  80. }
  81. /***** 销售额 END *****/
  82. /***** 商品总数 START *****/
  83. // 截止到目前商品的总数量(已上架和未删除)
  84. $goods_count = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->count(1);
  85. // 截止到当前时间的本周商品数量 开始截止时间为:$current_before_week_time ~ $current_time
  86. $this_week_sell_goods = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->andWhere(['>=', 'created_at', $current_before_week_time])->andWhere(['<=', 'created_at', $current_time])->count(1);
  87. // 截止到当前时间前一周时间点的上周商品数量 开始截止时间为:$far_before_week_time ~ $current_before_week_time
  88. $before_week_sell_goods = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->andWhere(['>=', 'created_at', $far_before_week_time])->andWhere(['<=', 'created_at', $current_before_week_time])->count(1);
  89. if ($before_week_sell_goods) {
  90. // 商品数量周环比(与上一周相比)当前一周:$current_before_week_time ~ $current_time,上一周:$far_before_week_time ~ $current_before_week_time
  91. $this_week_goods_percentage = ($this_week_sell_goods - $before_week_sell_goods) / $before_week_sell_goods;
  92. // 销售额周环比百分数
  93. $this_week_goods_basis_percent = sprintf("%01.2f", $this_week_goods_percentage * 100).'%';
  94. } else {
  95. $this_week_goods_basis_percent = '-';
  96. }
  97. // 日新增 当天00:00:00 ~ 当前时间
  98. $this_time_goods = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->andWhere(['>=', 'created_at', strtotime(date('Y-m-d 00:00:00', time()))])->andWhere(['<=', 'created_at', $current_time])->count(1);
  99. // 截止到当前时间的本天商品数量 开始截止时间为:$current_before_day_time ~ $current_time
  100. $this_day_sell_goods = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->andWhere(['>=', 'created_at', $current_before_day_time])->andWhere(['<=', 'created_at', $current_time])->count(1);
  101. // 截止到当前时间前一天时间点的上天商品数量 开始截止时间为:$far_before_day_time ~ $current_before_day_time
  102. $before_day_sell_goods = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL, 'is_delete' => 0])->andWhere(['>=', 'created_at', $far_before_day_time])->andWhere(['<=', 'created_at', $current_before_day_time])->count(1);
  103. if ($before_day_sell_goods) {
  104. // 商品数量日环比(与上一天相比)当前一天:$current_before_day_time ~ $current_time,上一周:$far_before_day_time ~ $current_before_day_time
  105. $this_day_goods_percentage = ($this_day_sell_goods - $before_day_sell_goods) / $before_day_sell_goods;
  106. // 销售额日环比百分数
  107. $this_day_goods_basis_percent = sprintf("%01.2f", $this_day_goods_percentage * 100).'%';
  108. } else {
  109. $this_day_goods_basis_percent = '-';
  110. }
  111. /***** 商品总数 END *****/
  112. if (get_mch_id() == 0) {
  113. // 用户总量
  114. $user_total_count = User::find()->where(['store_id' => $this->store_id, 'is_delete' => User::USER_NOT_DELETE])->count(1);
  115. // 用户今日新增
  116. $this_time_user_count = User::find()->where(['store_id' => $this->store_id, 'is_delete' => User::USER_NOT_DELETE])->andWhere(['>=', 'created_at', strtotime(date('Y-m-d 00:00:00', time()))])->andWhere(['<=', 'created_at', $current_time])->count(1);
  117. }
  118. // 订单总数
  119. $order_total_count = Order::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'is_pay' => Order::IS_PAY_TRUE])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->count(1);
  120. // 今日新增
  121. $this_time_order_count = Order::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'is_pay' => Order::IS_PAY_TRUE])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', strtotime(date('Y-m-d 00:00:00', time()))])->andWhere(['<=', 'pay_time', $current_time])->count(1);
  122. // 最近10天的新增数量
  123. $user_day_data = [];
  124. $order_day_data = [];
  125. $mch_order_money = [];
  126. for ($i = 1; $i <= 10; $i++) {
  127. $date_start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-". $i ." day", $current_time)));
  128. $date_end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-". $i ." day", $current_time)));
  129. /***** 商户订单金额走势 *****/
  130. if (get_mch_id() > 0) {
  131. $money = Order::find()->alias('o')->where(['o.is_pay' => 1, 'mch_id' => $this->mch_id])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'o.created_at', $date_start_time])->andWhere(['<=', 'o.created_at', $date_end_time]);
  132. $money->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  133. $day_money = $money->SUM('od.total_price');
  134. if (!$day_money) {
  135. $day_money = '0.00';
  136. }
  137. $mch_order_money[] = [
  138. 'x' => date('Y-m-d', $date_start_time),
  139. 'y' => $day_money
  140. ];
  141. }
  142. /***** 用户和订单总量 START *****/
  143. if (get_mch_id() == 0) {
  144. // 用户
  145. $user_count = User::find()->where(['store_id' => $this->store_id, 'is_delete' => User::USER_NOT_DELETE])->andWhere(['>=', 'created_at', $date_start_time])->andWhere(['<=', 'created_at', $date_end_time])->count(1);
  146. $user_day_data[] = [
  147. 'x' => date('Y-m-d', $date_start_time),
  148. 'y' => $user_count
  149. ];
  150. }
  151. // 订单
  152. $order_count = Order::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'is_pay' => Order::IS_PAY_TRUE])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $date_start_time])->andWhere(['<=', 'pay_time', $date_end_time])->count(1);
  153. $order_day_data[] = [
  154. 'x' => date('Y-m-d', $date_start_time),
  155. 'y' => $order_count
  156. ];
  157. }
  158. /***** 用户和订单总量 END *****/
  159. $result = [
  160. 'code' => 0,
  161. 'msg' => 'success',
  162. 'data' => [
  163. 'sell' => [
  164. 'total_money' => $total_sell_money ? $total_sell_money : 0,
  165. 'today_sell_money' => $this_time_sell_money ? $this_time_sell_money : 0,
  166. 'week_sell_percent' => $this_week_sell_basis_percent,
  167. 'day_sell_percent' => $this_day_sell_basis_percent
  168. ],
  169. 'goods' => [
  170. 'total_goods' => $goods_count,
  171. 'today_goods' => $this_time_goods,
  172. 'week_goods_percent' => $this_week_goods_basis_percent,
  173. 'day_goods_percent' => $this_day_goods_basis_percent
  174. ],
  175. 'user' => [
  176. 'total' => $user_total_count ? $user_total_count : 0,
  177. 'today_user' => $this_time_user_count ? $this_time_user_count : 0,
  178. 'data' => $user_day_data
  179. ],
  180. 'order' => [
  181. 'total' => $order_total_count,
  182. 'today_order' => $this_time_order_count,
  183. 'data' => $order_day_data
  184. ],
  185. 'mch_order_money' => [
  186. 'total_money' => $total_sell_money ? $total_sell_money : 0,
  187. 'today_sell_money' => $this_time_sell_money ? $this_time_sell_money : 0,
  188. 'mch_order_money' => $mch_order_money
  189. ]
  190. ]
  191. ];
  192. return $result;
  193. }
  194. public function getOrderData() {
  195. $start_time = $this->start_time;
  196. $end_time = $this->end_time;
  197. $condition = false; // 天
  198. if ((strtotime($end_time) - strtotime($start_time)) < 24 * 3600) {
  199. $condition = true; // 小时
  200. }
  201. $order_day_data = [];
  202. if ($condition) {
  203. for ($i = 1; $i <= 24; $i++) {
  204. $date_start_time = strtotime(date('Y-m-d H:00:00', strtotime("-". $i ." hour")));
  205. $date_end_time = strtotime(date('Y-m-d H:59:59', strtotime("-". $i ." hour")));
  206. // 订单
  207. $order_count = Order::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'is_pay' => Order::IS_PAY_TRUE])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $date_start_time])->andWhere(['<=', 'pay_time', $date_end_time])->count(1);
  208. $order_day_data[] = [
  209. 'x' => date('Y-m-d H:i:s', $date_start_time) . "-" . date('Y-m-d H:i:s', $date_end_time),
  210. 'y' => $order_count
  211. ];
  212. }
  213. } else {
  214. $date = floor((strtotime($end_time) - strtotime($start_time)) / 86400);
  215. for ($i = 1; $i <= $date; $i++) {
  216. $date_start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-". $i ." day")));
  217. $date_end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-". $i ." day")));
  218. // 订单
  219. $order_count = Order::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'is_pay' => Order::IS_PAY_TRUE])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['>=', 'pay_time', $date_start_time])->andWhere(['<=', 'pay_time', $date_end_time])->count(1);
  220. $order_day_data[] = [
  221. 'x' => date('Y-m-d H:i:s', $date_start_time) . "-" . date('Y-m-d H:i:s', $date_end_time),
  222. 'y' => $order_count
  223. ];
  224. }
  225. }
  226. $list = [];
  227. if (get_mch_id() == 0) {
  228. $o_table = Order::tableName();
  229. $m_table = Mch::tableName();
  230. $sql = "select {$m_table}.name, (SELECT count(*) as count FROM {$o_table} where {$o_table}.mch_id = {$m_table}.id and {$o_table}.store_id = {$this->store_id} and {$o_table}.is_pay = 1 and {$o_table}.trade_status != 3) as count FROM {$m_table} ORDER BY count desc LIMIT 7";
  231. $list = \Yii::$app->db->createCommand($sql)->queryAll();
  232. }
  233. return [
  234. 'code' => 0,
  235. 'msg' => 'success',
  236. 'data' => $order_day_data,
  237. 'shop_list' => $list
  238. ];
  239. }
  240. /**
  241. * 商品销量列表
  242. * @return array
  243. */
  244. public function GoodsSearch()
  245. {
  246. $query = Goods::find()->where(['store_id' => $this->store_id, 'mch_id' => $this->mch_id, 'status' => Goods::STATUS_NORMAL])->select('id, name');
  247. $list = pagination_make($query);
  248. $listArray = $list['list'];
  249. foreach ($listArray as $k => $v) {
  250. // 访客数
  251. $fangke = UserViews::find()->where(['goods_id' => $v['id']]);
  252. $fangke = $fangke->SUM('visitors');
  253. if (!$fangke) {
  254. $fangke = 0;
  255. }
  256. // 浏览量
  257. $views = UserViews::find()->where(['goods_id' => $v['id']]);
  258. $views = $views->SUM('views');
  259. if (!$views) {
  260. $views = 0;
  261. }
  262. // 加购数
  263. $cart = Cart::find()->where(['goods_id' => $v['id'], 'mch_id' => $this->mch_id]);
  264. $cart = $cart->count();
  265. if (!$cart) {
  266. $cart = 0;
  267. }
  268. // 付款人数
  269. $paynum = Order::find()->alias('o')->where(['o.is_pay' => 1, 'mch_id' => $this->mch_id])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  270. $paynum->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  271. $paynum->andWhere(['od.goods_id' => $v['id']]);
  272. $paynum = $paynum->count();
  273. if (!$paynum) {
  274. $paynum = 0;
  275. }
  276. // 付款金额
  277. $jine = Order::find()->alias('o')->where(['o.is_pay' => 1, 'mch_id' => $this->mch_id])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  278. $jine->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  279. $jine->andWhere(['od.goods_id' => $v['id']]);
  280. $jine = $jine->SUM('od.total_price');
  281. if (!$jine) {
  282. $jine = '0.00';
  283. }
  284. $listArray[$k]['cart_data'] = [];
  285. // 近7天加购数
  286. for ($i = 1; $i <= 7; $i++) {
  287. $date_start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-". $i ." day")));
  288. $date_end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-". $i ." day")));
  289. $cart_count = Cart::find()->where(['goods_id' => $v['id'], 'mch_id' => $this->mch_id])->andWhere(['>=', 'created_at', $date_start_time])->andWhere(['<=', 'created_at', $date_end_time])->count(1);
  290. $listArray[$k]['cart_data'][] = [
  291. 'x' => date('Y-m-d H:i:s', $date_start_time) . "-" . date('Y-m-d H:i:s', $date_end_time),
  292. 'y' => $cart_count
  293. ];
  294. }
  295. $listArray[$k]['sell_data'] = [];
  296. // 近7天销量
  297. for ($i = 1; $i <= 7; $i++) {
  298. $date_start_time = strtotime(date('Y-m-d 00:00:00', strtotime("-". $i ." day")));
  299. $date_end_time = strtotime(date('Y-m-d 23:59:59', strtotime("-". $i ." day")));
  300. $query = Order::find()->alias('o')->where(['o.is_pay' => 1, 'mch_id' => $this->mch_id])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  301. $query->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  302. $query->andWhere(['od.goods_id' => $v['id']]);
  303. $order_count = $query->andWhere(['>=', 'o.created_at', $date_start_time])->andWhere(['<=', 'o.created_at', $date_end_time])->count(1);
  304. $listArray[$k]['sell_data'][] = [
  305. 'x' => date('Y-m-d H:i:s', $date_start_time) . "-" . date('Y-m-d H:i:s', $date_end_time),
  306. 'y' => $order_count
  307. ];
  308. }
  309. $listArray[$k]['fangke'] = $fangke;
  310. $listArray[$k]['views'] = $views;
  311. $listArray[$k]['cart'] = $cart;
  312. $listArray[$k]['paynum'] = $paynum;
  313. $listArray[$k]['jine'] = $jine;
  314. }
  315. $list['totalCount'] = count($listArray);
  316. $list['data'] = $listArray;
  317. unset($list['list']);
  318. return [
  319. 'code' => 0,
  320. 'msg' => 'success',
  321. 'data' => $list
  322. ];
  323. }
  324. public function operations() {
  325. $this->model->store_id = $this->store_id;
  326. $this->model->mch_id = $this->mch_id;
  327. $this->model->text = !$this->text ? '{}' : Json::encode($this->text);
  328. if ($this->model->isNewRecord) {
  329. $this->model->created_at = time();
  330. }
  331. if (!$this->model->save()) {
  332. return [
  333. 'code' => 1,
  334. 'msg' => $this->model->errors[0]
  335. ];
  336. } else {
  337. return [
  338. 'code' => 0,
  339. 'msg' => '保存成功'
  340. ];
  341. }
  342. }
  343. }