1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
 
namespace app\agent\service;
 
use app\agent\model\Agent;
use app\agent\model\Client;
use app\agent\model\Store;
use app\agent\model\order\OrderRefund;
use app\agent\model\product\Product;
use app\agent\model\order\Order;
use app\agent\model\user\User;
use app\agent\model\product\Comment;
use app\agent\model\plus\agent\Cash as AgentCashModel;
use app\agent\model\supplier\Supplier as SupplierModel;
use app\agent\model\plus\agent\Apply as AgentApplyModel;
use app\agent\model\supplier\Apply as SupplierApplyModel;
use app\agent\model\supplier\Cash as SupplierCashModel;
use app\agent\model\supplier\DepositRefund as DepositRefundModel;
use app\agent\model\plus\point\Product as PointProductModel;
use app\agent\model\plus\bargain\Product as BargainProductModel;
use app\agent\model\plus\assemble\Product as AssembleProductModel;
use app\agent\model\plus\seckill\Product as SeckillProductModel;
use app\agent\model\supplier\ServiceApply as ServiceApplyModel;
use app\agent\model\user\BalanceLog;
 
use app\common\model\agent\User as AgentUser;
 
/**
 * 商城模型
 */
class ShopService
{
    // 商品模型
    private $ProductModel;
    // 订单模型
    private $OrderModel;
    // 用户模型
    private $UserModel;
    // 订单退款模型
    private $OrderRefund;
    
    // 门店模型
    private $StoreModel;
    // 余额模型
    private $AgentModel;
    private $ClientModel;
 
    /**
     * 构造方法
     */
    public function __construct()
    {
        /* 初始化模型 */
        $this->ProductModel = new Product();
        $this->OrderModel = new Order();
        $this->UserModel = new User();
        $this->OrderRefund = new OrderRefund();
 
        $this->StoreModel = new Store();
        $this->AgentModel = new Agent();
        $this->ClientModel = new Client();
    }
 
    /**
     * 后台首页数据
     */
    public function getHomeData($username)
    {
        $agent_id= (new AgentUser())//获取当前登录的代理商id
                ->where('user_name', '=', $username[0])
                ->where('is_delete', '=', 0)->find();
 
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        // 最近七天日期
        $lately7days = $this->getLately7days();
        $data = [
            'top_data' => [
                // 客户总量
                'product_total' => $this->getClinetTotal($agent_id['agent_id']),
                // 代理商总量
                'order_total' => $this->getAgentTotal($agent_id['agent_id']),
                // 门店总量
                'user_total' => $this->getStoreTotal($agent_id['agent_id']),
                // 账户余额
                'supplier_total' => $this->getBalanceTotal($agent_id['agent_id']),
 
                // // 订单总量
                // 'order_total' => $this->getOrderTotal(),
                // // 用户总量
                // 'user_total' => $this->getUserTotal(),
                // // 店铺总量
                // 'supplier_total' => $this->getSupplierTotal()
            ],
            'wait_data' => [
                //订单
                'order' => [
                    'disposal' => $this->getReviewOrderTotal(),
                    'refund' => $this->getRefundOrderTotal(),
                    'plate' => $this->getPlateOrderTotal(),
                ],
                // //分销商
                // 'agent' => [
                //     'cash_apply' => $this->getAgentApplyTotal(10),
                //     'apply' => AgentApplyModel::getApplyCount(),
                //     'cash_money' => $this->getAgentApplyTotal(20),
                // ],
                // //供应商
                // 'supplier' => [
                //     'apply' => SupplierApplyModel::getApplyCount(),
                //     'cash_apply' => SupplierCashModel::getApplyCount(10),
                //     'cash_money' => SupplierCashModel::getApplyCount(20),
                //     'refund' => DepositRefundModel::getRefundCount(),
                //     'service' => ServiceApplyModel::getApplyCount(),
                // ],
                // //活动
                // 'activity' => [
                //     'point' => PointProductModel::getApplyCount(),
                //     'bargain' => BargainProductModel::getApplyCount(),
                //     'assemble' => AssembleProductModel::getApplyCount(),
                //     'seckill' => SeckillProductModel::getApplyCount(),
                // ],
                // // 待审核
                // 'audit' => [
                //     'comment' => $this->getReviewCommentTotal(),
                //     'product' => $this->ProductModel->getProductTotal([
                //         'product_status' => '40'
                //     ]),
                // ]
            ],
            'today_data' => [
                // 销售额(元)-->余额变动
                'order_total_price' => $this->getBalanceByDay($agent_id['agent_id']),
                // 支付订单数
                'order_total' => [
                    'tday' => $this->getOrderTotal($today),
                    'ytd' => $this->getOrderTotal($yesterday)
                ],
                // 新增门店数
                'new_user_total' => [
                    'tday' => $this->getStoreTotal($agent_id['agent_id'],$today),
                    'ytd' => $this->getStoreTotal($agent_id['agent_id'],$yesterday)
                ],
                // 新供应商数-->新增客户
                'new_supplier_total' => $this->getClinetByDay($agent_id['agent_id']),
                // 申请供应商数--客户申请
                'apply_supplier_total' => [
                    'tday' => SupplierApplyModel::getApplyCountByDay($today),
                    'ytd' => SupplierApplyModel::getApplyCountByDay($yesterday)
                ]
            ],
        ];
        return $data;
    }
 
