ConsoleController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\goods\classes\GoodsClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\stat\classes\StatVisitClass;
  7. use bizGhs\item\classes\ItemClass;
  8. use common\components\dateUtil;
  9. use common\components\dict;
  10. use common\components\util;
  11. class ConsoleController extends BaseController
  12. {
  13. public $guestAccess = ['init', 'profile'];
  14. //总览
  15. public function actionStat()
  16. {
  17. $cacheKey = 'hd_console_stat_overview:' . $this->mainId . '_' . $this->shopId;
  18. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  19. $cached = !empty($cache) ? json_decode($cache, true) : null;
  20. if (is_array($cached)
  21. && isset($cached['totalItemNum'], $cached['totalCost'], $cached['goodsCount'], $cached['customCount'], $cached['orderCount'], $cached['debtCount'])) {
  22. $overview = [
  23. ['name' => "花材数", "url" => '/admin/stat/stock', 'value' => $cached['totalItemNum'], 'type' => 1],
  24. ['name' => "花材值", "url" => '/admin/stat/stock', 'value' => $cached['totalCost'], 'type' => 1],
  25. ['name' => "花束", "url" => '/admin/custom/showTotalBalance', 'value' => $cached['goodsCount'], 'type' => 1],
  26. ['name' => "总客户", "url" => '/admin/stat/stock', 'value' => $cached['customCount'], 'type' => 1],
  27. ['name' => "总订单", "url" => '/admin/home/member', 'value' => $cached['orderCount'], 'type' => 4],
  28. ['name' => "总赊账", "url" => '/admin/home/order', 'value' => $cached['debtCount'], 'type' => 4],
  29. ];
  30. util::success(['overview' => $overview]);
  31. }
  32. // 缓存未命中时计算
  33. $itemList = ItemClass::getAllByCondition(['mainId' => $this->mainId], null, 'id,stock,cost,virtualStock,delStatus', null, true);
  34. $totalItemNum = 0;
  35. $totalCost = 0;
  36. if (!empty($itemList)) {
  37. foreach ($itemList as $item) {
  38. if ($item->virtualStock == 0 && $item->delStatus == 0) {
  39. $stock = $item->stock ?? 0;
  40. $cost = $item->cost ?? 0;
  41. $totalItemNum = bcadd($stock, $totalItemNum, 2);
  42. $currentCost = bcmul($stock, $cost, 2);
  43. $totalCost = bcadd($currentCost, $totalCost, 2);
  44. }
  45. }
  46. }
  47. $totalItemNum = floor($totalItemNum);
  48. $goodsCount = GoodsClass::getCount(['mainId'=>$this->mainId]);
  49. $customCount = CustomClass::getCount(['shopId'=>$this->shopId]);
  50. $orderCount = OrderClass::getCount(['mainId'=>$this->mainId, 'status'=>4]);
  51. $debtCount = OrderClass::debtCount($this->mainId);
  52. $overview = [
  53. ['name' => "花材数", "url" => '/admin/stat/stock', 'value' => $totalItemNum, 'type' => 1],
  54. ['name' => "花材值", "url" => '/admin/stat/stock', 'value' => $totalCost, 'type' => 1],
  55. ['name' => "花束", "url" => '/admin/goods/list', 'value' => $goodsCount, 'type' => 1],
  56. ['name' => "总客户", "url" => '/admin/home/member', 'value' => $customCount, 'type' => 1],
  57. ['name' => "总订单", "url" => '/admin/home/order', 'value' => $orderCount, 'type' => 4],
  58. ['name' => "总赊账", "url" => '/admin/home/order', 'value' => $debtCount, 'type' => 4],
  59. ];
  60. $numbers = [
  61. 'totalItemNum' => $totalItemNum,
  62. 'totalCost' => $totalCost,
  63. 'goodsCount' => $goodsCount,
  64. 'customCount' => $customCount,
  65. 'orderCount' => $orderCount,
  66. 'debtCount' => $debtCount,
  67. ];
  68. // 缓存一小时
  69. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 3600, json_encode($numbers, JSON_UNESCAPED_UNICODE)]);
  70. util::success(['overview' => $overview]);
  71. }
  72. //概况
  73. public function actionProfile()
  74. {
  75. //查看财务的权限
  76. $lookMoney = \bizGhs\shop\classes\ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  77. $lsIncome = 0;
  78. $orderCount = 0;
  79. $yesterdayIncome = 0;
  80. if ($lookMoney == 1) {
  81. $mainId = $this->mainId;
  82. $todayStr = date('Y-m-d');
  83. // 今天零售订单总数与收入
  84. $cacheKey = 'hd_console_profile:' . 'today_'.$todayStr.'_income_' . $this->shopId;
  85. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  86. if ($cache) {
  87. $cacheData = json_decode($cache, true);
  88. $lsIncome = $cacheData['lsIncome'];
  89. $orderCount = $cacheData['orderCount'];
  90. } else {
  91. $period = dateUtil::formatTime('today', '', '', true);
  92. $start = $period['startTime'];
  93. $end = $period['endTime'];
  94. $currentStartDate = date('Y-m-d', strtotime($start));
  95. $currentEndDate = date('Y-m-d', strtotime($end));
  96. $currentStartTime = $currentStartDate . ' 00:00:00';
  97. $currentEndTime = $currentEndDate . ' 23:59:59';
  98. $where['time'] = ['between', [$start, $end]];
  99. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  100. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  101. $orderCount = OrderClass::getCount($where);
  102. $lsIncome = 0;
  103. $hdOrderList = OrderClass::getAllByCondition($where, null, 'mainPay, forward, cash');
  104. foreach ($hdOrderList as $hdKey => $hdOrder) {
  105. $actPrice = $hdOrder['mainPay'] ?? 0;
  106. $forward = $hdOrder['forward'] ?? 0;
  107. if ($forward == 1) { //零售售后付款
  108. // $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  109. } else {
  110. $cash = $hdOrder['cash'] ?? 0;
  111. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  112. $lsIncome = bcadd($lsIncome, $cash, 2);
  113. }
  114. }
  115. $cacheData = [
  116. 'lsIncome' => $lsIncome,
  117. 'orderCount' => $orderCount,
  118. ];
  119. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 1800, json_encode($cacheData, JSON_UNESCAPED_UNICODE)]);
  120. }
  121. // 昨天零售收入
  122. $cacheKey = 'hd_console_profile:' . 'yesterday_'.$todayStr.'_income_' . $this->shopId;
  123. $cache = \Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  124. if ($cache) {
  125. $cacheData = json_decode($cache, true);
  126. $yesterdayIncome = $cacheData['yesterdayIncome'];
  127. } else {
  128. $period = dateUtil::formatTime('yesterday', '', '', true);
  129. $start = $period['startTime'];
  130. $end = $period['endTime'];
  131. $currentStartDate = date('Y-m-d', strtotime($start));
  132. $currentEndDate = date('Y-m-d', strtotime($end));
  133. $currentStartTime = $currentStartDate . ' 00:00:00';
  134. $currentEndTime = $currentEndDate . ' 23:59:59';
  135. $where['time'] = ['between', [$start, $end]];
  136. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  137. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  138. $yesterdayIncome = 0;
  139. $hdOrderList = OrderClass::getAllByCondition($where, null, 'mainPay, forward, cash');
  140. foreach ($hdOrderList as $hdKey => $hdOrder) {
  141. $actPrice = $hdOrder['mainPay'] ?? 0;
  142. $forward = $hdOrder['forward'] ?? 0;
  143. if ($forward == 1) { //零售售后付款
  144. // $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
  145. } else {
  146. $cash = $hdOrder['cash'] ?? 0;
  147. $yesterdayIncome = bcadd($actPrice, $yesterdayIncome, 2);
  148. $yesterdayIncome = bcadd($yesterdayIncome, $cash, 2);
  149. }
  150. }
  151. $cacheData = [
  152. 'yesterdayIncome' => $yesterdayIncome,
  153. ];
  154. \Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 86400, json_encode($cacheData, JSON_UNESCAPED_UNICODE)]);
  155. }
  156. }
  157. //访客数
  158. $visitCustom = StatVisitClass::getVisitNum($this->shopId);
  159. // $wastage = \bizGhs\order\classes\StockWastageOrderClass::getTodayWaste($this->mainId);
  160. // $hsReturn = StatOrderClass::statHsNum($this->mainId);
  161. // $hsNum = $hsReturn['num'] ?? 0;
  162. $menu = [
  163. ["name" => "商城", "img" => "ghs/home/shop2.png", "url" => "/admin/cg/mall", 'pf' => 1,],
  164. ["name" => "改价", "img" => "ghs/home/gj2.png", "url" => "/admin/changePrice/list2", 'pf' => 1,],
  165. ["name" => "花材绿植", "img" => "ghs/home/hcgl2.png", "url" => "/admin/item/list2", 'pf' => 1,],
  166. ["name" => "接花束", "img" => "ghs/home/shop2.png", "url" => "/admin/order/workOrder", 'pf' => 1,],
  167. ["name" => "拆散", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/part/list", 'pf' => 1,],
  168. ["name" => "损耗", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/breakage/list", 'pf' => 1,],
  169. ["name" => "任务", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/work/list", 'pf' => 1,],
  170. ["name" => "买花", "img" => "ghs/home/shop2.png", "url" => "/pagesPurchase/order", 'pf' => 1,],
  171. ["name" => "买花记录", "img" => "ghs/home/shop2.png", "url" => "/pagesPurchase/shopping", 'pf' => 1,],
  172. ["name" => "花束", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/goods/list", 'pf' => 1,],
  173. ["name" => "客户充值", "img" => "ghs/home/kcyjs2.png", "url" => "/admin/recharge/rechargeCode", 'pf' => 1,],
  174. ];
  175. $warning = '';
  176. //$warning = '本月4号凌晨4点~5点系统升级,暂停使用';
  177. util::success([
  178. 'warning' => $warning,
  179. 'todayData' => [
  180. 'visit' => $visitCustom,
  181. 'income' => $lsIncome, //$todaySale,
  182. 'order' => $orderCount, //$orderNum,
  183. // 'wastage' => $wastage,
  184. // 'bouquet' => $hsNum
  185. ],
  186. 'yesterdayIncome' => $yesterdayIncome,
  187. 'menu' => $menu,
  188. 'lookMoney' => $lookMoney,
  189. ]);
  190. }
  191. //常用初始化
  192. public function actionInit()
  193. {
  194. $dict = dict::get($this->mainId);
  195. $shop = isset($this->shop) && !empty($this->shop) ? $this->shop->attributes : [];
  196. util::success(['dict' => $dict, 'shop' => $shop]);
  197. }
  198. //系统收入、客服收入、欠款
  199. public function actionStrokeCount()
  200. {
  201. if (empty($this->mainId)) {
  202. util::success([
  203. 'systemIncome' => 0,
  204. 'systemCount' => 0,
  205. 'debtCount' => 0,
  206. 'debtIncome' => 0,
  207. 'kfWxCount' => 0,
  208. 'kfWxIncome' => 0,
  209. 'cashCount' => 0,
  210. 'cashIncome' => 0,
  211. ]);
  212. }
  213. $return = \bizGhs\stat\classes\StatKdClass::eachChannelIncome($this->mainId, $this->shop);
  214. $incomeList = $return['incomeList'];
  215. $systemIncome1 = $incomeList['system']['category']['ls']['amount'] ?? 0;
  216. $systemCount1 = $incomeList['system']['category']['ls']['num'] ?? 0;
  217. $systemIncome2 = $incomeList['system']['category']['pf']['amount'] ?? 0;
  218. $systemCount2 = $incomeList['system']['category']['pf']['num'] ?? 0;
  219. $systemCount = bcadd($systemCount1, $systemCount2);
  220. $systemIncome = bcadd($systemIncome1, $systemIncome2, 2);
  221. $debtIncome1 = $incomeList['debt']['category']['ls']['amount'] ?? 0;
  222. $debtCount1 = $incomeList['debt']['category']['ls']['num'] ?? 0;
  223. $debtIncome2 = $incomeList['debt']['category']['pf']['amount'] ?? 0;
  224. $debtCount2 = $incomeList['debt']['category']['pf']['num'] ?? 0;
  225. $debtIncome = bcadd($debtIncome1, $debtIncome2, 2);
  226. $debtCount = bcadd($debtCount1, $debtCount2);
  227. $kfWxIncome1 = $incomeList['kfWx']['category']['ls']['amount'] ?? 0;
  228. $kfWxCount1 = $incomeList['kfWx']['category']['ls']['num'] ?? 0;
  229. $kfWxIncome2 = $incomeList['kfWx']['category']['pf']['amount'] ?? 0;
  230. $kfWxCount2 = $incomeList['kfWx']['category']['pf']['num'] ?? 0;
  231. $kfWxIncome = bcadd($kfWxIncome1, $kfWxIncome2, 2);
  232. $kfWxCount = bcadd($kfWxCount1, $kfWxCount2);
  233. $cashIncome1 = $incomeList['cash']['category']['ls']['amount'] ?? 0;
  234. $cashCount1 = $incomeList['cash']['category']['ls']['num'] ?? 0;
  235. $cashIncome2 = $incomeList['cash']['category']['pf']['amount'] ?? 0;
  236. $cashCount2 = $incomeList['cash']['category']['pf']['num'] ?? 0;
  237. $cashIncome = bcadd($cashIncome1, $cashIncome2, 2);
  238. $cashCount = bcadd($cashCount1, $cashCount2);
  239. util::success([
  240. 'systemIncome' => $systemIncome,
  241. 'systemCount' => $systemCount,
  242. 'debtCount' => $debtCount,
  243. 'debtIncome' => $debtIncome,
  244. 'kfWxCount' => $kfWxCount,
  245. 'kfWxIncome' => $kfWxIncome,
  246. 'cashCount' => $cashCount,
  247. 'cashIncome' => $cashIncome,
  248. ]);
  249. }
  250. }