TradeStatisticServices.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\statistic;
  12. use app\services\BaseServices;
  13. use app\services\order\StoreOrderRefundServices;
  14. use app\services\other\export\ExportServices;
  15. use app\services\order\OtherOrderServices;
  16. use app\services\order\StoreOrderServices;
  17. use app\services\user\UserExtractServices;
  18. use app\services\user\UserMoneyServices;
  19. use app\services\user\UserRechargeServices;
  20. /**
  21. * Class TradeStatisticServices
  22. * @package app\services\statistic
  23. */
  24. class TradeStatisticServices extends BaseServices
  25. {
  26. public $_day = ['00时', '01时', '02时', '03时', '04时', '05时', '06时', '07时', '08时', '09时', '10时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18时', '19时', '20时', '21时', '22时', '23时', '24时'];
  27. /**
  28. * 基本概况
  29. * @param $where
  30. * @return mixed
  31. */
  32. public function getTopLeftTrade($where)
  33. {
  34. //总交易额
  35. $selectType = "sum";
  36. $tradeTotalMoney = $this->tradeTotalMoney($where, $selectType);
  37. //交易曲线
  38. $selectType = "group";
  39. $hourTotalMoney = $this->tradeGroupMoney($where, $selectType);
  40. return ['total_money' => $tradeTotalMoney, 'curve' => $hourTotalMoney];
  41. }
  42. public function getTopRightOneTrade()
  43. {
  44. /** @var StoreOrderServices $orderService */
  45. $orderService = app()->make(StoreOrderServices::class);
  46. /** day订单数 */
  47. //今日订单数
  48. $orderCountWhere['paid'] = 1;
  49. $orderCountWhere['pid'] = 0;
  50. $orderCountWhere['timeKey'] = $this->TimeConvert("today");
  51. $todayOrderCount = $orderService->getOrderCountByWhere($orderCountWhere);
  52. //今日订单数曲线
  53. $todayHourOrderCount = $orderService->getOrderGroupCountByWhere($orderCountWhere);
  54. $todayHourOrderCount = $this->trendYdata((array)$todayHourOrderCount, $orderCountWhere['timeKey']);
  55. //昨日订单数
  56. $yestodayWhere['paid'] = 1;
  57. $yestodayWhere['pid'] = 0;
  58. $yestodayWhere['timeKey'] = $this->TimeConvert("yestoday");
  59. $yesTodayOrderCount = $orderService->getOrderCountByWhere($yestodayWhere);
  60. //昨日订单曲线
  61. // $yestodayHourOrderCount = $orderService->getOrderGroupCountByWhere($yestodayWhere);
  62. // $yestodayHourOrderCount = $this->trendYdata((array)$yestodayHourOrderCount, 'day');
  63. //订单数环比增长率
  64. $orderCountDayChain = $this->countRate($todayOrderCount, $yesTodayOrderCount);
  65. $data[] = [
  66. 'name' => "今日订单数",
  67. 'now_value' => $todayOrderCount,
  68. 'last_value' => $yesTodayOrderCount,
  69. 'rate' => $orderCountDayChain,
  70. 'curve' => $todayHourOrderCount
  71. ];
  72. /** day支付人数 */
  73. //今日支付人数
  74. $orderPeopleWhere['timeKey'] = $this->TimeConvert("today");
  75. $orderPeopleWhere['paid'] = 1;
  76. $orderPeopleWhere['pid'] = 0;
  77. $todayPayOrderPeople = count($orderService->getPayOrderPeopleByWhere($orderPeopleWhere));
  78. //今日支付人数曲线
  79. $todayHourOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($orderPeopleWhere);
  80. $todayHourOrderPeople = $this->trendYdata((array)$todayHourOrderPeople, $orderPeopleWhere['timeKey']);
  81. //昨日支付人数
  82. $yestodayOrderPeopleWhere['timeKey'] = $this->TimeConvert("yestoday");
  83. $yestodayOrderPeopleWhere['paid'] = 1;
  84. $yestodayPayOrderPeople = count($orderService->getPayOrderPeopleByWhere($yestodayOrderPeopleWhere));
  85. //昨日支付曲线
  86. // $yestodayHourOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($yestodayOrderPeopleWhere);
  87. // $yestodayHourOrderPeople = $this->trendYdata((array)$yestodayHourOrderPeople, 'day');
  88. //订单支付人数环比
  89. $orderPeopleDayChain = $this->countRate($todayPayOrderPeople, $yestodayPayOrderPeople);
  90. $data[] = [
  91. 'name' => "今日支付人数",
  92. 'now_value' => $todayPayOrderPeople,
  93. 'last_value' => $yestodayPayOrderPeople,
  94. 'rate' => $orderPeopleDayChain,
  95. 'curve' => $todayHourOrderPeople
  96. ];
  97. $new_data = [];
  98. foreach ($data as $k => $v) {
  99. $new_data['x'] = $v['curve']['x'];
  100. $new_data['series'][$k]['name'] = $v['name'];
  101. $new_data['series'][$k]['now_money'] = $v['now_value'];
  102. $new_data['series'][$k]['last_money'] = $v['last_value'];
  103. $new_data['series'][$k]['rate'] = $v['rate'];
  104. $new_data['series'][$k]['value'] = array_values($v['curve']['y']);
  105. }
  106. return $new_data;
  107. }
  108. public function getTopRightTwoTrade()
  109. {
  110. /** @var StoreOrderServices $orderService */
  111. $orderService = app()->make(StoreOrderServices::class);
  112. /** month订单数 */
  113. $monthOrderCountWhere['paid'] = 1;
  114. $monthOrderCountWhere['pid'] = 0;
  115. $monthOrderCountWhere['timeKey'] = $this->TimeConvert("month");
  116. $monthOrderCount = $orderService->getOrderCountByWhere($monthOrderCountWhere);
  117. //本月订单数曲线
  118. $monthCurveOrderCount = $orderService->getOrderGroupCountByWhere($monthOrderCountWhere);
  119. $monthCurveOrderCount = $this->trendYdata((array)$monthCurveOrderCount, $monthOrderCountWhere['timeKey']);
  120. //上月订单数
  121. $lastOrderCountWhere['timeKey'] = $this->TimeConvert("last_month");
  122. $lastOrderCountWhere['is_del'] = 0;
  123. $lastOrderCountWhere['paid'] = 1;
  124. $lastOrderCountWhere['pid'] = 0;
  125. $lastOrderCount = $orderService->getOrderCountByWhere($lastOrderCountWhere);
  126. //上月订单曲线
  127. // $lastCurveOrderCount = $orderService->getOrderGroupCountByWhere($lastOrderCountWhere);
  128. // $lastCurveOrderCount = $this->trendYdata((array)$lastCurveOrderCount, 'month');
  129. //订单数环比增长率
  130. // $orderCountMonthChain = (($monthOrderCount - $lastOrderCount) / $lastOrderCount) * 100;
  131. $orderCountMonthChain = $this->countRate($monthOrderCount, $lastOrderCount);
  132. $data[] = [
  133. 'name' => "本月订单数",
  134. 'now_value' => $monthOrderCount,
  135. 'last_value' => $lastOrderCount,
  136. 'rate' => $orderCountMonthChain,
  137. 'curve' => $monthCurveOrderCount
  138. ];
  139. /** month下单人数 */
  140. //本月支付人数
  141. $monthOrderPeopleWhere['timeKey'] = $this->TimeConvert("month");;
  142. $monthOrderPeopleWhere['paid'] = 1;
  143. $monthPayOrderPeople = count($orderService->getPayOrderPeopleByWhere($monthOrderPeopleWhere));
  144. //本月支付人数曲线
  145. $monthCurveOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($monthOrderPeopleWhere);
  146. $monthCurveOrderPeople = $this->trendYdata((array)$monthCurveOrderPeople, $monthOrderPeopleWhere['timeKey']);
  147. //上月支付人数
  148. $lastOrderPeopleWhere['timeKey'] = $this->TimeConvert("last_month");
  149. $lastOrderPeopleWhere['paid'] = 1;
  150. $lastPayOrderPeople = count($orderService->getPayOrderPeopleByWhere($lastOrderPeopleWhere));
  151. //上月支付曲线
  152. // $lastCurveOrderPeople = $orderService->getPayOrderGroupPeopleByWhere($lastOrderPeopleWhere);
  153. // $lastCurveOrderPeople = $this->trendYdata((array)$lastCurveOrderPeople, 'month');
  154. //订单支付人数环比
  155. $orderPeopleDayChain = $this->countRate($monthPayOrderPeople, $lastPayOrderPeople);
  156. $data[] = [
  157. 'name' => "本月支付人数",
  158. 'now_value' => $monthPayOrderPeople,
  159. 'last_value' => $lastPayOrderPeople,
  160. 'rate' => $orderPeopleDayChain,
  161. 'curve' => $monthCurveOrderPeople
  162. ];
  163. $new_data = [];
  164. foreach ($data as $k => $v) {
  165. $new_data[$k]['name'] = $v['name'];
  166. $new_data[$k]['now_money'] = $v['now_value'];
  167. $new_data[$k]['last_money'] = $v['last_value'];
  168. $new_data[$k]['rate'] = $v['rate'];
  169. $new_data[$k]['value'] = $v['curve']['y'];
  170. }
  171. return $new_data;
  172. }
  173. /**
  174. * 交易总额
  175. * @param $where
  176. * @param $selectType
  177. * @return array|float|int|mixed
  178. */
  179. public function tradeTotalMoney($where, $selectType, $isNum = false)
  180. {
  181. /** 收入营业额 */
  182. //商品订单收入
  183. $inOrderMoney = $this->getOrderTotalMoney($where, $selectType, "", $isNum);
  184. //用户充值收入
  185. $inRechargeMoneyHome = $this->getRechargeTotalMoney($where, $selectType, "", $isNum);
  186. $inrechgeMoneyAdmin = $this->getBillYeTotalMoney($where, $selectType, '', $isNum);
  187. $inRechargeMoney = bcadd($inRechargeMoneyHome, $inrechgeMoneyAdmin, 2);
  188. //购买会员收入
  189. $inMemberMoney = $this->getMemberTotalMoney($where, $selectType, "", $isNum);
  190. //线下收款收入
  191. $inOfflineMoney = $this->getOfflineTotalMoney($where, $selectType, "", $isNum);
  192. //总交易额
  193. $inTotalMoney = bcadd(bcadd($inOrderMoney, $inRechargeMoney, 2), bcadd($inMemberMoney, $inOfflineMoney, 2), 2);/* - $outExtractUserMoney*/
  194. return $inTotalMoney;
  195. }
  196. /**
  197. * 交易额曲线图
  198. * @param $where
  199. * @param $selectType
  200. * @return array
  201. */
  202. public function tradeGroupMoney($where, $selectType)
  203. {
  204. //商品订单收入
  205. $orderGroup = "add_time";
  206. $OrderMoney = $this->getOrderTotalMoney($where, $selectType, $orderGroup);
  207. //用户充值收入
  208. $rechargeGroup = "add_time";
  209. $RechargeMoneyHome = $this->getRechargeTotalMoney($where, $selectType, $rechargeGroup);
  210. $RechargeMoneyAdmin = $this->getBillYeTotalMoney($where, $selectType, $rechargeGroup);
  211. $RechargeMoney = $this->totalArrData([$RechargeMoneyHome, $RechargeMoneyAdmin]);
  212. //购买会员收入
  213. $memberGroup = "add_time";
  214. $MemberMoney = $this->getMemberTotalMoney($where, $selectType, $memberGroup);
  215. //线下收款收入
  216. $offlineGroup = "add_time";
  217. $OfflineMoney = $this->getOfflineTotalMoney($where, $selectType, $offlineGroup);
  218. return $this->totalArrData([$OrderMoney, $RechargeMoney, $MemberMoney, $OfflineMoney]);
  219. }
  220. /**
  221. * 底部数据
  222. * @param $where
  223. * @return array
  224. * @throws \Exception
  225. */
  226. public function getBottomTrade($where)
  227. {
  228. if (!$where['data']) {
  229. $where['time'] = ['start_time' => date('Y-m-d 00:00:00', time()), "end_time" => date('Y-m-d 23:59:59', time())];
  230. } else {
  231. $time = explode("-", $where['data']);
  232. $where['time'] = ['start_time' => date('Y-m-d 00:00:00', strtotime($time[0])), "end_time" => date('Y-m-d 23:59:59', strtotime($time[1]))];
  233. }
  234. unset($where['data']);
  235. /** @var ExportServices $exportService */
  236. $exportService = app()->make(ExportServices::class);
  237. $chainTime = $this->chainTime($where['time']);
  238. $isNum = false;
  239. if ($chainTime == "other") $isNum = true;
  240. $dateWhere['time'] = $isNum ? $where['time'] : $chainTime;
  241. $topData = array();
  242. $Chain = array();
  243. /** 商品支付金额 */
  244. $OrderMoney = $this->getOrderTotalMoney($where, "sum");
  245. $lastOrderMoney = $this->getOrderTotalMoney($dateWhere, "sum", "", $isNum);
  246. $OrderCurve = $this->getOrderTotalMoney($where, "group", "add_time");
  247. $OrderChain = $this->countRate($OrderMoney, $lastOrderMoney);
  248. $topData[2] = [
  249. 'title' => '商品支付金额',
  250. 'desc' => '选定条件下,用户购买商品的实际支付金额,包括微信支付、余额支付、支付宝支付、线下支付金额(拼团商品在成团之后计入,线下支付订单在后台确认支付后计入)',
  251. 'total_money' => $OrderMoney,
  252. 'rate' => $OrderChain,
  253. 'value' => $OrderCurve['y'],
  254. 'type' => 1,
  255. 'sign' => 'goods',
  256. ];
  257. $Chain['goods'] = $OrderCurve;
  258. /** 购买会员金额 */
  259. $memberMoney = $this->getMemberTotalMoney($where, 'sum');
  260. $lastMemberMoney = $this->getMemberTotalMoney($dateWhere, 'sum', "", $isNum);
  261. $memberCurve = $this->getMemberTotalMoney($where, 'group', "pay_time");
  262. $MemberChain = $this->countRate($memberMoney, $lastMemberMoney);
  263. $topData[3] = [
  264. 'title' => '购买会员金额',
  265. 'desc' => '选定条件下,用户成功购买付费会员的金额',
  266. 'total_money' => $memberMoney,
  267. 'rate' => $MemberChain,
  268. 'value' => $memberCurve['y'],
  269. 'type' => 1,
  270. 'sign' => 'member',
  271. ];
  272. $Chain['member'] = $memberCurve;
  273. /** 充值金额 */
  274. $rechgeMoneyHome = $this->getRechargeTotalMoney($where, 'sum');
  275. $rechgeMoneyAdmin = $this->getBillYeTotalMoney($where, 'sum');
  276. $rechgeMoneyTotal = bcadd($rechgeMoneyHome, $rechgeMoneyAdmin, 2);
  277. $lastRechgeMoneyHome = $this->getRechargeTotalMoney($dateWhere, 'sum', "", $isNum);
  278. $lastRechgeMoneyAdmin = $this->getBillYeTotalMoney($dateWhere, 'sum', "", $isNum);
  279. $lastRechgeMoneyTotal = bcadd($lastRechgeMoneyHome, $lastRechgeMoneyAdmin, 2);
  280. $RechgeHomeCurve = $this->getRechargeTotalMoney($where, 'group', "pay_time");
  281. $RechgeAdminCurve = $this->getBillYeTotalMoney($where, 'group', "add_time");
  282. $RechgeTotalCurve = $this->totalArrData([$RechgeHomeCurve, $RechgeAdminCurve]);
  283. $RechgeChain = $this->countRate($rechgeMoneyTotal, $lastRechgeMoneyTotal);
  284. $topData[4] = [
  285. 'title' => '充值金额',
  286. 'desc' => '选定条件下,用户成功充值的金额',
  287. 'total_money' => $rechgeMoneyTotal,
  288. 'rate' => $RechgeChain,
  289. 'value' => $RechgeTotalCurve['y'],
  290. 'type' => 1,
  291. 'sign' => 'rechge',
  292. ];
  293. $Chain['rechage'] = $RechgeTotalCurve;
  294. /** 线下收银 */
  295. $offlineMoney = $this->getOfflineTotalMoney($where, 'sum');
  296. $lastOfflineMoney = $this->getOfflineTotalMoney($dateWhere, 'sum', "", $isNum);
  297. $offlineCurve = $this->getOfflineTotalMoney($where, 'group', "pay_time");
  298. $offlineChain = $this->countRate($offlineMoney, $lastOfflineMoney);
  299. $topData[5] = [
  300. 'title' => '线下收银金额',
  301. 'desc' => '选定条件下,用户在线下扫码支付的金额',
  302. 'total_money' => $offlineMoney,
  303. 'rate' => $offlineChain,
  304. 'value' => $offlineCurve['y'],
  305. 'type' => 0,
  306. 'sign' => 'offline',
  307. ];
  308. $Chain['offline'] = $offlineCurve;
  309. /** 支出*/
  310. //余额支付商品
  311. $outYeOrderMoney = $this->getOrderTotalMoney(['pay_type' => "yue", 'time' => $where['time']], 'sum');
  312. $lastOutYeOrderMoney = $this->getOrderTotalMoney(['pay_type' => "yue", 'time' => $dateWhere['time']], 'sum', "", $isNum);
  313. $outYeOrderCurve = $this->getOrderTotalMoney(['pay_type' => "yue", 'time' => $where['time']], 'group', 'pay_time');
  314. $outYeOrderChain = $this->countRate($outYeOrderMoney, $lastOutYeOrderMoney);
  315. //余额购买会员
  316. $outYeMemberMoney = $this->getMemberTotalMoney(['pay_type' => "yue", 'time' => $where['time']], 'sum');
  317. $lastOutYeMemberMoney = $this->getMemberTotalMoney(['pay_type' => "yue", 'time' => $dateWhere['time']], 'sum', "", $isNum);
  318. $outYeMemberCurve = $this->getMemberTotalMoney(['pay_type' => "yue", 'time' => $where['time']], 'group', "pay_time");
  319. $outYeMemberChain = $this->countRate($outYeMemberMoney, $lastOutYeMemberMoney);
  320. //余额支付
  321. $outYeMoney = bcadd($outYeOrderMoney, $outYeMemberMoney, 2);
  322. $lastOutYeMoney = bcadd($lastOutYeOrderMoney, $lastOutYeMemberMoney, 2);
  323. $outYeCurve = $this->totalArrData([$outYeOrderCurve, $outYeMemberCurve]);
  324. $outYeChain = $this->countRate($outYeOrderChain, $outYeMemberChain);
  325. $topData[7] = [
  326. 'title' => '余额支付金额',
  327. 'desc' => '用户下单时使用余额实际支付的金额',
  328. 'total_money' => $outYeMoney,
  329. 'rate' => $outYeChain,
  330. 'value' => $outYeCurve['y'],
  331. 'type' => 0,
  332. 'sign' => 'yue',
  333. ];
  334. $Chain['out_ye'] = $outYeCurve;
  335. //支付佣金金额
  336. $outExtractMoney = $this->getExtractTotalMoney($where, 'sum');
  337. $lastOutExtractMoney = $this->getExtractTotalMoney($dateWhere, 'sum', "", $isNum);
  338. $OutExtractCurve = $this->getExtractTotalMoney($where, 'group', "add_time");
  339. $OutExtractChain = $this->countRate($outExtractMoney, $lastOutExtractMoney);
  340. $topData[8] = [
  341. 'title' => '支付佣金金额',
  342. 'desc' => '后台给推广员支付的推广佣金,以实际支付为准',
  343. 'total_money' => $outExtractMoney,
  344. 'rate' => $OutExtractChain,
  345. 'value' => $OutExtractCurve['y'],
  346. 'type' => 0,
  347. 'sign' => 'yong',
  348. ];
  349. $Chain['extract'] = $OutExtractCurve;
  350. //商品退款金额
  351. $outOrderRefund = $this->getOrderRefundTotalMoney(['refund_type' => 6, 'time' => $where['time']], 'sum');
  352. $lastOutOrderRefund = $this->getOrderRefundTotalMoney(['refund_type' => 6, 'time' => $dateWhere['time']], 'sum', "", $isNum);
  353. $outOrderRefundCurve = $this->getOrderRefundTotalMoney(['refund_type' => 6, 'time' => $where['time']], 'group', 'add_time');
  354. $orderRefundChain = $this->countRate($outOrderRefund, $lastOutOrderRefund);
  355. $topData[9] = [
  356. 'title' => '商品退款金额',
  357. 'desc' => '用户成功退款的商品金额',
  358. 'total_money' => $outOrderRefund,
  359. 'rate' => $orderRefundChain,
  360. 'value' => $outOrderRefundCurve['y'],
  361. 'type' => 0,
  362. 'sign' => 'refund',
  363. ];
  364. $Chain['refund'] = $outOrderRefundCurve;
  365. //支出金额
  366. $outTotalMoney = bcadd($outYeMoney, $outExtractMoney, 2);
  367. $lastOutTotalMoney = bcadd($lastOutYeMoney, $lastOutExtractMoney, 2);
  368. $outTotalCurve = $this->totalArrData([$outYeCurve, $OutExtractCurve]);
  369. $outTotalChain = $this->countRate($outTotalMoney, $lastOutTotalMoney);
  370. $topData[6] = [
  371. 'title' => '支出金额',
  372. 'desc' => '余额支付金额、支付佣金金额',
  373. 'total_money' => $outTotalMoney,
  374. 'rate' => $outTotalChain,
  375. 'value' => $outTotalCurve['y'],
  376. 'type' => 1,
  377. 'sign' => 'out',
  378. ];
  379. $Chain['out'] = $outTotalCurve;
  380. /** 交易毛利金额*/
  381. $jiaoyiMoney = $this->tradeTotalMoney($where, "sum");
  382. $jiaoyiMoney = bcsub($jiaoyiMoney, $outTotalMoney, 2);
  383. $lastJiaoyiMoney = $this->tradeTotalMoney($dateWhere, "sum", $isNum);
  384. $lastJiaoyiMoney = bcsub($lastJiaoyiMoney, $lastOutTotalMoney, 2);
  385. $jiaoyiCurve = $this->tradeGroupMoney($where, "group");
  386. $jiaoyiCurve = $this->subdutionArrData($jiaoyiCurve, $outTotalCurve);
  387. $jiaoyiChain = $this->countRate($jiaoyiMoney, $lastJiaoyiMoney);
  388. $topData[1] = [
  389. 'title' => '交易毛利金额',
  390. 'desc' => '交易毛利金额 = 营业额 - 支出金额',
  391. 'total_money' => $jiaoyiMoney,
  392. 'rate' => $jiaoyiChain,
  393. 'value' => $jiaoyiCurve['y'],
  394. 'type' => 1,
  395. 'sign' => 'jiaoyi',
  396. ];
  397. $Chain['jiaoyi'] = $jiaoyiCurve;
  398. /** @var 营业额 $inTotalMoney */
  399. $inTotalMoney = $this->tradeTotalMoney($where, "sum");
  400. $lastInTotalMoney = $this->tradeTotalMoney($dateWhere, "sum", $isNum);
  401. $inTotalCurve = $this->tradeGroupMoney($where, "group");
  402. $inTotalChain = $this->countRate($inTotalMoney, $lastInTotalMoney);
  403. $topData[0] = [
  404. 'title' => '营业额',
  405. 'desc' => '商品支付金额、充值金额、购买付费会员金额、线下收银金额',
  406. 'total_money' => $inTotalMoney,
  407. 'rate' => $inTotalChain,
  408. 'value' => $inTotalCurve['y'],
  409. 'type' => 1,
  410. 'sign' => 'in',
  411. ];
  412. ksort($topData);
  413. $data = [];
  414. foreach ($topData as $k => $v) {
  415. $data['x'] = $Chain['out']['x'];
  416. $data['series'][$k]['name'] = $v['title'];
  417. $data['series'][$k]['desc'] = $v['desc'];
  418. $data['series'][$k]['money'] = $v['total_money'];
  419. $data['series'][$k]['type'] = $v['type'];
  420. $data['series'][$k]['rate'] = $v['rate'];
  421. $data['series'][$k]['value'] = array_values($v['value']);
  422. }
  423. $export = $exportService->tradeData($data, '交易统计', 2);
  424. $data['export'] = $export[0];
  425. return $data;
  426. }
  427. /**
  428. * 多个数组相加
  429. * @param array $arr
  430. * @return array|false
  431. */
  432. public function totalArrData(array $arr)
  433. {
  434. if (!$arr || !is_array($arr)) return false;
  435. $item = array();
  436. $y = array_column($arr, "y");
  437. $x = array_column($arr, "x")[0];
  438. foreach ($y as $key => $value) {
  439. foreach ($value as $k => $v) {
  440. if (isset($item[$k])) {
  441. $item[$k] = bcadd($item[$k], $v, 2);
  442. } else {
  443. $item[$k] = $v;
  444. }
  445. }
  446. }
  447. return ['x' => $x, 'y' => $item];
  448. }
  449. /**
  450. * 数组相减
  451. * @param array $arr1
  452. * @param array $arr2
  453. * @return array
  454. */
  455. public function subdutionArrData(array $arr1, array $arr2)
  456. {
  457. $item = array();
  458. foreach ($arr1['y'] as $key => $value) {
  459. $item['y'][$key] = bcsub($value, $arr2['y'][$key], 2);
  460. }
  461. $item['x'] = $arr1['x'];
  462. return $item;
  463. }
  464. /**
  465. * 搜索时间转换
  466. * @param $timeKey
  467. * @param false $isNum
  468. * @return array
  469. * @throws \Exception
  470. */
  471. public function TimeConvert($timeKey, $isNum = false)
  472. {
  473. switch ($timeKey) {
  474. case "today" :
  475. $data['start_time'] = date('Y-m-d 00:00:00', time());
  476. $data['end_time'] = date('Y-m-d 23:59:59', time());
  477. $data['days'] = 1;
  478. break;
  479. case "yestoday" :
  480. $data['start_time'] = date('Y-m-d 00:00:00', strtotime('-1 day'));
  481. $data['end_time'] = date('Y-m-d 23:59:59', strtotime('-1 day'));
  482. $data['days'] = 1;
  483. break;
  484. case "last_month" :
  485. $data['start_time'] = date('Y-m-01 00:00:00', strtotime('-1 month'));
  486. $data['end_time'] = date('Y-m-t 23:59:59', strtotime('-1 month'));
  487. $data['days'] = 30;
  488. break;
  489. case "month" :
  490. $data['start_time'] = $month_start_time = date('Y-m-01 00:00:00', strtotime(date("Y-m-d")));
  491. $data['end_time'] = date('Y-m-d 23:59:59', strtotime("$month_start_time +1 month -1 day"));
  492. $data['days'] = 30;
  493. break;
  494. case "year" :
  495. $data['start_time'] = date('Y-01-01 00:00:00', time());
  496. $data['end_time'] = date('Y-12-t 23:59:59', time());
  497. $data['days'] = 365;
  498. break;
  499. case "last_year" :
  500. $data['start_time'] = date('Y-01-01 00:00:00', strtotime('-1 year'));
  501. $data['end_time'] = date('Y-12-t 23:59:59', strtotime('-1 year'));
  502. $data['days'] = 365;
  503. break;
  504. case 30 :
  505. case 15 :
  506. case 7 :
  507. if (!$isNum) {
  508. $data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$timeKey day"));
  509. $data['end_time'] = date('Y-m-d 23:59:59', time());
  510. $data['days'] = $timeKey;
  511. } else {
  512. $day = $timeKey * 2;
  513. $data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$day day"));
  514. $data['end_time'] = date("Y-m-d 23:59:59", strtotime("-$timeKey day"));
  515. $data['days'] = $timeKey;
  516. }
  517. break;
  518. default:
  519. $datetime_start = new \DateTime($timeKey['start_time']);
  520. $datetime_end = new \DateTime($timeKey['end_time']);
  521. $days = $datetime_start->diff($datetime_end)->days;
  522. $days = $days > 0 ? $days : 1;
  523. if (!$isNum) {
  524. $data['start_time'] = $timeKey['start_time'];
  525. $data['end_time'] = $timeKey['end_time'];
  526. $data['days'] = $days;
  527. } else {
  528. $data['start_time'] = date("Y-m-d 00:00:00", strtotime("-$days day"));
  529. $data['end_time'] = $timeKey['start_time'];
  530. $data['days'] = $days;
  531. }
  532. }
  533. return $data;
  534. }
  535. /**
  536. * 获取订单退款
  537. * @param $where
  538. * @param string $selectType
  539. * @param string $group
  540. * @param bool $isNum
  541. * @return array|float|int
  542. * @throws \Exception
  543. */
  544. public function getOrderRefundTotalMoney($where, string $selectType, string $group = '', bool $isNum = false)
  545. {
  546. $orderSumField = isset($where['refund_type']) ? "refunded_price" : "refund_price";
  547. $whereOrderMoner['refund_type'] = isset($where['refund_type']) ? $where['refund_type'] : 6;
  548. $whereOrderMoner['is_cancel'] = 0;
  549. $whereOrderMoner['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  550. $storeOrderRefundServices = app()->make(StoreOrderRefundServices::class);
  551. $totalMoney = $storeOrderRefundServices->getOrderRefundMoneyByWhere($whereOrderMoner, $orderSumField, $selectType, $group);
  552. if ($group) {
  553. $totalMoney = $this->trendYdata((array)$totalMoney, $whereOrderMoner['timeKey']);
  554. }
  555. return $totalMoney;
  556. }
  557. /**
  558. * 获取商品营收
  559. * @param $where
  560. * @param string $selectType
  561. * @param string $group
  562. * @param bool $isNum
  563. * @return array|float|int
  564. * @throws \Exception
  565. */
  566. public function getOrderTotalMoney($where, string $selectType, string $group = "", bool $isNum = false)
  567. {
  568. /** 普通商品订单支付金额 */
  569. /** @var StoreOrderServices $storeOrderService */
  570. $storeOrderService = app()->make(StoreOrderServices::class);
  571. $orderSumField = isset($where['refund_status']) ? "refund_price" : "pay_price";
  572. $whereOrderMoner['refund_status'] = isset($where['refund_status']) ? $where['refund_status'] : [0, 3];
  573. $whereOrderMoner['paid'] = 1;
  574. $whereOrderMoner['pid'] = 0;
  575. if (isset($where['pay_type'])) {
  576. $whereOrderMoner['pay_type'] = $where['pay_type'];
  577. } else {
  578. // $whereOrderMoner['pay_type_no'] = 2;
  579. }
  580. $whereOrderMoner['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  581. $totalMoney = $storeOrderService->getOrderMoneyByWhere($whereOrderMoner, $orderSumField, $selectType, $group);
  582. if ($group) {
  583. $totalMoney = $this->trendYdata((array)$totalMoney, $whereOrderMoner['timeKey']);
  584. }
  585. return $totalMoney;
  586. }
  587. /**
  588. * 支付佣金
  589. * @param $where
  590. * @param string $selectType
  591. * @param string $group
  592. * @param bool $isNum
  593. * @return array|float|mixed
  594. * @throws \Exception
  595. */
  596. public function getExtractTotalMoney($where, string $selectType, string $group = "", bool $isNum = false)
  597. {
  598. /** 普通商品订单支付金额 */
  599. /** @var UserExtractServices $extractService */
  600. $extractService = app()->make(UserExtractServices::class);
  601. $orderSumField = "extract_price";
  602. $whereData['status'] = 1;
  603. $whereData['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  604. $totalMoney = $extractService->getOutMoneyByWhere($whereData, $orderSumField, $selectType, $group);
  605. if ($group) {
  606. $totalMoney = $this->trendYdata((array)$totalMoney, $whereData['timeKey']);
  607. }
  608. return $totalMoney;
  609. }
  610. /**
  611. * 获取用户充值营收
  612. * @param array $where
  613. * @param string $selectType
  614. * @param string $group
  615. * @param bool $isNum
  616. * @return array|float|int
  617. * @throws \Exception
  618. */
  619. public function getRechargeTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
  620. {
  621. /** 用户充值金额 */
  622. /** @var UserRechargeServices $userRechageService */
  623. $userRechageService = app()->make(UserRechargeServices::class);
  624. $rechargeSumField = "price";
  625. $whereInRecharge['paid'] = 1;
  626. $whereInRecharge['refund_price'] = '0.00';
  627. $whereInRecharge['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  628. $whereInRecharge['store_id'] = 0;
  629. $totalMoney = $userRechageService->getRechargeMoneyByWhere($whereInRecharge, $rechargeSumField, $selectType, $group);
  630. if ($group) {
  631. $totalMoney = $this->trendYdata((array)$totalMoney, $whereInRecharge['timeKey']);
  632. }
  633. return $totalMoney;
  634. }
  635. /**
  636. * 后台手动充值
  637. * @param array $where
  638. * @param string $selectType
  639. * @param string $group
  640. * @param bool $isNum
  641. * @return array|float|int
  642. * @throws \Exception
  643. */
  644. public function getBillYeTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
  645. {
  646. /** 后台用户充值金额 */
  647. $rechargeSumField = "number";
  648. $whereInRecharge['pm'] = 1;
  649. $whereInRecharge['type'] = 'system_add';
  650. $whereInRecharge['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  651. $whereInRecharge['store_id'] = 0;
  652. /** @var UserMoneyServices $userMoneyServices */
  653. $userMoneyServices = app()->make(UserMoneyServices::class);
  654. $totalMoney = $userMoneyServices->getRechargeMoneyByWhere($whereInRecharge, $rechargeSumField, $selectType, $group);
  655. if ($group) {
  656. $totalMoney = $this->trendYdata((array)$totalMoney, $whereInRecharge['timeKey']);
  657. }
  658. return $totalMoney;
  659. }
  660. /**
  661. * 购买会员总额
  662. * @param array $where
  663. * @param string $selectType
  664. * @param string $group
  665. * @param bool $isNum
  666. * @return array|mixed
  667. * @throws \Exception
  668. */
  669. public function getMemberTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
  670. {
  671. /** 购买会员 */
  672. /** @var OtherOrderServices $otherOrderService */
  673. $otherOrderService = app()->make(OtherOrderServices::class);
  674. $memberSumField = "pay_price";
  675. $whereInMember['type'] = 1;
  676. $whereInMember['paid'] = 1;
  677. $whereInMember['store_id'] = 0;
  678. if (isset($where['pay_type'])) {
  679. $whereInMember['pay_type'] = $where['pay_type'];
  680. } else {
  681. //$whereInMember['pay_type_no'] = 'yue';
  682. }
  683. $whereInMember['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  684. $totalMoney = $otherOrderService->getMemberMoneyByWhere($whereInMember, $memberSumField, $selectType, $group);
  685. if ($group) {
  686. $totalMoney = $this->trendYdata((array)$totalMoney, $whereInMember['timeKey']);
  687. }
  688. return $totalMoney;
  689. }
  690. /**
  691. * 线下付款总额
  692. * @param array $where
  693. * @param string $selectType
  694. * @param string $group
  695. * @param bool $isNum
  696. * @return array|mixed
  697. * @throws \Exception
  698. */
  699. public function getOfflineTotalMoney(array $where, string $selectType, string $group = "", bool $isNum = false)
  700. {
  701. /** 线下付款总额 */
  702. /** @var OtherOrderServices $otherOrderService */
  703. $otherOrderService = app()->make(OtherOrderServices::class);
  704. $offlineSumField = "pay_price";
  705. $whereOffline['type'] = 3;
  706. $whereOffline['paid'] = 1;
  707. $whereOffline['store_id'] = 0;
  708. // $whereOffline['pay_type_no'] = 'yue';
  709. $whereOffline['timeKey'] = $this->TimeConvert($where['time'], $isNum);
  710. $totalMoney = $otherOrderService->getMemberMoneyByWhere($whereOffline, $offlineSumField, $selectType, $group);
  711. if ($group) {
  712. $totalMoney = $this->trendYdata((array)$totalMoney, $whereOffline['timeKey']);
  713. }
  714. return $totalMoney;
  715. }
  716. /**
  717. * 处理Y坐标数据
  718. * @param array $data
  719. * @param array $timeKey
  720. * @return array
  721. * @throws \Exception
  722. */
  723. public function trendYdata(array $data, array $timeKey)
  724. {
  725. $hourMoney = array();
  726. $timeData = array();
  727. //获取日期之间的天数
  728. $getDayRange = function ($date, $timeKey) {
  729. $datearr = [];
  730. $stime = strtotime($timeKey['start_time']);
  731. $etime = strtotime($timeKey['end_time']);
  732. while ($stime <= $etime) {
  733. $datearr['x'][] = date($date, $stime);
  734. $datearr['y'][] = date($date, $stime);
  735. $stime = $stime + 86400;
  736. }
  737. return $datearr;
  738. };
  739. //获取日期之间的月份
  740. $getMonthRange = function ($date, $timeKey) {
  741. $datearr = [];
  742. $stime = date('Y-m-d', strtotime($timeKey['start_time']));
  743. $etime = date('Y-m-d', strtotime($timeKey['end_time']));
  744. $start = new \DateTime($stime);
  745. $end = new \DateTime($etime);
  746. $interval = \DateInterval::createFromDateString('1 month');
  747. $period = new \DatePeriod($start, $interval, $end);
  748. foreach ($period as $dt) {
  749. $datearr['x'][] = $dt->format($date);
  750. $datearr['y'][] = $dt->format($date);
  751. }
  752. return $datearr;
  753. };
  754. if ($timeKey['days'] == 1) {
  755. for ($i = 0; $i <= 24; $i++) {
  756. $timeData['x'][] = (string)($i < 10 ? ('0' . $i) : $i);
  757. $timeData['y'][] = $i < 10 ? ('0' . $i) : $i;
  758. //$timeData['y'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00";
  759. //$timeData['x'][] = $i < 10 ? ('0' . $i . ":00") : $i . ":00";
  760. }
  761. } elseif ($timeKey['days'] == 30) {
  762. $timeData = $getDayRange('Y-m-d', $timeKey);
  763. } elseif ($timeKey['days'] == 365) {
  764. $timeData = $getMonthRange('Y-m', $timeKey);
  765. } elseif ($timeKey['days'] > 1 && $timeKey['days'] < 30) {
  766. $timeData = $getDayRange('Y-m-d', $timeKey);
  767. } elseif ($timeKey['days'] > 30 && $timeKey['days'] < 365) {
  768. $timeData = $getMonthRange('Y-m', $timeKey);
  769. }
  770. if ($data) {
  771. $hourMoney = array_column($data, 'number', 'time');
  772. }
  773. $y = array();
  774. foreach ($timeData['y'] as $k => $v) {
  775. if (array_key_exists($v, $hourMoney)) {
  776. $y[$v] = $hourMoney[$v];
  777. } else {
  778. $y[$v] = 0;
  779. }
  780. }
  781. return ['x' => $timeData['x'], 'y' => $y];
  782. }
  783. /**
  784. * 获取环比时间类型
  785. * @param $timeKey
  786. * @return string
  787. */
  788. public function chainTime($timeKey)
  789. {
  790. switch ($timeKey) {
  791. case "today" :
  792. return "yestoday";
  793. case "month" :
  794. return "last_month";
  795. case "year" :
  796. return "last_year";
  797. default :
  798. return "other";
  799. }
  800. }
  801. }