Stat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace app\model\store;
  11. use app\model\BaseModel;
  12. use Carbon\Carbon;
  13. use think\facade\Cache;
  14. use think\facade\Db;
  15. use think\facade\Log;
  16. use app\model\system\Stat as SystemStat;
  17. /**
  18. * 统计
  19. * @author Administrator
  20. *
  21. */
  22. class Stat extends BaseModel
  23. {
  24. /**
  25. * 添加店铺统计(按照天统计)
  26. * @param array $data
  27. */
  28. public function addStoreStat($data)
  29. {
  30. Log::write('addStoreStat' . '-' . date('y-m-d H:i:s', time()) . '-' . json_encode($data));
  31. $site_id = $data[ 'site_id' ];
  32. $store_id = $data[ 'store_id' ] ?? 0;
  33. $carbon = Carbon::now();
  34. $condition = [
  35. 'site_id' => $site_id,
  36. 'year' => $carbon->year,
  37. 'month' => $carbon->month,
  38. 'day' => $carbon->day,
  39. 'store_id' => $store_id,
  40. ];
  41. $info = model('stat_store')->getInfo($condition, 'id');
  42. //在这里会整体处理总支出 总收入
  43. $stat_data = $this->getStatData($data);
  44. if (empty($info)) {
  45. $insert_data = [
  46. 'site_id' => $site_id,
  47. 'store_id' => $store_id,
  48. 'year' => $carbon->year,
  49. 'month' => $carbon->month,
  50. 'day' => $carbon->day,
  51. 'day_time' => time(),
  52. 'create_time' => time()
  53. ];
  54. $insert_data = array_merge($insert_data, $stat_data);
  55. $res = model('stat_store')->add(
  56. $insert_data
  57. );
  58. } else {
  59. $update_data = array ();
  60. if (!empty($stat_data)) {
  61. foreach ($stat_data as $k => $v) {
  62. if ($v > 0) {
  63. $update_data[ $k ] = Db::raw($k . '+' . $v);
  64. } else if ($v < 0) {
  65. $update_data[ $k ] = Db::raw($k . '-' . abs($v));
  66. }
  67. }
  68. }
  69. if (!empty($update_data)) {
  70. $res = Db::name('stat_store')->where($condition)
  71. ->update($update_data);
  72. Log::write('addStoreStat' . Db::name('stat_store')->getLastSql());
  73. Cache::tag("cache_table" . "stat_store")->clear();
  74. }
  75. }
  76. //增加当天时统计
  77. $this->addShopHourStat($data, $carbon);
  78. // 添加店铺统计
  79. $shop_stat = [
  80. 'site_id' => $site_id
  81. ];
  82. foreach ($stat_data as $k => $value) {
  83. $shop_stat[ 'cashier_' . $k ] = $value;
  84. }
  85. ( new SystemStat() )->addShopStat($shop_stat);
  86. return $this->success($res ?? 0);
  87. }
  88. /**
  89. * 增加当日的时统计记录
  90. * @param $data
  91. */
  92. public function addShopHourStat($data, $carbon)
  93. {
  94. $site_id = $data[ 'site_id' ];
  95. $store_id = $data[ 'store_id' ] ?? 0;
  96. $condition = [
  97. 'site_id' => $site_id,
  98. 'store_id' => $store_id,
  99. 'year' => $carbon->year,
  100. 'month' => $carbon->month,
  101. 'day' => $carbon->day,
  102. 'hour' => $carbon->hour
  103. ];
  104. $info = model('stat_store_hour')->getInfo($condition, 'id');
  105. //在这里会整体处理总支出 总收入 总预计收入
  106. $stat_data = $this->getStatData($data);
  107. if (empty($info)) {
  108. $insert_data = [
  109. 'site_id' => $site_id,
  110. 'store_id' => $store_id,
  111. 'year' => $carbon->year,
  112. 'month' => $carbon->month,
  113. 'day' => $carbon->day,
  114. 'day_time' => time(),
  115. 'create_time' => time(),
  116. 'hour' => $carbon->hour
  117. ];
  118. $insert_data = array_merge($insert_data, $stat_data);
  119. $res = model('stat_store_hour')->add(
  120. $insert_data
  121. );
  122. } else {
  123. $update_data = array ();
  124. if (!empty($stat_data)) {
  125. foreach ($stat_data as $k => $v) {
  126. if ($v > 0) {
  127. $update_data[ $k ] = Db::raw($k . '+' . $v);
  128. } else if ($v < 0) {
  129. $update_data[ $k ] = Db::raw($k . '-' . abs($v));
  130. }
  131. }
  132. }
  133. if (!empty($update_data)) {
  134. $res = Db::name('stat_store_hour')->where($condition)
  135. ->update($update_data);
  136. Cache::tag("cache_table" . "stat_store_hour")->clear();
  137. }
  138. }
  139. return $this->success($res ?? 0);
  140. }
  141. /**
  142. * 整理数据
  143. * @param $data
  144. * @return mixed
  145. */
  146. public function getStatData($data)
  147. {
  148. unset($data[ 'site_id' ]);
  149. unset($data[ 'store_id' ]);
  150. $data = array_filter($data);
  151. return $data;
  152. }
  153. /**
  154. * 获取店铺统计(按照天查询)
  155. * @param unknown $site_id 0表示平台
  156. * @param unknown $year
  157. * @param unknown $month
  158. * @param unknown $day
  159. */
  160. public function getStatShop($site_id, $year, $month, $day, $store_id = 0)
  161. {
  162. $condition = [
  163. 'site_id' => $site_id,
  164. 'year' => $year,
  165. 'month' => $month,
  166. 'day' => $day
  167. ];
  168. if (!empty($store_id)) $condition[ 'store_id' ] = $store_id;
  169. $info = model('stat_store')->setIsCache(0)->getInfo($condition);
  170. if (empty($info)) {
  171. $condition[ 'day_time' ] = strtotime(date("{$year}-{$month}-{$day}"));
  172. model('stat_store')->add($condition);
  173. $info = model('stat_store')->getInfo($condition);
  174. }
  175. return $this->success($info);
  176. }
  177. /**
  178. * 获取数据之和
  179. * @param $site_id
  180. * @param $start_time
  181. * @param $end_time
  182. * @param int $store_id
  183. * @return array
  184. */
  185. public function getShopStatSum($site_id, $start_time, $end_time, $store_id = 0,$store_id_arr=[])
  186. {
  187. $condition = [
  188. [ 'site_id', '=', $site_id ],
  189. [ 'day_time', '>=', $start_time ],
  190. [ 'day_time', '<=', $end_time ],
  191. ];
  192. // if (!empty($store_id)) {
  193. // $condition[] = [ 'store_id', '=', $store_id ];
  194. // }else{
  195. // $condition[] = [ 'store_id', 'in', $store_id_arr ];
  196. // }
  197. if (!empty($store_id)) {
  198. $condition[] = [ 'store_id', '=', $store_id ];
  199. }else if(!empty($store_id_arr)){
  200. $condition[] = [ 'store_id', 'in', $store_id_arr ];
  201. }
  202. $field = array_map(function($field) {
  203. return "ifnull(sum($field), 0) as $field";
  204. }, $this->getStatField());
  205. $field[] = $this->getEstimatedRevenueSum();
  206. $list = model('stat_store')->getInfo($condition, implode(',', $field));
  207. return $this->success($list);
  208. }
  209. /**
  210. * 获取预计收入总和
  211. */
  212. private function getEstimatedRevenueSum()
  213. {
  214. return 'ifnull(sum(billing_money) + sum(buycard_money) + sum(recharge_money) - sum(refund_money), 0) as expected_earnings_total_money';
  215. }
  216. /**
  217. * 获取预计收入
  218. */
  219. private function getEstimatedRevenue()
  220. {
  221. return 'ifnull(sum(billing_money) + sum(buycard_money) + sum(recharge_money) - sum(refund_money),0) as expected_earnings_total_money';
  222. }
  223. /**
  224. * 获取店铺统计列表
  225. * @param $site_id
  226. * @param $start_time
  227. * @param $end_time
  228. * @param int $store_id
  229. * @return array
  230. */
  231. public function getShopStatList($site_id, $start_time, $end_time, $store_id = 0)
  232. {
  233. $condition = [
  234. [ 'site_id', '=', $site_id ],
  235. [ 'day_time', '>=', $start_time ],
  236. [ 'day_time', '<=', $end_time ],
  237. ];
  238. if (!empty($store_id)) {
  239. $condition[] = [ 'store_id', 'in', $store_id ];
  240. }
  241. // $field = '*,' . $this->getEstimatedRevenue();
  242. // $list = model('stat_store')->getList($condition, $field);
  243. $field = 'day,sum(billing_count) billing_count,sum(billing_money) billing_money ,sum(buycard_count) buycard_count,
  244. sum(buycard_money) buycard_money ,sum(recharge_count) recharge_count,
  245. sum(recharge_money) recharge_money,sum(refund_count) refund_count,sum(refund_money) refund_money,sum(order_member_count) order_member_count,sum(balance_money) balance_money ,' . $this->getEstimatedRevenue();
  246. $list = model('stat_store')->getList($condition, $field,'day asc','','','day');
  247. return $this->success($list);
  248. }
  249. /**
  250. * 获取小时统计数据
  251. * @param $site_id
  252. * @param $year
  253. * @param $month
  254. * @param $day
  255. * @return array
  256. */
  257. public function getShopStatHourList($site_id, $year, $month, $day, $store_id = 0)
  258. {
  259. $condition = [
  260. [ 'site_id', '=', $site_id ],
  261. [ 'year', '=', $year ],
  262. [ 'month', '=', $month ],
  263. [ 'day', '=', $day ],
  264. ];
  265. if (!empty($store_id)) $condition[] = [ 'store_id', 'in', $store_id ];
  266. // $field = '*,' . $this->getEstimatedRevenue();
  267. // $list = model('stat_store_hour')->getList($condition, $field, 'id desc');
  268. $field = 'year,month,day,hour,sum(billing_count) billing_count,sum(billing_money) billing_money ,sum(buycard_count) buycard_count,
  269. sum(buycard_money) buycard_money ,sum(recharge_count) recharge_count,
  270. sum(recharge_money) recharge_money,sum(refund_count) refund_count,sum(refund_money) refund_money,sum(order_member_count) order_member_count,sum(balance_money) balance_money ,' . $this->getEstimatedRevenue();
  271. $list = model('stat_store_hour')->getList($condition, $field, '','','','hour');
  272. return $this->success($list);
  273. }
  274. /**
  275. * 获取天统计表统计字段
  276. * @return array
  277. */
  278. public function getStatField()
  279. {
  280. $fields = Db::name('stat_store')->getTableFields('');
  281. $fields = array_values(array_diff($fields, [ 'id', 'site_id', 'year', 'month', 'day', 'day_time', 'store_id' ]));
  282. return $fields;
  283. }
  284. /**
  285. * 获取时统计表统计字段
  286. * @return array
  287. */
  288. public function getStatHourField()
  289. {
  290. $fields = Db::name('stat_store_hour')->getTableFields('');
  291. $fields = array_values(array_diff($fields, [ 'id', 'site_id', 'year', 'month', 'day', 'hour', 'day_time', 'store_id' ]));
  292. return $fields;
  293. }
  294. }