    //获取代理商余额
    private function getBalanceTotal($id){
        return number_format($this->AgentModel->getBalanceTotal($id));
    }
 
    //获取下级代理商总数
    private function getAgentTotal($id)
    {
        return number_format($this->AgentModel->getAgentTotal($id));
    }
 
    //获取代理商下的客户总数
    private function getClinetTotal($id)
    {
        return number_format($this->ClientModel->getClinetTotal($id));
    }
 
    //获取代理商下的 客户 今日昨日
    private function getClinetByDay($id)
    {
        return $this->ClientModel->getClinetByDay($id);
    }
 
    //获取余额变动
    private function getBalanceByDay($id)
    {
        return $this->AgentModel->getBalanceByDay($id);
    }
 
 
 
    /**
     * 最近七天日期
     */
    private function getLately7days()
    {
        // 获取当前周几
        $date = [];
        for ($i = 0; $i < 7; $i++) {
            $date[] = date('Y-m-d', strtotime('-' . $i . ' days'));
        }
        return array_reverse($date);
    }
 
    /**
     * 获取商品总量
     */
    private function getProductTotal()
    {
        return number_format($this->ProductModel->getProductTotal());
    }
 
    /**
     * 获取待审核提现总数量
     */
    private function getAgentApplyTotal($apply_status)
    {
        $model = new AgentCashModel;
        return number_format($model->getAgentApplyTotal($apply_status));
    }
 
    /**
     * 获取门店总量
     */
    private function getStoreTotal($agent_id,$day = null)
    {
        return number_format($this->StoreModel->getStoreTotal($agent_id,$day));
    }
 
    /**
     * 获取订单总量
     */
    private function getOrderTotal($day = null)
    {
        return number_format($this->OrderModel->getOrderData($day, null, 'order_total'));
    }
 
    /**
     * 获取待处理订单总量
     */
    private function getReviewOrderTotal()
    {
        return number_format($this->OrderModel->getReviewOrderTotal());
    }
 
    /**
     * 获取售后订单总量
     */
    private function getRefundOrderTotal()
    {
        return number_format($this->OrderRefund->getRefundOrderTotal());
    }
 
    /**
     * 获取平台售后订单总量
     */
    private function getPlateOrderTotal()
    {
        return number_format($this->OrderRefund->getPlateOrderTotal());
    }
 
    /**
     * 获取供应商总量
     */
    private function getSupplierTotal()
    {
        $model = new SupplierModel;
        return number_format($model->getSupplierTotal());
    }
    /**
     * 获取待审核评价总量
     */
    private function getReviewCommentTotal()
    {
        $model = new Comment;
        return number_format($model->getReviewCommentTotal());
    }
 
    /**
     * 获取某天的总销售额
     */
    private function getOrderTotalPrice($day)
    {
        return sprintf('%.2f', $this->OrderModel->getOrderTotalPrice($day));
    }
}